damagesystem.adoc 3.4 KB

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