From 9a7b8c07afd4be9c3a27087924f81b9490decdcb Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 3 Jan 2023 19:57:45 +0900 Subject: [PATCH] Impl basic working game --- src/constants.h | 2 +- src/ems.cc | 11 +++-- src/ems.h | 2 +- src/game.cc | 96 ++++++++++++++++++++++---------------------- src/game.h | 1 + src/main.cc | 3 +- wasm_build/client.js | 2 +- wasm_build/logic.js | 15 ++----- 8 files changed, 62 insertions(+), 70 deletions(-) diff --git a/src/constants.h b/src/constants.h index 58ac1f2..5c0cc33 100644 --- a/src/constants.h +++ b/src/constants.h @@ -20,6 +20,6 @@ constexpr float RESULTS_TIMER_MAX = 1.0F; constexpr float SCORE_CHANGE_TIMER_MAX = 1.0F; -constexpr float REQUEST_TIMER_MAX = 2.0F; +constexpr float REQUEST_TIMER_MAX = 3.0F; #endif diff --git a/src/ems.cc b/src/ems.cc index 6b1d8ce..ff47b8e 100644 --- a/src/ems.cc +++ b/src/ems.cc @@ -4,8 +4,7 @@ #include #include -EM_JS(void, js_set_ready, (bool is_ready), - { Rune.actions.set_ready({is_ready}); }); +EM_JS(void, js_set_ready, (), { Rune.actions.set_ready("unused"); }); EM_JS(void, js_set_choices, (const char *first, const char *second, const char *third), { @@ -23,13 +22,13 @@ EM_JS(int, canvas_get_width, (), EM_JS(int, canvas_get_height, (), { return document.getElementById("canvas").clientHeight; }); -#else -#include #endif -void call_js_set_ready(bool ready) { +#include + +void call_js_set_ready() { #ifdef __EMSCRIPTEN__ - js_set_ready(ready); + js_set_ready(); #else std::clog << "WARNING: emscripten not enabled, cannot call js_set_ready()!" << std::endl; diff --git a/src/ems.h b/src/ems.h index ee02345..bb99c12 100644 --- a/src/ems.h +++ b/src/ems.h @@ -1,7 +1,7 @@ #ifndef ROCK_PAPER_SCISSORS_DUEL_EMSCRIPTEN_H_ #define ROCK_PAPER_SCISSORS_DUEL_EMSCRIPTEN_H_ -extern void call_js_set_ready(bool ready = true); +extern void call_js_set_ready(); extern void call_js_set_choices(const char *first, const char *second, const char *third); extern void call_js_request_update(); diff --git a/src/game.cc b/src/game.cc index 35f010a..495f181 100644 --- a/src/game.cc +++ b/src/game.cc @@ -13,9 +13,10 @@ #include "helpers.h" Game::Game() - : spriteSheet(std::nullopt), status("Unknown status"), readyTimer(0.0F), - resultsTimer(RESULTS_TIMER_MAX), scoreChangeTimer(SCORE_CHANGE_TIMER_MAX), - requestTimer(REQUEST_TIMER_MAX), prevPos(0), cachedPos(0) { + : spriteSheet(std::nullopt), status("Unknown status"), prevTime(0.0), + readyTimer(0.0F), resultsTimer(RESULTS_TIMER_MAX), + scoreChangeTimer(SCORE_CHANGE_TIMER_MAX), requestTimer(REQUEST_TIMER_MAX), + prevPos(0), cachedPos(0) { spriteSheet = LoadTexture("resources/rockpaperscissorsSpriteSheet.png"); picked[0] = 0; @@ -41,8 +42,8 @@ void Game::update_state(const char *playerOne, const char *playerTwo, << " p1: " << first_first << ", " << first_second << ", " << first_third << "\n p2: " << second_first << ", " << second_second << ", " << second_third << "\nfirst is " - << (first_ready ? "ready" : "not ready") << "\nsecond is " - << (second_ready ? "ready" : "not ready") << "\npos: " << pos + << (first_ready ? "ready" : "NOT ready") << "\nsecond is " + << (second_ready ? "ready" : "NOT ready") << "\npos: " << pos << " matchup_idx: " << matchup_idx << std::endl; std::clog << "flags: " << flags.to_string().substr(32 - 13) << std::endl; } @@ -96,41 +97,6 @@ void Game::update_state(const char *playerOne, const char *playerTwo, prevPos = cachedPos; cachedPos = pos; } - - if (flags.test(0) && flags.test(4) && flags.test(6) && flags.test(7) && - flags.test(8) && first_first == '?' && first_second == '?' && - first_third == '?' && second_first == '?' && second_second == '?' && - second_third == '?') { - std::cout << "Resetting for next round" << (isPlayerOne ? " (1) " : " (2) ") - << "..." << std::endl; - flags.reset(0); - flags.reset(1); - flags.reset(3); - flags.reset(4); - flags.reset(5); - flags.reset(6); - flags.reset(7); - flags.reset(8); - flags.reset(9); - flags.reset(10); - flags.reset(11); - flags.reset(12); - - readyTimer = 0; - resultsTimer = RESULTS_TIMER_MAX; - - picked[0] = 0; - picked[1] = 0; - picked[2] = 0; - - opponentPicked[0] = 0; - opponentPicked[1] = 0; - opponentPicked[2] = 0; - - call_js_set_ready(false); - - std::clog << "flags: " << flags.to_string().substr(32 - 13) << std::endl; - } } void Game::do_update() { @@ -143,7 +109,12 @@ void Game::update_impl() { return; } - const float dt = GetFrameTime(); + float dt = 0.0F; + { + double timeNow = GetTime(); + dt = timeNow - prevTime; + prevTime = timeNow; + } readyTimer -= dt; if (readyTimer <= 0.0F) { @@ -241,7 +212,7 @@ void Game::update_impl() { GetTouchY() <= GetScreenHeight() - triple_single_width * 2) { if (picked[0] != 0 && picked[1] != 0 && picked[2] != 0 && !flags.test(0)) { - call_js_set_ready(true); + call_js_set_ready(); flags.set(0); flags.set(3); } @@ -276,9 +247,10 @@ void Game::update_impl() { resultsTimer = RESULTS_TIMER_MAX; } else if (flags.test(9)) { if (!flags.test(8)) { - call_js_set_ready(true); + call_js_set_ready(); flags.reset(9); flags.set(5); + } else { } } else { resultsTimer -= dt; @@ -287,15 +259,12 @@ void Game::update_impl() { if (!flags.test(6)) { flags.set(6); flags.set(9); - call_js_set_ready(true); } else if (!flags.test(7)) { flags.set(7); flags.set(9); - call_js_set_ready(true); } else if (!flags.test(8)) { flags.set(8); flags.set(9); - call_js_set_ready(true); } } } @@ -305,16 +274,47 @@ void Game::update_impl() { prevPos == cachedPos && is_choices_set() && is_opponent_choices_set()) { flags.reset(12); call_js_request_update(); - std::cout << "Requesting update..." << std::endl; + // std::cout << "Requesting update..." << std::endl; // TODO DEBUG } requestTimer -= dt; if (requestTimer <= 0.0F) { + requestTimer = REQUEST_TIMER_MAX; if (flags.test(10) && flags.test(11)) { call_js_request_update(); - std::cout << "Requesting update (timer)..." << std::endl; + // std::cout << "Requesting update (timer)..." << std::endl; // TODO DEBUG } } + + if (flags.test(0) && flags.test(6) && flags.test(7) && flags.test(8)) { + // std::cout << "Resetting for next round" << (isPlayerOne ? " (1) " : " (2) + // ") << "..." << std::endl; // TODO DEBUG + flags.reset(0); + flags.reset(1); + flags.reset(3); + flags.reset(4); + flags.reset(5); + flags.reset(6); + flags.reset(7); + flags.reset(8); + flags.reset(9); + flags.reset(10); + flags.reset(11); + flags.reset(12); + + readyTimer = 0; + resultsTimer = RESULTS_TIMER_MAX; + + picked[0] = 0; + picked[1] = 0; + picked[2] = 0; + + opponentPicked[0] = 0; + opponentPicked[1] = 0; + opponentPicked[2] = 0; + + std::clog << "flags: " << flags.to_string().substr(32 - 13) << std::endl; + } } void Game::draw_impl() { diff --git a/src/game.h b/src/game.h index 0d9f6c8..4c49997 100644 --- a/src/game.h +++ b/src/game.h @@ -43,6 +43,7 @@ private: std::string playerOne; std::string playerTwo; std::string status; + double prevTime; /* * 0 - ready flag (ready to submit moves) * 1 - readyTimer fade to gray (fade to black otherwise) diff --git a/src/main.cc b/src/main.cc index 0e3c7b6..f0ecff5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -21,7 +21,7 @@ static void *global_game_ptr = nullptr; // em exposed fns extern "C" { -void EMSCRIPTEN_KEEPALIVE game_visual_update( +int EMSCRIPTEN_KEEPALIVE game_visual_update( const char *playerOne, const char *playerTwo, const char *currentPlayer, char first_first, char first_second, char first_third, char second_first, char second_second, char second_third, bool first_ready, bool second_ready, @@ -30,6 +30,7 @@ void EMSCRIPTEN_KEEPALIVE game_visual_update( ->update_state(playerOne, playerTwo, currentPlayer, first_first, first_second, first_third, second_first, second_second, second_third, first_ready, second_ready, pos, matchup_idx); + return 0; } } // end em exposed functions diff --git a/wasm_build/client.js b/wasm_build/client.js index 909813d..039bc24 100644 --- a/wasm_build/client.js +++ b/wasm_build/client.js @@ -3,7 +3,7 @@ Rune.initClient({ const { player1, player2, first_choices, second_choices, ready, pos, matchup_idx } = newGame; Module.ccall('game_visual_update', - undefined, + 'number', ['string', 'string', 'string', 'number', 'number', 'number', 'number', 'number', 'number', diff --git a/wasm_build/logic.js b/wasm_build/logic.js index e03409c..c2094ab 100644 --- a/wasm_build/logic.js +++ b/wasm_build/logic.js @@ -53,13 +53,13 @@ Rune.initLogic({ game.second_choices[2] = third; } }, - set_ready: ({ is_ready }, { game, playerId }) => { + set_ready: (unused, { game, playerId }) => { let is_first = game.player1 === playerId; if (is_first) { - game.ready[0] = is_ready; + game.ready[0] = true; } else { - game.ready[1] = is_ready; + game.ready[1] = true; } }, request_update: (unused, {game, playerId}) => { @@ -88,8 +88,6 @@ Rune.initLogic({ && game.second_choices[game.matchup_idx] === 'r') || (game.first_choices[game.matchup_idx] === 's' && game.second_choices[game.matchup_idx] === 'p')) { - //game.first_choices[game.matchup_idx] = 'w'; - //game.second_choices[game.matchup_idx] = 'l'; game.pos = game.pos + 1; } // check if second won the matchup @@ -99,15 +97,8 @@ Rune.initLogic({ && game.second_choices[game.matchup_idx] === 's') || (game.first_choices[game.matchup_idx] === 's' && game.second_choices[game.matchup_idx] === 'r')) { - //game.first_choices[game.matchup_idx] = 'l'; - //game.second_choices[game.matchup_idx] = 'w'; game.pos = game.pos - 1; } - // matchup was a draw - //else { - //game.first_choices[game.matchup_idx] = 'd'; - //game.second_choices[game.matchup_idx] = 'd'; - //} game.matchup_idx = game.matchup_idx + 1; game.ready[0] = false; -- 2.49.0