Compare commits

..

3 Commits

Author SHA1 Message Date
36ff9e8854 make monsters killable and playing soundeffects 2025-11-29 23:23:18 +01:00
b513370152 arranged large classes 2025-11-29 23:08:20 +01:00
0dde029719 added hit detection when player hits monsters 2025-11-29 22:57:39 +01:00
8 changed files with 165 additions and 81 deletions

View File

@@ -62,18 +62,12 @@ public class GamePanel extends JPanel implements Runnable {
this.setFocusable(true);
}
public void setupGame() {
assetSetter.setObject();
assetSetter.setNPC();
assetSetter.setMonster();
gameState = GameState.TITLE;
}
// GAME THREAD / CORE
public void startGameThread() {
gameThread = new Thread(this);
gameThread.start();
}
@Override
public void run() {
double drawInterval = (double) 1000000000 / FPS;
@@ -103,6 +97,7 @@ public class GamePanel extends JPanel implements Runnable {
}
}
// FRAME GENERATION
public void update() {
switch(gameState) {
case PLAY:
@@ -114,7 +109,6 @@ public class GamePanel extends JPanel implements Runnable {
break;
}
}
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2d = (Graphics2D) graphics;
@@ -159,19 +153,26 @@ public class GamePanel extends JPanel implements Runnable {
graphics.dispose();
}
// MUSIC
public void playMusic(int i) {
music.setFile(i);
music.play();
music.loop();
}
public void stopMusic() {
music.stop();
}
public void playSE(int i) {
se.setFile(i);
se.play();
}
// SETTING THINGS UP
public void setupGame() {
assetSetter.setObject();
assetSetter.setNPC();
assetSetter.setMonster();
gameState = GameState.TITLE;
}
}

View File

