Skip to content

Commit

Permalink
small refactor, move render stuff to render/
Browse files Browse the repository at this point in the history
  • Loading branch information
gtanczyk committed Aug 22, 2024
1 parent d0735c9 commit 570e8f7
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 37 deletions.
6 changes: 5 additions & 1 deletion js13k2024/game/src/game-states/gameplay/game-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
isInExplosionRange,
} from './monster-logic';
import { generateLevel } from './level-generator';
import { BLASTER_SHOT_DURATION, MOVE_ANIMATION_DURATION, OBSTACLE_DESTRUCTION_DURATION } from './animation-utils';
import {
BLASTER_SHOT_DURATION,
MOVE_ANIMATION_DURATION,
OBSTACLE_DESTRUCTION_DURATION,
} from './render/animation-utils';
import { soundEngine } from '../../sound/sound-engine';
import {
getDirectionFromKey,
Expand Down
24 changes: 12 additions & 12 deletions js13k2024/game/src/game-states/gameplay/game-render.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { BonusType, GameState, LevelConfig } from './gameplay-types';
import { drawObstacles, drawGoal } from './grid-objects-render';
import { drawGrid } from './grid-render';
import { drawPlayer } from './player-render';
import { drawMonsters } from './monster-render';
import { drawBonuses, drawLandMines, drawTimeBombs } from './bonus-render';
import { drawExplosions } from './explosion-render';
import { calculateDrawingOrder } from './isometric-utils';
import { calculateShakeOffset, interpolatePosition } from './animation-utils';
import { drawTooltip } from './tooltip-render';
import { drawElectricalDischarges } from './discharges-render';
import { drawPlatform } from './grid-render';
import { drawBlasterShot, drawSlideTrail, drawTsunamiEffect } from './bonus-effect-render';
import { drawObstacles, drawGoal } from './render/grid-objects-render';
import { drawGrid } from './render/grid-render';
import { drawPlayer } from './render/player-render';
import { drawMonsters } from './render/monster-render';
import { drawBonuses, drawLandMines, drawTimeBombs } from './render/bonus-render';
import { drawExplosions } from './render/explosion-render';
import { calculateDrawingOrder } from './render/isometric-utils';
import { calculateShakeOffset, interpolatePosition } from './render/animation-utils';
import { drawTooltip } from './render/tooltip-render';
import { drawElectricalDischarges } from './render/discharges-render';
import { drawPlatform } from './render/grid-render';
import { drawBlasterShot, drawSlideTrail, drawTsunamiEffect } from './render/bonus-effect-render';

export const PLATFORM_HEIGHT = 20;

Expand Down
2 changes: 1 addition & 1 deletion js13k2024/game/src/game-states/gameplay/gameplay.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FunctionComponent, useState, useRef, useEffect } from 'react';
import { drawGameState } from './game-render';
import { drawMoveArrows } from './move-arrows-render';
import { drawMoveArrows } from './render/move-arrows-render';
import { doGameUpdate, handleKeyPress, isGameEnding } from './game-logic';
import { getMoveFromClick, getValidMoves } from './move-utils';
import { HUD } from './hud';
Expand Down
2 changes: 1 addition & 1 deletion js13k2024/game/src/game-states/gameplay/move-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BonusType, Direction, GameState, LevelConfig, Position } from './gameplay-types';
import { getArrowShape } from './move-arrows-render';
import { getArrowShape } from './render/move-arrows-render';

export const isPositionEqual = (pos1: Position, pos2: Position): boolean => pos1.x === pos2.x && pos1.y === pos2.y;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElectricalDischarge } from './gameplay-types';
import { ElectricalDischarge } from '../gameplay-types';

export const MOVE_ANIMATION_DURATION = 250; // 1/4 second
export const TELEPORT_ANIMATION_DURATION = 500; // 1/2 second
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { interpolatePosition } from './animation-utils';
import { BlasterShot, Direction, GameState, Position } from './gameplay-types';
import { BlasterShot, Direction, GameState, Position } from '../gameplay-types';
import { toIsometric } from './isometric-utils';

export const drawTsunamiEffect = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Position, Bonus, BonusType } from './gameplay-types';
import { Position, Bonus, BonusType } from '../gameplay-types';
import { toIsometric, TILE_WIDTH, TILE_HEIGHT } from './isometric-utils';
import { drawShadow } from './grid-render';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Explosion } from './gameplay-types';
import { Explosion } from '../gameplay-types';
import { toIsometric, TILE_WIDTH, TILE_HEIGHT } from './isometric-utils';

