damagesystem.adoc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. = Damage Systems Example
  2. :revnumber: 2.0
  3. :revdate: 2020/7/27
  4. As written in the Entity System Advanced article, there should only be one class which changes one special Component.
  5. This makes the code easy to debug and prevents component changes from being skipped.
  6. == The Components
  7. === The HealthComponent
  8. [source,java]
  9. ----
  10. public class HealthComponent extends Component {
  11. public float health;
  12. public HealthComponent(float health)
  13. {
  14. this.health = health;
  15. }
  16. public float getHealth()
  17. {
  18. return health;
  19. }
  20. }
  21. ----
  22. === The DamageComponent
  23. [source,java]
  24. ----
  25. private float damage;
  26. private EntityId target;
  27. public DamageComponent(EntityId target, float damage)
  28. {
  29. this.damage=damage;
  30. this.target=target;
  31. }
  32. public float getDamage()
  33. {
  34. return damage;
  35. }
  36. public EntityId getTarget()
  37. {
  38. return target;
  39. }
  40. ----
  41. == The AppState
  42. [source,java]
  43. ----
  44. public class DamageAppState extends AbstractAppState {
  45. EntitySet damageSet;
  46. EntitySet healthSet;
  47. public void initialize(AppStateManager stateManager, Application app) {
  48. EntitySystem entitySystem = ((Main)app).getEntitySystem();
  49. damageSet = entitySystem.getEntitySet(DamageComponent.class);
  50. healthSet = entitySystem.getEntitySet(HealthComponent.class);
  51. }
  52. @Override
  53. public void update(float tpf) {
  54. Map<EntityId,Float> damageSummary = new HashMap();
  55. damageSet.applyChanges();
  56. healthSet.applyChanges();
  57. Iterator<Entity> iterator = damageSet.getIterator();
  58. while(iterator.hasNext())
  59. {
  60. Entity entity = iterator.next();
  61. DamageComponent dc = entity.getComponent(DamageComponent.class);
  62. float f = 0;
  63. if(damageSummary.containsKey(dc.getTarget()))
  64. {
  65. f = damageSummary.get(dc.getTarget());
  66. }
  67. f += dc.getDamage();
  68. damageSummary.put(dc.getTarget(),f);
  69. entity.destroy();
  70. }
  71. Iterator<EntityId> keyIterator = damageSummary.keySet().iterator();
  72. while(keyIterator.hasNext())
  73. {
  74. EntityId entityId = keyIterator.next();
  75. float damage = damageSummary.get(entityId);
  76. Entity entity = healthSet.getEntity(entityId);
  77. if(entity != null)
  78. {
  79. HealthComponent hc = entity.getComponent(HealthComponent.class);
  80. float newLife = hc.getHealth()-damage;
  81. if(newLife < 0)
  82. {
  83. entity.removeComponent(HealthComponent.class);
  84. }else{
  85. entity.setComponent(new HealthComponent(newLife));
  86. }
  87. }
  88. }
  89. }
  90. }
  91. ----
  92. == Usage
  93. [source,java]
  94. ----
  95. //Create a new Entity System
  96. entitySystem = new EntitySystem(new MapEntityData());
  97. //Add the DamageAppState to the Application
  98. stateManager.attach(new DamageAppState());
  99. //Create a new Entity with 100 HP
  100. EntityId healthEntity = entitySystem.newEntity(new HealthComponent(100));
  101. //Create a new Damage Entity who deals 5 damage to the health entity
  102. entitySystem.newEntity(new DamageComponent(healthEntity,5));
  103. ----