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

@@ -31,7 +31,8 @@ public class GamePanel extends JPanel implements Runnable {
public final int maxWorldRow = 50;
//FPS
int FPS = 60;
final int FPS = 60;
int fpsMeasure;
// SYSTEM
public TileManager tileM = new TileManager(this);
@@ -96,7 +97,7 @@ public class GamePanel extends JPanel implements Runnable {
drawCount++;
}
if(timer >= 1000000000) {
System.out.println("FPS: " + drawCount);
fpsMeasure = drawCount;
drawCount = 0;
timer = 0;
}
@@ -150,8 +151,9 @@ public class GamePanel extends JPanel implements Runnable {
if(keyH.checkDrawTime) {
graphics2d.setColor(Color.white);
graphics2d.drawString("Draw Time: " + passed, 10, 400);
System.out.println("Draw Time: " + passed);
Logger.getLogger("DEBUG").log(Level.FINE, "Draw Time: " + passed);
graphics2d.drawString("FPS: " + fpsMeasure, 10, 400+tileSize);
graphics2d.drawString("Invincible: " + player.invincibleCount, 10, 400+tileSize*2);
Boot.logger.log(Level.FINE, "Draw Time: " + passed);
}
// END

View File

@@ -0,0 +1,9 @@
package de.miaurizius.jgame2d.core.enums;
public enum EntityType {
PLAYER,
NPC,
MONSTER,
}

View File

@@ -3,7 +3,6 @@ package de.miaurizius.jgame2d.core.handlers;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.entity.OldManNPC;
import de.miaurizius.jgame2d.entity.monster.GreenSlimeMON;
import de.miaurizius.jgame2d.entity.objects.DoorObj;
public class AssetSetter {
@@ -18,31 +17,31 @@ public class AssetSetter {
}
public void setNPC() {
// panel.npc[0] = new OldManNPC(panel);
// panel.npc[0].worldX = panel.tileSize*21;
// panel.npc[0].worldY = panel.tileSize*21;
panel.npc[0] = new OldManNPC(panel);
panel.npc[0].worldX = panel.tileSize*9;
panel.npc[0].worldY = panel.tileSize*10;
panel.npc[0].worldX = panel.tileSize*21;
panel.npc[0].worldY = panel.tileSize*21;
// panel.npc[0] = new OldManNPC(panel);
// panel.npc[0].worldX = panel.tileSize*9;
// panel.npc[0].worldY = panel.tileSize*10;
}
public void setMonster() {
// panel.monster[0] = new GreenSlimeMON(panel);
// panel.monster[0].worldX = panel.tileSize*23;
// panel.monster[0].worldY = panel.tileSize*36;
//
// panel.monster[1] = new GreenSlimeMON(panel);
// panel.monster[1].worldX = panel.tileSize*23;
// panel.monster[1].worldY = panel.tileSize*37;
panel.monster[0] = new GreenSlimeMON(panel);
panel.monster[0].worldX = panel.tileSize*11;
panel.monster[0].worldY = panel.tileSize*10;
panel.monster[0].worldX = panel.tileSize*23;
panel.monster[0].worldY = panel.tileSize*36;
panel.monster[1] = new GreenSlimeMON(panel);
panel.monster[1].worldX = panel.tileSize*11;
panel.monster[1].worldY = panel.tileSize*11;
panel.monster[1].worldX = panel.tileSize*23;
panel.monster[1].worldY = panel.tileSize*37;
// panel.monster[0] = new GreenSlimeMON(panel);
// panel.monster[0].worldX = panel.tileSize*11;
// panel.monster[0].worldY = panel.tileSize*10;
//
// panel.monster[1] = new GreenSlimeMON(panel);
// panel.monster[1].worldX = panel.tileSize*11;
// panel.monster[1].worldY = panel.tileSize*11;
}
}

View File

@@ -113,7 +113,8 @@ public class CollisionHandler {
return index;
}
public void checkPlayer(Entity entity) {
public boolean checkPlayer(Entity entity) {
boolean contactPlayer = false;
entity.solidArea.x += entity.worldX;
entity.solidArea.y += entity.worldY;
@@ -122,12 +123,16 @@ public class CollisionHandler {
parseSolidArea(entity);
if (entity.solidArea.intersects(panel.player.solidArea)) entity.collisionOn = true;
if (entity.solidArea.intersects(panel.player.solidArea)) {
entity.collisionOn = true;
contactPlayer = true;
}
entity.solidArea.x = entity.solidAreaDefaultX;
entity.solidArea.y = entity.solidAreaDefaultY;
panel.player.solidArea.x = panel.player.solidAreaDefaultX;
panel.player.solidArea.y = panel.player.solidAreaDefaultY;
return contactPlayer;
}
private void parseSolidArea(Entity entity) {

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;