icon.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "game/icon.h"
  2. const Icon *get_icon(char *name)
  3. {
  4. if (strcmp(name, "earth") == 0)
  5. {
  6. return &I_icon_earth;
  7. }
  8. if (strcmp(name, "home") == 0)
  9. {
  10. return &I_icon_home;
  11. }
  12. if (strcmp(name, "info") == 0)
  13. {
  14. return &I_icon_info;
  15. }
  16. if (strcmp(name, "man") == 0)
  17. {
  18. return &I_icon_man;
  19. }
  20. if (strcmp(name, "plant") == 0)
  21. {
  22. return &I_icon_plant;
  23. }
  24. if (strcmp(name, "tree") == 0)
  25. {
  26. return &I_icon_tree;
  27. }
  28. if (strcmp(name, "woman") == 0)
  29. {
  30. return &I_icon_woman;
  31. }
  32. return NULL;
  33. }
  34. // Icon entity description
  35. static void icon_collision(Entity *self, Entity *other, GameManager *manager, void *context)
  36. {
  37. UNUSED(manager);
  38. UNUSED(self);
  39. IconContext *icon = (IconContext *)context;
  40. UNUSED(icon);
  41. if (entity_description_get(other) == &player_desc)
  42. {
  43. PlayerContext *player = (PlayerContext *)entity_context_get(other);
  44. if (player)
  45. {
  46. Vector pos = entity_pos_get(other);
  47. // Bounce the player back by 3 units opposite their last movement direction
  48. pos.x -= player->dx * 3;
  49. pos.y -= player->dy * 3;
  50. entity_pos_set(other, pos);
  51. }
  52. }
  53. }
  54. static void icon_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  55. {
  56. UNUSED(manager);
  57. IconContext *icon_ctx = (IconContext *)context;
  58. Vector pos = entity_pos_get(self);
  59. canvas_draw_icon(canvas, pos.x - camera_x - 8, pos.y - camera_y - 8, icon_ctx->icon);
  60. }
  61. static void icon_start(Entity *self, GameManager *manager, void *context)
  62. {
  63. UNUSED(manager);
  64. UNUSED(context);
  65. // Just add the collision rectangle for 16x16 icon
  66. entity_collider_add_rect(self, 16, 16);
  67. }
  68. const EntityDescription icon_desc = {
  69. .start = icon_start,
  70. .stop = NULL,
  71. .update = NULL,
  72. .render = icon_render,
  73. .collision = icon_collision,
  74. .event = NULL,
  75. .context_size = sizeof(IconContext),
  76. };