game_item.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "game_item.h"
  2. #include "sdl_helper.h"
  3. #include "game_math.h"
  4. #include <time.h>
  5. #include "game_map.h"
  6. #define SPAWN_FREQUENCY 100
  7. // initialise texture for items, their size, and all inactive
  8. void game_item_manager_init(struct game_item_manager *s) {
  9. srand((unsigned int) time(NULL));
  10. // image stats are constant
  11. s->tex = load_image("images/crystal.png");
  12. s->tex_rect.x = 0;
  13. s->tex_rect.y = 0;
  14. s->tex_rect.w = 48;
  15. s->tex_rect.h = 48;
  16. // all items have the same size
  17. for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
  18. s->active_item[i] = 0;
  19. s->item[i].w = 48;
  20. s->item[i].h = 48;
  21. }
  22. // starting countdown
  23. s->new_item_countdown = SPAWN_FREQUENCY;
  24. }
  25. // add a new item at location x y, only if there is space for new items
  26. void game_item_manager_add_item(struct game_item_manager *s, int x, int y) {
  27. for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
  28. if (s->active_item[i] == 0) {
  29. s->item[i].x = x;
  30. s->item[i].y = y;
  31. s->active_item[i] = 1;
  32. break;
  33. }
  34. }
  35. }
  36. void game_item_manager_update(struct game_item_manager *s) {
  37. for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
  38. if (s->active_item[i] != 0) {
  39. s->item[i].y += 1;
  40. }
  41. }
  42. }
  43. // scroll all items based on given x and y
  44. void game_item_manager_scroll(struct game_item_manager *s, int x, int y) {
  45. for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
  46. if (s->active_item[i] != 0) {
  47. s->item[i].x -= x;
  48. s->item[i].y -= y;
  49. }
  50. }
  51. s->new_item_countdown += y;
  52. if (s->new_item_countdown <= 0) {
  53. game_item_manager_add_item(s,
  54. game_map_platform_x(s->map, 0) +rand()
  55. %(game_map_platform_x(s->map,
  56. game_map_platform_number(s->map)-1)),
  57. -s->item[0].h);
  58. s->new_item_countdown = SPAWN_FREQUENCY;
  59. }
  60. }
  61. // draw all active items
  62. void game_item_manager_draw(struct game_item_manager *s) {
  63. for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
  64. if (s->active_item[i] != 0) {
  65. SDL_RenderCopy(ren, s->tex, &s->tex_rect, &s->item[i]);
  66. }
  67. }
  68. }
  69. void game_item_manager_remove(struct game_item_manager *s, int index) { s->active_item[index] = 0; }
  70. /* check all items for collision
  71. * on success, remove item and provide effect
  72. */
  73. int game_item_manager_collide(struct game_item_manager *s, struct game_player *p) {
  74. // for all items
  75. for (int i = 0; i < ITEM_MANAGER_MAX_ITEMS; i++) {
  76. // item is active
  77. if (s->active_item[i] != 0) {
  78. // player touched it
  79. if (game_math_collide(&s->item[i], &p->rect)) {
  80. // remove item
  81. game_item_manager_remove(s, i);
  82. // do effect ?
  83. p->level++;
  84. // item touched
  85. return 1;
  86. }
  87. }
  88. }
  89. // no item touched
  90. return 0;
  91. }