-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multiplayer game replay visualizer
- Loading branch information
Showing
9 changed files
with
140 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useEffect, useRef } from "react"; | ||
import { useRenderer } from "./Renderer"; | ||
import { ZombieSurvival, type ZombieSurvivalOptions } from "@/simulator"; | ||
import { Action } from "@/simulator/Action"; | ||
import { replay } from "@/simulator/Replay"; | ||
|
||
export function ReplayVisualizer({ | ||
actions, | ||
cellSize = 64, | ||
map, | ||
playerLabels, | ||
simulatorOptions, | ||
}: { | ||
actions: Action[]; | ||
cellSize?: number; | ||
map: string[][]; | ||
playerLabels: Record<string, string>; | ||
simulatorOptions?: ZombieSurvivalOptions; | ||
}) { | ||
const simulator = useRef( | ||
new ZombieSurvival( | ||
replay(new ZombieSurvival(map, simulatorOptions), actions).getState(), | ||
simulatorOptions, | ||
), | ||
); | ||
|
||
const cachedActions = useRef(actions); | ||
const canvas = useRef<HTMLCanvasElement | null>(null); | ||
const renderer = useRenderer(map, canvas, playerLabels, cellSize); | ||
|
||
useEffect(() => { | ||
if (renderer !== null) { | ||
renderer.render(simulator.current.getAllEntities()); | ||
} | ||
}, [renderer]); | ||
|
||
useEffect(() => { | ||
const newActions = actions.slice(cachedActions.current.length); | ||
simulator.current.resetVisualEvents(); | ||
replay(simulator.current, newActions); | ||
cachedActions.current.push(...newActions); | ||
renderer?.render(simulator.current.getAllEntities()); | ||
}, [actions]); | ||
|
||
return <canvas ref={canvas} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { type Action, ActionType } from "./Action"; | ||
import { type ZombieSurvival } from "./ZombieSurvival"; | ||
|
||
export function replay( | ||
simulator: ZombieSurvival, | ||
actions: Action | Action[], | ||
): ZombieSurvival { | ||
const actualActions = Array.isArray(actions) ? actions : [actions]; | ||
|
||
for (const action of actualActions) { | ||
switch (action.type) { | ||
case ActionType.PlayerShoot: { | ||
if (action.position === undefined) { | ||
throw new Error( | ||
"Action position is required when player is shooting", | ||
); | ||
} | ||
|
||
const zombie = simulator.getZombieAt(action.position); | ||
|
||
if (zombie === undefined) { | ||
throw new Error("Unable to get action's zombie at a given position"); | ||
} | ||
|
||
zombie.hit(); | ||
break; | ||
} | ||
case ActionType.PlayerWalk: { | ||
if (action.position === undefined) { | ||
throw new Error("Action position is required when moving a player"); | ||
} | ||
|
||
const player = simulator.getPlayer(action.token); | ||
|
||
if (player === undefined) { | ||
throw new Error("Unable to get action's token player"); | ||
} | ||
|
||
player.moveTo(action.position); | ||
break; | ||
} | ||
case ActionType.ZombieSpawn: { | ||
if (action.position === undefined) { | ||
throw new Error("Action position is required when spawning a zombie"); | ||
} | ||
|
||
simulator.spawnZombieAt(action.position); | ||
break; | ||
} | ||
case ActionType.ZombieStep: { | ||
simulator.stepZombies(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return simulator; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters