make monsters killable and playing soundeffects

This commit is contained in:
2025-11-29 23:23:18 +01:00
parent b513370152
commit 36ff9e8854
4 changed files with 36 additions and 16 deletions

View File

@@ -2,6 +2,7 @@ package de.miaurizius.jgame2d.entity;
import de.miaurizius.jgame2d.core.*;
import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.enums.EntityType;
import de.miaurizius.jgame2d.core.enums.GameState;
import de.miaurizius.jgame2d.core.handlers.KeyHandler;
@@ -17,7 +18,9 @@ public class Player extends Entity {
public Player(GamePanel panel, KeyHandler keyH) {
super(panel);
this.keyH = keyH;
name = "player";
type = EntityType.PLAYER;
screenX = panel.screenWidth/2 - panel.tileSize/2;
screenY = panel.screenHeight/2 - panel.tileSize/2;
@@ -91,6 +94,8 @@ public class Player extends Entity {
spriteCounter = 0;
}
}
// INVINCIBLE COUNTER
if(!invincible) return;
invincibleCount++;
if(invincibleCount > 60) {
@@ -98,21 +103,6 @@ public class Player extends Entity {
invincibleCount = 0;
}
}
public void draw(Graphics2D graphics2d) {
int screenX = worldX - panel.player.worldX + panel.player.screenX;
int screenY = worldY - panel.player.worldY + panel.player.screenY;
if(invincible) {
graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
}
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));
}
// INTERACTION
public void pickObject(int index) {
@@ -123,6 +113,7 @@ public class Player extends Entity {
if(index == 999) return;
if(invincible) return;
life -= 1;
panel.playSE(6);
invincible = true;
}
public void attacking() {
@@ -161,6 +152,11 @@ public class Player extends Entity {
}
public void damageMonster(int index) {
if(index == 999) return;
if(panel.monster[index].invincible) return;
panel.monster[index].life -= 1;
panel.playSE(5);
panel.monster[index].invincible = true;
if(panel.monster[index].life <= 0) panel.monster[index] = null;
}
public void interactNPC(int index) {