world.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include <game/world.h>
  2. void draw_bounds(Canvas *canvas)
  3. {
  4. // Draw the outer bounds adjusted by camera offset
  5. // we draw this last to ensure users can see the bounds
  6. canvas_draw_frame(canvas, -camera_x, -camera_y, WORLD_WIDTH, WORLD_HEIGHT);
  7. }
  8. bool draw_json_world(Level *level, const char *json_data)
  9. {
  10. for (int i = 0; i < MAX_WORLD_OBJECTS; i++)
  11. {
  12. char *data = get_json_array_value("json_data", i, json_data);
  13. if (data == NULL)
  14. {
  15. break;
  16. }
  17. char *icon = get_json_value("icon", data);
  18. char *x = get_json_value("x", data);
  19. char *y = get_json_value("y", data);
  20. char *width = get_json_value("width", data);
  21. char *height = get_json_value("height", data);
  22. char *amount = get_json_value("amount", data);
  23. char *horizontal = get_json_value("horizontal", data);
  24. if (icon == NULL || x == NULL || y == NULL || width == NULL || height == NULL || amount == NULL || horizontal == NULL)
  25. {
  26. return false;
  27. }
  28. // if amount is less than 2, we spawn a single icon
  29. if (atoi(amount) < 2)
  30. {
  31. spawn_icon(level, get_icon(icon), atoi(x), atoi(y), atoi(width), atoi(height));
  32. free(data);
  33. free(icon);
  34. free(x);
  35. free(y);
  36. free(width);
  37. free(height);
  38. free(amount);
  39. free(horizontal);
  40. continue;
  41. }
  42. spawn_icon_line(level, get_icon(icon), atoi(x), atoi(y), atoi(width), atoi(height), atoi(amount), strcmp(horizontal, "true") == 0);
  43. free(data);
  44. free(icon);
  45. free(x);
  46. free(y);
  47. free(width);
  48. free(height);
  49. free(amount);
  50. free(horizontal);
  51. }
  52. return true;
  53. }
  54. bool draw_json_world_furi(Level *level, FuriString *json_data)
  55. {
  56. for (int i = 0; i < MAX_WORLD_OBJECTS; i++)
  57. {
  58. FuriString *data = get_json_array_value_furi("json_data", i, json_data);
  59. if (data == NULL)
  60. {
  61. break;
  62. }
  63. FuriString *icon = get_json_value_furi("icon", data);
  64. FuriString *x = get_json_value_furi("x", data);
  65. FuriString *y = get_json_value_furi("y", data);
  66. FuriString *width = get_json_value_furi("width", data);
  67. FuriString *height = get_json_value_furi("height", data);
  68. FuriString *amount = get_json_value_furi("amount", data);
  69. FuriString *horizontal = get_json_value_furi("horizontal", data);
  70. if (!icon || !x || !y || !width || !height || !amount || !horizontal)
  71. {
  72. furi_string_free(data);
  73. furi_string_free(icon);
  74. furi_string_free(x);
  75. furi_string_free(y);
  76. furi_string_free(width);
  77. furi_string_free(height);
  78. furi_string_free(amount);
  79. furi_string_free(horizontal);
  80. return false;
  81. }
  82. // if amount is less than 2, we spawn a single icon
  83. if (atoi(furi_string_get_cstr(amount)) < 2)
  84. {
  85. spawn_icon(
  86. level,
  87. get_icon_furi(icon),
  88. atoi(furi_string_get_cstr(x)),
  89. atoi(furi_string_get_cstr(y)),
  90. atoi(furi_string_get_cstr(width)),
  91. atoi(furi_string_get_cstr(height)));
  92. furi_string_free(data);
  93. furi_string_free(icon);
  94. furi_string_free(x);
  95. furi_string_free(y);
  96. furi_string_free(width);
  97. furi_string_free(height);
  98. furi_string_free(amount);
  99. furi_string_free(horizontal);
  100. continue;
  101. }
  102. spawn_icon_line(
  103. level,
  104. get_icon_furi(icon),
  105. atoi(furi_string_get_cstr(x)),
  106. atoi(furi_string_get_cstr(y)),
  107. atoi(furi_string_get_cstr(width)),
  108. atoi(furi_string_get_cstr(height)),
  109. atoi(furi_string_get_cstr(amount)),
  110. furi_string_cmp(horizontal, "true") == 0);
  111. furi_string_free(data);
  112. furi_string_free(icon);
  113. furi_string_free(x);
  114. furi_string_free(y);
  115. furi_string_free(width);
  116. furi_string_free(height);
  117. furi_string_free(amount);
  118. furi_string_free(horizontal);
  119. }
  120. return true;
  121. }
  122. void draw_example_world(Level *level)
  123. {
  124. spawn_icon(level, get_icon("earth"), 112, 56, 15, 16);
  125. spawn_icon(level, get_icon("home"), 128, 24, 15, 16);
  126. spawn_icon(level, get_icon("info"), 144, 24, 15, 16);
  127. spawn_icon(level, get_icon("man"), 160, 56, 7, 16);
  128. spawn_icon(level, get_icon("woman"), 168, 56, 9, 16);
  129. spawn_icon(level, get_icon("plant"), 168, 32, 16, 16);
  130. }
  131. void draw_tree_world(Level *level)
  132. {
  133. // Spawn two full left/up tree lines
  134. for (int i = 0; i < 2; i++)
  135. {
  136. // Horizontal line of 22 icons
  137. spawn_icon_line(level, get_icon("tree"), 5, 2 + i * 17, 16, 16, 22, true);
  138. // Vertical line of 11 icons
  139. spawn_icon_line(level, get_icon("tree"), 5 + i * 17, 2, 16, 16, 11, false);
  140. }
  141. // Spawn two full down tree lines
  142. for (int i = 9; i < 11; i++)
  143. {
  144. // Horizontal line of 22 icons
  145. spawn_icon_line(level, get_icon("tree"), 5, 2 + i * 17, 16, 16, 22, true);
  146. }
  147. // Spawn two full right tree lines
  148. for (int i = 20; i < 22; i++)
  149. {
  150. // Vertical line of 8 icons starting further down (y=50)
  151. spawn_icon_line(level, get_icon("tree"), 5 + i * 17, 50, 16, 16, 8, false);
  152. }
  153. // Labyrinth lines
  154. // Third line (14 left, then a gap, then 3 middle)
  155. spawn_icon_line(level, get_icon("tree"), 5, 2 + 2 * 17, 16, 16, 14, true);
  156. spawn_icon_line(level, get_icon("tree"), 5 + 16 * 17, 2 + 2 * 17, 16, 16, 3, true);
  157. // Fourth line (3 left, 6 middle, 4 right)
  158. spawn_icon_line(level, get_icon("tree"), 5, 2 + 3 * 17, 16, 16, 3, true); // 3 left
  159. spawn_icon_line(level, get_icon("tree"), 5 + 7 * 17, 2 + 3 * 17, 16, 16, 6, true); // 6 middle
  160. spawn_icon_line(level, get_icon("tree"), 5 + 15 * 17, 2 + 3 * 17, 16, 16, 4, true); // 4 right
  161. // Fifth line (6 left, 7 middle)
  162. spawn_icon_line(level, get_icon("tree"), 5, 2 + 4 * 17, 16, 16, 6, true);
  163. spawn_icon_line(level, get_icon("tree"), 5 + 7 * 17, 2 + 4 * 17, 16, 16, 7, true);
  164. // Sixth line (5 left, 3 middle, 7 right)
  165. spawn_icon_line(level, get_icon("tree"), 5, 2 + 5 * 17, 16, 16, 5, true); // 5 left
  166. spawn_icon_line(level, get_icon("tree"), 5 + 7 * 17, 2 + 5 * 17, 16, 16, 3, true); // 3 middle
  167. spawn_icon_line(level, get_icon("tree"), 5 + 15 * 17, 2 + 5 * 17, 16, 16, 7, true); // 7 right
  168. // Seventh line (0 left, 7 middle, 4 right)
  169. spawn_icon_line(level, get_icon("tree"), 5 + 6 * 17, 2 + 6 * 17, 16, 16, 7, true); // 7 middle
  170. spawn_icon_line(level, get_icon("tree"), 5 + 14 * 17, 2 + 6 * 17, 16, 16, 4, true); // 4 right
  171. // Eighth line (4 left, 3 middle, 4 right)
  172. spawn_icon_line(level, get_icon("tree"), 5, 2 + 7 * 17, 16, 16, 4, true); // 4 left
  173. spawn_icon_line(level, get_icon("tree"), 5 + 7 * 17, 2 + 7 * 17, 16, 16, 3, true); // 3 middle
  174. spawn_icon_line(level, get_icon("tree"), 5 + 15 * 17, 2 + 7 * 17, 16, 16, 4, true); // 4 right
  175. // Ninth line (3 left, 1 middle, 3 right)
  176. spawn_icon_line(level, get_icon("tree"), 5, 2 + 8 * 17, 16, 16, 3, true); // 3 left
  177. spawn_icon_line(level, get_icon("tree"), 5 + 5 * 17, 2 + 8 * 17, 16, 16, 1, true); // 1 middle
  178. spawn_icon_line(level, get_icon("tree"), 5 + 11 * 17, 2 + 8 * 17, 16, 16, 3, true); // 3 right
  179. }
  180. void draw_town_world(Level *level)
  181. {
  182. // house-fence group 1
  183. spawn_icon(level, get_icon("house"), 148, 36, 48, 32);
  184. spawn_icon(level, get_icon("fence"), 148, 72, 16, 8);
  185. spawn_icon(level, get_icon("fence"), 164, 72, 16, 8);
  186. spawn_icon(level, get_icon("fence_end"), 180, 72, 16, 8);
  187. // house-fence group 4 (the left of group 1)
  188. spawn_icon(level, get_icon("house"), 96, 36, 48, 32);
  189. spawn_icon(level, get_icon("fence"), 96, 72, 16, 8);
  190. spawn_icon(level, get_icon("fence"), 110, 72, 16, 8);
  191. spawn_icon(level, get_icon("fence_end"), 126, 72, 16, 8);
  192. // house-fence group 5 (the left of group 4)
  193. spawn_icon(level, get_icon("house"), 40, 36, 48, 32);
  194. spawn_icon(level, get_icon("fence"), 40, 72, 16, 8);
  195. spawn_icon(level, get_icon("fence"), 56, 72, 16, 8);
  196. spawn_icon(level, get_icon("fence_end"), 72, 72, 16, 8);
  197. // line of fences on the 8th row (using spawn_icon_line)
  198. spawn_icon_line(level, get_icon("fence"), 8, 100, 16, 8, 10, true);
  199. // plants spaced out underneath the fences
  200. spawn_icon_line(level, get_icon("plant"), 40, 110, 16, 16, 6, true);
  201. spawn_icon_line(level, get_icon("flower"), 40, 140, 16, 16, 6, true);
  202. // man and woman
  203. spawn_icon(level, get_icon("man"), 156, 110, 7, 16);
  204. spawn_icon(level, get_icon("woman"), 164, 110, 9, 16);
  205. // lake
  206. // Top row
  207. spawn_icon(level, get_icon("lake_top_left"), 240, 52, 24, 22);
  208. spawn_icon(level, get_icon("lake_top"), 264, 52, 31, 22);
  209. spawn_icon(level, get_icon("lake_top_right"), 295, 52, 24, 22);
  210. // Middle row
  211. spawn_icon(level, get_icon("lake_left"), 231, 74, 11, 31);
  212. spawn_icon(level, get_icon("lake_right"), 317, 74, 11, 31);
  213. // Bottom row
  214. spawn_icon(level, get_icon("lake_bottom_left"), 240, 105, 24, 22);
  215. spawn_icon(level, get_icon("lake_bottom"), 264, 124, 31, 12);
  216. spawn_icon(level, get_icon("lake_bottom_right"), 295, 105, 24, 22);
  217. // Spawn two full left/up tree lines
  218. for (int i = 0; i < 2; i++)
  219. {
  220. // Horizontal line of 22 icons
  221. spawn_icon_line(level, get_icon("tree"), 5, 2 + i * 17, 16, 16, 22, true);
  222. // Vertical line of 11 icons
  223. spawn_icon_line(level, get_icon("tree"), 5 + i * 17, 2, 16, 16, 11, false);
  224. }
  225. // Spawn two full down tree lines
  226. for (int i = 9; i < 11; i++)
  227. {
  228. // Horizontal line of 22 icons
  229. spawn_icon_line(level, get_icon("tree"), 5, 2 + i * 17, 16, 16, 22, true);
  230. }
  231. // Spawn two full right tree lines
  232. for (int i = 20; i < 22; i++)
  233. {
  234. // Vertical line of 8 icons starting further down (y=50)
  235. spawn_icon_line(level, get_icon("tree"), 5 + i * 17, 50, 16, 16, 8, false);
  236. }
  237. }
  238. /*
  239. {
  240. "name": "town_world",
  241. "author": "JBlanked",
  242. "json_data": [
  243. { "icon": "house", "x": 148, "y": 36, "width": 48, "height": 32, "amount": 1, "horizontal": true },
  244. { "icon": "fence", "x": 148, "y": 72, "width": 16, "height": 8, "amount": 1, "horizontal": true },
  245. { "icon": "fence", "x": 164, "y": 72, "width": 16, "height": 8, "amount": 1, "horizontal": true },
  246. { "icon": "fence_end", "x": 180, "y": 72, "width": 16, "height": 8, "amount": 1, "horizontal": true },
  247. { "icon": "house", "x": 96, "y": 36, "width": 48, "height": 32, "amount": 1, "horizontal": true },
  248. { "icon": "fence", "x": 96, "y": 72, "width": 16, "height": 8, "amount": 1, "horizontal": true },
  249. { "icon": "fence", "x": 110, "y": 72, "width": 16, "height": 8, "amount": 1, "horizontal": true },
  250. { "icon": "fence_end", "x": 126, "y": 72, "width": 16, "height": 8, "amount": 1, "horizontal": true },
  251. { "icon": "house", "x": 40, "y": 36, "width": 48, "height": 32, "amount": 1, "horizontal": true },
  252. { "icon": "fence", "x": 40, "y": 72, "width": 16, "height": 8, "amount": 1, "horizontal": true },
  253. { "icon": "fence", "x": 56, "y": 72, "width": 16, "height": 8, "amount": 1, "horizontal": true },
  254. { "icon": "fence_end", "x": 72, "y": 72, "width": 16, "height": 8, "amount": 1, "horizontal": true },
  255. { "icon": "fence", "x": 8, "y": 100, "width": 16, "height": 8, "amount": 10, "horizontal": true },
  256. { "icon": "plant", "x": 40, "y": 110, "width": 16, "height": 16, "amount": 6, "horizontal": true },
  257. { "icon": "flower", "x": 40, "y": 140, "width": 16, "height": 16, "amount": 6, "horizontal": true },
  258. { "icon": "man", "x": 156, "y": 110, "width": 7, "height": 16, "amount": 1, "horizontal": true },
  259. { "icon": "woman", "x": 164, "y": 110, "width": 9, "height": 16, "amount": 1, "horizontal": true },
  260. { "icon": "lake_top_left", "x": 240, "y": 52, "width": 24, "height": 22, "amount": 1, "horizontal": true },
  261. { "icon": "lake_top", "x": 264, "y": 52, "width": 31, "height": 22, "amount": 1, "horizontal": true },
  262. { "icon": "lake_top_right", "x": 295, "y": 52, "width": 24, "height": 22, "amount": 1, "horizontal": true },
  263. { "icon": "lake_left", "x": 231, "y": 74, "width": 11, "height": 31, "amount": 1, "horizontal": true },
  264. { "icon": "lake_right", "x": 317, "y": 74, "width": 11, "height": 31, "amount": 1, "horizontal": true },
  265. { "icon": "lake_bottom_left", "x": 240, "y": 105, "width": 24, "height": 22, "amount": 1, "horizontal": true },
  266. { "icon": "lake_bottom", "x": 264, "y": 124, "width": 31, "height": 12, "amount": 1, "horizontal": true },
  267. { "icon": "lake_bottom_right", "x": 295, "y": 105, "width": 24, "height": 22, "amount": 1, "horizontal": true },
  268. { "icon": "tree", "x": 5, "y": 2, "width": 16, "height": 16, "amount": 22, "horizontal": true },
  269. { "icon": "tree", "x": 5, "y": 2, "width": 16, "height": 16, "amount": 11, "horizontal": false },
  270. { "icon": "tree", "x": 22, "y": 2, "width": 16, "height": 16, "amount": 22, "horizontal": true },
  271. { "icon": "tree", "x": 22, "y": 2, "width": 16, "height": 16, "amount": 11, "horizontal": false },
  272. { "icon": "tree", "x": 5, "y": 155, "width": 16, "height": 16, "amount": 22, "horizontal": true },
  273. { "icon": "tree", "x": 5, "y": 172, "width": 16, "height": 16, "amount": 22, "horizontal": true },
  274. { "icon": "tree", "x": 345, "y": 50, "width": 16, "height": 16, "amount": 8, "horizontal": false },
  275. { "icon": "tree", "x": 362, "y": 50, "width": 16, "height": 16, "amount": 8, "horizontal": false }
  276. ]
  277. }
  278. */