diff --git a/assets/Data/creatures.json b/assets/Data/creatures.json index ed6ee009..bb8841ae 100644 --- a/assets/Data/creatures.json +++ b/assets/Data/creatures.json @@ -99,6 +99,10 @@ "damageEndFrame" : 50 } ], + "healthSystem" : { + "maxHealth" : 100, + "onDamageIFrames" : 30 + }, "modelPath" : "Models/person1animpass2.fbx" }, @@ -203,6 +207,10 @@ "damageEndFrame" : 30 } ], + "healthSystem" : { + "maxHealth" : 100, + "onDamageIFrames" : 30 + }, "modelPath" : "Models/goblin1.fbx" } diff --git a/src/main/java/electrosphere/entity/EntityDataStrings.java b/src/main/java/electrosphere/entity/EntityDataStrings.java index 1bd45a01..37fac8ed 100644 --- a/src/main/java/electrosphere/entity/EntityDataStrings.java +++ b/src/main/java/electrosphere/entity/EntityDataStrings.java @@ -109,6 +109,11 @@ public class EntityDataStrings { public static final String ATTACK_MOVE_TYPE_ACTIVE = "attackMoveTypeActive"; public static final String ATTACK_MOVE_TYPE_MELEE_SWING_ONE_HAND = "MELEE_WEAPON_SWING_ONE_HAND"; + /* + Health System + */ + public static final String LIFE_STATE = "lifeState"; + /* idle behavior tree */ diff --git a/src/main/java/electrosphere/entity/EntityManager.java b/src/main/java/electrosphere/entity/EntityManager.java index 941ca72d..44c8ae72 100644 --- a/src/main/java/electrosphere/entity/EntityManager.java +++ b/src/main/java/electrosphere/entity/EntityManager.java @@ -22,6 +22,7 @@ public class EntityManager { static CopyOnWriteArrayList attachList = new CopyOnWriteArrayList(); static CopyOnWriteArrayList attackerList = new CopyOnWriteArrayList(); static CopyOnWriteArrayList creatureList = new CopyOnWriteArrayList(); + static CopyOnWriteArrayList lifeStateList = new CopyOnWriteArrayList(); public EntityManager(){ @@ -96,6 +97,14 @@ public class EntityManager { return creatureList; } + public void registerLifeStateEntity(Entity e){ + lifeStateList.add(e); + } + + public CopyOnWriteArrayList getLifeStateEntities(){ + return lifeStateList; + } + public void deregisterEntity(Entity e){ if(lightList.contains(e)){ lightList.remove(e); @@ -122,6 +131,9 @@ public class EntityManager { if(creatureList.contains(e)){ creatureList.remove(e); } + if(lifeStateList.contains(e)){ + lifeStateList.remove(e); + } } public void overrideEntityId(Entity e, int id){ diff --git a/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java b/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java index 6f3cdba0..2629ed13 100644 --- a/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java +++ b/src/main/java/electrosphere/entity/types/creature/CreatureUtils.java @@ -10,6 +10,7 @@ import electrosphere.game.server.creature.type.CreatureType; import electrosphere.game.server.creature.type.MovementSystem; import electrosphere.entity.state.AttackTree; import electrosphere.entity.state.IdleTree; +import electrosphere.entity.types.life.LifeState; import electrosphere.game.server.creature.type.AttackMove; import electrosphere.main.Globals; import electrosphere.main.Main; @@ -81,6 +82,7 @@ public class CreatureUtils { break; } } + //add all attack moves for(AttackMove attackMove : rawType.getAttackMoves()){ switch(attackMove.getType()){ case EntityDataStrings.ATTACK_MOVE_TYPE_MELEE_SWING_ONE_HAND: @@ -88,6 +90,10 @@ public class CreatureUtils { break; } } + //add health system + rVal.putData(EntityDataStrings.LIFE_STATE, new LifeState(rVal, rawType.getHealthSystem())); + Globals.entityManager.registerLifeStateEntity(rVal); + //idle tree & generic stuff all creatures have rVal.putData(EntityDataStrings.IDLE_TREE, new IdleTree(rVal)); Globals.entityManager.registerCreatureEntity(rVal); rVal.putData(EntityDataStrings.DATA_STRING_CREATURE_IS_CREATURE, true); diff --git a/src/main/java/electrosphere/entity/types/hitbox/HitboxUtils.java b/src/main/java/electrosphere/entity/types/hitbox/HitboxUtils.java index bb04e781..8d181e2a 100644 --- a/src/main/java/electrosphere/entity/types/hitbox/HitboxUtils.java +++ b/src/main/java/electrosphere/entity/types/hitbox/HitboxUtils.java @@ -5,6 +5,7 @@ import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityUtils; import electrosphere.entity.types.attach.AttachUtils; import electrosphere.entity.types.item.ItemUtils; +import electrosphere.entity.types.life.LifeUtils; import electrosphere.main.Globals; import org.joml.Matrix4f; import org.joml.Quaternionf; @@ -116,10 +117,18 @@ public class HitboxUtils { if(isItem){ if(hitboxAttachParent != hurtboxParent){ - EntityUtils.getPosition(hurtboxParent).set(Globals.spawnPoint); + LifeUtils.getLifeState(hurtboxParent).damage(20); + if(!LifeUtils.getLifeState(hurtboxParent).isIsAlive()){ + EntityUtils.getPosition(hurtboxParent).set(Globals.spawnPoint); + LifeUtils.getLifeState(hurtboxParent).revive(); + } } } else { - EntityUtils.getPosition(hurtboxParent).set(Globals.spawnPoint); + LifeUtils.getLifeState(hurtboxParent).damage(20); + if(!LifeUtils.getLifeState(hurtboxParent).isIsAlive()){ + EntityUtils.getPosition(hurtboxParent).set(Globals.spawnPoint); + LifeUtils.getLifeState(hurtboxParent).revive(); + } } } diff --git a/src/main/java/electrosphere/entity/types/life/AliveManager.java b/src/main/java/electrosphere/entity/types/life/AliveManager.java deleted file mode 100644 index 24bf0e80..00000000 --- a/src/main/java/electrosphere/entity/types/life/AliveManager.java +++ /dev/null @@ -1,33 +0,0 @@ -package electrosphere.entity.types.life; - -import electrosphere.entity.Entity; -import java.util.ArrayList; -import java.util.List; - -/** - * - * @author amaterasu - */ -public class AliveManager { - List aliveList = new ArrayList(); - List deadList = new ArrayList(); - List invulnerableList = new ArrayList(); - - public List getAliveEntities(){ - return aliveList; - } - - public List getDeadEntities(){ - return deadList; - } - - public List getInvulnerableEntities(){ - return invulnerableList; - } - - - public void registerAliveEntity(Entity e){ - aliveList.add(e); - } - -} diff --git a/src/main/java/electrosphere/entity/types/life/LifeState.java b/src/main/java/electrosphere/entity/types/life/LifeState.java new file mode 100644 index 00000000..f02e3b41 --- /dev/null +++ b/src/main/java/electrosphere/entity/types/life/LifeState.java @@ -0,0 +1,112 @@ +package electrosphere.entity.types.life; + +import electrosphere.entity.Entity; +import electrosphere.game.server.creature.type.HealthSystem; + +public class LifeState { + + Entity parent; + + boolean isAlive; + boolean isInvincible; + int lifeCurrent; + int lifeMax; + int iFrameMaxCount; + int iFrameCurrent; + + public LifeState(Entity parent, HealthSystem system){ + this.parent = parent; + isAlive = true; + isInvincible = false; + lifeMax = system.getMaxHealth(); + lifeCurrent = lifeMax; + iFrameMaxCount = system.getOnDamageIFrames(); + iFrameCurrent = 0; + } + + public LifeState(Entity parent, boolean isAlive, boolean isInvincible, int lifeCurrent, int lifeMax, int iFrameMaxCount) { + this.parent = parent; + this.isAlive = isAlive; + this.isInvincible = isInvincible; + this.lifeCurrent = lifeCurrent; + this.lifeMax = lifeMax; + this.iFrameMaxCount = iFrameMaxCount; + } + + public boolean isIsAlive() { + return isAlive; + } + + public boolean isIsInvincible() { + return isInvincible; + } + + public int getLifeCurrent() { + return lifeCurrent; + } + + public int getLifeMax() { + return lifeMax; + } + + public void setIsAlive(boolean isAlive) { + this.isAlive = isAlive; + } + + public void setIsInvincible(boolean isInvincible) { + this.isInvincible = isInvincible; + } + + public void setLifeCurrent(int lifeCurrent) { + this.lifeCurrent = lifeCurrent; + } + + public void setLifeMax(int lifeMax) { + this.lifeMax = lifeMax; + } + + public int getiFrameMaxCount() { + return iFrameMaxCount; + } + + public int getiFrameCurrent() { + return iFrameCurrent; + } + + public void setiFrameMaxCount(int iFrameMaxCount) { + this.iFrameMaxCount = iFrameMaxCount; + } + + public void setiFrameCurrent(int iFrameCurrent) { + this.iFrameCurrent = iFrameCurrent; + } + + public void damage(int damage){ + if(!isInvincible){ + lifeCurrent = lifeCurrent - damage; + isInvincible = true; + if(lifeCurrent < 0){ + lifeCurrent = 0; + isAlive = false; + } else { + iFrameCurrent = iFrameMaxCount; + } + } + } + + public void revive(){ + isAlive = true; + isInvincible = false; + lifeCurrent = lifeMax; + } + + public void simulate(){ + if(iFrameCurrent > 0){ + iFrameCurrent--; + if(iFrameCurrent == 0){ + isInvincible = false; + } + } + } + +} diff --git a/src/main/java/electrosphere/entity/types/life/LifeUtils.java b/src/main/java/electrosphere/entity/types/life/LifeUtils.java new file mode 100644 index 00000000..a8c8e23b --- /dev/null +++ b/src/main/java/electrosphere/entity/types/life/LifeUtils.java @@ -0,0 +1,16 @@ +package electrosphere.entity.types.life; + +import electrosphere.entity.Entity; +import electrosphere.entity.EntityDataStrings; + +/** + * + * @author amaterasu + */ +public class LifeUtils { + + + public static LifeState getLifeState(Entity e){ + return (LifeState)e.getData(EntityDataStrings.LIFE_STATE); + } +} diff --git a/src/main/java/electrosphere/game/server/creature/type/CreatureType.java b/src/main/java/electrosphere/game/server/creature/type/CreatureType.java index 59601ad8..a6471b77 100644 --- a/src/main/java/electrosphere/game/server/creature/type/CreatureType.java +++ b/src/main/java/electrosphere/game/server/creature/type/CreatureType.java @@ -10,6 +10,7 @@ public class CreatureType { List tokens; List movementSystems; List attackMoves; + HealthSystem healthSystem; String modelPath; public String getName() { @@ -39,6 +40,10 @@ public class CreatureType { public List getAttackMoves() { return attackMoves; } + + public HealthSystem getHealthSystem() { + return healthSystem; + } diff --git a/src/main/java/electrosphere/game/server/creature/type/HealthSystem.java b/src/main/java/electrosphere/game/server/creature/type/HealthSystem.java new file mode 100644 index 00000000..c26d0390 --- /dev/null +++ b/src/main/java/electrosphere/game/server/creature/type/HealthSystem.java @@ -0,0 +1,26 @@ +package electrosphere.game.server.creature.type; + +/** + * + * @author amaterasu + */ +public class HealthSystem { + int maxHealth; + int onDamageIFrames; + + public int getMaxHealth() { + return maxHealth; + } + + public int getOnDamageIFrames() { + return onDamageIFrames; + } + + public HealthSystem clone(){ + HealthSystem rVal = new HealthSystem(); + rVal.maxHealth = maxHealth; + rVal.onDamageIFrames = onDamageIFrames; + return rVal; + } + +} diff --git a/src/main/java/electrosphere/game/state/MicroSimulation.java b/src/main/java/electrosphere/game/state/MicroSimulation.java index 9e6d5d4d..99e2dc19 100644 --- a/src/main/java/electrosphere/game/state/MicroSimulation.java +++ b/src/main/java/electrosphere/game/state/MicroSimulation.java @@ -9,6 +9,8 @@ import electrosphere.entity.state.MovementTree; import electrosphere.entity.types.creature.CreatureUtils; import electrosphere.entity.types.hitbox.HitboxUtils; import electrosphere.entity.types.item.ItemUtils; +import electrosphere.entity.types.life.LifeState; +import electrosphere.entity.types.life.LifeUtils; import electrosphere.main.Globals; import static electrosphere.main.Main.deltaTime; import electrosphere.renderer.Actor; @@ -53,6 +55,11 @@ public class MicroSimulation { IdleTree idleTree = CreatureUtils.getIdleTree(currentIdler); idleTree.simulate(); } + //life state updates + for(Entity lifeStateEntity : Globals.entityManager.getLifeStateEntities()){ + LifeState lifeState = LifeUtils.getLifeState(lifeStateEntity); + lifeState.simulate(); + } //update attached entity positions AttachUtils.updateAttachedEntityPositions(); //update hitbox positions diff --git a/src/main/java/electrosphere/main/Globals.java b/src/main/java/electrosphere/main/Globals.java index 8ec10a48..bb6453cf 100644 --- a/src/main/java/electrosphere/main/Globals.java +++ b/src/main/java/electrosphere/main/Globals.java @@ -17,7 +17,6 @@ import electrosphere.game.client.player.ClientPlayerData; import electrosphere.game.client.terrain.manager.ClientTerrainManager; import electrosphere.game.client.world.ClientWorldData; import electrosphere.game.collision.CommonWorldData; -import electrosphere.entity.types.life.AliveManager; import electrosphere.engine.LoadingThread; import electrosphere.game.server.creature.type.model.CreatureTypeMap; import electrosphere.game.server.item.type.model.ItemTypeMap; @@ -207,9 +206,6 @@ public class Globals { //ui text box for loading text public static TextBox loadingBox; - //life status entity manager - public static AliveManager aliveManager; - //collision world data public static CommonWorldData commonWorldData; @@ -255,8 +251,6 @@ public class Globals { widgetManager = new WidgetManager(); //hitbox manager hitboxManager = new HitboxManager(); - //alive manager - aliveManager = new AliveManager(); } public static void initDefaultGraphicalResources(){