Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
feat: add input system
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 20, 2022
1 parent 44520ba commit 7c8ec27
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/client/src/layers/network/createNetworkLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export async function createNetworkLayer(config: GameConfig) {
const actions = createActionSystem(world, txReduced$);

// --- API ------------------------------------------------------------------------
function move(entity: number, coord: Coord) {
systems["system.Move"].executeTyped(BigNumber.from(entity), coord);
function move(coord: Coord) {
systems["system.Move"].executeTyped(BigNumber.from(network.connectedAddress.get()), coord);
}

// --- CONTEXT --------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/layers/phaser/createPhaserLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { namespaceWorld } from "@latticexyz/recs";
import { createPhaserEngine } from "@latticexyz/phaserx";
import { phaserConfig } from "./config";
import { NetworkLayer } from "../network";
import { createPositionSystem } from "./systems";
import { createPositionSystem, createInputSystem } from "./systems";

/**
* The Phaser layer is responsible for rendering game objects to the screen.
Expand All @@ -29,6 +29,7 @@ export async function createPhaserLayer(network: NetworkLayer) {

// --- SYSTEMS --------------------------------------------------------------------
createPositionSystem(network, context);
createInputSystem(network, context);

return context;
}
25 changes: 25 additions & 0 deletions packages/client/src/layers/phaser/systems/createInputSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { pixelCoordToTileCoord } from "@latticexyz/phaserx";
import { NetworkLayer } from "../../network";
import { PhaserLayer } from "../types";

export function createInputSystem(network: NetworkLayer, phaser: PhaserLayer) {
const {
world,
scenes: {
Main: {
input,
maps: {
Main: { tileWidth, tileHeight },
},
},
},
} = phaser;

const clickSub = input.click$.subscribe((p) => {
const pointer = p as Phaser.Input.Pointer;
const tilePos = pixelCoordToTileCoord({ x: pointer.worldX, y: pointer.worldY }, tileWidth, tileHeight);
network.api.move(tilePos);
});

world.registerDisposer(() => clickSub?.unsubscribe());
}
1 change: 1 addition & 0 deletions packages/client/src/layers/phaser/systems/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { createPositionSystem } from "./createPositionSystem";
export { createInputSystem } from "./createInputSystem";

0 comments on commit 7c8ec27

Please sign in to comment.