game_map.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "game_map.h"
  2. #include "sdl_helper.h"
  3. // init map
  4. void game_map_init(struct game_map *m) {
  5. // background
  6. m->bg_tex = load_image("images/map_0.png");
  7. /* currently all platforms have fixed width
  8. * height is only meant for visual reasons
  9. * maybe it will be replaced with the texture's
  10. * actual height
  11. */
  12. m->grid_width = 72;
  13. m->grid_height = 36;
  14. // sample data
  15. m->tex_top = load_image("images/map_0_platform_top.png");
  16. m->tex_mid = load_image("images/map_0_platform_bottom.png");
  17. m->rect.x = 0;
  18. m->rect.y = 0;
  19. m->rect.w = m->grid_width;
  20. m->rect.h = m->grid_height;
  21. // give some height on the platforms for testing purposes
  22. int lowest = 0;
  23. for (int i = 0; i < 10; i++) {
  24. if (i < 5) {
  25. m->grid[i] = GAME_HEIGHT/2 -(GAME_HEIGHT/4) *i/10;
  26. }
  27. else {
  28. m->grid[i] = GAME_HEIGHT/4 +(GAME_HEIGHT/3) *i/10;
  29. }
  30. m->grid_actual[i] = m->grid[i];
  31. if (m->grid[i] > m->grid[lowest]) {
  32. lowest = i;
  33. }
  34. }
  35. // starting water level is on the first grid for now, should be lowest grid
  36. m->water_rect.x = 0;
  37. m->water_rect.y = m->water_actual = m->grid[lowest] +m->grid_width/2;
  38. m->water_rect.w = 640;
  39. m->water_rect.h = 0;
  40. m->water_delay = 0;
  41. m->water_speed = 1;
  42. m->offset_x = 0;
  43. m->offset_y = 0;
  44. } // init
  45. // all platforms on the map, keep approaching their targets constantly
  46. void game_map_update(struct game_map *m) {
  47. // update grids and keep lowest
  48. int lowest = 0;
  49. int highest = 0;
  50. for (int i = 0; i < 10; i++) {
  51. m->grid[i] = m->grid[i] +(m->grid_actual[i] -m->grid[i]) *0.1;
  52. if (m->grid[i] > m->grid[lowest]) {
  53. lowest = i;
  54. }
  55. if (m->grid[i] < m->grid[highest]) {
  56. highest = i;
  57. }
  58. }
  59. // water keeps rising to an offset of the highest grid
  60. if (m->grid[highest] +24 +300 < m->water_actual) {
  61. m->water_actual = m->grid[highest] +24 +300;
  62. }
  63. else
  64. // water keeps rising to the lowest grid
  65. if (m->grid[lowest] +24 < m->water_actual) {
  66. m->water_actual = m->grid[lowest] +24;
  67. }
  68. else
  69. // or water keeps rising passively
  70. if (++m->water_delay >= 40) {
  71. m->water_delay = 0;
  72. m->water_actual -= m->water_speed;
  73. m->water_speed += 0.1;
  74. }
  75. m->water_rect.y = m->water_rect.y +(m->water_actual -m->water_rect.y) *0.1;
  76. }
  77. /* draw the map, one platform at a time
  78. */
  79. void game_map_draw(struct game_map *m) {
  80. // background
  81. m->rect.w = 48;
  82. for (int x = m->offset_x %m->rect.w -m->rect.w; x < GAME_WIDTH ; x += m->rect.w)
  83. for (int y = m->offset_y %m->rect.h -m->rect.h; y < GAME_HEIGHT; y += m->rect.h) {
  84. m->rect.x = x;
  85. m->rect.y = y;
  86. SDL_RenderCopy(ren, m->bg_tex, NULL, &m->rect);
  87. }
  88. // draw platforms
  89. m->rect.w = m->grid_width;
  90. for (int i = 0; i < 10; i++) {
  91. // draw the top part
  92. m->rect.x = m->offset_x +i *m->grid_width;
  93. m->rect.y = m->offset_y +m->grid[i] -m->rect.h/2;
  94. SDL_RenderCopy(ren, m->tex_top, NULL, &m->rect);
  95. // draw the middle parts until it's below water
  96. m->rect.y += m->rect.h;
  97. m->rect.h *= 2;
  98. while (m->rect.y < m->water_rect.y +m->offset_y) {
  99. SDL_RenderCopy(ren, m->tex_mid, NULL, &m->rect);
  100. m->rect.y += m->rect.h;
  101. }
  102. m->rect.h /= 2;
  103. }
  104. } // draw
  105. /* draw front layer of the map,
  106. * this is meant to cover objects like players
  107. * currently used for the water, but could add more
  108. * things later
  109. */
  110. void game_map_draw_front(struct game_map *m) {
  111. // draw water
  112. m->water_rect.y += m->offset_y;
  113. m->water_rect.h = 480 -m->water_rect.y;
  114. SDL_SetRenderDrawColor(ren, 80, 140, 200, 255);
  115. SDL_RenderFillRect(ren, &m->water_rect);
  116. m->water_rect.y -= m->offset_y;
  117. }
  118. // get ground height
  119. int game_map_platform_y(struct game_map *m, int index) {
  120. if (index < 0 || index >= 10) return GAME_HEIGHT *2;
  121. return m->offset_y +m->grid[index];
  122. }
  123. // get ground index
  124. int game_map_platform_id(struct game_map *m, int player_loc) {
  125. float grid_index = (player_loc -m->offset_x) /m->grid_width;
  126. if (player_loc < 0) grid_index = -1;
  127. if (grid_index < 0) grid_index = -1;
  128. else if (grid_index >= 10) grid_index = 10;
  129. return (int) grid_index;
  130. }
  131. int game_map_platform_x(struct game_map *m, int index) {
  132. return m->offset_x +index *m->grid_width;
  133. }
  134. void game_map_offset(struct game_map *s, int x, int y) {
  135. s->offset_x += x;
  136. s->offset_y += y;
  137. }
  138. void game_map_platform_rise(struct game_map *s, int index, int amount) {
  139. if (index < 0 || index > 9) return;
  140. s->grid_actual[index] += amount;
  141. }
  142. int game_map_water_y(struct game_map *s) {
  143. return s->water_actual +s->offset_y;
  144. }
  145. int game_map_platform_width(struct game_map *s) {
  146. return s->grid_width;
  147. }
  148. int game_map_platform_number(struct game_map *s) {
  149. return 10;
  150. }