Skip to content

Commit 0ee3f6f

Browse files
committed
implemented player actions
1 parent b8e4c85 commit 0ee3f6f

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

src/game/game-server.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Peer, type DataConnection } from 'peerjs';
2-
import type { PlayerAction } from "./player";
2+
import type { PlayerAction } from "./player-action";
33
import { getRole } from "../device";
44

55
type Events = {
@@ -32,9 +32,6 @@ export class GameServer {
3232
return () => this.listeners[k]!.delete(fn);
3333
}
3434

35-
36-
37-
3835
static async host(): Promise<GameServer> {
3936
const host = new Peer(this.id);
4037
const game = new GameServer();

src/game/player-action.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Vector3 } from "three";
2+
3+
export type Vec3 = [number, number, number]
4+
5+
export const toVec3 = (v: { x: number; y: number; z: number }): Vec3 => [v.x, v.y, v.z];
6+
export const fromVec3 = (v: Vec3) => new Vector3(v[0], v[1], v[2]);
7+
8+
9+
export interface PlayerAction<K extends keyof Actions = keyof Actions> {
10+
type: K;
11+
payload: Actions[K];
12+
}
13+
14+
export type Actions = {
15+
ability: never,
16+
attack: never,
17+
move: { target: Vec3 },
18+
none: never
19+
}
20+
21+
export function createAction<K extends keyof Actions>(
22+
type: K,
23+
...payload: Actions[K] extends never ? [] : [payload: Actions[K]]
24+
) {
25+
return (payload.length
26+
? { type, payload: payload[0] }
27+
: { type }) as Actions[K] extends never // explicitly define return type
28+
? { type: K }
29+
: { type: K, payload: Actions[K] };
30+
}

src/game/player.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
1-
import { Vector3 } from "three";
2-
3-
export type Vec3 = [number, number, number]
4-
5-
export const toVec3 = (v: { x: number; y: number; z: number }): Vec3 => [v.x, v.y, v.z];
6-
export const fromVec3 = (v: Vec3) => new Vector3(v[0], v[1], v[2]);
7-
81
export interface Player {
92
id?: number;
103
name?: string;
11-
}
12-
13-
export type ActionType = 'Ability' | 'Attack' | 'Move' | 'None';
14-
15-
export interface PlayerAction {
16-
type: ActionType;
17-
targetPosition?: Vec3;
184
}

src/main.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
77
import { UnrealBloomPass } from "three/examples/jsm/postprocessing/UnrealBloomPass.js";
88
import { getRole, isMobile } from "./device";
99
import { GameServer } from "./game/game-server";
10-
import { fromVec3, toVec3, type PlayerAction, type Vec3 } from "./game/player";
10+
import { createAction, fromVec3, toVec3, type PlayerAction } from "./game/player-action";
1111

1212
// Required for Github Pages deployment
1313
THREE.DefaultLoadingManager.setURLModifier((url) => {
@@ -207,7 +207,8 @@ async function init() {
207207
currentTarget = picked.getWorldPosition(new THREE.Vector3());
208208
currentTarget.x += 0.4;
209209
currentTarget.y = scar.position.y;
210-
playAction({ type: 'Move', targetPosition: toVec3(currentTarget) });
210+
const move = createAction('move', { target: toVec3(currentTarget) })
211+
playAction(move);
211212
if (currentPick) {
212213
// restore material
213214
currentPick.material = currentMaterial!;
@@ -311,7 +312,7 @@ async function init() {
311312

312313
function playActionLocal(action: PlayerAction) {
313314
switch (action.type) {
314-
case 'Move': currentTarget = mapXZFrom(fromVec3(action.targetPosition!), scar.position);
315+
case 'move': currentTarget = mapXZFrom(fromVec3(action.payload.target), scar.position);
315316
}
316317
}
317318

0 commit comments

Comments
 (0)