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

@@ -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;
}
}
}