Skip to content

Commit

Permalink
Various scenario tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Jan 15, 2025
1 parent 1af9052 commit 4d3f1dc
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 30 deletions.
12 changes: 8 additions & 4 deletions src/scenarios/grenadeIsland.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ export default async function runScenario(game: Game) {
ammo: {
[IWeaponCode.Bazooka]: 999,
},
uuid: "dummy",
},
{
name: "The Invisible Duo",
uuid: "invisible",
group: TeamGroup.Red,
worms: [
{
Expand Down Expand Up @@ -118,36 +120,38 @@ export default async function runScenario(game: Game) {
// world.addEntity(newProjectile);
// }));

const [dummyteam, playerteam] = gameState.getActiveTeams();

const dummy = world.addEntity(
TestDummy.create(
parent,
world,
Coordinate.fromScreen(650, 620),
gameState.getTeamByIndex(0).worms[0],
dummyteam.worms[0],
),
);
world.addEntity(
TestDummy.create(
parent,
world,
Coordinate.fromScreen(1500, 300),
gameState.getTeamByIndex(0).worms[1],
dummyteam.worms[1],
),
);
world.addEntity(
TestDummy.create(
parent,
world,
Coordinate.fromScreen(1012, 678),
gameState.getTeamByIndex(0).worms[2],
dummyteam.worms[2],
),
);
world.addEntity(
Worm.create(
parent,
world,
Coordinate.fromScreen(600, 550),
gameState.getTeamByIndex(1).worms[0],
playerteam.worms[0],
async () => {
return [];
},
Expand Down
24 changes: 12 additions & 12 deletions src/scenarios/netGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Logger from "../log";
import { RemoteWorm } from "../entities/playable/remoteWorm";
import { logger } from "matrix-js-sdk/lib/logger";
import { getDefinitionForCode } from "../weapons";
import { NetGameState } from "../net/netGameState";

const log = new Logger("scenario");

Expand Down Expand Up @@ -148,17 +149,18 @@ export default async function runScenario(game: Game) {
const myUserId = gameInstance.myUserId;

const stateLogger = new Logger("StateRecorder");
const stateRecorder = new StateRecorder(world, {
const stateRecorder = new StateRecorder({
async writeLine(data) {
stateLogger.debug("Writing state", data);
gameInstance.writeAction(data);
},
});
const gameState = new GameState(
const gameState = new NetGameState(
initialTeams,
world,
gameInstance.rules,
gameInstance.isHost ? stateRecorder : undefined,
stateRecorder,
gameInstance.myUserId,
);

const bg = await world.addEntity(
Expand All @@ -184,9 +186,7 @@ export default async function runScenario(game: Game) {
game.viewport.screenHeight,
);

const waterLevel = parseFloat(
level.objects.find((v) => v.type === "wormgine.water")?.tra.y ?? "0",
);
const waterLevel = level.objects.find((v) => v.type === "wormgine.water")?.tra.y ?? 0;

const water = world.addEntity(
new Water(
Expand All @@ -209,8 +209,8 @@ export default async function runScenario(game: Game) {
const t = new WeaponTarget(
world,
Coordinate.fromScreen(
parseFloat(levelObject.tra.x),
parseFloat(levelObject.tra.y),
levelObject.tra.x,
levelObject.tra.y,
),
parent,
);
Expand All @@ -236,8 +236,8 @@ export default async function runScenario(game: Game) {
}
const nextLocation = spawnPositions[nextLocationIdx];
const pos = Coordinate.fromScreen(
parseFloat(nextLocation.tra.x),
parseFloat(nextLocation.tra.y),
nextLocation.tra.x,
nextLocation.tra.y,
);
const fireFn: FireFn = async (worm, definition, opts) => {
const newProjectile = definition.fireFn(parent, world, worm, opts);
Expand All @@ -246,7 +246,7 @@ export default async function runScenario(game: Game) {
CameraLockPriority.LockIfNotLocalPlayer;
world.addEntity(newProjectile);
}
stateRecorder.syncEntityState();
stateRecorder.syncEntityState(world);
const res = await newProjectile.onFireResult;
return res;
};
Expand Down Expand Up @@ -355,7 +355,7 @@ export default async function runScenario(game: Game) {
}
}
if (endOfRoundWaitDuration === null) {
stateRecorder.syncEntityState();
stateRecorder.syncEntityState(world);
const nextState = gameState.advanceRound();
if ("winningTeams" in nextState) {
if (nextState.winningTeams.length) {
Expand Down
171 changes: 171 additions & 0 deletions src/scenarios/netGameTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { Assets, Ticker } from "pixi.js";
import { Background } from "../entities/background";
import { BitmapTerrain } from "../entities/bitmapTerrain";
import type { Game } from "../game";
import { Water } from "../entities/water";
import { FireFn, Worm } from "../entities/playable/worm";
import { Coordinate, MetersValue } from "../utils/coodinate";
import { GameState } from "../logic/gamestate";
import { GameStateOverlay } from "../overlays/gameStateOverlay";
import {
GameDrawText,
TeamWinnerText,
templateRandomText,
} from "../text/toasts";
import { PhysicsEntity } from "../entities/phys/physicsEntity";
import staticController, { InputKind } from "../input";
import { StateRecorder } from "../state/recorder";
import { CameraLockPriority, ViewportCamera } from "../camera";
import { getAssets } from "../assets";
import { scenarioParser } from "../levels/scenarioParser";
import { WeaponTarget } from "../entities/phys/target";
import { WormSpawnRecordedState } from "../entities/state/wormSpawn";
import { InnerWormState } from "../entities/playable/wormState";
import Logger from "../log";
import { RemoteWorm } from "../entities/playable/remoteWorm";
import { logger } from "matrix-js-sdk/lib/logger";
import { getDefinitionForCode } from "../weapons";
import { NetGameState } from "../net/netGameState";

const log = new Logger("scenario");

export default async function runScenario(game: Game) {
if (!game.level) {
throw Error("Level required!");
}
if (!game.netGameInstance) {
throw Error("Network required!");
}
const gameInstance = game.netGameInstance;
const parent = game.viewport;
const world = game.world;
const { worldWidth } = game.viewport;

const player = gameInstance.player;
player.on("started", () => {
logger.info("started playback");
});

const assets = getAssets();
const level = await scenarioParser(game.level, assets.data, assets.textures);
const bitmapPosition = Coordinate.fromScreen(
level.terrain.x,
level.terrain.y,
);
const terrain = BitmapTerrain.create(
game.world,
level.terrain.bitmap,
bitmapPosition,
level.terrain.destructible,
);

const initialTeams = gameInstance.gameStateImmediate.teams;

for (const team of gameInstance.gameStateImmediate.teams) {
if (team.flag) {
Assets.add({ alias: `team-flag-${team.name}`, src: team.flag });
await Assets.load(`team-flag-${team.name}`);
}
}

const stateLogger = new Logger("StateRecorder");
const stateRecorder = new StateRecorder({
async writeLine(data) {
stateLogger.debug("Writing state", data);
gameInstance.writeAction(data);
},
});
const gameState = new NetGameState(
initialTeams,
world,
gameInstance.rules,
stateRecorder,
gameInstance.myUserId,
);

const bg = await world.addEntity(
Background.create(
game.viewport.screenWidth,
game.viewport.screenHeight,
game.viewport,
[20, 21, 50, 35],
terrain,
world,
),
);
bg.addToWorld(game.pixiApp.stage, parent);
world.addEntity(terrain);
terrain.addToWorld(parent);

const overlay = new GameStateOverlay(
game.pixiApp.ticker,
game.pixiApp.stage,
gameState,
world,
game.viewport.screenWidth,
game.viewport.screenHeight,
);

const waterLevel = level.objects.find((v) => v.type === "wormgine.water")?.tra.y ?? 0;

const water = world.addEntity(
new Water(
MetersValue.fromPixels(worldWidth * 4),
MetersValue.fromPixels(waterLevel),
world,
),
);
water.addToWorld(parent, world);

const camera = new ViewportCamera(game.viewport, world, water.waterHeight);
camera.snapToPosition(
bitmapPosition.toScreenPoint(),
CameraLockPriority.SuggestedLockNonLocal,
false,
);

for (const levelObject of level.objects) {
if (levelObject.type === "wormgine.target") {
const t = new WeaponTarget(
world,
Coordinate.fromScreen(
levelObject.tra.x,
levelObject.tra.y,
),
parent,
);
world.addEntity(t);
parent.addChild(t.sprite);
}
}

player.on('gameState', (s) => {
log.info('New game state recieved:', s.iteration);
gameState.applyGameStateUpdate(s);
});

await gameInstance.ready();
await gameInstance.allClientsReady();

const gameHasStarted = gameInstance.gameStateImmediate.iteration > 0;

if (gameInstance.isHost && !gameHasStarted) {
await stateRecorder.writeHeader();
log.info("All clients are ready! Beginning round");
gameState.advanceRound();
gameState.beginRound();
} else if (!gameHasStarted) {
await gameInstance.ready();
log.info("Marked as ready");
}

if (gameInstance.isHost) {
setInterval(() => {
gameState.markAsFinished();
gameState.advanceRound();
gameState.beginRound();
}, 3000);
}

log.info("Game can now begin");
}
5 changes: 1 addition & 4 deletions src/scenarios/replayTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ export default async function runScenario(game: Game) {
await RemoteWorm.create(
parent,
world,
new Coordinate(
parseFloat(existingEntData.tra.x),
parseFloat(existingEntData.tra.y),
),
new Coordinate(existingEntData.tra.x, existingEntData.tra.y),
wormInstance,
async (worm, definition, opts) => {
const newProjectile = definition.fireFn(parent, world, worm, opts);
Expand Down
18 changes: 8 additions & 10 deletions src/scenarios/tiledMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default async function runScenario(game: Game) {
const recordedGameplayKey = `wormgine_recorded_${new Date().toISOString()}`;
let recordedState = "";

const stateRecorder = new StateRecorder(world, {
const stateRecorder = new StateRecorder({
async writeLine(data) {
recordedState += `${JSON.stringify(data)}|`;
localStorage.setItem(recordedGameplayKey, recordedState);
Expand Down Expand Up @@ -114,9 +114,7 @@ export default async function runScenario(game: Game) {
game.viewport.screenHeight,
);

const waterLevel = parseFloat(
level.objects.find((v) => v.type === "wormgine.water")?.tra.y ?? "0",
);
const waterLevel = level.objects.find((v) => v.type === "wormgine.water")?.tra.y ?? 0;

const water = world.addEntity(
new Water(
Expand All @@ -139,8 +137,8 @@ export default async function runScenario(game: Game) {
const t = new WeaponTarget(
world,
Coordinate.fromScreen(
parseFloat(levelObject.tra.x),
parseFloat(levelObject.tra.y),
levelObject.tra.x,
levelObject.tra.y,
),
parent,
);
Expand All @@ -167,8 +165,8 @@ export default async function runScenario(game: Game) {
parent,
world,
Coordinate.fromScreen(
parseFloat(nextLocation.tra.x),
parseFloat(nextLocation.tra.y),
nextLocation.tra.x,
nextLocation.tra.y,
),
wormInstance,
async (worm, definition, opts) => {
Expand All @@ -178,7 +176,7 @@ export default async function runScenario(game: Game) {
CameraLockPriority.LockIfNotLocalPlayer;
world.addEntity(newProjectile);
}
stateRecorder.syncEntityState();
stateRecorder.syncEntityState(world);
const res = await newProjectile.onFireResult;
return res;
},
Expand Down Expand Up @@ -258,7 +256,7 @@ export default async function runScenario(game: Game) {
}
}
if (endOfRoundWaitDuration === null) {
stateRecorder.syncEntityState();
stateRecorder.syncEntityState(world);
const nextState = gameState.advanceRound();
if ("winningTeams" in nextState) {
if (nextState.winningTeams.length) {
Expand Down

0 comments on commit 4d3f1dc

Please sign in to comment.