Allow NPCs to be damaged and killed

This commit is contained in:
Luc Lagarde
2017-12-31 18:26:44 -06:00
parent b21e33601e
commit b31c187ad5
4 changed files with 115 additions and 5 deletions

View File

@@ -8,10 +8,16 @@ public class HarvesterNPC extends NonPlayerCharacter {
public static final int ID = 10;
public static final int MAX_HEALTH = 100;
public static final int HEAL_RATE = 5;
public HarvesterNPC() {
setTask(new HarvestTask());
hp = 10;
setHp(MAX_HEALTH);
setMaxHp(MAX_HEALTH);
setHealRate(HEAL_RATE);
}
@Override
@@ -44,7 +50,7 @@ public class HarvesterNPC extends NonPlayerCharacter {
json.put("x", getX());
json.put("y", getY());
json.put("direction", getDirection().ordinal());
json.put("hp", hp);
json.put("hp", getHp());
json.put("energy", energy);
json.put("action", getAction().ordinal());
json.put("t", ID);
@@ -58,7 +64,7 @@ public class HarvesterNPC extends NonPlayerCharacter {
npc.setObjectId((long) json.get("i"));
npc.setX((int) (long) json.get("x"));
npc.setY((int) (long) json.get("y"));
npc.hp = (int) (long) json.get("hp");
npc.setHp((int) (long) json.get("hp"));
npc.setDirection(Direction.getDirection((int) (long) json.get("direction")));
npc.energy = (int) (long) json.get("energy");
npc.maxEnergy = GameServer.INSTANCE.getConfig().getInt("battery_max_energy");

View File

@@ -6,13 +6,14 @@ import net.simon987.server.game.Action;
import net.simon987.server.game.Direction;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Updatable;
import net.simon987.server.game.Attackable;
import net.simon987.server.game.pathfinding.Node;
import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.logging.LogManager;
import java.util.ArrayList;
public abstract class NonPlayerCharacter extends GameObject implements Updatable {
public abstract class NonPlayerCharacter extends GameObject implements Updatable, Attackable {
private static final int MAP_INFO = 0x0040;
@@ -20,8 +21,11 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable
public static final int LIFETIME = GameServer.INSTANCE.getConfig().getInt("npc_lifetime");
// Set these just in case they aren't overridden in the subclass
public static final int HP_MAX_DEFAULT = 100;
public static final int HP_REGEN_RATE_DEFAULT = 5;
//Unused
int hp;
int energy;
int maxEnergy;
@@ -48,6 +52,21 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable
*/
private int age = 0;
/**
* Current health of the npc
*/
private int hp = HP_MAX_DEFAULT;
/**
* Health regeneration rate of the npc
*/
private int hpRegenerationRate = HP_REGEN_RATE_DEFAULT;
/**
* Maximum health of the npc
*/
private int maxHp = HP_MAX_DEFAULT;
@Override
public char getMapInfo() {
return MAP_INFO;
@@ -66,6 +85,9 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable
selfDestroyNextTick = true;
}
//Heal the NPC
heal(hpRegenerationRate);
}
/**
@@ -158,6 +180,52 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable
}
}
@Override
public void setHealRate(int hp) {
hpRegenerationRate = hp;
}
@Override
public int getHp() {
return hp;
}
@Override
public void setHp(int hp) {
this.hp = hp;
}
@Override
public int getMaxHp() {
return maxHp;
}
@Override
public void setMaxHp(int hp) {
this.maxHp = hp;
this.hp = hp;
}
@Override
public void heal(int amount) {
hp += amount;
//Can't heal above max
if (hp > maxHp) {
hp = maxHp;
}
}
@Override
public void damage(int amount) {
hp -= amount;
//YOU ARE DEAD
if (hp <= 0) {
setDead(true);
}
}
public NPCTask getTask() {
return task;
}