table.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. #include <toolbox/dir_walk.h>
  2. #include <toolbox/path.h>
  3. #include <toolbox/stream/stream.h>
  4. #include <toolbox/stream/file_stream.h>
  5. #include <toolbox/args.h>
  6. #include "nxjson/nxjson.h"
  7. #include "pinball0.h"
  8. #include "graphics.h"
  9. #include "table.h"
  10. #include "notifications.h"
  11. // Table defaults
  12. #define LIVES 3
  13. #define LIVES_POS Vec2(20, 20)
  14. bool ON_TABLE(const Vec2& p) {
  15. return 0 <= p.x && p.x <= 630 && 0 <= p.y && p.y <= 1270;
  16. }
  17. void Lives::draw(Canvas* canvas) {
  18. // we don't draw the last one, as it's in play!
  19. constexpr float r = 20;
  20. if(display && value > 0) {
  21. float x = p.x;
  22. float y = p.y;
  23. float x_off = alignment == Align::Horizontal ? (2 * r) + r : 0;
  24. float y_off = alignment == Align::Vertical ? (2 * r) + r : 0;
  25. for(auto l = 0; l < value - 1; x += x_off, y += y_off, l++) {
  26. gfx_draw_disc(canvas, x + r, y + r, 20);
  27. }
  28. }
  29. }
  30. void Score::draw(Canvas* canvas) {
  31. if(display) {
  32. char buf[32];
  33. snprintf(buf, 32, "%d", value);
  34. gfx_draw_str(canvas, p.x, p.y, AlignRight, AlignTop, buf);
  35. }
  36. }
  37. Table::~Table() {
  38. for(size_t i = 0; i < objects.size(); i++) {
  39. delete objects[i];
  40. }
  41. if(plunger != nullptr) {
  42. delete plunger;
  43. }
  44. }
  45. void Table::draw(Canvas* canvas) {
  46. lives.draw(canvas);
  47. // da balls
  48. for(auto& b : balls) {
  49. b.draw(canvas);
  50. }
  51. // loop through objects on the table and draw them
  52. for(auto& o : objects) {
  53. o->draw(canvas);
  54. }
  55. for(auto& f : flippers) {
  56. f.draw(canvas);
  57. }
  58. if(plunger) {
  59. plunger->draw(canvas);
  60. }
  61. score.draw(canvas);
  62. }
  63. void table_table_list_init(void* ctx) {
  64. PinballApp* pb = (PinballApp*)ctx;
  65. // using the asset file path, read the table files, and for each one, extract their
  66. // display name (oof). let's just use their filenames for now (stripping any XX_ prefix)
  67. // sort tables by original filename
  68. const char* paths[] = {APP_ASSETS_PATH("tables"), APP_DATA_PATH("tables")};
  69. for(size_t p = 0; p < 2; p++) {
  70. const char* path = paths[p];
  71. // const char* asset_path = APP_ASSETS_PATH("tables");
  72. FURI_LOG_I(TAG, "Loading table list from: %s", path);
  73. FuriString* table_path = furi_string_alloc();
  74. DirWalk* dir_walk = dir_walk_alloc(pb->storage);
  75. dir_walk_set_recursive(dir_walk, false);
  76. if(dir_walk_open(dir_walk, path)) {
  77. while(dir_walk_read(dir_walk, table_path, NULL) == DirWalkOK) {
  78. FURI_LOG_I(TAG, furi_string_get_cstr(table_path));
  79. // set display 'name' and 'filename'
  80. TableMenuItem tmi;
  81. const char* cpath = furi_string_get_cstr(table_path);
  82. tmi.filename = furi_string_alloc_set_str(cpath);
  83. tmi.name = furi_string_alloc();
  84. path_extract_filename_no_ext(cpath, tmi.name);
  85. // If filename starts with XX_ (for custom sorting) strip the prefix
  86. char c = furi_string_get_char(tmi.name, 2);
  87. if(c == '_') {
  88. char a = furi_string_get_char(tmi.name, 0);
  89. char b = furi_string_get_char(tmi.name, 1);
  90. if(a >= '0' && a <= '9' && b >= '0' && b <= '9') {
  91. furi_string_right(tmi.name, 3);
  92. }
  93. }
  94. // Insert in sorted order
  95. size_t i = 0;
  96. auto it = pb->table_list.menu_items.begin();
  97. for(; it != pb->table_list.menu_items.end(); it++, i++) {
  98. if(strcmp(
  99. furi_string_get_cstr(tmi.filename),
  100. furi_string_get_cstr(it->filename)) > 0) {
  101. continue;
  102. }
  103. pb->table_list.menu_items.insert(it, tmi);
  104. break;
  105. }
  106. if(pb->table_list.menu_items.size() == i) {
  107. pb->table_list.menu_items.push_back(tmi);
  108. }
  109. }
  110. }
  111. furi_string_free(table_path);
  112. dir_walk_free(dir_walk);
  113. }
  114. // Add 'Settings' as last element
  115. TableMenuItem settings;
  116. settings.filename = furi_string_alloc_set_str("99_Settings");
  117. settings.name = furi_string_alloc_set_str("SETTINGS");
  118. pb->table_list.menu_items.push_back(settings);
  119. FURI_LOG_I(TAG, "Found %d tables", pb->table_list.menu_items.size());
  120. for(auto& tmi : pb->table_list.menu_items) {
  121. FURI_LOG_I(TAG, "%s", furi_string_get_cstr(tmi.name));
  122. }
  123. pb->table_list.display_size = 5; // how many tables to display at once
  124. pb->table_list.selected = 0;
  125. }
  126. // json parse helper function
  127. bool table_file_parse_vec2(const nx_json* json, const char* key, Vec2& v) {
  128. const nx_json* item = nx_json_get(json, key);
  129. if(!item || item->children.length != 2) {
  130. return false;
  131. }
  132. v.x = nx_json_item(item, 0)->num.dbl_value;
  133. v.y = nx_json_item(item, 1)->num.dbl_value;
  134. return true;
  135. }
  136. bool table_file_parse_int(const nx_json* json, const char* key, int& v) {
  137. const nx_json* item = nx_json_get(json, key);
  138. if(!item) return false;
  139. v = item->num.u_value;
  140. return true;
  141. }
  142. bool table_file_parse_bool(const nx_json* json, const char* key, bool& v) {
  143. int value = v == true ? 1 : 0; // set default value
  144. if(table_file_parse_int(json, key, value)) {
  145. v = value > 0 ? true : false;
  146. return true;
  147. }
  148. return false;
  149. }
  150. bool table_file_parse_float(const nx_json* json, const char* key, float& v) {
  151. const nx_json* item = nx_json_get(json, key);
  152. if(!item) return false;
  153. v = item->num.dbl_value;
  154. return true;
  155. }
  156. Table* table_load_table_from_file(PinballApp* pb, size_t index) {
  157. auto& tmi = pb->table_list.menu_items[index];
  158. FURI_LOG_I(TAG, "Reading file: %s", furi_string_get_cstr(tmi.filename));
  159. File* file = storage_file_alloc(pb->storage);
  160. FileInfo fileinfo;
  161. FS_Error error =
  162. storage_common_stat(pb->storage, furi_string_get_cstr(tmi.filename), &fileinfo);
  163. if(error != FSE_OK) {
  164. FURI_LOG_E(TAG, "Could not find file");
  165. storage_file_free(file);
  166. return NULL;
  167. }
  168. // TODO: determine an appropriate max file size and make configurable
  169. FURI_LOG_I(TAG, "Found file ok!");
  170. if(fileinfo.size >= 8192) {
  171. FURI_LOG_E(TAG, "Table file size too big");
  172. snprintf(pb->text, 256, "Table file\nis too big!\n> 8192 bytes");
  173. storage_file_free(file);
  174. return NULL;
  175. }
  176. FURI_LOG_I(TAG, "File size is ok!");
  177. bool ok =
  178. storage_file_open(file, furi_string_get_cstr(tmi.filename), FSAM_READ, FSOM_OPEN_EXISTING);
  179. FURI_LOG_I(TAG, "File opened? %s", ok ? "YES" : "NO");
  180. // read the file as a string
  181. uint8_t* buffer;
  182. uint64_t file_size = storage_file_size(file);
  183. if(file_size > 8192) { // TODO - what's the right size?
  184. FURI_LOG_E(TAG, "Table file is too large! (> 8192 bytes)");
  185. snprintf(pb->text, 256, "Table file\nis too big!\n> 8192 bytes");
  186. storage_file_free(file);
  187. return NULL;
  188. }
  189. buffer = (uint8_t*)malloc(file_size);
  190. size_t read_count = storage_file_read(file, buffer, file_size);
  191. // if(storage_file_get_error(file) != FSE_OK) {
  192. // FURI_LOG_E(TAG, "Um, couldn't read file");
  193. // storage_file_free(file);
  194. // return NULL;
  195. // }
  196. storage_file_free(file);
  197. if(read_count != file_size) {
  198. FURI_LOG_E(TAG, "Error reading file. expected %lld, got %d", file_size, read_count);
  199. free(buffer);
  200. return NULL;
  201. }
  202. FURI_LOG_I(TAG, "Read file into buffer! %d bytes", read_count);
  203. // let's parse this shit
  204. char* json_buffer = (char*)malloc(read_count * sizeof(char) + 1);
  205. for(uint16_t i = 0; i < read_count; i++) {
  206. json_buffer[i] = buffer[i];
  207. }
  208. json_buffer[read_count] = 0;
  209. free(buffer);
  210. const nx_json* json = nx_json_parse(json_buffer, 0);
  211. if(!json) {
  212. FURI_LOG_E(TAG, "Failed to parse table json!");
  213. snprintf(pb->text, 256, "Failed to\nparse table\njson!!");
  214. free(json_buffer);
  215. return NULL;
  216. }
  217. Table* table = new Table();
  218. do {
  219. const nx_json* lives = nx_json_get(json, "lives");
  220. if(lives) {
  221. table_file_parse_int(lives, "value", table->lives.value);
  222. table_file_parse_bool(lives, "display", table->lives.display);
  223. table_file_parse_vec2(lives, "position", table->lives.p);
  224. const nx_json* align = nx_json_get(lives, "align");
  225. if(align && !strcmp(align->text_value, "VERTICAL")) {
  226. table->lives.alignment = Lives::Vertical;
  227. }
  228. }
  229. const nx_json* score = nx_json_get(json, "score");
  230. if(score) {
  231. table_file_parse_bool(score, "display", table->score.display);
  232. table_file_parse_vec2(score, "position", table->score.p);
  233. }
  234. const nx_json* balls = nx_json_get(json, "balls");
  235. if(balls) {
  236. for(int i = 0; i < balls->children.length; i++) {
  237. const nx_json* ball = nx_json_item(balls, i);
  238. if(!ball) continue;
  239. Vec2 p;
  240. if(!table_file_parse_vec2(ball, "position", p)) {
  241. FURI_LOG_E(TAG, "Ball missing \"position\", skipping");
  242. continue;
  243. }
  244. if(!ON_TABLE(p)) {
  245. FURI_LOG_W(
  246. TAG,
  247. "Ball with position %.1f,%.1f is not on table!",
  248. (double)p.x,
  249. (double)p.y);
  250. }
  251. Ball new_ball(p);
  252. table_file_parse_float(ball, "radius", new_ball.r);
  253. Vec2 v = (Vec2){0, 0};
  254. table_file_parse_vec2(ball, "velocity", v);
  255. new_ball.accelerate(v);
  256. table->balls_initial.push_back(new_ball);
  257. table->balls.push_back(new_ball);
  258. }
  259. }
  260. if(table->balls.size() == 0) {
  261. FURI_LOG_E(TAG, "Table has NO BALLS");
  262. snprintf(pb->text, 256, "No balls\nfound in\ntable file!");
  263. delete table;
  264. table = NULL;
  265. break;
  266. }
  267. // TODO: plungers need work
  268. const nx_json* plunger = nx_json_get(json, "plunger");
  269. if(plunger) {
  270. Vec2 p;
  271. table_file_parse_vec2(plunger, "position", p);
  272. int s = 100;
  273. table_file_parse_int(plunger, "size", s);
  274. table->plunger = new Plunger(p);
  275. } else {
  276. FURI_LOG_W(
  277. TAG, "Table has NO PLUNGER - s'ok, we don't really support one anyway (yet)");
  278. }
  279. const nx_json* flippers = nx_json_get(json, "flippers");
  280. if(flippers) {
  281. for(int i = 0; i < flippers->children.length; i++) {
  282. const nx_json* flipper = nx_json_item(flippers, i);
  283. Vec2 p;
  284. if(!table_file_parse_vec2(flipper, "position", p)) {
  285. FURI_LOG_E(TAG, "Flipper missing \"position\", skipping");
  286. continue;
  287. }
  288. if(!ON_TABLE(p)) {
  289. FURI_LOG_W(
  290. TAG,
  291. "Flipper with position %.1f,%.1f is not on table!",
  292. (double)p.x,
  293. (double)p.y);
  294. }
  295. const nx_json* side = nx_json_get(flipper, "side");
  296. Flipper::Side sd = Flipper::LEFT;
  297. if(side && !strcmp(side->text_value, "RIGHT")) {
  298. sd = Flipper::RIGHT;
  299. }
  300. int sz = DEF_FLIPPER_SIZE;
  301. table_file_parse_int(flipper, "size", sz);
  302. Flipper flip(p, sd, sz);
  303. // flip.notification = &notify_flipper;
  304. table->flippers.push_back(flip);
  305. }
  306. }
  307. const nx_json* bumpers = nx_json_get(json, "bumpers");
  308. if(bumpers) {
  309. for(int i = 0; i < bumpers->children.length; i++) {
  310. const nx_json* bumper = nx_json_item(bumpers, i);
  311. Vec2 p;
  312. if(!table_file_parse_vec2(bumper, "position", p)) {
  313. FURI_LOG_E(TAG, "Bumper missing \"position\", skipping");
  314. continue;
  315. }
  316. if(!ON_TABLE(p)) {
  317. FURI_LOG_W(
  318. TAG,
  319. "Bumper with position %.1f,%.1f is not on table!",
  320. (double)p.x,
  321. (double)p.y);
  322. }
  323. int r = DEF_BUMPER_RADIUS;
  324. table_file_parse_int(bumper, "radius", r);
  325. float bnc = DEF_BUMPER_BOUNCE;
  326. table_file_parse_float(bumper, "bounce", bnc);
  327. Bumper* new_bumper = new Bumper(p, r);
  328. new_bumper->bounce = bnc;
  329. new_bumper->notification = notify_bumper_hit;
  330. table->objects.push_back(new_bumper);
  331. }
  332. }
  333. constexpr float pi_180 = M_PI / 180;
  334. const nx_json* arcs = nx_json_get(json, "arcs");
  335. if(arcs) {
  336. for(int i = 0; i < arcs->children.length; i++) {
  337. const nx_json* arc = nx_json_item(arcs, i);
  338. Vec2 p;
  339. if(!table_file_parse_vec2(arc, "position", p)) {
  340. FURI_LOG_E(TAG, "Arc missing \"position\"");
  341. continue;
  342. }
  343. if(!ON_TABLE(p)) {
  344. FURI_LOG_W(
  345. TAG,
  346. "Arc with position %.1f,%.1f is not on table!",
  347. (double)p.x,
  348. (double)p.y);
  349. }
  350. int r = DEF_BUMPER_RADIUS;
  351. table_file_parse_int(arc, "radius", r);
  352. float bnc = 0.95f; // DEF_BUMPER_BOUNCE?
  353. table_file_parse_float(arc, "bounce", bnc);
  354. float start_angle = 0.0;
  355. table_file_parse_float(arc, "start_angle", start_angle);
  356. start_angle *= pi_180;
  357. float end_angle = 0.0;
  358. table_file_parse_float(arc, "end_angle", end_angle);
  359. end_angle *= pi_180;
  360. Arc::Surface surface = Arc::OUTSIDE;
  361. const nx_json* stype = nx_json_get(arc, "surface");
  362. if(stype && !strcmp(stype->text_value, "INSIDE")) {
  363. surface = Arc::INSIDE;
  364. }
  365. Arc* new_bumper = new Arc(p, r, start_angle, end_angle, surface);
  366. new_bumper->bounce = bnc;
  367. table->objects.push_back(new_bumper);
  368. }
  369. }
  370. const nx_json* rails = nx_json_get(json, "rails");
  371. if(rails) {
  372. for(int i = 0; i < rails->children.length; i++) {
  373. const nx_json* rail = nx_json_item(rails, i);
  374. Vec2 s;
  375. if(!table_file_parse_vec2(rail, "start", s)) {
  376. FURI_LOG_E(TAG, "Rail missing \"start\", skipping");
  377. continue;
  378. }
  379. if(!ON_TABLE(s)) {
  380. FURI_LOG_W(
  381. TAG,
  382. "Rail with starting position %.1f,%.1f is not on table!",
  383. (double)s.x,
  384. (double)s.y);
  385. }
  386. Vec2 e;
  387. if(!table_file_parse_vec2(rail, "end", e)) {
  388. FURI_LOG_E(TAG, "Rail missing \"end\", skipping");
  389. continue;
  390. }
  391. if(!ON_TABLE(e)) {
  392. FURI_LOG_W(
  393. TAG,
  394. "Rail with ending position %.1f,%.1f is not on table!",
  395. (double)e.x,
  396. (double)e.y);
  397. }
  398. Polygon* new_rail = new Polygon();
  399. new_rail->add_point(s);
  400. new_rail->add_point(e);
  401. float bnc = DEF_RAIL_BOUNCE;
  402. table_file_parse_float(rail, "bounce", bnc);
  403. new_rail->bounce = bnc;
  404. int double_sided = 0;
  405. table_file_parse_int(rail, "double_sided", double_sided);
  406. new_rail->finalize();
  407. new_rail->notification = &notify_rail_hit;
  408. table->objects.push_back(new_rail);
  409. if(double_sided) {
  410. new_rail = new Polygon();
  411. new_rail->add_point(e);
  412. new_rail->add_point(s);
  413. new_rail->bounce = bnc;
  414. new_rail->finalize();
  415. new_rail->notification = &notify_rail_hit;
  416. table->objects.push_back(new_rail);
  417. }
  418. }
  419. }
  420. const nx_json* portals = nx_json_get(json, "portals");
  421. if(portals) {
  422. for(int i = 0; i < portals->children.length; i++) {
  423. const nx_json* portal = nx_json_item(portals, i);
  424. Vec2 a1;
  425. if(!table_file_parse_vec2(portal, "a_start", a1)) {
  426. FURI_LOG_E(TAG, "Portal missing \"a_start\", skipping");
  427. continue;
  428. }
  429. if(!ON_TABLE(a1)) {
  430. FURI_LOG_W(
  431. TAG,
  432. "Portal A with starting position %.1f,%.1f is not on table!",
  433. (double)a1.x,
  434. (double)a1.y);
  435. }
  436. Vec2 a2;
  437. if(!table_file_parse_vec2(portal, "a_end", a2)) {
  438. FURI_LOG_E(TAG, "Portal missing \"a_end\", skipping");
  439. continue;
  440. }
  441. if(!ON_TABLE(a2)) {
  442. FURI_LOG_W(
  443. TAG,
  444. "Portal A with ending position %.1f,%.1f is not on table!",
  445. (double)a2.x,
  446. (double)a2.y);
  447. }
  448. Vec2 b1;
  449. if(!table_file_parse_vec2(portal, "b_start", b1)) {
  450. FURI_LOG_E(TAG, "Portal missing \"b_start\", skipping");
  451. continue;
  452. }
  453. if(!ON_TABLE(b1)) {
  454. FURI_LOG_W(
  455. TAG,
  456. "Portal B with starting position %.1f,%.1f is not on table!",
  457. (double)b1.x,
  458. (double)b1.y);
  459. }
  460. Vec2 b2;
  461. if(!table_file_parse_vec2(portal, "b_end", b2)) {
  462. FURI_LOG_E(TAG, "Portal missing \"b_end\", skipping");
  463. continue;
  464. }
  465. if(!ON_TABLE(b2)) {
  466. FURI_LOG_W(
  467. TAG,
  468. "Portal B with ending position %.1f,%.1f is not on table!",
  469. (double)b2.x,
  470. (double)b2.y);
  471. }
  472. Portal* new_portal = new Portal(a1, a2, b1, b2);
  473. new_portal->finalize();
  474. new_portal->notification = &notify_portal;
  475. table->objects.push_back(new_portal);
  476. }
  477. }
  478. const nx_json* rollovers = nx_json_get(json, "rollovers");
  479. if(rollovers) {
  480. for(int i = 0; i < rollovers->children.length; i++) {
  481. const nx_json* rollover = nx_json_item(rollovers, i);
  482. Vec2 p;
  483. if(!table_file_parse_vec2(rollover, "position", p)) {
  484. FURI_LOG_E(TAG, "Rollover missing \"position\", skipping");
  485. continue;
  486. }
  487. if(!ON_TABLE(p)) {
  488. FURI_LOG_W(
  489. TAG,
  490. "Rollover with position %.1f,%.1f is not on table!",
  491. (double)p.x,
  492. (double)p.y);
  493. }
  494. char sym = '*';
  495. const nx_json* symbol = nx_json_get(rollover, "symbol");
  496. if(symbol) {
  497. sym = symbol->text_value[0];
  498. }
  499. Rollover* new_rollover = new Rollover(p, sym);
  500. table->objects.push_back(new_rollover);
  501. }
  502. }
  503. const nx_json* turbos = nx_json_get(json, "turbos");
  504. if(turbos) {
  505. for(int i = 0; i < turbos->children.length; i++) {
  506. const nx_json* turbo = nx_json_item(turbos, i);
  507. Vec2 p;
  508. if(!table_file_parse_vec2(turbo, "position", p)) {
  509. FURI_LOG_E(TAG, "Turbo missing \"position\"");
  510. continue;
  511. }
  512. if(!ON_TABLE(p)) {
  513. FURI_LOG_W(
  514. TAG,
  515. "Turbo with position %.1f,%.1f is not on table!",
  516. (double)p.x,
  517. (double)p.y);
  518. }
  519. float angle = 0;
  520. table_file_parse_float(turbo, "angle", angle);
  521. angle *= pi_180;
  522. float boost = 10;
  523. table_file_parse_float(turbo, "boost", boost);
  524. Turbo* new_turbo = new Turbo(p, angle, boost);
  525. table->objects.push_back(new_turbo);
  526. }
  527. }
  528. break;
  529. } while(false);
  530. nx_json_free(json);
  531. free(json_buffer);
  532. return table;
  533. }
  534. TableList::~TableList() {
  535. for(auto& mi : menu_items) {
  536. furi_string_free(mi.name);
  537. furi_string_free(mi.filename);
  538. }
  539. }
  540. Table* table_init_table_select(void* ctx) {
  541. UNUSED(ctx);
  542. Table* table = new Table();
  543. table->balls.push_back(Ball(Vec2(20, 880), 35));
  544. table->balls.back().add_velocity(Vec2(7, 0), .10f);
  545. table->balls.push_back(Ball(Vec2(610, 920), 30));
  546. table->balls.back().add_velocity(Vec2(-8, 0), .10f);
  547. table->balls.push_back(Ball(Vec2(250, 980), 20));
  548. table->balls.back().add_velocity(Vec2(10, 0), .10f);
  549. table->balls_released = true;
  550. Polygon* new_rail = new Polygon();
  551. new_rail->add_point({-1, 840});
  552. new_rail->add_point({-1, 1280});
  553. new_rail->finalize();
  554. new_rail->hidden = true;
  555. table->objects.push_back(new_rail);
  556. new_rail = new Polygon();
  557. new_rail->add_point({-1, 1280});
  558. new_rail->add_point({640, 1280});
  559. new_rail->finalize();
  560. new_rail->hidden = true;
  561. table->objects.push_back(new_rail);
  562. new_rail = new Polygon();
  563. new_rail->add_point({640, 1280});
  564. new_rail->add_point({640, 840});
  565. new_rail->finalize();
  566. new_rail->hidden = true;
  567. table->objects.push_back(new_rail);
  568. int gap = 8;
  569. int speed = 3;
  570. float top = 20;
  571. // right side
  572. table->objects.push_back(new Chaser(Vec2(32, top), Vec2(62, top), gap, speed));
  573. table->objects.push_back(new Chaser(Vec2(62, top), Vec2(62, 84), gap, speed));
  574. table->objects.push_back(new Chaser(Vec2(62, 84), Vec2(32, 84), gap, speed));
  575. // left side
  576. table->objects.push_back(new Chaser(Vec2(32, top), Vec2(1, top), gap, speed));
  577. table->objects.push_back(new Chaser(Vec2(1, top), Vec2(1, 84), gap, speed));
  578. table->objects.push_back(new Chaser(Vec2(1, 84), Vec2(32, 84), gap, speed));
  579. return table;
  580. }
  581. Table* table_init_table_error(void* ctx) {
  582. UNUSED(ctx);
  583. // PinballApp* pb = (PinballApp*)ctx;
  584. Table* table = new Table();
  585. table->balls.push_back(Ball(Vec2(20, 880), 30));
  586. table->balls.back().add_velocity(Vec2(7, 0), .10f);
  587. // table->balls.push_back(Ball(Vec2(610, 920), 30));
  588. // table->balls.back().add_velocity(Vec2(-8, 0), .10f);
  589. // table->balls.push_back(Ball(Vec2(250, 980), 20));
  590. // table->balls.back().add_velocity(Vec2(10, 0), .10f);
  591. table->balls_released = true;
  592. Polygon* new_rail = new Polygon();
  593. new_rail->add_point({-1, 840});
  594. new_rail->add_point({-1, 1280});
  595. new_rail->finalize();
  596. new_rail->hidden = true;
  597. table->objects.push_back(new_rail);
  598. new_rail = new Polygon();
  599. new_rail->add_point({-1, 1280});
  600. new_rail->add_point({640, 1280});
  601. new_rail->finalize();
  602. new_rail->hidden = true;
  603. table->objects.push_back(new_rail);
  604. new_rail = new Polygon();
  605. new_rail->add_point({640, 1280});
  606. new_rail->add_point({640, 840});
  607. new_rail->finalize();
  608. new_rail->hidden = true;
  609. table->objects.push_back(new_rail);
  610. int gap = 8;
  611. int speed = 3;
  612. float top = 20;
  613. table->objects.push_back(new Chaser(Vec2(2, top), Vec2(61, top), gap, speed, Chaser::SLASH));
  614. table->objects.push_back(new Chaser(Vec2(2, top), Vec2(2, 84), gap, speed, Chaser::SLASH));
  615. table->objects.push_back(new Chaser(Vec2(2, 84), Vec2(61, 84), gap, speed, Chaser::SLASH));
  616. table->objects.push_back(new Chaser(Vec2(61, top), Vec2(61, 84), gap, speed, Chaser::SLASH));
  617. return table;
  618. }
  619. Table* table_init_table_settings(void* ctx) {
  620. UNUSED(ctx);
  621. Table* table = new Table();
  622. // table->balls.push_back(Ball(Vec2(20, 880), 10));
  623. // table->balls.back().add_velocity(Vec2(7, 0), .10f);
  624. // table->balls.push_back(Ball(Vec2(610, 920), 10));
  625. // table->balls.back().add_velocity(Vec2(-8, 0), .10f);
  626. // table->balls.push_back(Ball(Vec2(250, 980), 10));
  627. // table->balls.back().add_velocity(Vec2(10, 0), .10f);
  628. table->balls_released = true;
  629. Polygon* new_rail = new Polygon();
  630. new_rail->add_point({-1, 840});
  631. new_rail->add_point({-1, 1280});
  632. new_rail->finalize();
  633. new_rail->hidden = true;
  634. table->objects.push_back(new_rail);
  635. new_rail = new Polygon();
  636. new_rail->add_point({-1, 1280});
  637. new_rail->add_point({640, 1280});
  638. new_rail->finalize();
  639. new_rail->hidden = true;
  640. table->objects.push_back(new_rail);
  641. new_rail = new Polygon();
  642. new_rail->add_point({640, 1280});
  643. new_rail->add_point({640, 840});
  644. new_rail->finalize();
  645. new_rail->hidden = true;
  646. table->objects.push_back(new_rail);
  647. return table;
  648. }
  649. bool table_load_table(void* ctx, size_t index) {
  650. PinballApp* pb = (PinballApp*)ctx;
  651. // read the index'th file in pb->table_list and allocate
  652. FURI_LOG_I(TAG, "Loading table %u", index);
  653. // if there's already a table loaded, free it
  654. if(pb->table) {
  655. delete pb->table;
  656. pb->table = nullptr;
  657. }
  658. pb->gameStarted = false;
  659. switch(index) {
  660. case TABLE_SELECT:
  661. pb->table = table_init_table_select(ctx);
  662. break;
  663. case TABLE_ERROR:
  664. pb->table = table_init_table_error(ctx);
  665. break;
  666. case TABLE_SETTINGS:
  667. pb->table = table_init_table_settings(ctx);
  668. break;
  669. default:
  670. pb->table = table_load_table_from_file(pb, index - TABLE_INDEX_OFFSET);
  671. break;
  672. }
  673. return pb->table != NULL;
  674. }