DefaultHealth.java 2.1 KB

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