added knockback to monsters

This commit is contained in:
2025-12-12 23:45:57 +01:00
parent 2e73a012d1
commit 1d6f53a70e
8 changed files with 73 additions and 37 deletions

View File

@@ -41,16 +41,19 @@ public class Entity {
public boolean hpBarOn;
public boolean consumable;
public boolean onPath;
public boolean knockback;
// COUNTER
public int spriteCounter;
public int spriteCount;
public int actionLock;
public int invincibleCount;
int dyingCount;
int hpBarCounter;
int hpBarCount;
int knockbackCount;
// CHARACTER ATTRIBUTES
public EntityType type;
public int defaultSpeed;
public String name;
public int speed;
public int maxLife;
@@ -79,6 +82,7 @@ public class Entity {
public int useCost;
public int value;
public int price;
public int knockbackVal;
public Entity(GamePanel panel) {
this.panel = panel;
@@ -86,24 +90,48 @@ public class Entity {
// DEFAULT
public void update() {
setAction();
checkCollision();
if(knockback) {
checkCollision();
if(collisionOn) {
knockbackCount = 0;
knockback = false;
speed = defaultSpeed;
return;
}
if(!collisionOn) {
switch (direction) {
switch(panel.player.direction) {
case UP -> worldY -= speed;
case DOWN -> worldY += speed;
case LEFT ->worldX -= speed;
case RIGHT -> worldX += speed;
}
knockbackCount++;
if(knockbackCount != 10) return;
knockback = false;
knockbackCount = 0;
speed = defaultSpeed;
} else {
setAction();
checkCollision();
if(!collisionOn) {
switch (direction) {
case UP -> worldY -= speed;
case DOWN -> worldY += speed;
case LEFT ->worldX -= speed;
case RIGHT -> worldX += speed;
}
}
}
spriteCounter++;
if(spriteCounter > 24) {
spriteCount++;
if(spriteCount > 24) {
if(spriteNum == 1) spriteNum = 2;
else if(spriteNum == 2) spriteNum = 1;
else spriteNum = 0;
spriteCounter = 0;
spriteCount = 0;
}
// INVINCIBLE COUNTER
@@ -132,9 +160,9 @@ public class Entity {
graphics2d.setColor(new Color(255, 0, 30));
graphics2d.fillRect(screenX, screenY-5, (int) ((double) panel.tileSize/maxLife)*life, 10);
hpBarCounter++;
if(hpBarCounter > 600) { //bar disappears after 10 seconds
hpBarCounter = 0;
hpBarCount++;
if(hpBarCount > 600) { //bar disappears after 10 seconds
hpBarCount = 0;
hpBarOn = false;
}
}
@@ -142,7 +170,7 @@ public class Entity {
// DRAW ENTITY
if(invincible) {
hpBarOn = true;
hpBarCounter = 0;
hpBarCount = 0;
changeOpacity(graphics2d, 0.4f);
}
if(dying) dyingAnimation(graphics2d);