export const drawExplosions = (ctx: CanvasRenderingContext2D, explosions: Explosion[]) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Position, Obstacle } from './gameplay-types';
import { Position, Obstacle } from '../gameplay-types';
import { toIsometric, TILE_WIDTH, TILE_HEIGHT } from './isometric-utils';
import { calculateObstacleHeight } from './animation-utils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PLATFORM_HEIGHT } from './game-render';
import { PLATFORM_HEIGHT } from '../game-render';
import { TILE_HEIGHT, TILE_WIDTH, toIsometric } from './isometric-utils';
import { GameState } from './gameplay-types';
import { GameState } from '../gameplay-types';

export const drawPlatform = (ctx: CanvasRenderingContext2D, gridSize: number) => {
const topLeft = toIsometric(0, 0);
Expand Down Expand Up @@ -94,18 +94,18 @@ const drawTsunamiTile = (ctx: CanvasRenderingContext2D, x: number, y: number, ts
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(isoX - TILE_WIDTH / 2, isoY + TILE_HEIGHT / 2 - waterHeight + Math.sin(Date.now() / 200 + x * 0.5) * 3);
ctx.lineTo(isoX + TILE_WIDTH / 2, isoY + TILE_HEIGHT / 2 - waterHeight + Math.sin(Date.now() / 200 + (x + 1) * 0.5) * 3);
ctx.lineTo(
isoX + TILE_WIDTH / 2,
isoY + TILE_HEIGHT / 2 - waterHeight + Math.sin(Date.now() / 200 + (x + 1) * 0.5) * 3,
);
ctx.stroke();
};

const drawSlideTile = (ctx: CanvasRenderingContext2D, x: number, y: number) => {
const { x: isoX, y: isoY } = toIsometric(x, y);

// Draw a subtle ice-like effect
const gradient = ctx.createLinearGradient(
isoX - TILE_WIDTH / 2, isoY,
isoX + TILE_WIDTH / 2, isoY + TILE_HEIGHT
);
const gradient = ctx.createLinearGradient(isoX - TILE_WIDTH / 2, isoY, isoX + TILE_WIDTH / 2, isoY + TILE_HEIGHT);
gradient.addColorStop(0, 'rgba(200, 200, 255, 0.2)');
gradient.addColorStop(0.5, 'rgba(220, 220, 255, 0.3)');
gradient.addColorStop(1, 'rgba(200, 200, 255, 0.2)');
Expand Down Expand Up @@ -140,4 +140,4 @@ export const drawShadow = (ctx: CanvasRenderingContext2D, x: number, y: number,
ctx.beginPath();
ctx.ellipse(x + width / 2, y + height / 2 + SHADOW_OFFSET, width / 2, height / 4, 0, 0, Math.PI * 2);
ctx.fill();
};
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Position } from './gameplay-types';
import { Position } from '../gameplay-types';

// Constants for isometric tile dimensions
export const TILE_WIDTH = 60;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Monster } from './gameplay-types';
import { Monster } from '../gameplay-types';
import { toIsometric } from './isometric-utils';
import { interpolatePosition } from './animation-utils';
import { EntityRenderParams } from './entity-render-utils';
import { renderEntity } from './entity-render';

export const drawMonsters = (ctx: CanvasRenderingContext2D, monsters: Monster[], cellSize: number, isPlayerMonster: boolean) => {
export const drawMonsters = (
ctx: CanvasRenderingContext2D,
monsters: Monster[],
cellSize: number,
isPlayerMonster: boolean,
) => {
monsters.forEach((monster) => {
const interpolatedPosition = interpolatePosition(monster.position, monster.previousPosition, monster.moveTimestamp);

Expand Down Expand Up @@ -67,4 +72,4 @@ const drawPlayerFeatures = (ctx: CanvasRenderingContext2D, x: number, y: number,
}

ctx.restore();
};
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Position, Direction } from './gameplay-types';
import { Position, Direction } from '../gameplay-types';
import { toIsometric } from './isometric-utils';
import { getNewPosition, getOrthogonalDirection } from './move-utils';
import { getNewPosition, getOrthogonalDirection } from '../move-utils';

// Updated function to draw move arrows

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Obstacle, Player } from './gameplay-types';
import { Obstacle, Player } from '../gameplay-types';
import { toIsometric } from './isometric-utils';
import {
interpolatePosition,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Position, BonusType, getBonusDescription } from './gameplay-types';
import { Position, BonusType, getBonusDescription } from '../gameplay-types';
import { toIsometric } from './isometric-utils';

export const drawTooltip = (
Expand Down
2 changes: 1 addition & 1 deletion js13k2024/game/src/game-states/intro/game-preview.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FunctionComponent, useEffect, useRef } from 'react';
import { drawGameState } from '../gameplay/game-render';
import { drawGrid } from '../gameplay/grid-render';
import { drawGrid } from '../gameplay/render/grid-render';
import { GameState, BonusType } from '../gameplay/gameplay-types';

const PREVIEW_WIDTH = 300;
Expand Down

0 comments on commit 570e8f7

Please sign in to comment.