Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
gtanczyk committed Aug 23, 2024
1 parent b7b7ceb commit c092568
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 181 deletions.
2 changes: 1 addition & 1 deletion js13k2024/game/src/game-states/gameplay/bonus-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const handleBlasterShot = (gameState: GameState, direction: Direction, le
});
};

export const isMonsterOnBlasterPath = (
const isMonsterOnBlasterPath = (
monsterPos: Position,
start: Position,
end: Position,
Expand Down
8 changes: 1 addition & 7 deletions js13k2024/game/src/game-states/gameplay/game-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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();
};
56 changes: 2 additions & 54 deletions js13k2024/game/src/game-states/gameplay/gameplay-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -75,7 +75,7 @@ export type Explosion = {
duration: number;
};

export type TimeBomb = {
type TimeBomb = {
position: Position;
timer: number;
shakeIntensity: number;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { spawnDynamicBonus } from '../bonus-logic';
import { GameState, LevelConfig, BonusType, Position } from '../gameplay-types';
import {
createPosition,
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
};
2 changes: 1 addition & 1 deletion js13k2024/game/src/game-states/gameplay/monster-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface AnimationParams {
heightAnimationFactor: number;
}

export interface Position {
interface Position {
x: number;
y: number;
}
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions js13k2024/game/src/game-states/gameplay/render/entity-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,7 +21,7 @@ export const drawEntityBody = (
ctx.fill();
};

export const drawEntityHead = (
const drawEntityHead = (
ctx: CanvasRenderingContext2D,
isoX: number,
isoY: number,
Expand All @@ -36,7 +36,7 @@ export const drawEntityHead = (
ctx.fill();
};

export const drawEntityEyes = (
const drawEntityEyes = (
ctx: CanvasRenderingContext2D,
isoX: number,
isoY: number,
Expand All @@ -60,7 +60,7 @@ export const drawEntityEyes = (
ctx.fill();
};

export const drawEntityMouth = (
const drawEntityMouth = (
ctx: CanvasRenderingContext2D,
isoX: number,
isoY: number,
Expand All @@ -75,7 +75,7 @@ export const drawEntityMouth = (
ctx.stroke();
};

export const drawEntityTentacles = (
const drawEntityTentacles = (
ctx: CanvasRenderingContext2D,
isoX: number,
isoY: number,
Expand Down
87 changes: 0 additions & 87 deletions js13k2024/game/src/game-states/gameplay/render/isometric-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c092568

Please sign in to comment.