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 image, image2, image3;
|
||||
public Rectangle solidArea = new Rectangle(0, 0, 48, 48);
|
||||
public Rectangle attackArea = new Rectangle(0, 0, 0, 0);
|
||||
public int solidAreaDefaultX, solidAreaDefaultY;
|
||||
public boolean collision = false;
|
||||
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) {
|
||||
try {
|
||||
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) {
|
||||
super(panel);
|
||||
name = "oldman-npc";
|
||||
|
||||
direction = Direction.DOWN;
|
||||
speed = 1;
|
||||
|
||||
@@ -17,6 +17,7 @@ public class Player extends Entity {
|
||||
public Player(GamePanel panel, KeyHandler keyH) {
|
||||
super(panel);
|
||||
this.keyH = keyH;
|
||||
name = "player";
|
||||
|
||||
screenX = panel.screenWidth/2 - panel.tileSize/2;
|
||||
screenY = panel.screenHeight/2 - panel.tileSize/2;
|
||||
@@ -29,6 +30,9 @@ public class Player extends Entity {
|
||||
solidArea.width = 24;
|
||||
solidArea.height = 24;
|
||||
|
||||
attackArea.width = 36;
|
||||
attackArea.height = 36;
|
||||
|
||||
setDefaultValues();
|
||||
getPlayerImage();
|
||||
getPlayerAttackImage();
|
||||
@@ -46,12 +50,18 @@ public class Player extends Entity {
|
||||
}
|
||||
|
||||
public void update() {
|
||||
if(attacking) {
|
||||
attacking();
|
||||
return;
|
||||
}
|
||||
// MOVEMENT
|
||||
if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed || keyH.spacePressed) {
|
||||
if(keyH.upPressed) direction = Direction.UP;
|
||||
else if(keyH.downPressed) direction = Direction.DOWN;
|
||||
else if(keyH.leftPressed) direction = Direction.LEFT;
|
||||
else direction = Direction.RIGHT;
|
||||
if(!keyH.spacePressed) {
|
||||
if(keyH.upPressed) direction = Direction.UP;
|
||||
else if(keyH.downPressed) direction = Direction.DOWN;
|
||||
else if(keyH.leftPressed) direction = Direction.LEFT;
|
||||
else direction = Direction.RIGHT;
|
||||
}
|
||||
|
||||
// CHECK TILE COLLISION
|
||||
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) {
|
||||
int screenX = worldX - panel.player.worldX + panel.player.screenX;
|
||||
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.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));
|
||||
}
|
||||
@@ -117,7 +169,10 @@ public class Player extends Entity {
|
||||
}
|
||||
|
||||
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
|
||||
panel.gameState = GameState.DIALOGUE;
|
||||
panel.npc[index].speak();
|
||||
|
||||
Reference in New Issue
Block a user