DefaultHealth.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package me.hammerle.supersnuvi.entity.components;
  2. import me.hammerle.supersnuvi.Game;
  3. import me.hammerle.supersnuvi.entity.Entity;
  4. import me.hammerle.supersnuvi.tiles.Tile;
  5. import me.hammerle.supersnuvi.util.SoundUtils;
  6. import me.hammerle.supersnuvi.util.SoundUtils.Sound;
  7. public class DefaultHealth extends Health
  8. {
  9. private final float maxHealth;
  10. private float health;
  11. private int hurtTicks = 0;
  12. private int invincibility = 0;
  13. private final IDeath death;
  14. private final Sound soundHeal;
  15. private final Sound soundHurt;
  16. private final Sound soundDeath;
  17. public DefaultHealth(Entity ent, IDeath death, float maxHealth,
  18. Sound soundHeal, Sound soundHurt, Sound soundDeath)
  19. {
  20. super(ent);
  21. this.death = death;
  22. this.maxHealth = maxHealth;
  23. this.health = maxHealth;
  24. this.soundHeal = soundHeal;
  25. this.soundHurt = soundHurt;
  26. this.soundDeath = soundDeath;
  27. }
  28. @Override
  29. public void tick()
  30. {
  31. if(invincibility > 0)
  32. {
  33. invincibility--;
  34. }
  35. if(hurtTicks > 0)
  36. {
  37. hurtTicks--;
  38. }
  39. else if(hurtTicks < 0)
  40. {
  41. hurtTicks++;
  42. }
  43. }
  44. @Override
  45. public boolean isDead()
  46. {
  47. return health <= 0.0f;
  48. }
  49. @Override
  50. public boolean shouldDespawn()
  51. {
  52. return (isDead() && !ent.isAnimated());
  53. }
  54. @Override
  55. public void onDespawn()
  56. {
  57. death.onDeath(ent);
  58. }
  59. @Override
  60. public boolean wasHurt()
  61. {
  62. return hurtTicks > 0;
  63. }
  64. @Override
  65. public boolean wasHealed()
  66. {
  67. return hurtTicks < 0;
  68. }
  69. @Override
  70. public float getMaxHealth()
  71. {
  72. return maxHealth;
  73. }
  74. @Override
  75. public float getHealth()
  76. {
  77. return health;
  78. }
  79. @Override
  80. public void addHealth(float h)
  81. {
  82. if(h < 0)
  83. {
  84. if(invincibility > 0)
  85. {
  86. return;
  87. }
  88. invincibility = Game.getTicksForMillis(500);
  89. hurtTicks = Game.getTicksForMillis(500);
  90. SoundUtils.playSound(soundHurt);
  91. }
  92. else
  93. {
  94. hurtTicks = -Game.getTicksForMillis(250);
  95. SoundUtils.playSound(soundHeal);
  96. }
  97. health += h;
  98. if(health > maxHealth)
  99. {
  100. health = maxHealth;
  101. }
  102. else if(health < 0.0f)
  103. {
  104. health = 0.0f;
  105. SoundUtils.playSound(soundDeath);
  106. }
  107. }
  108. }