added hit detection when player hits monsters
This commit is contained in:
@@ -20,6 +20,7 @@ public class Entity {
|
|||||||
public BufferedImage attackUp1, attackUp2, attackDown1, attackDown2, attackLeft1, attackLeft2, attackRight1, attackRight2;
|
public BufferedImage attackUp1, attackUp2, attackDown1, attackDown2, attackLeft1, attackLeft2, attackRight1, attackRight2;
|
||||||
public BufferedImage image, image2, image3;
|
public BufferedImage image, image2, image3;
|
||||||
public Rectangle solidArea = new Rectangle(0, 0, 48, 48);
|
public Rectangle solidArea = new Rectangle(0, 0, 48, 48);
|
||||||
|
public Rectangle attackArea = new Rectangle(0, 0, 0, 0);
|
||||||
public int solidAreaDefaultX, solidAreaDefaultY;
|
public int solidAreaDefaultX, solidAreaDefaultY;
|
||||||
public boolean collision = false;
|
public boolean collision = false;
|
||||||
String[] dialogue = new String[20];
|
String[] dialogue = new String[20];
|
||||||
@@ -119,6 +120,15 @@ public class Entity {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BufferedImage parseSpriteATK() {
|
||||||
|
return switch (direction) {
|
||||||
|
case UP -> (spriteNum == 1) ? attackUp1 : attackUp2;
|
||||||
|
case DOWN -> (spriteNum == 1) ? attackDown1 : attackDown2;
|
||||||
|
case LEFT -> (spriteNum == 1) ? attackLeft1 : attackLeft2;
|
||||||
|
case RIGHT -> (spriteNum == 1) ? attackRight1 : attackRight2;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ public class OldManNPC extends Entity {
|
|||||||
|
|
||||||
public OldManNPC(GamePanel panel) {
|
public OldManNPC(GamePanel panel) {
|
||||||
super(panel);
|
super(panel);
|
||||||
|
name = "oldman-npc";
|
||||||
|
|
||||||
direction = Direction.DOWN;
|
direction = Direction.DOWN;
|
||||||
speed = 1;
|
speed = 1;
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ public class Player extends Entity {
|
|||||||
public Player(GamePanel panel, KeyHandler keyH) {
|
public Player(GamePanel panel, KeyHandler keyH) {
|
||||||
super(panel);
|
super(panel);
|
||||||
this.keyH = keyH;
|
this.keyH = keyH;
|
||||||
|
name = "player";
|
||||||
|
|
||||||
screenX = panel.screenWidth/2 - panel.tileSize/2;
|
screenX = panel.screenWidth/2 - panel.tileSize/2;
|
||||||
screenY = panel.screenHeight/2 - panel.tileSize/2;
|
screenY = panel.screenHeight/2 - panel.tileSize/2;
|
||||||
@@ -29,6 +30,9 @@ public class Player extends Entity {
|
|||||||
solidArea.width = 24;
|
solidArea.width = 24;
|
||||||
solidArea.height = 24;
|
solidArea.height = 24;
|
||||||
|
|
||||||
|
attackArea.width = 36;
|
||||||
|
attackArea.height = 36;
|
||||||
|
|
||||||
setDefaultValues();
|
setDefaultValues();
|
||||||
getPlayerImage();
|
getPlayerImage();
|
||||||
getPlayerAttackImage();
|
getPlayerAttackImage();
|
||||||
@@ -46,12 +50,18 @@ public class Player extends Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
|
if(attacking) {
|
||||||
|
attacking();
|
||||||
|
return;
|
||||||
|
}
|
||||||
// MOVEMENT
|
// MOVEMENT
|
||||||
if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed || keyH.spacePressed) {
|
if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed || keyH.spacePressed) {
|
||||||
|
if(!keyH.spacePressed) {
|
||||||
if(keyH.upPressed) direction = Direction.UP;
|
if(keyH.upPressed) direction = Direction.UP;
|
||||||
else if(keyH.downPressed) direction = Direction.DOWN;
|
else if(keyH.downPressed) direction = Direction.DOWN;
|
||||||
else if(keyH.leftPressed) direction = Direction.LEFT;
|
else if(keyH.leftPressed) direction = Direction.LEFT;
|
||||||
else direction = Direction.RIGHT;
|
else direction = Direction.RIGHT;
|
||||||
|
}
|
||||||
|
|
||||||
// CHECK TILE COLLISION
|
// CHECK TILE COLLISION
|
||||||
collisionOn = false;
|
collisionOn = false;
|
||||||
@@ -99,6 +109,45 @@ public class Player extends Entity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void attacking() {
|
||||||
|
spriteCounter++;
|
||||||
|
if(spriteCounter <= 5) spriteNum = 1;
|
||||||
|
if(spriteCounter > 5 && spriteCounter <= 25) {
|
||||||
|
spriteNum = 2;
|
||||||
|
int currentWorldX = worldX;
|
||||||
|
int currentWorldY = worldY;
|
||||||
|
int solidAreaWidth = solidArea.width;
|
||||||
|
int solidAreaHeight = solidArea.height;
|
||||||
|
|
||||||
|
switch(direction) {
|
||||||
|
case UP -> worldY -= attackArea.height;
|
||||||
|
case DOWN -> worldY += attackArea.height;
|
||||||
|
case LEFT -> worldX -= attackArea.width;
|
||||||
|
case RIGHT -> worldX += attackArea.width;
|
||||||
|
}
|
||||||
|
solidArea.width = attackArea.width;
|
||||||
|
solidArea.height = attackArea.height;
|
||||||
|
|
||||||
|
int monsterIndex = panel.collisionH.checkEntity(this, panel.monster);
|
||||||
|
damageMonster(monsterIndex);
|
||||||
|
|
||||||
|
worldX = currentWorldX;
|
||||||
|
worldY = currentWorldY;
|
||||||
|
solidArea.width = solidAreaWidth;
|
||||||
|
solidArea.height = solidAreaHeight;
|
||||||
|
|
||||||
|
}
|
||||||
|
if(spriteCounter > 25) {
|
||||||
|
spriteNum = 1;
|
||||||
|
spriteCounter = 0;
|
||||||
|
attacking = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void damageMonster(int index) {
|
||||||
|
if(index == 999) return;
|
||||||
|
}
|
||||||
|
|
||||||
public void draw(Graphics2D graphics2d) {
|
public void draw(Graphics2D graphics2d) {
|
||||||
int screenX = worldX - panel.player.worldX + panel.player.screenX;
|
int screenX = worldX - panel.player.worldX + panel.player.screenX;
|
||||||
int screenY = worldY - panel.player.worldY + panel.player.screenY;
|
int screenY = worldY - panel.player.worldY + panel.player.screenY;
|
||||||
@@ -107,7 +156,10 @@ public class Player extends Entity {
|
|||||||
graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
|
graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
|
||||||
}
|
}
|
||||||
|
|
||||||
graphics2d.drawImage(parseSprite(), screenX, screenY, null);
|
if(attacking) graphics2d.drawImage(parseSpriteATK(),
|
||||||
|
(direction == Direction.LEFT) ? screenX - panel.tileSize : screenX,
|
||||||
|
(direction == Direction.UP) ? screenY - panel.tileSize : screenY, null);
|
||||||
|
else graphics2d.drawImage(parseSprite(), screenX, screenY, null);
|
||||||
|
|
||||||
graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
|
graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
|
||||||
}
|
}
|
||||||
@@ -117,7 +169,10 @@ public class Player extends Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void interactNPC(int index) {
|
public void interactNPC(int index) {
|
||||||
if(index == 999) return;
|
if(index == 999) {
|
||||||
|
if(panel.keyH.spacePressed) attacking = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
//if(!panel.keyH.spacePressed) return; //Only uncomment if text should only appear if player hits space
|
//if(!panel.keyH.spacePressed) return; //Only uncomment if text should only appear if player hits space
|
||||||
panel.gameState = GameState.DIALOGUE;
|
panel.gameState = GameState.DIALOGUE;
|
||||||
panel.npc[index].speak();
|
panel.npc[index].speak();
|
||||||
|
|||||||
Reference in New Issue
Block a user