added monster damage

This commit is contained in:
2025-11-29 01:01:17 +01:00
parent d07077a5f1
commit a215cef7d7
8 changed files with 84 additions and 33 deletions

View File

@@ -4,6 +4,7 @@ import de.miaurizius.jgame2d.core.Boot;
import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.Utility;
import de.miaurizius.jgame2d.core.enums.EntityType;
import javax.imageio.ImageIO;
import java.awt.*;
@@ -25,11 +26,14 @@ public class Entity {
public int solidAreaDefaultX, solidAreaDefaultY;
public boolean collisionOn = false;
public int actionLock = 0;
public boolean invincible = false;
public int invincibleCount = 0;
String[] dialogue = new String[20];
int dialogueIndex = 0;
public BufferedImage image, image2, image3;
public String name;
public boolean collision = false;
public EntityType type;
// CHARACTER STATUS
public int maxLife;
@@ -61,7 +65,13 @@ public class Entity {
panel.collisionH.checkObject(this, false);
panel.collisionH.checkEntity(this, panel.npc);
panel.collisionH.checkEntity(this, panel.monster);
panel.collisionH.checkPlayer(this);
boolean contactPlayer = panel.collisionH.checkPlayer(this);
if(this.type == EntityType.MONSTER && contactPlayer) {
if(panel.player.invincible) return;
panel.player.life -= 1;
panel.player.invincible = true;
}
if(!collisionOn) {
switch (direction) {
@@ -81,7 +91,7 @@ public class Entity {
}
}
public void draw(Graphics graphics2d) {
public void draw(Graphics2D graphics2d) {
int screenX = worldX - panel.player.worldX + panel.player.screenX;
int screenY = worldY - panel.player.worldY + panel.player.screenY;

View File

@@ -32,6 +32,7 @@ public class OldManNPC extends Entity {
dialogue[1] = "So you've come to this island to \nfind the treasure?";
dialogue[2] = "I used to be a great wizard but now... \nI'm a bit too old for taking an \nadventure.";
dialogue[3] = "Well, good luck on you.";
dialogue[4] = "I heard drinking the water of the \nholy lake makes you feel fine again...";
}
public void setAction() {

View File

@@ -34,10 +34,10 @@ public class Player extends Entity {
}
public void setDefaultValues() {
// worldX = panel.tileSize * 23;
// worldY = panel.tileSize * 21;
worldX = panel.tileSize * 10;
worldY = panel.tileSize * 13;
worldX = panel.tileSize * 23;
worldY = panel.tileSize * 21;
// worldX = panel.tileSize * 10;
// worldY = panel.tileSize * 13;
speed = 4;
direction = Direction.DOWN;
@@ -79,6 +79,7 @@ public class Player extends Entity {
// CHECK MONSTER COLLISION
int monsterIndex = panel.collisionH.checkEntity(this, panel.monster);
interactMonster(monsterIndex);
// CHECK EVENT
panel.eventH.checkEvent();
@@ -102,6 +103,25 @@ public class Player extends Entity {
spriteCounter = 0;
}
}
if(!invincible) return;
invincibleCount++;
if(invincibleCount > 60) {
invincible = false;
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));
}
graphics2d.drawImage(parseSprite(), screenX, screenY, null);
graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
}
public void pickObject(int index) {
@@ -115,8 +135,11 @@ public class Player extends Entity {
panel.npc[index].speak();
}
public void draw(Graphics2D graphics2d) {
graphics2d.drawImage(parseSprite(), screenX, screenY, null);
public void interactMonster(int index) {
if(index == 999) return;
if(invincible) return;
life -= 1;
invincible = true;
}
public void speak() {

View File

@@ -2,6 +2,7 @@ package de.miaurizius.jgame2d.entity.monster;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.enums.EntityType;
import de.miaurizius.jgame2d.entity.Entity;
import java.util.Random;
@@ -10,6 +11,7 @@ public class GreenSlimeMON extends Entity {
public GreenSlimeMON(GamePanel panel) {
super(panel);
type = EntityType.MONSTER;
name = "green-slime";
speed = 1;
maxLife = 4;