constexpr float SCORE_CHANGE_TIMER_MAX = 1.0F;
-constexpr float REQUEST_TIMER_MAX = 2.0F;
+constexpr float REQUEST_TIMER_MAX = 3.0F;
#endif
#include <emscripten.h>
#include <emscripten/html5.h>
-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), {
EM_JS(int, canvas_get_height, (),
{ return document.getElementById("canvas").clientHeight; });
-#else
-#include <iostream>
#endif
-void call_js_set_ready(bool ready) {
+#include <iostream>
+
+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;
#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();
#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;
<< " 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;
}
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() {
return;
}
- const float dt = GetFrameTime();
+ float dt = 0.0F;
+ {
+ double timeNow = GetTime();
+ dt = timeNow - prevTime;
+ prevTime = timeNow;
+ }
readyTimer -= dt;
if (readyTimer <= 0.0F) {
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);
}
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;
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);
}
}
}
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() {
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)
// 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,
->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
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',
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}) => {
&& 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
&& 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;