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

@@ -22,6 +22,8 @@ public class Sound {
soundURL[2] = new File("assets/sounds/powerup.wav").toURI().toURL(); soundURL[2] = new File("assets/sounds/powerup.wav").toURI().toURL();
soundURL[3] = new File("assets/sounds/unlock.wav").toURI().toURL(); soundURL[3] = new File("assets/sounds/unlock.wav").toURI().toURL();
soundURL[4] = new File("assets/sounds/fanfare.wav").toURI().toURL(); soundURL[4] = new File("assets/sounds/fanfare.wav").toURI().toURL();
soundURL[5] = new File("assets/sounds/hitmonster.wav").toURI().toURL();
soundURL[6] = new File("assets/sounds/receivedamage.wav").toURI().toURL();
} catch(MalformedURLException e) { } catch(MalformedURLException e) {
Boot.logger.log(Level.SEVERE, e.getMessage()); Boot.logger.log(Level.SEVERE, e.getMessage());
} }

View File

@@ -82,6 +82,14 @@ public class Entity {
else spriteNum = 0; else spriteNum = 0;
spriteCounter = 0; spriteCounter = 0;
} }
// INVINCIBLE COUNTER
if(!invincible) return;
invincibleCount++;
if(invincibleCount > 40) {
invincible = false;
invincibleCount = 0;
}
} }
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;
@@ -92,7 +100,19 @@ public class Entity {
worldY + panel.tileSize > panel.player.worldY - panel.player.screenY && worldY + panel.tileSize > panel.player.worldY - panel.player.screenY &&
worldY - panel.tileSize < panel.player.worldY + panel.player.screenY worldY - panel.tileSize < panel.player.worldY + panel.player.screenY
) { ) {
graphics2d.drawImage(parseSprite(), screenX, screenY, panel.tileSize, panel.tileSize, null); if(invincible) {
graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
}
// only modify sprite render position for player because I dont know yet how monster attack sprite are gonna look
if(type == EntityType.PLAYER) {
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);
} else graphics2d.drawImage(parseSprite(), screenX, screenY, null);
graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
} }
} }

View File

@@ -2,6 +2,7 @@ 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 de.miaurizius.jgame2d.core.enums.EntityType;
import java.util.Random; import java.util.Random;
@@ -9,6 +10,7 @@ public class OldManNPC extends Entity {
public OldManNPC(GamePanel panel) { public OldManNPC(GamePanel panel) {
super(panel); super(panel);
type = EntityType.NPC;
name = "oldman-npc"; name = "oldman-npc";
direction = Direction.DOWN; direction = Direction.DOWN;

View File

@@ -2,6 +2,7 @@ package de.miaurizius.jgame2d.entity;
import de.miaurizius.jgame2d.core.*; import de.miaurizius.jgame2d.core.*;
import de.miaurizius.jgame2d.core.enums.Direction; 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.enums.GameState;
import de.miaurizius.jgame2d.core.handlers.KeyHandler; import de.miaurizius.jgame2d.core.handlers.KeyHandler;
@@ -17,7 +18,9 @@ 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"; name = "player";
type = EntityType.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;
@@ -91,6 +94,8 @@ public class Player extends Entity {
spriteCounter = 0; spriteCounter = 0;
} }
} }
// INVINCIBLE COUNTER
if(!invincible) return; if(!invincible) return;
invincibleCount++; invincibleCount++;
if(invincibleCount > 60) { if(invincibleCount > 60) {
@@ -98,21 +103,6 @@ public class Player extends Entity {
invincibleCount = 0; 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 // INTERACTION
public void pickObject(int index) { public void pickObject(int index) {
@@ -123,6 +113,7 @@ public class Player extends Entity {
if(index == 999) return; if(index == 999) return;
if(invincible) return; if(invincible) return;
life -= 1; life -= 1;
panel.playSE(6);
invincible = true; invincible = true;
} }
public void attacking() { public void attacking() {
@@ -161,6 +152,11 @@ public class Player extends Entity {
} }
public void damageMonster(int index) { public void damageMonster(int index) {
if(index == 999) return; 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) { public void interactNPC(int index) {