diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-06-29 11:40:46 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-06-29 11:40:46 -0400 |
commit | f5fcce110a915fca1b114001962170733276e5df (patch) | |
tree | 6ce82c649d7377b42e75c9feb88d73e4aa15d713 /client/src/logic | |
parent | ghost (diff) | |
download | tuxman-f5fcce110a915fca1b114001962170733276e5df.tar.gz tuxman-f5fcce110a915fca1b114001962170733276e5df.tar.bz2 tuxman-f5fcce110a915fca1b114001962170733276e5df.zip |
audio, finalize gameplay, wrap around map, stuff
Diffstat (limited to 'client/src/logic')
-rw-r--r-- | client/src/logic/ai.ts | 237 | ||||
-rw-r--r-- | client/src/logic/items.ts | 16 | ||||
-rw-r--r-- | client/src/logic/logic.ts | 83 | ||||
-rw-r--r-- | client/src/logic/movement.ts | 113 | ||||
-rw-r--r-- | client/src/logic/players.ts | 5 |
5 files changed, 406 insertions, 48 deletions
diff --git a/client/src/logic/ai.ts b/client/src/logic/ai.ts index 875621a..25e5d25 100644 --- a/client/src/logic/ai.ts +++ b/client/src/logic/ai.ts @@ -1,7 +1,73 @@ import { getMap } from "../map.js"; import { Map, Vec2, GhostType, GameState, SpawnIndex, Player, Rotation, GhostState, Tile, Ghost } from "../types.js"; import { random } from "./logic.js"; -import { MOVE_SPEED, roundPos, isStablePos, getTile, getTileFrontWithRot, incrementPos } from "./movement.js"; +import { MOVE_SPEED, roundPos, isStablePos, getTile, getTileFrontWithRot, incrementPos, wrapPos } from "./movement.js"; + +const vec2eq = (a: Vec2, b: Vec2): boolean => { + return a.x == b.x && a.y == b.y +} + +const canPath = (tile: Tile): boolean => tile != Tile.WALL +const canCollide = (tile: Tile, isPathing: boolean): boolean => tile != Tile.WALL && (isPathing ? true : tile != Tile.GHOST_EXIT) + +const path = (start: Vec2, end: Vec2, map: Map) => { + let mask: Vec2[] = new Array(map.width * map.height) + let queue: Vec2[] = [] + queue.push(start) + + while (true) { + + let next: Vec2 = queue.shift() + if (!next) { + console.log('failed') + return undefined + } + if (vec2eq(next, end)) break + + let north = canPath(getTile(map, next, 0, -1)) + let south = canPath(getTile(map, next, 0, 1)) + let east = canPath(getTile(map, next, 1, 0)) + let west = canPath(getTile(map, next, -1, 0)) + + if (north && !mask[(next.y - 1) * map.width + (next.x)]) { + queue.push({x: next.x, y: next.y - 1}) + mask[(next.y - 1) * map.width + (next.x)] = next + } + + if (south && !mask[(next.y + 1) * map.width + (next.x)]) { + queue.push({x: next.x, y: next.y + 1}) + mask[(next.y + 1) * map.width + (next.x)] = next + } + + if (east && !mask[(next.y) * map.width + (next.x + 1)]) { + queue.push({x: next.x + 1, y: next.y}) + mask[(next.y) * map.width + (next.x + 1)] = next + } + + if (west && !mask[(next.y) * map.width + (next.x - 1)]) { + queue.push({x: next.x - 1, y: next.y}) + mask[(next.y) * map.width + (next.x - 1)] = next + } + } + + let solution = [] + + let next = end + while (true) { + solution.push(structuredClone(next)) + if (vec2eq(next, start)) { + break + } else { + next = mask[next.y * map.width + next.x] + } + } + + solution = solution.reverse() + solution.shift() + + return solution + +} const diff = (a: Vec2, b:Vec2): Vec2 => { return {x: a.x - b.x, y: a.y - b.y} @@ -80,6 +146,12 @@ const pickTargetChase = (state: GameState, type: GhostType): Vec2 => { const pickTarget = (state: GameState, map: Map, type: GhostType): Vec2 => { let ghost: Ghost = state.ghosts[type] + + let target = ghost.targetQueue.shift() + if (target) { + return target + } + switch (ghost.state) { case GhostState.SCATTER: return pickTargetScatter(state, type) @@ -95,17 +167,105 @@ const pickTarget = (state: GameState, map: Map, type: GhostType): Vec2 => { } } -const flipRot = (rot: Rotation) => { - switch (rot) { - case Rotation.NORTH: - return Rotation.SOUTH - case Rotation.SOUTH: - return Rotation.NORTH - case Rotation.EAST: - return Rotation.WEST - case Rotation.WEST: - return Rotation.EAST +const checkIfEaten = (ghost: Ghost, state: GameState): boolean => { + + if (ghost.state != GhostState.SCARED) { + return false } + + for (let id in state.players) { + let player = state.players[id] + + if (player.thiccLeft > 0 && dist(player.pos, ghost.pos) <= 1) { + return true + } + } + + return false +} + +const updateKilled = (ghost: Ghost, state: GameState) => { + + if (ghost.state == GhostState.EATEN) { + return + } + + for (let id in state.players) { + let player = state.players[id] + + if (dist(player.pos, ghost.pos) > 1) { + continue + } + + if (ghost.state != GhostState.SCARED || player.thiccLeft < 1) { + player.dead = true + player.framesDead = 0 + } + } + +} + +const getGhostState = (ghost: Ghost, state: GameState): GhostState => { + + if (ghost.state == GhostState.EATEN || checkIfEaten(ghost, state)) { + ghost.hasRespawned = false + return GhostState.EATEN + } + + if (Object.values(state.players).filter((p: Player) => p.thiccLeft > 0).length > 0) { + if (!ghost.hasRespawned) { + return GhostState.SCARED + } + } else { + ghost.hasRespawned = false + } + + return (Object.keys(state.items).length % 100 < 10 ? GhostState.SCATTER : GhostState.CHASE) +} + +const getGhostPath = (ghost: Ghost, map: Map): Vec2[] => { + + if (ghost.targetQueue.length > 0) { // already pathing + return undefined + } + + if (ghost.state == GhostState.EATEN) { // dead go back to spawn + if (vec2eq(ghost.pos, map.spawns[SpawnIndex.GHOST_SPAWN])) { // returned to spawn, exit the box + ghost.state = GhostState.CHASE + ghost.hasRespawned = true + return path(ghost.pos, map.spawns[SpawnIndex.GHOST_EXIT], map) + } else { // go to the box still dead + return path(ghost.pos, map.spawns[SpawnIndex.GHOST_SPAWN], map) + } + } + + let tile = getTile(map, ghost.pos, 0, 0) + if (tile != Tile.GHOST_WALL && tile != Tile.GHOST_SPAWN) { // not in the box + return undefined + } + + let threshold = 0 + switch (ghost.type) { + case GhostType.BLINKY: + threshold = 60 * 5 + break + case GhostType.PINKY: + threshold = 60 * 10 + break + case GhostType.INKY: + threshold = 60 * 15 + break + case GhostType.CLYDE: + threshold = 60 * 20 + break + } + + if (ghost.framesInBox < threshold) { + return undefined + } + + return path(ghost.pos, map.spawns[SpawnIndex.GHOST_EXIT], map) + } const updateGhost = (state: GameState, map: Map, type: GhostType) => { @@ -115,22 +275,45 @@ const updateGhost = (state: GameState, map: Map, type: GhostType) => { ghost = { pos: structuredClone(map.spawns[SpawnIndex.GHOST_SPAWN]), target: structuredClone(map.spawns[SpawnIndex.GHOST_SPAWN]), + targetQueue: [], type, - state: GhostState.SCARED, + state: GhostState.SCATTER, currentDirection: Rotation.EAST, + hasRespawned: false, + framesInBox: 0, } state.ghosts[type] = ghost } + + let tile = getTile(map, ghost.pos, 0, 0) + if (tile == Tile.GHOST_WALL || tile == Tile.GHOST_SPAWN) { + ghost.framesInBox++ + } + + ghost.state = getGhostState(ghost, state) if (isStablePos(ghost.pos)) { ghost.pos = roundPos(ghost.pos) + + let newPath = getGhostPath(ghost, map) + if (newPath) { + ghost.targetQueue = newPath + } - let front = getTileFrontWithRot(map, ghost.pos, ghost.currentDirection) == Tile.WALL - let north = getTile(map, ghost.pos, 0, -1) != Tile.WALL - let east = getTile(map, ghost.pos, 1, 0) != Tile.WALL - let south = getTile(map, ghost.pos, 0, 1) != Tile.WALL - let west = getTile(map, ghost.pos, -1, 0) != Tile.WALL + let frontTile = getTileFrontWithRot(map, ghost.pos, ghost.currentDirection) + let northTile = getTile(map, ghost.pos, 0, -1) + let eastTile = getTile(map, ghost.pos, 1, 0) + let southTile = getTile(map, ghost.pos, 0, 1) + let westTile = getTile(map, ghost.pos, -1, 0) + + let isPathing = ghost.targetQueue.length > 0 + + let front = canCollide(frontTile, isPathing) + let north = canCollide(northTile, isPathing) + let east = canCollide(eastTile, isPathing) + let south = canCollide(southTile, isPathing) + let west = canCollide(westTile, isPathing) let isIntersection = (north && east) || @@ -138,18 +321,20 @@ const updateGhost = (state: GameState, map: Map, type: GhostType) => { (south && west) || (west && north) - if (!isIntersection && front) { - ghost.currentDirection = flipRot(ghost.currentDirection) - } else if (isIntersection) { + if (isIntersection || !front || (isPathing && vec2eq(ghost.pos, ghost.target))) { let target = pickTarget(state, map, type) ghost.target = target + + if ((!isIntersection && !front) || isPathing) { + ghost.currentDirection = undefined + } let newRot = ghost.currentDirection let min = undefined if (north && ghost.currentDirection !== Rotation.SOUTH) { let d = dist({x: ghost.pos.x, y: ghost.pos.y - 1}, target) - if (!min || min > d) { + if (min === undefined || min > d) { min = d newRot = Rotation.NORTH } @@ -157,7 +342,7 @@ const updateGhost = (state: GameState, map: Map, type: GhostType) => { if (east && ghost.currentDirection !== Rotation.WEST) { let d = dist({x: ghost.pos.x + 1, y: ghost.pos.y}, target) - if (!min || min > d) { + if (min === undefined || min > d) { min = d newRot = Rotation.EAST } @@ -165,7 +350,7 @@ const updateGhost = (state: GameState, map: Map, type: GhostType) => { if (south && ghost.currentDirection !== Rotation.NORTH) { let d = dist({x: ghost.pos.x, y: ghost.pos.y + 1}, target) - if (!min || min > d) { + if (min === undefined || min > d) { min = d newRot = Rotation.SOUTH } @@ -173,7 +358,7 @@ const updateGhost = (state: GameState, map: Map, type: GhostType) => { if (west && ghost.currentDirection !== Rotation.EAST) { let d = dist({x: ghost.pos.x - 1, y: ghost.pos.y}, target) - if (!min || min > d) { + if (min === undefined || min > d) { min = d newRot = Rotation.WEST } @@ -183,7 +368,9 @@ const updateGhost = (state: GameState, map: Map, type: GhostType) => { } } - incrementPos(ghost.pos, ghost.currentDirection, MOVE_SPEED) + incrementPos(ghost.pos, ghost.currentDirection, MOVE_SPEED) + wrapPos(ghost.pos, map) + updateKilled(ghost, state) } export const updateGhosts = (state: GameState) => { diff --git a/client/src/logic/items.ts b/client/src/logic/items.ts index 5f8a38e..79624d3 100644 --- a/client/src/logic/items.ts +++ b/client/src/logic/items.ts @@ -1,5 +1,5 @@ import { getMap, getItemKey } from "../map.js" -import { GameState, Map, Player } from "../types.js" +import { GameState, ItemType, Map, Player } from "../types.js" const ceilHalf = (n: number): number => { return Math.ceil(n*2)/2 @@ -12,10 +12,24 @@ const floorHalf = (n: number): number => { const eatItems = (data: GameState, map: Map, player: Player) => { let pos = player.pos + + player.atePellets = Math.max(player.atePellets - 1, 0) for (let x = ceilHalf(pos.x-.5); x <= floorHalf(pos.x+.5); x += .5) { for (let y = ceilHalf(pos.y-.5); y <= floorHalf(pos.y+.5); y += .5) { let item_key = getItemKey(x, y, map.width) + + let item = data.items[item_key] + if (!item) { + continue + } + + player.atePellets = 30 + + if (item.type == ItemType.THICC_DOT) { + player.thiccLeft += 60 * 10 + } + delete data.items[item_key] } } diff --git a/client/src/logic/logic.ts b/client/src/logic/logic.ts index d9ac6c8..1c3ec1a 100644 --- a/client/src/logic/logic.ts +++ b/client/src/logic/logic.ts @@ -3,7 +3,7 @@ import { updatePlayers } from "./players.js" import { updateUI } from "./ui.js" import { updateMovement } from "./movement.js" import { updateItems } from "./items.js" -import { GameState, Input } from "../types.js"; +import { GameState, Input, Rotation, SpawnIndex } from "../types.js"; import { updateGhosts } from "./ai.js"; export const InitialState: GameState = { @@ -14,7 +14,9 @@ export const InitialState: GameState = { items: {}, mapId: undefined, frame: 0, - rng: 0 + rng: 0, + roundTimer: 0, + endRoundTimer: undefined } export const random = (state: GameState): number => { @@ -32,36 +34,85 @@ export const onLogic = ( 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) { - updateMovement(data) - updateItems(data) - updateGhosts(data) + 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) { - initMap(data, 0) - data.started = true; + 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 initMap = (gameData: GameState, mapId: number) => { +const nextMap = (gameData: GameState) => { - document.getElementById("lobby").style.display = "none" + if (gameData.mapId === undefined) { + gameData.mapId = 0 + } else { + gameData.mapId++ + } - gameData.mapId = mapId + if (gameData.mapId > 3) { + gameData.mapId = undefined + gameData.started = false + return + } + + gameData.ghosts = [undefined, undefined, undefined, undefined] - let map = getMap(mapId) - // if (!map) { - // let {width, height, data} = decompressMap(maps[mapId]) - // map = genMap(width, height, data, mapId) - // } + 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 } diff --git a/client/src/logic/movement.ts b/client/src/logic/movement.ts index f2a06e7..726f87a 100644 --- a/client/src/logic/movement.ts +++ b/client/src/logic/movement.ts @@ -1,5 +1,5 @@ import { getMap } from "../map.js" -import { Vec2, Map, Rotation, Key, Player, GameState, Tile } from "../types.js" +import { Vec2, Map, Rotation, Key, Player, GameState, Tile, BoundingBox } from "../types.js" export const MOVE_SPEED = .08333 @@ -20,10 +20,18 @@ export const getTile = ( ): number => { let x = Math.round(pos.x + ox) let y = Math.round(pos.y + oy) - if (x < 0 || x >= map.width || y < 0 || y >= map.height) return Tile.WALL + + x = (x + map.width) % map.width + y = (y + map.height) % map.height + return map.data[y * map.width + x] } +export const wrapPos = (pos: Vec2, map: Map) => { + pos.x = (pos.x + map.width) % map.width + pos.y = (pos.y + map.height) % map.height +} + export const getTileFrontWithRot = ( map: Map, pos: Vec2, @@ -86,10 +94,11 @@ const updateMovementForPlayer = ( let inputRot = getRot(inputKey) let moveRot = player.moveRotation + let speed = MOVE_SPEED let currentPosition = player.pos let turningFrontTile = getTileFrontWithRot(map, currentPosition, inputRot) - if (turningFrontTile == Tile.WALL || turningFrontTile == Tile.GHOST_WALL) { + if (turningFrontTile == Tile.WALL || turningFrontTile == Tile.GHOST_WALL || turningFrontTile == Tile.GHOST_EXIT) { inputRot = Rotation.NOTHING } @@ -97,24 +106,110 @@ const updateMovementForPlayer = ( player.inputRotation = inputRot - if (turning && isStablePos(currentPosition)) { + if (player.velocityRotation != Rotation.NOTHING) { + moveRot = player.velocityRotation + speed *= 1.5 + } else if (turning && isStablePos(currentPosition)) { currentPosition = roundPos(currentPosition) player.moveRotation = inputRot moveRot = inputRot } let movePos = structuredClone(currentPosition) - incrementPos(movePos, moveRot, MOVE_SPEED) + incrementPos(movePos, moveRot, speed) + wrapPos(movePos, map) let frontTile = getTileFrontWithRot(map, currentPosition, moveRot) - if (frontTile != Tile.WALL && frontTile != Tile.GHOST_WALL) { + if (frontTile != Tile.WALL && frontTile != Tile.GHOST_WALL && frontTile != Tile.GHOST_EXIT) { player.pos = movePos player.moving = true } else { player.pos = roundPos(currentPosition) player.moving = false + player.velocityRotation = Rotation.NOTHING + } + +} + +const createBoundingBox = (player: Player): BoundingBox => { + let pos = player.pos + let width = player.thiccLeft > 0 ? 2 : 1 + return { + x: pos.x - width/2, + y: pos.y - width/2, + w: width, + h: width + } +} + +const checkBoundingBox = (aBB: BoundingBox, bBB: BoundingBox): Rotation => { + if ( + aBB.x < bBB.x + bBB.w && + aBB.x + aBB.w > bBB.x && + aBB.y < bBB.y + bBB.h && + aBB.y + aBB.h > bBB.y + ) { + let diff = {x: aBB.x - bBB.x, y: aBB.y - bBB.y} + + if (Math.abs(diff.x) > Math.abs(diff.y)) { + if (diff.x > 0) { + return Rotation.EAST + } else { + return Rotation.WEST + } + } else { + if (diff.y < 0) { + return Rotation.NORTH + } else { + return Rotation.SOUTH + } + } + + } else { + return Rotation.NOTHING + } +} + +const flipRotation = (rot: Rotation): Rotation => { + switch (rot) { + case Rotation.NOTHING: + return Rotation.NOTHING + case Rotation.NORTH: + return Rotation.SOUTH + case Rotation.SOUTH: + return Rotation.NORTH + case Rotation.EAST: + return Rotation.WEST + case Rotation.WEST: + return Rotation.EAST } +} +const updateCollision = (data: GameState) => { + + let players: Player[] = Object.values(data.players).filter(p => p != null && p !== undefined) + let bb = structuredClone(players).map(p => createBoundingBox(p)) + let num = players.length + + for (let i = 0; i < num - 1; i++) { + for (let j = i + 1; j < num; j++) { + let rot = checkBoundingBox(bb[i], bb[j]) + if (rot == Rotation.NOTHING) { + continue + } + + if (players[i].thiccLeft > 0 && players[j].thiccLeft == 0) { + players[j].dead = true + players[i].framesDead = 0 + } else if (players[j].thiccLeft > 0 && players[i].thiccLeft == 0) { + players[i].dead = true + players[i].framesDead = 0 + } else { + players[i].velocityRotation = rot + players[j].velocityRotation = flipRotation(rot) + } + } + } } @@ -123,9 +218,15 @@ export const updateMovement = (data: GameState) => { let map = getMap(data.mapId) if (!map) return + updateCollision(data) + for (const id in data.players) { const player = data.players[id] + + if (player.thiccLeft > 0) { + player.thiccLeft-- + } if(!player) { continue diff --git a/client/src/logic/players.ts b/client/src/logic/players.ts index 96779fe..96337ea 100644 --- a/client/src/logic/players.ts +++ b/client/src/logic/players.ts @@ -34,7 +34,12 @@ export const updatePlayers = (data: GameState, input: Input) => { pos: {x: 1, y: 1}, inputRotation: Rotation.EAST, moveRotation: Rotation.NOTHING, + velocityRotation: Rotation.NOTHING, moving: false, + thiccLeft: 0, + dead: false, + framesDead: 0, + atePellets: 0 }; } |