added old man npc and AI
This commit is contained in:
@@ -5,6 +5,7 @@ import de.miaurizius.jgame2d.core.handlers.AssetSetter;
|
|||||||
import de.miaurizius.jgame2d.core.handlers.CollisionHandler;
|
import de.miaurizius.jgame2d.core.handlers.CollisionHandler;
|
||||||
import de.miaurizius.jgame2d.core.handlers.KeyHandler;
|
import de.miaurizius.jgame2d.core.handlers.KeyHandler;
|
||||||
import de.miaurizius.jgame2d.core.handlers.Sound;
|
import de.miaurizius.jgame2d.core.handlers.Sound;
|
||||||
|
import de.miaurizius.jgame2d.entity.Entity;
|
||||||
import de.miaurizius.jgame2d.entity.Player;
|
import de.miaurizius.jgame2d.entity.Player;
|
||||||
import de.miaurizius.jgame2d.object.SuperObject;
|
import de.miaurizius.jgame2d.object.SuperObject;
|
||||||
import de.miaurizius.jgame2d.tile.TileManager;
|
import de.miaurizius.jgame2d.tile.TileManager;
|
||||||
@@ -47,6 +48,7 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
// ENTITY AND OBJECT
|
// ENTITY AND OBJECT
|
||||||
public Player player = new Player(this, keyH);
|
public Player player = new Player(this, keyH);
|
||||||
public SuperObject[] obj = new SuperObject[10];
|
public SuperObject[] obj = new SuperObject[10];
|
||||||
|
public Entity[] npc = new Entity[10];
|
||||||
|
|
||||||
// GAME STATE
|
// GAME STATE
|
||||||
public GameState gameState;
|
public GameState gameState;
|
||||||
@@ -61,6 +63,7 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
|
|
||||||
public void setupGame() {
|
public void setupGame() {
|
||||||
assetSetter.setObject();
|
assetSetter.setObject();
|
||||||
|
assetSetter.setNPC();
|
||||||
playMusic(0); //Play main theme
|
playMusic(0); //Play main theme
|
||||||
gameState = GameState.PLAY;
|
gameState = GameState.PLAY;
|
||||||
}
|
}
|
||||||
@@ -103,6 +106,7 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
switch(gameState) {
|
switch(gameState) {
|
||||||
case PLAY:
|
case PLAY:
|
||||||
player.update();
|
player.update();
|
||||||
|
for(Entity entity : npc) if(entity != null) entity.update();
|
||||||
break;
|
break;
|
||||||
case PAUSE:
|
case PAUSE:
|
||||||
break;
|
break;
|
||||||
@@ -117,9 +121,10 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
long drawStart = 0;
|
long drawStart = 0;
|
||||||
drawStart = System.nanoTime();
|
drawStart = System.nanoTime();
|
||||||
|
|
||||||
//Draw all components
|
// COMPONENTS
|
||||||
tileM.draw(graphics2d);
|
tileM.draw(graphics2d);
|
||||||
for (SuperObject superObject : obj) if (superObject != null) superObject.draw(graphics2d, this);
|
for (SuperObject superObject : obj) if (superObject != null) superObject.draw(graphics2d, this);
|
||||||
|
for(Entity npc : npc) if(npc != null) npc.draw(graphics2d);
|
||||||
player.draw(graphics2d);
|
player.draw(graphics2d);
|
||||||
ui.draw(graphics2d);
|
ui.draw(graphics2d);
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package de.miaurizius.jgame2d.core.handlers;
|
package de.miaurizius.jgame2d.core.handlers;
|
||||||
|
|
||||||
import de.miaurizius.jgame2d.core.GamePanel;
|
import de.miaurizius.jgame2d.core.GamePanel;
|
||||||
|
import de.miaurizius.jgame2d.entity.OldManNPC;
|
||||||
|
|
||||||
public class AssetSetter {
|
public class AssetSetter {
|
||||||
|
|
||||||
@@ -14,4 +15,10 @@ public class AssetSetter {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setNPC() {
|
||||||
|
panel.npc[0] = new OldManNPC(panel);
|
||||||
|
panel.npc[0].worldX = panel.tileSize*21;
|
||||||
|
panel.npc[0].worldY = panel.tileSize*21;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,11 +24,54 @@ public class Entity {
|
|||||||
public Rectangle solidArea = new Rectangle(0, 0, 48, 48);
|
public Rectangle solidArea = new Rectangle(0, 0, 48, 48);
|
||||||
public int solidAreaDefaultX, solidAreaDefaultY;
|
public int solidAreaDefaultX, solidAreaDefaultY;
|
||||||
public boolean collisionOn = false;
|
public boolean collisionOn = false;
|
||||||
|
public int actionLock = 0;
|
||||||
|
|
||||||
public Entity(GamePanel panel) {
|
public Entity(GamePanel panel) {
|
||||||
this.panel = panel;
|
this.panel = panel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setAction() {}
|
||||||
|
public void update() {
|
||||||
|
setAction();
|
||||||
|
collisionOn = false;
|
||||||
|
panel.collisionH.checkTile(this);
|
||||||
|
|
||||||
|
if(!collisionOn) {
|
||||||
|
switch (direction) {
|
||||||
|
case UP -> worldY -= speed;
|
||||||
|
case DOWN -> worldY += speed;
|
||||||
|
case LEFT ->worldX -= speed;
|
||||||
|
case RIGHT -> worldX += speed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
spriteCounter++;
|
||||||
|
if(spriteCounter > 12) {
|
||||||
|
if(spriteNum == 1) spriteNum = 2;
|
||||||
|
else if(spriteNum == 2) spriteNum = 1;
|
||||||
|
else spriteNum = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void draw(Graphics graphics2d) {
|
||||||
|
int screenX = worldX - panel.player.worldX + panel.player.screenX;
|
||||||
|
int screenY = worldY - panel.player.worldY + panel.player.screenY;
|
||||||
|
|
||||||
|
if(worldX + panel.tileSize > panel.player.worldX - panel.player.screenX &&
|
||||||
|
worldX - panel.tileSize < panel.player.worldX + panel.player.screenX &&
|
||||||
|
worldY + panel.tileSize > panel.player.worldY - panel.player.screenY &&
|
||||||
|
worldY - panel.tileSize < panel.player.worldY + panel.player.screenY
|
||||||
|
) {
|
||||||
|
BufferedImage image = switch (direction) {
|
||||||
|
case UP -> (spriteNum == 1) ? up1 : up2;
|
||||||
|
case DOWN -> (spriteNum == 1) ? down1 : down2;
|
||||||
|
case LEFT -> (spriteNum == 1) ? left1 : left2;
|
||||||
|
case RIGHT -> (spriteNum == 1) ? right1 : right2;
|
||||||
|
};
|
||||||
|
graphics2d.drawImage(image, screenX, screenY, panel.tileSize, panel.tileSize, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public BufferedImage initEntitySprites(String name) {
|
public BufferedImage initEntitySprites(String name) {
|
||||||
try {
|
try {
|
||||||
return Utility.scaleImage(ImageIO.read(new FileInputStream("assets/" + name + ".png")), panel.tileSize, panel.tileSize);
|
return Utility.scaleImage(ImageIO.read(new FileInputStream("assets/" + name + ".png")), panel.tileSize, panel.tileSize);
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package de.miaurizius.jgame2d.entity;
|
|||||||
import de.miaurizius.jgame2d.core.enums.Direction;
|
import de.miaurizius.jgame2d.core.enums.Direction;
|
||||||
import de.miaurizius.jgame2d.core.GamePanel;
|
import de.miaurizius.jgame2d.core.GamePanel;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class OldManNPC extends Entity {
|
public class OldManNPC extends Entity {
|
||||||
|
|
||||||
public OldManNPC(GamePanel panel) {
|
public OldManNPC(GamePanel panel) {
|
||||||
@@ -24,4 +26,17 @@ public class OldManNPC extends Entity {
|
|||||||
right2 = initEntitySprites("npc/oldman_right_2");
|
right2 = initEntitySprites("npc/oldman_right_2");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setAction() {
|
||||||
|
actionLock++;
|
||||||
|
if(actionLock != 120) return; //lock action for x frames
|
||||||
|
Random rand = new Random();
|
||||||
|
int i = rand.nextInt(100)+1; //Generate number between 1 and 100
|
||||||
|
if(i <= 25) direction = Direction.UP;
|
||||||
|
if(i > 25 && i <= 50) direction = Direction.DOWN;
|
||||||
|
if(i > 50 && i <= 75) direction = Direction.LEFT;
|
||||||
|
if(i > 75) direction = Direction.RIGHT;
|
||||||
|
actionLock = 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user