@@ -52,6 +52,7 @@ public class UI {
}
}
// HUD
public void drawPlayerLife() {
int x = panel.tileSize / 2;
int y = panel.tileSize / 2;
@@ -78,13 +79,13 @@ public class UI {
}
}
// GAME STATES
public void drawPauseScreen() {
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 80));
String text = "PAUSED";
int y = panel.screenHeight / 2;
graphics2d.drawString(text, getCenteredX(text), y);
}
public void drawDialogueScreen() {
// WINDOW
int x = panel.tileSize*2;
@@ -102,7 +103,6 @@ public class UI {
y += 40;
}
}
public void drawTitleScreen() {
graphics2d.setColor(new Color(0, 0, 0));
graphics2d.fillRect(0, 0, panel.screenWidth, panel.screenHeight);
@@ -142,6 +142,7 @@ public class UI {
if(commandNum == 2) graphics2d.drawString(">", x-panel.tileSize, y);
}
// UTILITY
public void drawSubWindow(int x, int y, int width, int height) {
graphics2d.setColor(new Color(0,0,0,210));
graphics2d.fillRoundRect(x, y, width, height, 35, 35);
@@ -149,7 +150,6 @@ public class UI {
graphics2d.setStroke(new BasicStroke(5));
graphics2d.drawRoundRect(x+5, y+5, width-10, height-10, 25, 25);
}
public int getCenteredX(String text) {
return panel.screenWidth / 2 - (int) graphics2d.getFontMetrics().getStringBounds(text, graphics2d).getWidth() / 2;
}

View File

@@ -11,6 +11,7 @@ public class CollisionHandler {
this.panel = panel;
}
// WORLD COLLISION
public void checkTile(Entity entity) {
int entityLeftWorldX = entity.worldX + entity.solidArea.x;
int entityRightWorldX = entity.worldX + entity.solidArea.x + entity.solidArea.width;
@@ -51,7 +52,6 @@ public class CollisionHandler {
break;
}
}
public int checkObject(Entity entity, boolean player) {
int index = 999;
int c = -1;
@@ -112,7 +112,6 @@ public class CollisionHandler {
}
return index;
}
public boolean checkPlayer(Entity entity) {
boolean contactPlayer = false;
entity.solidArea.x += entity.worldX;
@@ -135,6 +134,7 @@ public class CollisionHandler {
return contactPlayer;
}
// UTILITY
private void parseSolidArea(Entity entity) {
switch (entity.direction) {
case UP -> entity.solidArea.y -= entity.speed;

View File

@@ -22,6 +22,8 @@ public class Sound {
soundURL[2] = new File("assets/sounds/powerup.wav").toURI().toURL();
soundURL[3] = new File("assets/sounds/unlock.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) {
Boot.logger.log(Level.SEVERE, e.getMessage());
}

View File

@@ -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];
@@ -49,21 +50,7 @@ public class Entity {
this.panel = panel;
}
public void setAction() {}
public void speak() {
if(dialogue[dialogueIndex] == null) dialogueIndex = 0;
panel.ui.currentDialogue = dialogue[dialogueIndex];
dialogueIndex++;
switch(panel.player.direction) {
case UP -> direction = Direction.DOWN;
case DOWN -> direction = Direction.UP;
case LEFT -> direction = Direction.RIGHT;
case RIGHT -> direction = Direction.LEFT;
}
}
// DEFAULT
public void update() {
setAction();
collisionOn = false;
@@ -95,8 +82,15 @@ public class Entity {
else spriteNum = 0;
spriteCounter = 0;
}
}
// INVINCIBLE COUNTER
if(!invincible) return;
invincibleCount++;
if(invincibleCount > 40) {
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;
@@ -106,10 +100,38 @@ public class Entity {
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));
}
}
// INTERACTION
public void speak() {
if(dialogue[dialogueIndex] == null) dialogueIndex = 0;
panel.ui.currentDialogue = dialogue[dialogueIndex];
dialogueIndex++;
switch(panel.player.direction) {
case UP -> direction = Direction.DOWN;
case DOWN -> direction = Direction.UP;
case LEFT -> direction = Direction.RIGHT;
case RIGHT -> direction = Direction.LEFT;
}
}
public void setAction() {}
// SETTING THINGS UP
BufferedImage parseSprite() {
return switch (direction) {
case UP -> (spriteNum == 1) ? up1 : up2;
@@ -118,7 +140,14 @@ public class Entity {
case RIGHT -> (spriteNum == 1) ? right1 : right2;
};
}
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);
@@ -127,7 +156,6 @@ public class Entity {
}
return null;
}
public BufferedImage initEntitySprites(String name, int width, int height) {
try {
return Utility.scaleImage(ImageIO.read(new FileInputStream("assets/" + name + ".png")), width, height);

View File

@@ -2,6 +2,7 @@ package de.miaurizius.jgame2d.entity;
import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.EntityType;
import java.util.Random;
@@ -9,6 +10,8 @@ public class OldManNPC extends Entity {
public OldManNPC(GamePanel panel) {
super(panel);
type = EntityType.NPC;
name = "oldman-npc";
direction = Direction.DOWN;
speed = 1;

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;
@@ -18,6 +19,9 @@ public class Player extends Entity {
super(panel);
this.keyH = keyH;
name = "player";
type = EntityType.PLAYER;
screenX = panel.screenWidth/2 - panel.tileSize/2;
screenY = panel.screenHeight/2 - panel.tileSize/2;
@@ -29,29 +33,28 @@ public class Player extends Entity {
solidArea.width = 24;
solidArea.height = 24;
attackArea.width = 36;
attackArea.height = 36;
setDefaultValues();
getPlayerImage();
getPlayerAttackImage();
}
public void setDefaultValues() {
worldX = panel.tileSize * 23;
worldY = panel.tileSize * 21;
speed = 4;
direction = Direction.DOWN;
// PLAYER STATUS (1 heart = 2 lives)
maxLife = 6;
life = maxLife;
}
// DEFAULT
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;
@@ -91,6 +94,8 @@ public class Player extends Entity {
spriteCounter = 0;
}
}
// INVINCIBLE COUNTER
if(!invincible) return;
invincibleCount++;
if(invincibleCount > 60) {
@@ -99,42 +104,86 @@ public class Player extends Entity {
}
}
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));
}
// INTERACTION
public void pickObject(int index) {
if(index == 999) return;
}
public void interactNPC(int index) {
if(index == 999) 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();
}
public void interactMonster(int index) {
if(index == 999) return;
if(invincible) return;
life -= 1;
panel.playSE(6);
invincible = true;
}
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;
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) {
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();
}
public void speak() {
//This method only exists for character specific things later...
super.speak();
}
// SETTING THINGS UP
public void setDefaultValues() {
worldX = panel.tileSize * 23;
worldY = panel.tileSize * 21;
speed = 4;
direction = Direction.DOWN;
// PLAYER STATUS (1 heart = 2 lives)
maxLife = 6;
life = maxLife;
}
public void getPlayerImage() {
up1 = initEntitySprites("player/boy_up_1");
up2 = initEntitySprites("player/boy_up_2");
@@ -145,7 +194,6 @@ public class Player extends Entity {
right1 = initEntitySprites("player/boy_right_1");
right2 = initEntitySprites("player/boy_right_2");
}
public void getPlayerAttackImage() {
attackUp1 = initEntitySprites("player/attack/boy_attack_up_1", panel.tileSize, panel.tileSize*2);
attackUp2 = initEntitySprites("player/attack/boy_attack_up_2", panel.tileSize, panel.tileSize*2);

View File

@@ -27,17 +27,7 @@ public class GreenSlimeMON extends Entity {
getImage();
}
public void getImage() {
up1 = initEntitySprites("monster/greenslime_down_1");
up2 = initEntitySprites("monster/greenslime_down_2");
down1 = initEntitySprites("monster/greenslime_down_1");
down2 = initEntitySprites("monster/greenslime_down_2");
left1 = initEntitySprites("monster/greenslime_down_1");
left2 = initEntitySprites("monster/greenslime_down_2");
right1 = initEntitySprites("monster/greenslime_down_1");
right2 = initEntitySprites("monster/greenslime_down_2");
}
// INTERACTION
public void setAction() {
actionLock++;
if(actionLock != 120) return; //lock action for x frames
@@ -50,4 +40,16 @@ public class GreenSlimeMON extends Entity {
actionLock = 0;
}
// SETTING THINGS UP
public void getImage() {
up1 = initEntitySprites("monster/greenslime_down_1");
up2 = initEntitySprites("monster/greenslime_down_2");
down1 = initEntitySprites("monster/greenslime_down_1");
down2 = initEntitySprites("monster/greenslime_down_2");
left1 = initEntitySprites("monster/greenslime_down_1");
left2 = initEntitySprites("monster/greenslime_down_2");
right1 = initEntitySprites("monster/greenslime_down_1");
right2 = initEntitySprites("monster/greenslime_down_2");
}
}