added projectile system and sample fireball projectile

This commit is contained in:
2025-12-07 00:07:47 +01:00
parent d639e65c15
commit 5da7c43edf
17 changed files with 126 additions and 6 deletions

View File

@@ -49,6 +49,7 @@ public class GamePanel extends JPanel implements Runnable {
public Entity[] obj = new Entity[10];
public Entity[] npc = new Entity[10];
public Entity[] monster = new Entity[20];
public ArrayList<Entity> projectileList = new ArrayList<>();
ArrayList<Entity> entityList = new ArrayList<>();
// GAME STATE
@@ -109,6 +110,13 @@ public class GamePanel extends JPanel implements Runnable {
if(!m.alive) monster[i] = null;
}
}
for(int i = 0; i < projectileList.size(); i++) {
Entity m = projectileList.get(i);
if(m != null) {
if(m.alive) m.update();
else projectileList.remove(i);
}
}
break;
case PAUSE:
break;
@@ -136,6 +144,7 @@ public class GamePanel extends JPanel implements Runnable {
for(Entity entity : npc) if(entity != null) entityList.add(entity);
for(Entity entity : obj) if(entity != null) entityList.add(entity);
for(Entity entity : monster) if(entity != null) entityList.add(entity);
for(Entity entity : projectileList) if(entity != null) entityList.add(entity);
entityList.sort(Comparator.comparingInt(o -> o.worldY));
for(Entity entity : entityList) entity.draw(graphics2d);
entityList.clear();

View File

@@ -7,6 +7,7 @@ public enum EntityType {
MONSTER,
ITEM,
WORLD,
PROJECTILE,
WEAPON,
SHIELD;

View File

@@ -8,7 +8,7 @@ import java.awt.event.KeyListener;
public class KeyHandler implements KeyListener {
public boolean upPressed, downPressed, leftPressed, rightPressed, spacePressed;
public boolean upPressed, downPressed, leftPressed, rightPressed, spacePressed, shotKeyPressed;
public GamePanel panel;
public boolean debug;
@@ -49,6 +49,7 @@ public class KeyHandler implements KeyListener {
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> leftPressed = true;
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> rightPressed = true;
case KeyEvent.VK_SPACE -> spacePressed = true;
case KeyEvent.VK_F -> shotKeyPressed = true;
// DEBUG OPTIONS
case KeyEvent.VK_T -> debug = !debug;
@@ -142,6 +143,7 @@ public class KeyHandler implements KeyListener {
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> downPressed = false;
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> leftPressed = false;
case KeyEvent.VK_D, KeyEvent.VK_RIGHT -> rightPressed = false;
case KeyEvent.VK_F -> shotKeyPressed = false;
}
}
}

View File

@@ -26,6 +26,7 @@ public class Sound {
load(7, "assets/sounds/blocked.wav");
load(8, "assets/sounds/levelup.wav");
load(9, "assets/sounds/cursor.wav");
load(10, "assets/sounds/burning.wav");
}
@Deprecated

View File

@@ -5,6 +5,7 @@ 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 de.miaurizius.jgame2d.entity.projectile.Projectile;
import javax.imageio.ImageIO;
import java.awt.*;
@@ -59,14 +60,18 @@ public class Entity {
public int exp;
public int nextLevelExp;
public int coins;
public int maxMana;
public int mana;
public Entity currentWeapon;
public Entity currentShield;
public Projectile projectile;
// ITEM ATTRIBUTES
public EntityType.WeaponType weaponType;
public int attackValue;
public int defenseValue;
public String description;
public int useCost;
public Entity(GamePanel panel) {
this.panel = panel;
@@ -187,7 +192,6 @@ public class Entity {
if(dyingCount > incr*6 && dyingCount <= incr*7) changeOpacity(graphics2d, 0f);
if(dyingCount > incr*7 && dyingCount <= incr*8) changeOpacity(graphics2d, 1f);
if(dyingCount > incr*8) {
dying = false;
alive = false;
}
}

View File

@@ -7,6 +7,7 @@ import de.miaurizius.jgame2d.core.enums.GameState;
import de.miaurizius.jgame2d.core.handlers.KeyHandler;
import de.miaurizius.jgame2d.entity.item.ShieldWoodObj;
import de.miaurizius.jgame2d.entity.item.SwordNormalObj;
import de.miaurizius.jgame2d.entity.projectile.FireballObj;
import java.awt.*;
import java.util.ArrayList;
@@ -106,6 +107,12 @@ public class Player extends Entity {
}
}
if(panel.keyH.shotKeyPressed && !projectile.alive) {
projectile.set(worldX, worldY, direction, true, this);
panel.projectileList.add(projectile);
panel.playSE(10);
}
// INVINCIBLE COUNTER
if(!invincible) return;
invincibleCount++;
@@ -130,8 +137,7 @@ public class Player extends Entity {
public void interactMonster(int index) {
if(index == 999) return;
if(invincible) return;
if(panel.monster[index].dying || !panel.monster[index].alive) return;
if(invincible || panel.monster[index].dying || !panel.monster[index].alive) return;
int damage = panel.monster[index].attack - defense;
@@ -163,7 +169,7 @@ public class Player extends Entity {
solidArea.height = attackArea.height;
int monsterIndex = panel.collisionH.checkEntity(this, panel.monster);
damageMonster(monsterIndex);
damageMonster(monsterIndex, attack);
worldX = currentWorldX;
worldY = currentWorldY;
@@ -177,7 +183,7 @@ public class Player extends Entity {
attacking = false;
}
}
public void damageMonster(int index) {
public void damageMonster(int index, int attack) {
if(index == 999) return;
if(panel.monster[index].invincible) return;
@@ -262,6 +268,7 @@ public class Player extends Entity {
coins = 0;
currentWeapon = new SwordNormalObj(panel);
currentShield = new ShieldWoodObj(panel);
projectile = new FireballObj(panel);
attack = getAttack();
defense = getDefense();

View File

@@ -0,0 +1,35 @@
package de.miaurizius.jgame2d.entity.projectile;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.EntityType;
public class FireballObj extends Projectile {
GamePanel panel;
public FireballObj(GamePanel panel) {
super(panel);
this.panel = panel;
name = "Fireball";
type = EntityType.PROJECTILE;
speed = 5;
maxLife = 80;
life = maxLife;
attack = 2;
useCost = 1;
alive = false;
// INITIALISATION OF IMAGES
up1 = initEntitySprites("/projectile/fireball_up_1");
up2 = initEntitySprites("/projectile/fireball_up_2");
down1 = initEntitySprites("/projectile/fireball_down_1");
down2 = initEntitySprites("/projectile/fireball_down_2");
left1 = initEntitySprites("/projectile/fireball_left_1");
left2 = initEntitySprites("/projectile/fireball_left_2");
right1 = initEntitySprites("/projectile/fireball_right_1");
right2 = initEntitySprites("/projectile/fireball_right_2");
}
}

View File

@@ -0,0 +1,61 @@
package de.miaurizius.jgame2d.entity.projectile;
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;
public class Projectile extends Entity {
Entity user;
public Projectile(GamePanel panel) {
super(panel);
}
public void set(int worldX, int worldY, Direction direction, boolean alive, Entity user) {
this.worldX = worldX;
this.worldY = worldY;
this.direction = direction;
this.alive = alive;
this.user = user;
this.life = maxLife;
}
public void update() {
if(user.type == EntityType.PLAYER) {
int monsterIndex = panel.collisionH.checkEntity(this, panel.monster);
if(monsterIndex != 999) {
panel.player.damageMonster(monsterIndex, attack);
alive = false;
}
}
if(user.type == EntityType.MONSTER) {
}
switch(direction) {
case UP -> worldY -= speed;
case DOWN -> worldY += speed;
case LEFT -> worldX -= speed;
case RIGHT -> worldX += speed;
}
life--;
if(life <= 0) {
alive = false;
return;
}
spriteCounter++;
if(spriteCounter > 12) {
if(spriteNum == 1) spriteNum = 2;
else if(spriteNum == 2) spriteNum = 1;
else spriteNum = 0;
spriteCounter = 0;
}
}
}