health.c 820 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "health.h"
  2. #include "graphics.h"
  3. #include "tutorial.h"
  4. health *get_health(float max, float position, float size, sound *hit) {
  5. health *h = c_new(health);
  6. h->max = max;
  7. h->value = max;
  8. h->display = max;
  9. h->position = position;
  10. h->size = size;
  11. h->hit = hit;
  12. return h;
  13. }
  14. declare_draw(health, {
  15. float x = parent->position.x - self->size * 0.5f;
  16. float y = parent->position.y + self->position;
  17. draw_rect((color) { 1.f, 0.f, 0.f}, x, y, self->size * (self->display / self->max), 2);
  18. })
  19. declare_tick(health, {
  20. self->display += (self->value - self->display) * 0.5f;
  21. if(self->value <= 0) {
  22. save_tutorial_step(10);
  23. return 1;
  24. }
  25. })
  26. declare_head(health)
  27. void damage(entity *e, float amount) {
  28. health *h = get_component(e, health);
  29. if(h) {
  30. h->value -= amount;
  31. if(h->hit) play_sound(h->hit);
  32. }
  33. }