Guide for the frontend code

As we prompted previously, the entire frontend project is located in `src/`, and the frontend of the rogoulike card game is located in `src/games/roguelike`.

`src/games/roguelike` has the following structure. These file names are self-explanation.

.
โ”œโ”€โ”€ card.tsx
โ”œโ”€โ”€ controller.tsx
โ”œโ”€โ”€ death.tsx
โ”œโ”€โ”€ images
โ”‚ย ย  โ”œโ”€โ”€ cheems-monster-01.jpg
โ”‚ย ย  โ”œโ”€โ”€ cheems-monster-02.jpg
โ”‚ย ย  โ”œโ”€โ”€ cheems.jpg
โ”‚ย ย  โ”œโ”€โ”€ d0.png
โ”‚ย ย  โ”œโ”€โ”€ d1.png
โ”‚ย ย  โ”œโ”€โ”€ d10.png
โ”‚ย ย  โ”œโ”€โ”€ d11.png
โ”‚ย ย  โ”œโ”€โ”€ d2.png
โ”‚ย ย  โ”œโ”€โ”€ d3.png
โ”‚ย ย  โ”œโ”€โ”€ d4.png
โ”‚ย ย  โ”œโ”€โ”€ d5.png
โ”‚ย ย  โ”œโ”€โ”€ d6.png
โ”‚ย ย  โ”œโ”€โ”€ d7.png
โ”‚ย ย  โ”œโ”€โ”€ d8.png
โ”‚ย ย  โ”œโ”€โ”€ d9.png
โ”‚ย ย  โ””โ”€โ”€ gameover.png
โ”œโ”€โ”€ js
โ”‚ย ย  โ”œโ”€โ”€ config.js
โ”‚ย ย  โ”œโ”€โ”€ game.js
โ”‚ย ย  โ”œโ”€โ”€ gameplay.d.ts
โ”‚ย ย  โ”œโ”€โ”€ gameplay_bg.js
โ”‚ย ย  โ”œโ”€โ”€ gameplay_bg.wasm
โ”‚ย ย  โ”œโ”€โ”€ gameplay_bg.wasm.d.ts
โ”‚ย ย  โ””โ”€โ”€ package.json
โ”œโ”€โ”€ style.scss
โ””โ”€โ”€ tile.ts

Files in js dir were copied from the backend project.

In the `controller.tsx`, we can see:

import init, * as gameplay from "./js";

  function initGame(l2account: number) {
    (init as any)().then(() => {
      console.log("setting instance");
      console.log(gameplay);
      gameplay.new_game();
      gameplay.challenge_next_floor();
      const stateStr = gameplay.state();
      const state = JSON.parse(stateStr);
            console.log(":state:", state);
      setState(state);
      dispatch(setMD5(ImageMD5));
      dispatch(setLoaded(true));
      //drawObjects(objects);
    });
  }

...

We import the exported facilities of the backend into the frontend project, naming it as `gameplay`, and later use this handle to call functions in wasm. Just like:

      gameplay.new_game();
      gameplay.challenge_next_floor();

in the above code snippet.

Once you get it, the thing will become clear to you. Other code is just ordinary typescript/tsx code, which is easy to study.

Last updated