]> git.seodisparate.com - RockPaperScissorsDuel/commitdiff
Glue code for multiplayer rune sdk
authorStephen Seo <seo.disparate@gmail.com>
Mon, 2 Jan 2023 05:03:07 +0000 (14:03 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 2 Jan 2023 05:03:07 +0000 (14:03 +0900)
src/game.cc
src/game.h
wasm_build/.gitignore
wasm_build/Makefile
wasm_build/client.js
wasm_build/custom_shell.html
wasm_build/logic.js

index c53012e9fb7e0070529c2e488a71133b69648e4d..40476490c3bc7509be4cc41cf90af7f044a8b186 100644 (file)
@@ -1,15 +1,35 @@
 #include "game.h"
 
+// standard library includes
+#include <cstring>
+
 // third party includes
 #include <raylib.h>
 
-Game::Game() {}
+Game::Game() : status("Unknown status") {}
 
 void Game::update_state(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, int pos) {}
+                        bool second_ready, int pos) {
+  if (playerOne) {
+    this->playerOne = playerOne;
+  }
+  if (playerTwo) {
+    this->playerTwo = playerTwo;
+  }
+
+  if (std::strcmp(currentPlayer, "undefined") == 0) {
+    status = "spectator";
+  } else if (std::strcmp(currentPlayer, playerOne) == 0) {
+    status = "player one";
+  } else if (std::strcmp(currentPlayer, playerTwo) == 0) {
+    status = "player two";
+  } else {
+    status = "unknown player";
+  }
+}
 
 void Game::do_update() {
   update_impl();
@@ -22,5 +42,6 @@ void Game::draw_impl() {
   BeginDrawing();
   ClearBackground(BLACK);
   DrawText("Testing...", 100, 100, 30, RAYWHITE);
+  DrawText(status.c_str(), 0, 200, 30, RAYWHITE);
   EndDrawing();
 }
index 2b9e42b43147d9a47f88759c65becfe6fafd0e8e..1e7451ccfe1395b1eaff955be38d089ef390c2c4 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef ROCK_PAPER_SCISSORS_DUEL_GAME_H_
 #define ROCK_PAPER_SCISSORS_DUEL_GAME_H_
 
+#include <string>
+
 class Game {
 public:
   Game();
@@ -17,6 +19,11 @@ private:
   void update_impl();
   void draw_impl();
 
+  std::string playerOne;
+  std::string playerTwo;
+  std::string status;
+  bool isPlayerOne;
+
 };
 
 #endif
index 91769f1e9e52a2fe303e31660f5b7243ed59879e..644fe2fa6852f152b54db7edd7f9aae6f0f9b5c3 100644 (file)
@@ -1,5 +1,4 @@
-/rock_paper_scissors_duel.html
-/rock_paper_scissors_duel.js
-/rock_paper_scissors_duel.wasm
-/rock_paper_scissors_duel.data
-/index.html
+/outdir/
+/node_modules/
+/package.json
+/package-lock.json
index 6e4d4fc854095c0bf60da2dd34fb351e334fdb28..91a6807407245eb82911175e6d2e4695ada2fcd8 100644 (file)
@@ -14,10 +14,12 @@ HEADERS = \
 
 CXX = source ${HOME}/git/emsdk/emsdk_env.sh && em++
 
-all: | format rock_paper_scissors_duel.html index.html
+OUTDIR = outdir
 
-rock_paper_scissors_duel.html: ${SOURCES} ${HEADERS}
-       ${CXX} -o rock_paper_scissors_duel.html \
+all: | format ${OUTDIR} ${OUTDIR}/rock_paper_scissors_duel.html ${OUTDIR}/index.html ${OUTDIR}/logic.js ${OUTDIR}/client.js
+
+${OUTDIR}/rock_paper_scissors_duel.html: ${SOURCES} ${HEADERS} client.js logic.js
+       ${CXX} -o ${OUTDIR}/rock_paper_scissors_duel.html \
                -s USE_GLFW=3 -I../wasm_includes -L../wasm_libs -lraylib \
                --shell-file custom_shell.html \
                -sEXPORTED_FUNCTIONS=_main,_game_visual_update \
@@ -25,17 +27,22 @@ rock_paper_scissors_duel.html: ${SOURCES} ${HEADERS}
                ${OTHER_FLAGS} \
                ${SOURCES}
 
+${OUTDIR}:
+       mkdir ${OUTDIR}
+
+${OUTDIR}/logic.js:
+       cp logic.js ${OUTDIR}/
+
+${OUTDIR}/client.js:
+       cp client.js ${OUTDIR}/
+
 .PHONY: clean format index.html
 
-index.html:
-       ln -sf rock_paper_scissors_duel.html index.html
+${OUTDIR}/index.html:
+       ln -sf rock_paper_scissors_duel.html ${OUTDIR}/index.html
 
 clean:
-       rm -f rock_paper_scissors_duel.html
-       rm -f rock_paper_scissors_duel.js
-       rm -f rock_paper_scissors_duel.wasm
-       rm -f rock_paper_scissors_duel.data
-       rm -f index.html
+       rm -rf ${OUTDIR}
 
 format:
        clang-format -i --style=file ${SOURCES} ${HEADERS}
index 662ea3eb7993873044ef07b05a1acebec672e5d0..5d35abdf7d1cbee9932cc1eee15454442cc7760c 100644 (file)
@@ -2,7 +2,7 @@ Rune.initClient({
     visualUpdate: ({ newGame, yourPlayerId}) => {
         const { player1, player2, first_choices, second_choices, first_ready, second_ready, pos } = newGame;
 
-        Module.ccall('_game_visual_update',
+        Module.ccall('game_visual_update',
             undefined,
             ['string', 'string', 'string',
                 'number', 'number', 'number',
index c2386ff0aa5fafbb3ad50266dce0c1ac71e77a66..4bed2a78804df895b346fdeff4c4b57d1f29a2ee 100644 (file)
@@ -28,7 +28,9 @@
     </style>
   </head>
   <body>
-<!--    <script src="https://cdn.jsdelivr.net/npm/rune-games-sdk@2.5.2/dist/browser.min.js"></script> -->
+    <script src="https://cdn.jsdelivr.net/npm/rune-games-sdk@3/multiplayer.js"></script>
+    <script src="logic.js"></script>
+    <script src="client.js"></script>
     <canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas>
     <script type='text/javascript'>
       var Module = {
index 3fb3746c3cf9821e451bc3468a9832c17202eaa6..d9c2ad91c476fe7966ca2f07ee97801e84b0007a 100644 (file)
@@ -1,4 +1,4 @@
-Rune.initlogic({
+Rune.initLogic({
     minPlayers: 2,
     maxPlayers: 2,
     setup: (players) => ({
@@ -96,10 +96,10 @@ Rune.initlogic({
             game.first_ready = false;
             game.second_ready = false;
             if (!has_remaining) {
-                if (pos <= -3) {
+                if (game.pos <= -3) {
                     // second won
                     Rune.gameOver();
-                } else if (pos >= 3) {
+                } else if (game.pos >= 3) {
                     // first won
                     Rune.gameOver();
                 } else {