From c092568a3d193df60df93df77a07e48a80aa0577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Ta=C5=84czyk?= Date: Sat, 24 Aug 2024 00:27:17 +0300 Subject: [PATCH] clean up --- .../src/game-states/gameplay/bonus-logic.ts | 2 +- .../src/game-states/gameplay/game-logic.ts | 8 +- .../game-states/gameplay/gameplay-types.ts | 56 +----------- .../gameplay/levels/13-the-final-countdown.ts | 15 +--- .../src/game-states/gameplay/monster-logic.ts | 2 +- .../gameplay/render/animation-utils.ts | 15 +--- .../gameplay/render/entity-render.ts | 10 +-- .../gameplay/render/isometric-utils.ts | 87 ------------------- 8 files changed, 14 insertions(+), 181 deletions(-) diff --git a/js13k2024/game/src/game-states/gameplay/bonus-logic.ts b/js13k2024/game/src/game-states/gameplay/bonus-logic.ts index d807eca0..58e02a49 100644 --- a/js13k2024/game/src/game-states/gameplay/bonus-logic.ts +++ b/js13k2024/game/src/game-states/gameplay/bonus-logic.ts @@ -216,7 +216,7 @@ export const handleBlasterShot = (gameState: GameState, direction: Direction, le }); }; -export const isMonsterOnBlasterPath = ( +const isMonsterOnBlasterPath = ( monsterPos: Position, start: Position, end: Position, diff --git a/js13k2024/game/src/game-states/gameplay/game-logic.ts b/js13k2024/game/src/game-states/gameplay/game-logic.ts index 9547eaf2..590c4ec8 100644 --- a/js13k2024/game/src/game-states/gameplay/game-logic.ts +++ b/js13k2024/game/src/game-states/gameplay/game-logic.ts @@ -6,7 +6,6 @@ import { checkTimeBombExplosion, isInExplosionRange, } from './monster-logic'; -import { generateLevel } from './level-generator'; import { MOVE_ANIMATION_DURATION, OBSTACLE_DESTRUCTION_DURATION } from './render/animation-utils'; import { soundEngine } from '../../sound/sound-engine'; import { getDirectionFromKey, getNewPosition, isPositionEqual, isPositionOccupied, isValidMove } from './move-utils'; @@ -20,11 +19,6 @@ import { performTeleportation, } from './bonus-logic'; -export const initializeGame = (level: number): [GameState, LevelConfig] => { - const [gameState, config] = generateLevel(level); - return [{ ...gameState, gameEndingState: 'none', tsunamiLevel: 0, blasterShots: [] }, config]; -}; - export const handleKeyPress = (e: KeyboardEvent, gameState: GameState, levelConfig: LevelConfig): GameState => { if (isGameEnding(gameState)) { return gameState; @@ -215,7 +209,7 @@ export const startGameOverAnimation = (gameState: GameState): void => { soundEngine.playGameOver(); }; -export const startLevelCompleteAnimation = (gameState: GameState): void => { +const startLevelCompleteAnimation = (gameState: GameState): void => { gameState.player.isVictorious = true; soundEngine.playLevelComplete(); }; diff --git a/js13k2024/game/src/game-states/gameplay/gameplay-types.ts b/js13k2024/game/src/game-states/gameplay/gameplay-types.ts index e5106c14..67162311 100644 --- a/js13k2024/game/src/game-states/gameplay/gameplay-types.ts +++ b/js13k2024/game/src/game-states/gameplay/gameplay-types.ts @@ -49,7 +49,7 @@ export interface Obstacle { isDestroying: boolean; } -export type GameEndingState = 'none' | 'gameOver' | 'levelComplete'; +type GameEndingState = 'none' | 'gameOver' | 'levelComplete'; export interface GameState { player: Player; @@ -75,7 +75,7 @@ export type Explosion = { duration: number; }; -export type TimeBomb = { +type TimeBomb = { position: Position; timer: number; shakeIntensity: number; @@ -105,27 +105,6 @@ export interface LevelConfig { levelUpdater: (state: GameState, levelConfig: LevelConfig) => void; } -export interface Score { - points: number; - steps: number; - timeBonus: number; -} - -export interface PowerUp { - type: 'StepEraser' | 'MonsterFreeze' | 'Teleport'; - position: Position; -} - -export interface GameplayProps { - level: number; - score: number; - onGameOver: () => void; - onLevelComplete: () => void; - onGameComplete: () => void; - updateScore: (newScore: number) => void; - updateSteps: (newSteps: number) => void; -} - export interface PathfindingNode { position: Position; g: number; @@ -134,26 +113,6 @@ export interface PathfindingNode { parent: PathfindingNode | null; } -export interface GameConfig { - cellSize: number; - monsterSpawnInterval: number; - baseGridSize: number; - maxGridSize: number; - baseObstacleCount: number; - maxObstacleCount: number; - levelIncreaseFactor: number; - bonusDuration: number; - maxLevel: number; -} - -export type LevelGeneratorFunction = () => [GameState, LevelConfig, string]; - -export interface LevelData { - gameState: GameState; - levelConfig: LevelConfig; - story: string; -} - export interface BlasterShot { startPosition: Position; endPosition: Position; @@ -195,23 +154,12 @@ export function getBonusDescription(bonusType: BonusType): string { } } -// New interfaces for animation effects -export interface ScreenShake { - intensity: number; - duration: number; -} - export interface ElectricalDischarge { position: Position; intensity: number; duration: number; } -export interface EntityAnimationState { - bounceOffset: number; - tentacleAnimationFactor: number; -} - export function isActiveBonus(gameState: GameState, bonusType: BonusType) { return gameState.activeBonuses.some((bonus) => bonus.type === bonusType); } diff --git a/js13k2024/game/src/game-states/gameplay/levels/13-the-final-countdown.ts b/js13k2024/game/src/game-states/gameplay/levels/13-the-final-countdown.ts index 0316a060..cdf3288e 100644 --- a/js13k2024/game/src/game-states/gameplay/levels/13-the-final-countdown.ts +++ b/js13k2024/game/src/game-states/gameplay/levels/13-the-final-countdown.ts @@ -1,3 +1,4 @@ +import { spawnDynamicBonus } from '../bonus-logic'; import { GameState, LevelConfig, BonusType, Position } from '../gameplay-types'; import { createPosition, @@ -86,7 +87,7 @@ export const generateLevel = (): [GameState, LevelConfig, string] => { }; // This function should be called from the main game loop to update the level -const updateDynamicLevel = (state: GameState): void => { +const updateDynamicLevel = (state: GameState, levelConfig: LevelConfig): void => { // Spawn a new monster every 13 steps if (state.steps % 13 === 0) { let pos; @@ -96,16 +97,6 @@ const updateDynamicLevel = (state: GameState): void => { state.monsters.push(createMonster(pos.x, pos.y)); // Spawn a new bonus alongside the monster - spawnBonus(state); - } - - // Occasionally add new obstacles - if (state.steps % 50 === 0) { - spawnObstacles(state, 1); - } - - // Ensure there are always at least 3 bonuses on the board - while (state.bonuses.length < 3) { - spawnBonus(state); + spawnDynamicBonus(state, levelConfig); } }; diff --git a/js13k2024/game/src/game-states/gameplay/monster-logic.ts b/js13k2024/game/src/game-states/gameplay/monster-logic.ts index b831cc9f..fcde9eeb 100644 --- a/js13k2024/game/src/game-states/gameplay/monster-logic.ts +++ b/js13k2024/game/src/game-states/gameplay/monster-logic.ts @@ -222,7 +222,7 @@ export const checkLandMineCollision = (monsters: Monster[], landMines: Position[ return [newMonsters, newExplosions]; }; -export const removeLandMine = (landMines: Position[], landMine: Position): void => { +const removeLandMine = (landMines: Position[], landMine: Position): void => { landMines.splice(landMines.indexOf(landMine), 1); }; diff --git a/js13k2024/game/src/game-states/gameplay/render/animation-utils.ts b/js13k2024/game/src/game-states/gameplay/render/animation-utils.ts index 419267b3..c6bb71b9 100644 --- a/js13k2024/game/src/game-states/gameplay/render/animation-utils.ts +++ b/js13k2024/game/src/game-states/gameplay/render/animation-utils.ts @@ -21,7 +21,7 @@ export interface AnimationParams { heightAnimationFactor: number; } -export interface Position { +interface Position { x: number; y: number; } @@ -117,19 +117,6 @@ export const calculateObstacleHeight = (creationTime: number, isRaising: boolean return isRaising ? progress : 1; }; -// New function for bomb shaking animation -export const calculateBombShake = (remainingTime: number, maxTime: number): Position => { - const intensity = (1 - remainingTime / maxTime) * 5; // Increase intensity as time runs out - return calculateShakeOffset(intensity); -}; - -// New function for player blinking animation when invisibility is ending -export const calculateInvisibilityAlpha = (remainingTime: number, maxTime: number): number => { - if (remainingTime > maxTime * 0.2) return 0.5; // Normal invisibility alpha - const blinkSpeed = 10; // Adjust for faster/slower blinking - return 0.5 + 0.5 * Math.sin(remainingTime * blinkSpeed); -}; - // New function for confused monster shaking export const calculateConfusedShake = (): Position => { return calculateShakeOffset(2); // Constant small shake for confused monsters diff --git a/js13k2024/game/src/game-states/gameplay/render/entity-render.ts b/js13k2024/game/src/game-states/gameplay/render/entity-render.ts index 453dece5..1a018bcf 100644 --- a/js13k2024/game/src/game-states/gameplay/render/entity-render.ts +++ b/js13k2024/game/src/game-states/gameplay/render/entity-render.ts @@ -2,7 +2,7 @@ import { TILE_HEIGHT } from './isometric-utils'; import { calculateEntityDimensions, EntityRenderParams } from './entity-render-utils'; import { calculateBounceOffset, calculateConfusedShake } from './animation-utils'; -export const drawEntityBody = ( +const drawEntityBody = ( ctx: CanvasRenderingContext2D, isoX: number, isoY: number, @@ -21,7 +21,7 @@ export const drawEntityBody = ( ctx.fill(); }; -export const drawEntityHead = ( +const drawEntityHead = ( ctx: CanvasRenderingContext2D, isoX: number, isoY: number, @@ -36,7 +36,7 @@ export const drawEntityHead = ( ctx.fill(); }; -export const drawEntityEyes = ( +const drawEntityEyes = ( ctx: CanvasRenderingContext2D, isoX: number, isoY: number, @@ -60,7 +60,7 @@ export const drawEntityEyes = ( ctx.fill(); }; -export const drawEntityMouth = ( +const drawEntityMouth = ( ctx: CanvasRenderingContext2D, isoX: number, isoY: number, @@ -75,7 +75,7 @@ export const drawEntityMouth = ( ctx.stroke(); }; -export const drawEntityTentacles = ( +const drawEntityTentacles = ( ctx: CanvasRenderingContext2D, isoX: number, isoY: number, diff --git a/js13k2024/game/src/game-states/gameplay/render/isometric-utils.ts b/js13k2024/game/src/game-states/gameplay/render/isometric-utils.ts index 623820ba..3563652e 100644 --- a/js13k2024/game/src/game-states/gameplay/render/isometric-utils.ts +++ b/js13k2024/game/src/game-states/gameplay/render/isometric-utils.ts @@ -17,93 +17,6 @@ export function toIsometric(x: number, y: number): Position { }; } -/** - * Convert isometric screen coordinates to grid coordinates - * @param screenX Isometric screen x-coordinate - * @param screenY Isometric screen y-coordinate - * @returns Grid coordinates - */ -export function toGrid(screenX: number, screenY: number): Position { - const x = (screenX / (TILE_WIDTH / 2) + screenY / (TILE_HEIGHT / 2)) / 2; - const y = (screenY / (TILE_HEIGHT / 2) - screenX / (TILE_WIDTH / 2)) / 2; - return { x: Math.floor(x), y: Math.floor(y) }; -} - -/** - * Draw an isometric rectangle - * @param ctx Canvas rendering context - * @param x Top-left corner x-coordinate (in grid coordinates) - * @param y Top-left corner y-coordinate (in grid coordinates) - * @param width Width of the rectangle (in grid units) - * @param height Height of the rectangle (in grid units) - */ -export function drawIsometricRect( - ctx: CanvasRenderingContext2D, - x: number, - y: number, - width: number, - height: number, -): void { - const topLeft = toIsometric(x, y); - const topRight = toIsometric(x + width, y); - const bottomRight = toIsometric(x + width, y + height); - const bottomLeft = toIsometric(x, y + height); - - ctx.beginPath(); - ctx.moveTo(topLeft.x, topLeft.y); - ctx.lineTo(topRight.x, topRight.y); - ctx.lineTo(bottomRight.x, bottomRight.y); - ctx.lineTo(bottomLeft.x, bottomLeft.y); - ctx.closePath(); -} - -/** - * Draw an isometric cube - * @param ctx Canvas rendering context - * @param x Base x-coordinate (in grid coordinates) - * @param y Base y-coordinate (in grid coordinates) - * @param width Width of the cube (in grid units) - * @param height Height of the cube (in grid units) - * @param depth Depth of the cube (in pixels) - */ -export function drawIsometricCube( - ctx: CanvasRenderingContext2D, - x: number, - y: number, - width: number, - height: number, - depth: number, -): void { - const base = toIsometric(x, y); - const top = { x: base.x, y: base.y - depth }; - const right = toIsometric(x + width, y); - const back = toIsometric(x, y + height); - - // Draw top face - ctx.beginPath(); - ctx.moveTo(top.x, top.y); - ctx.lineTo(top.x + (right.x - base.x), top.y + (right.y - base.y)); - ctx.lineTo(top.x + (right.x - base.x) + (back.x - base.x), top.y + (right.y - base.y) + (back.y - base.y)); - ctx.lineTo(top.x + (back.x - base.x), top.y + (back.y - base.y)); - ctx.closePath(); - - // Draw right face - ctx.beginPath(); - ctx.moveTo(right.x, right.y); - ctx.lineTo(right.x, right.y + depth); - ctx.lineTo(right.x + (back.x - base.x), right.y + depth + (back.y - base.y)); - ctx.lineTo(right.x + (back.x - base.x), right.y + (back.y - base.y)); - ctx.closePath(); - - // Draw left face - ctx.beginPath(); - ctx.moveTo(base.x, base.y); - ctx.lineTo(base.x, base.y - depth); - ctx.lineTo(base.x + (back.x - base.x), base.y - depth + (back.y - base.y)); - ctx.lineTo(back.x, back.y); - ctx.closePath(); -} - // Function to get z-index for different object types function getZIndex(type: string): number { switch (type) {