icon.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_15x16;
  7. }
  8. if (strcmp(name, "home") == 0)
  9. {
  10. return &I_icon_home_15x16;
  11. }
  12. if (strcmp(name, "info") == 0)
  13. {
  14. return &I_icon_info_15x16;
  15. }
  16. if (strcmp(name, "man") == 0)
  17. {
  18. return &I_icon_man_7x16;
  19. }
  20. if (strcmp(name, "plant") == 0)
  21. {
  22. return &I_icon_plant_16x16;
  23. }
  24. if (strcmp(name, "tree") == 0)
  25. {
  26. return &I_icon_tree_16x16;
  27. }
  28. if (strcmp(name, "woman") == 0)
  29. {
  30. return &I_icon_woman_9x16;
  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 2 units opposite their last movement direction
  48. pos.x -= player->dx * 2;
  49. pos.y -= player->dy * 2;
  50. entity_pos_set(other, pos);
  51. // Reset player's movement direction to prevent immediate re-collision
  52. player->dx = 0;
  53. player->dy = 0;
  54. }
  55. }
  56. }
  57. static void icon_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  58. {
  59. UNUSED(manager);
  60. IconContext *icon_ctx = (IconContext *)context;
  61. Vector pos = entity_pos_get(self);
  62. canvas_draw_icon(canvas, pos.x - camera_x - icon_ctx->width / 2, pos.y - camera_y - icon_ctx->height / 2, icon_ctx->icon);
  63. }
  64. static void icon_start(Entity *self, GameManager *manager, void *context)
  65. {
  66. UNUSED(manager);
  67. IconContext *icon_ctx = (IconContext *)context;
  68. // Just add the collision rectangle for 16x16 icon
  69. entity_collider_add_rect(self, icon_ctx->width + COLLISION_BOX_PADDING_HORIZONTAL, icon_ctx->height + COLLISION_BOX_PADDING_VERTICAL);
  70. }
  71. const EntityDescription icon_desc = {
  72. .start = icon_start,
  73. .stop = NULL,
  74. .update = NULL,
  75. .render = icon_render,
  76. .collision = icon_collision,
  77. .event = NULL,
  78. .context_size = sizeof(IconContext),
  79. };