import { genItems, getMap } from "../map.js"; import { updatePlayers } from "./players.js" import { updateUI } from "./ui.js" import { updateMovement } from "./movement.js" import { updateItems } from "./items.js" import { GameState, Input, Rotation, SpawnIndex } from "../types.js"; import { updateGhosts } from "./ai.js"; export const InitialState: GameState = { started: false, input: {}, players: [], ghosts: [undefined, undefined, undefined, undefined], items: {}, mapId: undefined, frame: 0, rng: 0, roundTimer: 0, endRoundTimer: undefined } export const random = (state: GameState): number => { return state.rng = (state.rng * 926659 + 4294967291) % 16381 } export const onLogic = ( pastData: GameState = InitialState, input: Input = { players: {} }, frame: number ) => { let data = structuredClone(pastData) data.frame = frame random(data) let startPressed = updatePlayers(data, input); let playersLeft = 0 for (let id in data.players) { let player = data.players[id] if (player.dead) { player.framesDead++ } else { playersLeft++ } } if (data.started) { data.roundTimer++ if (data.roundTimer < 60 * 5) { // uh do nothing just draw shit } else if (playersLeft > 1) { updateMovement(data) updateItems(data) updateGhosts(data) } else { if (data.endRoundTimer === undefined) { data.endRoundTimer = 60 * 5 } data.endRoundTimer-- if (data.endRoundTimer < 1) { nextMap(data) } } } else { updateUI(data) } if (startPressed && !data.started && Object.values(data.players).length > 1) { nextMap(data) data.started = true; } let map = getMap(data.mapId) if (map && Object.keys(data.items).length < 1) { data.items = genItems(map, false) } return data } const nextMap = (gameData: GameState) => { if (gameData.mapId === undefined) { gameData.mapId = 0 } else { gameData.mapId++ } if (gameData.mapId > 3) { gameData.mapId = undefined gameData.started = false return } gameData.ghosts = [undefined, undefined, undefined, undefined] let map = getMap(gameData.mapId) let index = SpawnIndex.PAC_SPAWN_1 for (let id in gameData.players) { let player = gameData.players[id] player.pos = structuredClone(map.spawns[index++]) player.dead = false player.framesDead = 0 player.thiccLeft = 0 player.atePellets = 0 player.moving = false player.moveRotation = Rotation.NOTHING player.inputRotation = Rotation.NOTHING player.velocityRotation = Rotation.NOTHING } gameData.items = genItems(map) gameData.roundTimer = 0 gameData.endRoundTimer = undefined }