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/movement.ts | |
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/movement.ts')
-rw-r--r-- | client/src/logic/movement.ts | 113 |
1 files changed, 107 insertions, 6 deletions
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 |