123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package me.hammerle.supersnuvi.entity.components;
- import me.hammerle.supersnuvi.Game;
- import me.hammerle.supersnuvi.entity.Entity;
- import me.hammerle.supersnuvi.tiles.Tile;
- import me.hammerle.supersnuvi.util.SoundUtils;
- import me.hammerle.supersnuvi.util.SoundUtils.Sound;
- public class DefaultHealth extends Health
- {
- private final float maxHealth;
- private float health;
-
- private int hurtTicks = 0;
- private int invincibility = 0;
-
- private final IDeath death;
-
- private final Sound soundHeal;
- private final Sound soundHurt;
- private final Sound soundDeath;
-
- public DefaultHealth(Entity ent, IDeath death, float maxHealth,
- Sound soundHeal, Sound soundHurt, Sound soundDeath)
- {
- super(ent);
- this.death = death;
- this.maxHealth = maxHealth;
- this.health = maxHealth;
- this.soundHeal = soundHeal;
- this.soundHurt = soundHurt;
- this.soundDeath = soundDeath;
- }
- @Override
- public void tick()
- {
- if(invincibility > 0)
- {
- invincibility--;
- }
-
- if(hurtTicks > 0)
- {
- hurtTicks--;
- }
- else if(hurtTicks < 0)
- {
- hurtTicks++;
- }
-
- if(shouldDespawn())
- {
- death.onDeath(ent);
- }
- }
- @Override
- public boolean isDead()
- {
- return health <= 0.0f;
- }
-
- @Override
- public boolean shouldDespawn()
- {
- return (isDead() && !ent.isAnimated()) || (ent.getY() > ent.getLevel().getHeight() * Tile.SIZE);
- }
-
- @Override
- public boolean wasHurt()
- {
- return hurtTicks > 0;
- }
- @Override
- public boolean wasHealed()
- {
- return hurtTicks < 0;
- }
- @Override
- public float getMaxHealth()
- {
- return maxHealth;
- }
- @Override
- public float getHealth()
- {
- return health;
- }
- @Override
- public void addHealth(float h)
- {
- if(h < 0)
- {
- if(invincibility > 0)
- {
- return;
- }
- invincibility = Game.getTicksForMillis(500);
- hurtTicks = Game.getTicksForMillis(500);
- SoundUtils.playSound(soundHurt);
- }
- else
- {
- hurtTicks = -Game.getTicksForMillis(250);
- SoundUtils.playSound(soundHeal);
- }
- health += h;
- if(health > maxHealth)
- {
- health = maxHealth;
- }
- else if(health < 0.0f)
- {
- health = 0.0f;
- SoundUtils.playSound(soundDeath);
- }
- }
- }
|