added knockback to monsters
This commit is contained in:
@@ -151,6 +151,7 @@ public class KeyHandler implements KeyListener {
|
||||
}
|
||||
if(panel.ui.commandNum == 1) {
|
||||
panel.gameState = GameState.TITLE;
|
||||
panel.ui.commandNum = 0;
|
||||
panel.restart();
|
||||
panel.stopMusic();
|
||||
}
|
||||
|
||||
@@ -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,6 +90,29 @@ public class Entity {
|
||||
|
||||
// DEFAULT
|
||||
public void update() {
|
||||
if(knockback) {
|
||||
checkCollision();
|
||||
if(collisionOn) {
|
||||
knockbackCount = 0;
|
||||
knockback = false;
|
||||
speed = defaultSpeed;
|
||||
return;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
@@ -97,13 +124,14 @@ public class Entity {
|
||||
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);
|
||||
|
||||
@@ -4,7 +4,6 @@ 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.enums.Map;
|
||||
import de.miaurizius.jgame2d.core.handlers.KeyHandler;
|
||||
import de.miaurizius.jgame2d.entity.item.ShieldWoodObj;
|
||||
import de.miaurizius.jgame2d.entity.item.SwordNormalObj;
|
||||
@@ -95,18 +94,18 @@ public class Player extends Entity {
|
||||
if(keyH.spacePressed && !attackCancel) {
|
||||
panel.playSE(7);
|
||||
attacking = true;
|
||||
spriteCounter = 0;
|
||||
spriteCount = 0;
|
||||
}
|
||||
|
||||
attackCancel = false;
|
||||
panel.keyH.spacePressed = false;
|
||||
|
||||
spriteCounter++;
|
||||
if(spriteCounter > 12) {
|
||||
spriteCount++;
|
||||
if(spriteCount > 12) {
|
||||
if(spriteNum == 1) spriteNum = 2;
|
||||
else if(spriteNum == 2) spriteNum = 1;
|
||||
else spriteNum = 0;
|
||||
spriteCounter = 0;
|
||||
spriteCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,9 +167,9 @@ public class Player extends Entity {
|
||||
}
|
||||
public void attacking() {
|
||||
if(attackCancel) return;
|
||||
spriteCounter++;
|
||||
if(spriteCounter <= 5) spriteNum = 1;
|
||||
if(spriteCounter > 5 && spriteCounter <= 25) {
|
||||
spriteCount++;
|
||||
if(spriteCount <= 5) spriteNum = 1;
|
||||
if(spriteCount > 5 && spriteCount <= 25) {
|
||||
spriteNum = 2;
|
||||
int currentWorldX = worldX;
|
||||
int currentWorldY = worldY;
|
||||
@@ -187,7 +186,7 @@ public class Player extends Entity {
|
||||
solidArea.height = attackArea.height;
|
||||
|
||||
int monsterIndex = panel.collisionH.checkEntity(this, panel.monster[panel.currentMap.getIndex()]);
|
||||
damageMonster(monsterIndex, attack);
|
||||
damageMonster(monsterIndex, attack, currentWeapon.knockbackVal);
|
||||
|
||||
int iTileIndex = panel.collisionH.checkEntity(this, panel.iTile[panel.currentMap.getIndex()]);
|
||||
interactTile(iTileIndex);
|
||||
@@ -198,13 +197,13 @@ public class Player extends Entity {
|
||||
solidArea.height = solidAreaHeight;
|
||||
|
||||
}
|
||||
if(spriteCounter > 25) {
|
||||
if(spriteCount > 25) {
|
||||
spriteNum = 1;
|
||||
spriteCounter = 0;
|
||||
spriteCount = 0;
|
||||
attacking = false;
|
||||
}
|
||||
}
|
||||
public void damageMonster(int index, int attack) {
|
||||
public void damageMonster(int index, int attack, int knockbackVal) {
|
||||
if(index == 999) return;
|
||||
if(panel.monster[panel.currentMap.getIndex()][index].invincible) return;
|
||||
|
||||
@@ -212,6 +211,7 @@ public class Player extends Entity {
|
||||
|
||||
if(damage > 0) {
|
||||
panel.playSE(5);
|
||||
if(knockbackVal > 0) knockback(panel.monster[panel.currentMap.getIndex()][index], knockbackVal);
|
||||
panel.monster[panel.currentMap.getIndex()][index].life -= damage;
|
||||
panel.monster[panel.currentMap.getIndex()][index].invincible = true;
|
||||
}
|
||||
@@ -224,6 +224,12 @@ public class Player extends Entity {
|
||||
checkLevelUp();
|
||||
}
|
||||
}
|
||||
public void knockback(Entity entity, int knockbackVal) {
|
||||
entity.direction = direction;
|
||||
entity.speed += knockbackVal;
|
||||
entity.knockback = true;
|
||||
}
|
||||
|
||||
public void interactTile(int index) {
|
||||
if(index == 999 || !panel.iTile[panel.currentMap.getIndex()][index].destructible || panel.iTile[panel.currentMap.getIndex()][index].invincible) return;
|
||||
if(!panel.iTile[panel.currentMap.getIndex()][index].meetItemReq(this)) return;
|
||||
@@ -294,11 +300,8 @@ public class Player extends Entity {
|
||||
worldX = panel.tileSize * 23;
|
||||
worldY = panel.tileSize * 21;
|
||||
|
||||
// worldX = panel.tileSize * 12;
|
||||
// worldY = panel.tileSize * 12;
|
||||
// panel.currentMap = Map.HUT;
|
||||
|
||||
speed = 4;
|
||||
defaultSpeed = 4;
|
||||
speed = defaultSpeed;
|
||||
direction = Direction.DOWN;
|
||||
|
||||
// PLAYER STATUS (1 heart = 2 lives)
|
||||
|
||||
@@ -14,6 +14,7 @@ public class AxeObj extends Entity {
|
||||
weaponType = EntityType.WeaponType.AXE;
|
||||
down1 = initEntitySprites("/objects/axe");
|
||||
price = 50;
|
||||
knockbackVal = 10;
|
||||
|
||||
attackValue = 2;
|
||||
attackArea.width = 30;
|
||||
|
||||
@@ -15,6 +15,7 @@ public class SwordNormalObj extends Entity {
|
||||
weaponType = EntityType.WeaponType.SWORD;
|
||||
down1 = initEntitySprites("objects/sword_normal");
|
||||
price = 50;
|
||||
knockbackVal = 2;
|
||||
|
||||
attackValue = 1;
|
||||
attackArea.width = 36;
|
||||
|
||||
@@ -17,7 +17,8 @@ public class GreenSlimeMON extends Entity {
|
||||
super(panel);
|
||||
type = EntityType.MONSTER;
|
||||
name = "green-slime";
|
||||
speed = 1;
|
||||
defaultSpeed = 1;
|
||||
speed = defaultSpeed;
|
||||
maxLife = 4;
|
||||
life = maxLife;
|
||||
attack = 5;
|
||||
|
||||
@@ -8,6 +8,7 @@ import de.miaurizius.jgame2d.entity.Entity;
|
||||
public class Projectile extends Entity {
|
||||
|
||||
Entity user;
|
||||
//TODO: parry projectiles: https://youtu.be/8KitXuxcuqI?list=PL_QPQmz5C6WUF-pOQDsbsKbaBZqXj4qSq&t=75
|
||||
|
||||
public Projectile(GamePanel panel) {
|
||||
super(panel);
|
||||
@@ -27,7 +28,7 @@ public class Projectile extends Entity {
|
||||
if(user.type == EntityType.PLAYER) {
|
||||
int monsterIndex = panel.collisionH.checkEntity(this, panel.monster[panel.currentMap.getIndex()]);
|
||||
if(monsterIndex != 999) {
|
||||
panel.player.damageMonster(monsterIndex, attack);
|
||||
panel.player.damageMonster(monsterIndex, attack, knockbackVal);
|
||||
generateParticle(user.projectile, panel.monster[panel.currentMap.getIndex()][monsterIndex]);
|
||||
alive = false;
|
||||
}
|
||||
@@ -54,12 +55,12 @@ public class Projectile extends Entity {
|
||||
return;
|
||||
}
|
||||
|
||||
spriteCounter++;
|
||||
if(spriteCounter > 12) {
|
||||
spriteCount++;
|
||||
if(spriteCount > 12) {
|
||||
if(spriteNum == 1) spriteNum = 2;
|
||||
else if(spriteNum == 2) spriteNum = 1;
|
||||
else spriteNum = 0;
|
||||
spriteCounter = 0;
|
||||
spriteCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user