|
@@ -1,10 +1,6 @@
|
|
= Damage Systems Example
|
|
= Damage Systems Example
|
|
-:author:
|
|
|
|
-:revnumber:
|
|
|
|
-:revdate: 2016/03/17 20:48
|
|
|
|
-:relfileprefix: ../../../../
|
|
|
|
-:imagesdir: ../../../..
|
|
|
|
-ifdef::env-github,env-browser[:outfilesuffix: .adoc]
|
|
|
|
|
|
+:revnumber: 2.0
|
|
|
|
+:revdate: 2020/7/27
|
|
|
|
|
|
|
|
|
|
As written in the Entity System Advanced article, there should only be one class which changes one special Component.
|
|
As written in the Entity System Advanced article, there should only be one class which changes one special Component.
|
|
@@ -20,19 +16,19 @@ This makes the code easy to debug and prevents component changes from being skip
|
|
----
|
|
----
|
|
|
|
|
|
public class HealthComponent extends Component {
|
|
public class HealthComponent extends Component {
|
|
-
|
|
|
|
|
|
+
|
|
public float health;
|
|
public float health;
|
|
-
|
|
|
|
|
|
+
|
|
public HealthComponent(float health)
|
|
public HealthComponent(float health)
|
|
{
|
|
{
|
|
this.health = health;
|
|
this.health = health;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public float getHealth()
|
|
public float getHealth()
|
|
{
|
|
{
|
|
return health;
|
|
return health;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
----
|
|
----
|
|
@@ -45,18 +41,18 @@ public class HealthComponent extends Component {
|
|
|
|
|
|
private float damage;
|
|
private float damage;
|
|
private EntityId target;
|
|
private EntityId target;
|
|
-
|
|
|
|
|
|
+
|
|
public DamageComponent(EntityId target, float damage)
|
|
public DamageComponent(EntityId target, float damage)
|
|
{
|
|
{
|
|
this.damage=damage;
|
|
this.damage=damage;
|
|
this.target=target;
|
|
this.target=target;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public float getDamage()
|
|
public float getDamage()
|
|
{
|
|
{
|
|
return damage;
|
|
return damage;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public EntityId getTarget()
|
|
public EntityId getTarget()
|
|
{
|
|
{
|
|
return target;
|
|
return target;
|
|
@@ -71,54 +67,54 @@ public class HealthComponent extends Component {
|
|
----
|
|
----
|
|
|
|
|
|
public class DamageAppState extends AbstractAppState {
|
|
public class DamageAppState extends AbstractAppState {
|
|
-
|
|
|
|
|
|
+
|
|
EntitySet damageSet;
|
|
EntitySet damageSet;
|
|
EntitySet healthSet;
|
|
EntitySet healthSet;
|
|
-
|
|
|
|
|
|
+
|
|
public void initialize(AppStateManager stateManager, Application app) {
|
|
public void initialize(AppStateManager stateManager, Application app) {
|
|
EntitySystem entitySystem = ((Main)app).getEntitySystem();
|
|
EntitySystem entitySystem = ((Main)app).getEntitySystem();
|
|
damageSet = entitySystem.getEntitySet(DamageComponent.class);
|
|
damageSet = entitySystem.getEntitySet(DamageComponent.class);
|
|
healthSet = entitySystem.getEntitySet(HealthComponent.class);
|
|
healthSet = entitySystem.getEntitySet(HealthComponent.class);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void update(float tpf) {
|
|
public void update(float tpf) {
|
|
-
|
|
|
|
|
|
+
|
|
Map<EntityId,Float> damageSummary = new HashMap();
|
|
Map<EntityId,Float> damageSummary = new HashMap();
|
|
-
|
|
|
|
|
|
+
|
|
damageSet.applyChanges();
|
|
damageSet.applyChanges();
|
|
healthSet.applyChanges();
|
|
healthSet.applyChanges();
|
|
-
|
|
|
|
|
|
+
|
|
Iterator<Entity> iterator = damageSet.getIterator();
|
|
Iterator<Entity> iterator = damageSet.getIterator();
|
|
while(iterator.hasNext())
|
|
while(iterator.hasNext())
|
|
{
|
|
{
|
|
Entity entity = iterator.next();
|
|
Entity entity = iterator.next();
|
|
DamageComponent dc = entity.getComponent(DamageComponent.class);
|
|
DamageComponent dc = entity.getComponent(DamageComponent.class);
|
|
-
|
|
|
|
|
|
+
|
|
float f = 0;
|
|
float f = 0;
|
|
if(damageSummary.containsKey(dc.getTarget()))
|
|
if(damageSummary.containsKey(dc.getTarget()))
|
|
{
|
|
{
|
|
f = damageSummary.get(dc.getTarget());
|
|
f = damageSummary.get(dc.getTarget());
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
f += dc.getDamage();
|
|
f += dc.getDamage();
|
|
damageSummary.put(dc.getTarget(),f);
|
|
damageSummary.put(dc.getTarget(),f);
|
|
-
|
|
|
|
|
|
+
|
|
entity.destroy();
|
|
entity.destroy();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
Iterator<EntityId> keyIterator = damageSummary.keySet().iterator();
|
|
Iterator<EntityId> keyIterator = damageSummary.keySet().iterator();
|
|
while(keyIterator.hasNext())
|
|
while(keyIterator.hasNext())
|
|
{
|
|
{
|
|
EntityId entityId = keyIterator.next();
|
|
EntityId entityId = keyIterator.next();
|
|
float damage = damageSummary.get(entityId);
|
|
float damage = damageSummary.get(entityId);
|
|
-
|
|
|
|
|
|
+
|
|
Entity entity = healthSet.getEntity(entityId);
|
|
Entity entity = healthSet.getEntity(entityId);
|
|
if(entity != null)
|
|
if(entity != null)
|
|
{
|
|
{
|
|
HealthComponent hc = entity.getComponent(HealthComponent.class);
|
|
HealthComponent hc = entity.getComponent(HealthComponent.class);
|
|
float newLife = hc.getHealth()-damage;
|
|
float newLife = hc.getHealth()-damage;
|
|
-
|
|
|
|
|
|
+
|
|
if(newLife < 0)
|
|
if(newLife < 0)
|
|
{
|
|
{
|
|
entity.removeComponent(HealthComponent.class);
|
|
entity.removeComponent(HealthComponent.class);
|
|
@@ -128,7 +124,7 @@ public class DamageAppState extends AbstractAppState {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
----
|
|
----
|
|
@@ -141,13 +137,13 @@ public class DamageAppState extends AbstractAppState {
|
|
|
|
|
|
//Create a new Entity System
|
|
//Create a new Entity System
|
|
entitySystem = new EntitySystem(new MapEntityData());
|
|
entitySystem = new EntitySystem(new MapEntityData());
|
|
-
|
|
|
|
|
|
+
|
|
//Add the DamageAppState to the Application
|
|
//Add the DamageAppState to the Application
|
|
stateManager.attach(new DamageAppState());
|
|
stateManager.attach(new DamageAppState());
|
|
-
|
|
|
|
|
|
+
|
|
//Create a new Entity with 100 HP
|
|
//Create a new Entity with 100 HP
|
|
EntityId healthEntity = entitySystem.newEntity(new HealthComponent(100));
|
|
EntityId healthEntity = entitySystem.newEntity(new HealthComponent(100));
|
|
-
|
|
|
|
|
|
+
|
|
//Create a new Damage Entity who deals 5 damage to the health entity
|
|
//Create a new Damage Entity who deals 5 damage to the health entity
|
|
entitySystem.newEntity(new DamageComponent(healthEntity,5));
|
|
entitySystem.newEntity(new DamageComponent(healthEntity,5));
|
|
|
|
|