aboutsummaryrefslogtreecommitdiffstats
path: root/html
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-09-06 15:00:28 -0400
committerelioat <elioat@tilde.institute>2025-09-06 15:00:28 -0400
commit442ff24d348cb8fe492c6b0e17baf519f2dcda92 (patch)
tree940a218e06edc33a0de2c472048fdab8e77432b2 /html
parentde11f4366a652246df24024844c6403fbbb81421 (diff)
downloadtour-442ff24d348cb8fe492c6b0e17baf519f2dcda92.tar.gz
*
Diffstat (limited to 'html')
-rw-r--r--html/strata/game.js533
1 files changed, 529 insertions, 4 deletions
diff --git a/html/strata/game.js b/html/strata/game.js
index b0013f7..422d151 100644
--- a/html/strata/game.js
+++ b/html/strata/game.js
@@ -46,6 +46,13 @@
* - Curses and buffs persist across levels
* - Card Shuffle curse has limited duration (3-8 turns) regardless of level
*
+ * Shadow Breach Monster:
+ * - Spawns in final 3-5 moves of levels 3+ (escalates with level)
+ * - Hunts player using intelligent pathfinding (basic → smart → predictive)
+ * - Destroys cards it passes through, reducing strategic options
+ * - Instant game over if it catches the player
+ * - Creates visual atmosphere with bleeding shadows and tendrils
+ *
*
* DEBUG CONSOLE INTERFACE
* ======================
@@ -776,14 +783,28 @@ const createLevel = (grid, winCondition, levelNumber) => ({
});
/**
+ * Create monster state for the Shadow Breach
+ */
+const createMonster = (active = false, position = null, turnsUntilMove = 0, lastMoveTime = 0, tendrils = []) => ({
+ active,
+ position, // { row, col }
+ turnsUntilMove,
+ lastMoveTime,
+ tendrils, // Array of tendril objects with angle, length, opacity
+ pulsePhase: 0, // For breathing animation
+ trailCells: [] // Cells the monster has passed through for shadow trail effect
+});
+
+/**
* Create the main game state
*/
-const createGame = (player, currentLevel, gameOver = false, animationState = null, particles = []) => ({
+const createGame = (player, currentLevel, gameOver = false, animationState = null, particles = [], monster = null) => ({
player,
currentLevel,
gameOver,
animationState,
- particles
+ particles,
+ monster: monster || createMonster()
});
// ============================================================================
@@ -1548,6 +1569,318 @@ const calculateRowProbabilities = (rowCells, player) => {
};
// ============================================================================
+// MONSTER SYSTEM
+// ============================================================================
+
+/**
+ * Get monster configuration based on level
+ */
+const getMonsterConfig = (levelNumber) => {
+ if (levelNumber <= 2) {
+ // No monster in tutorial levels
+ return { enabled: false };
+ } else if (levelNumber <= 5) {
+ // Early levels: monster appears late, moves slowly
+ return {
+ enabled: true,
+ spawnThreshold: 3, // Appears when 3 moves left
+ moveFrequency: 2, // Moves every 2 player turns
+ intelligence: 'basic', // Simple pathfinding
+ startPosition: { row: 3, col: 3 } // Bottom-right corner
+ };
+ } else if (levelNumber <= 8) {
+ // Mid levels: monster appears earlier, moves faster
+ return {
+ enabled: true,
+ spawnThreshold: 4, // Appears when 4 moves left
+ moveFrequency: 1, // Moves every player turn
+ intelligence: 'smart', // Improved pathfinding
+ startPosition: { row: 3, col: 3 }
+ };
+ } else {
+ // High levels: monster appears very early, moves aggressively
+ return {
+ enabled: true,
+ spawnThreshold: 5, // Appears when 5 moves left
+ moveFrequency: 1, // Moves every turn
+ intelligence: 'predictive', // Anticipates player movement
+ startPosition: { row: 3, col: 3 }
+ };
+ }
+};
+
+/**
+ * Generate random tendrils for the shadow monster
+ */
+const generateMonsterTendrils = () => {
+ const tendrils = [];
+ const tendrilCount = randomInt(6, 10);
+
+ for (let i = 0; i < tendrilCount; i++) {
+ tendrils.push({
+ angle: (i / tendrilCount) * Math.PI * 2 + (Math.random() - 0.5) * 0.8,
+ length: randomInt(12, 28),
+ opacity: 0.3 + Math.random() * 0.4,
+ thickness: 1 + Math.random() * 2
+ });
+ }
+
+ return tendrils;
+};
+
+/**
+ * Calculate monster's next move based on intelligence level
+ */
+const calculateMonsterMove = (monster, player, intelligence) => {
+ if (!monster.position || !player.currentPosition) {
+ return monster.position;
+ }
+
+ const currentPos = monster.position;
+ const playerPos = player.currentPosition;
+
+ // Get all valid adjacent positions (monster can move to any adjacent cell, including diagonals)
+ const possibleMoves = [];
+ for (let rowDelta = -1; rowDelta <= 1; rowDelta++) {
+ for (let colDelta = -1; colDelta <= 1; colDelta++) {
+ if (rowDelta === 0 && colDelta === 0) continue; // Skip current position
+
+ const newRow = currentPos.row + rowDelta;
+ const newCol = currentPos.col + colDelta;
+
+ // Keep monster within grid bounds
+ if (newRow >= 0 && newRow < 4 && newCol >= 0 && newCol < 4) {
+ possibleMoves.push({ row: newRow, col: newCol });
+ }
+ }
+ }
+
+ if (possibleMoves.length === 0) {
+ return currentPos; // No valid moves
+ }
+
+ switch (intelligence) {
+ case 'basic':
+ // Basic: Move toward player using Manhattan distance
+ let bestMove = possibleMoves[0];
+ let bestDistance = Math.abs(bestMove.row - playerPos.row) + Math.abs(bestMove.col - playerPos.col);
+
+ possibleMoves.forEach(move => {
+ const distance = Math.abs(move.row - playerPos.row) + Math.abs(move.col - playerPos.col);
+ if (distance < bestDistance) {
+ bestDistance = distance;
+ bestMove = move;
+ }
+ });
+ return bestMove;
+
+ case 'smart':
+ // Smart: Use Euclidean distance and avoid getting stuck
+ let smartMoves = possibleMoves.map(move => {
+ const distance = Math.sqrt(
+ Math.pow(move.row - playerPos.row, 2) +
+ Math.pow(move.col - playerPos.col, 2)
+ );
+ return { move, distance };
+ });
+
+ // Sort by distance and pick best
+ smartMoves.sort((a, b) => a.distance - b.distance);
+ return smartMoves[0].move;
+
+ case 'predictive':
+ // Predictive: Try to anticipate player movement
+ // If player has a last position, predict their likely next move
+ let targetPos = { ...playerPos };
+
+ if (player.lastPosition) {
+ const playerDirection = {
+ row: playerPos.row - player.lastPosition.row,
+ col: playerPos.col - player.lastPosition.col
+ };
+
+ // Predict player will continue in same direction
+ targetPos = {
+ row: Math.max(0, Math.min(3, playerPos.row + playerDirection.row)),
+ col: Math.max(0, Math.min(3, playerPos.col + playerDirection.col))
+ };
+ }
+
+ // Move toward predicted position
+ let predictiveMoves = possibleMoves.map(move => {
+ const distance = Math.sqrt(
+ Math.pow(move.row - targetPos.row, 2) +
+ Math.pow(move.col - targetPos.col, 2)
+ );
+ return { move, distance };
+ });
+
+ predictiveMoves.sort((a, b) => a.distance - b.distance);
+ return predictiveMoves[0].move;
+
+ default:
+ return randomChoice(possibleMoves);
+ }
+};
+
+/**
+ * Check if monster has caught the player (same cell)
+ */
+const checkMonsterCollision = (game) => {
+ if (!game.monster.active || !game.monster.position || !game.player.currentPosition) {
+ return game;
+ }
+
+ const monsterPos = game.monster.position;
+ const playerPos = game.player.currentPosition;
+
+ // Check if monster and player are in the same cell
+ if (monsterPos.row === playerPos.row && monsterPos.col === playerPos.col) {
+ const deathMessages = [
+ '💀 The Shadow Breach consumed your essence... GAME OVER!',
+ '💀 Enveloped by eternal darkness... GAME OVER!',
+ '💀 The predator claims its prey... GAME OVER!',
+ '💀 Your light fades into the shadow realm... GAME OVER!',
+ '💀 The breach in reality devours you... GAME OVER!'
+ ];
+ addLogEntry(randomChoice(deathMessages), 'monster-death');
+
+ return {
+ ...game,
+ gameOver: true
+ };
+ }
+
+ return game;
+};
+
+/**
+ * Update monster state and handle movement
+ */
+const updateMonster = (game) => {
+ if (!game.monster.active) {
+ return game;
+ }
+
+ const config = getMonsterConfig(game.currentLevel.levelNumber);
+ let newMonster = { ...game.monster };
+
+ // Decrement turn counter
+ newMonster.turnsUntilMove--;
+
+ // Check if monster should move
+ if (newMonster.turnsUntilMove <= 0) {
+ // Reset move counter
+ newMonster.turnsUntilMove = config.moveFrequency;
+
+ // Add current position to trail
+ if (newMonster.position) {
+ newMonster.trailCells = [...newMonster.trailCells, { ...newMonster.position }];
+
+ // Limit trail length to prevent memory issues
+ if (newMonster.trailCells.length > 8) {
+ newMonster.trailCells.shift();
+ }
+ }
+
+ // Calculate and execute move
+ const newPosition = calculateMonsterMove(newMonster, game.player, config.intelligence);
+ newMonster.position = newPosition;
+
+ // Destroy any card at the monster's new position
+ if (newPosition) {
+ const cellIndex = game.currentLevel.grid.findIndex(
+ cell => cell.row === newPosition.row && cell.col === newPosition.col
+ );
+
+ if (cellIndex !== -1 && game.currentLevel.grid[cellIndex].card) {
+ // Remove the card
+ const destroyedCard = game.currentLevel.grid[cellIndex].card;
+ game.currentLevel.grid[cellIndex] = {
+ ...game.currentLevel.grid[cellIndex],
+ card: null
+ };
+
+ const destroyMessages = [
+ `The shadow consumed a ${destroyedCard.type} card!`,
+ `Darkness devoured the card's power!`,
+ `The monster obliterated a card!`,
+ `Shadow tendrils shredded the card!`
+ ];
+ addLogEntry(randomChoice(destroyMessages), 'monster-destroy');
+ }
+ }
+
+ // Regenerate tendrils for dynamic movement
+ newMonster.tendrils = generateMonsterTendrils();
+
+ // Log monster movement
+ const moveMessages = [
+ 'The shadow shifts closer...',
+ 'Darkness creeps forward!',
+ 'The predator stalks nearer...',
+ 'Cold tendrils reach toward you!',
+ 'The breach in reality expands!'
+ ];
+ addLogEntry(randomChoice(moveMessages), 'monster-move');
+ }
+
+ return {
+ ...game,
+ monster: newMonster
+ };
+};
+
+/**
+ * Check if monster should spawn and spawn it if needed
+ */
+const checkMonsterSpawn = (game) => {
+ const config = getMonsterConfig(game.currentLevel.levelNumber);
+
+ // Don't spawn if monster system is disabled for this level
+ if (!config.enabled) {
+ return game;
+ }
+
+ // Don't spawn if monster is already active
+ if (game.monster.active) {
+ return game;
+ }
+
+ // Check if we should spawn the monster
+ const movesRemaining = game.currentLevel.winCondition - game.currentLevel.movesMade;
+
+ if (movesRemaining <= config.spawnThreshold) {
+ const spawnedMonster = {
+ ...game.monster,
+ active: true,
+ position: { ...config.startPosition },
+ turnsUntilMove: config.moveFrequency,
+ tendrils: generateMonsterTendrils(),
+ pulsePhase: 0,
+ trailCells: []
+ };
+
+ // Add spawn message
+ const spawnMessages = [
+ '⚠️ The Shadow Breach emerges from the depths!',
+ '⚠️ Darkness stirs... something hunts you!',
+ '⚠️ A malevolent presence materializes!',
+ '⚠️ The shadows coalesce into something terrible!',
+ '⚠️ An otherworldly predator has awakened!'
+ ];
+ addLogEntry(randomChoice(spawnMessages), 'monster-spawn');
+
+ return {
+ ...game,
+ monster: spawnedMonster
+ };
+ }
+
+ return game;
+};
+
+// ============================================================================
// GAME STATE UPDATES
// ============================================================================
@@ -2068,6 +2401,24 @@ const processCellLanding = (game, cell) => {
// Update moves made
newLevel = { ...newLevel, movesMade: newLevel.movesMade + 1 };
+ // Update the game state before checking monster spawn
+ let updatedGame = { ...game, player: newPlayer, currentLevel: newLevel, gameOver, animationState };
+
+ // Check if monster should spawn (after move count is updated)
+ updatedGame = checkMonsterSpawn(updatedGame);
+
+ // Update monster movement (after spawn check)
+ updatedGame = updateMonster(updatedGame);
+
+ // Check for monster collision (after monster movement)
+ updatedGame = checkMonsterCollision(updatedGame);
+
+ // Extract updated components
+ newPlayer = updatedGame.player;
+ newLevel = updatedGame.currentLevel;
+ const newMonster = updatedGame.monster;
+ gameOver = updatedGame.gameOver; // Update game over state
+
// Check win condition - but don't advance immediately if we have an active movement animation
// This allows the player to see their final move complete before the level transition
let shouldAdvanceToNextLevel = false;
@@ -2087,6 +2438,7 @@ const processCellLanding = (game, cell) => {
currentLevel: newLevel,
gameOver,
animationState,
+ monster: newMonster,
shouldAdvanceToNextLevel
};
};
@@ -2150,16 +2502,148 @@ const advanceToNextLevel = (game) => {
const newGrid = generateGrid();
const newLevel = createLevel(newGrid, newWinCondition, newLevelNumber);
+ // Reset monster state for new level
+ const resetMonster = createMonster();
+
addLogEntry(`Level ${newLevelNumber} - Need ${newWinCondition} moves to progress`, 'level');
// Store new level data in animation state
animationState.newLevel = newLevel;
animationState.newPlayer = newPlayer;
+ animationState.newMonster = resetMonster;
return { ...game, animationState };
};
// ============================================================================
+// MONSTER RENDERING
+// ============================================================================
+
+/**
+ * Draw shadow trail effect for cells the monster has passed through
+ */
+const drawMonsterTrail = (ctx, trailCells, cellSize, padding, startX, canvas) => {
+ trailCells.forEach((trailCell, index) => {
+ const fadeOpacity = (trailCells.length - index) / trailCells.length * 0.2;
+
+ const bandHeight = canvas.height / 4;
+ const x = startX + trailCell.col * (cellSize + padding);
+ const y = trailCell.row * bandHeight + (bandHeight - cellSize) / 2;
+
+ ctx.save();
+ ctx.globalAlpha = fadeOpacity;
+ ctx.fillStyle = '#000';
+ ctx.fillRect(x - 2, y - 2, cellSize + 4, cellSize + 4);
+ ctx.restore();
+ });
+};
+
+/**
+ * Draw shadow tendril extending from monster core
+ */
+const drawShadowTendril = (ctx, centerX, centerY, tendril, pulsePhase) => {
+ const pulseFactor = 1 + Math.sin(pulsePhase + tendril.angle) * 0.2;
+ const length = tendril.length * pulseFactor;
+
+ const endX = centerX + Math.cos(tendril.angle) * length;
+ const endY = centerY + Math.sin(tendril.angle) * length;
+
+ // Create gradient for tendril
+ const gradient = ctx.createLinearGradient(centerX, centerY, endX, endY);
+ gradient.addColorStop(0, `rgba(0, 0, 0, ${tendril.opacity})`);
+ gradient.addColorStop(0.7, `rgba(0, 0, 0, ${tendril.opacity * 0.5})`);
+ gradient.addColorStop(1, 'rgba(0, 0, 0, 0)');
+
+ ctx.save();
+ ctx.strokeStyle = gradient;
+ ctx.lineWidth = tendril.thickness * pulseFactor;
+ ctx.lineCap = 'round';
+
+ // Draw tendril with slight curve
+ ctx.beginPath();
+ ctx.moveTo(centerX, centerY);
+
+ const midX = centerX + Math.cos(tendril.angle) * length * 0.6;
+ const midY = centerY + Math.sin(tendril.angle) * length * 0.6;
+ const curveOffset = Math.sin(pulsePhase * 2 + tendril.angle) * 3;
+
+ ctx.quadraticCurveTo(
+ midX + curveOffset,
+ midY + curveOffset,
+ endX,
+ endY
+ );
+ ctx.stroke();
+ ctx.restore();
+};
+
+/**
+ * Draw the main Shadow Breach monster
+ */
+const drawShadowMonster = (ctx, monster, cellSize, padding, startX, canvas, currentTime) => {
+ if (!monster.active || !monster.position) return;
+
+ // Update pulse phase for animation
+ const pulsePhase = (currentTime * 0.003) % (Math.PI * 2);
+
+ const bandHeight = canvas.height / 4;
+ const centerX = startX + monster.position.col * (cellSize + padding) + cellSize / 2;
+ const centerY = monster.position.row * bandHeight + (bandHeight - cellSize) / 2 + cellSize / 2;
+
+ ctx.save();
+
+ // Draw shadow bleed effect on adjacent cells
+ const bleedRadius = cellSize * 1.2;
+ const bleedGradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, bleedRadius);
+ bleedGradient.addColorStop(0, 'rgba(0, 0, 0, 0.3)');
+ bleedGradient.addColorStop(0.6, 'rgba(0, 0, 0, 0.15)');
+ bleedGradient.addColorStop(1, 'rgba(0, 0, 0, 0)');
+
+ ctx.fillStyle = bleedGradient;
+ ctx.beginPath();
+ ctx.arc(centerX, centerY, bleedRadius, 0, Math.PI * 2);
+ ctx.fill();
+
+ // Draw tendrils (extending beyond grid boundaries)
+ monster.tendrils.forEach(tendril => {
+ drawShadowTendril(ctx, centerX, centerY, tendril, pulsePhase);
+ });
+
+ // Draw main shadow body with pulsing effect
+ const pulseFactor = 1 + Math.sin(pulsePhase) * 0.15;
+ const coreSize = cellSize * 0.8 * pulseFactor;
+
+ const coreGradient = ctx.createRadialGradient(
+ centerX, centerY, 0,
+ centerX, centerY, coreSize / 2
+ );
+ coreGradient.addColorStop(0, 'rgba(0, 0, 0, 0.9)');
+ coreGradient.addColorStop(0.7, 'rgba(0, 0, 0, 0.7)');
+ coreGradient.addColorStop(1, 'rgba(0, 0, 0, 0.3)');
+
+ ctx.fillStyle = coreGradient;
+ ctx.beginPath();
+ ctx.arc(centerX, centerY, coreSize / 2, 0, Math.PI * 2);
+ ctx.fill();
+
+ // Add "eyes" or focal points in the shadow
+ const eyeGlow = Math.sin(pulsePhase * 2) * 0.3 + 0.5;
+ ctx.fillStyle = `rgba(120, 0, 0, ${eyeGlow})`;
+
+ // Left "eye"
+ ctx.beginPath();
+ ctx.arc(centerX - coreSize * 0.15, centerY - coreSize * 0.1, 2, 0, Math.PI * 2);
+ ctx.fill();
+
+ // Right "eye"
+ ctx.beginPath();
+ ctx.arc(centerX + coreSize * 0.15, centerY - coreSize * 0.1, 2, 0, Math.PI * 2);
+ ctx.fill();
+
+ ctx.restore();
+};
+
+// ============================================================================
// RENDERING SYSTEM
// ============================================================================
@@ -2701,6 +3185,11 @@ const render = (game, canvas, ctx) => {
ctx.fillText(`LEVEL ${levelNumber}`, canvas.width / 2, canvas.height / 2);
}
+ // Draw monster trail first (behind everything)
+ if (game.monster && game.monster.active) {
+ drawMonsterTrail(ctx, game.monster.trailCells, cellSize, padding, startX, canvas);
+ }
+
// Draw particles
game.particles.forEach(particle => {
const alpha = (particle.life / CARD_PARTICLE_LIFETIME) * particle.initialOpacity;
@@ -2713,6 +3202,39 @@ const render = (game, canvas, ctx) => {
ctx.fillRect(particle.x, particle.y, particle.size, particle.size);
});
+ // Draw monster (on top of everything except UI)
+ if (game.monster && game.monster.active) {
+ // Add atmospheric screen darkening when monster is active
+ const darkenOverlay = ctx.createRadialGradient(
+ canvas.width / 2, canvas.height / 2, 0,
+ canvas.width / 2, canvas.height / 2, Math.max(canvas.width, canvas.height)
+ );
+ darkenOverlay.addColorStop(0, 'rgba(0, 0, 0, 0.05)');
+ darkenOverlay.addColorStop(1, 'rgba(0, 0, 0, 0.15)');
+
+ ctx.save();
+ ctx.fillStyle = darkenOverlay;
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
+ ctx.restore();
+
+ drawShadowMonster(ctx, game.monster, cellSize, padding, startX, canvas, currentTime);
+
+ // Add warning text when monster is close
+ const monsterPos = game.monster.position;
+ const playerPos = game.player.currentPosition;
+ if (monsterPos && playerPos) {
+ const distance = Math.abs(monsterPos.row - playerPos.row) + Math.abs(monsterPos.col - playerPos.col);
+ if (distance <= 2) {
+ ctx.save();
+ ctx.fillStyle = 'rgba(255, 0, 0, 0.8)';
+ ctx.font = 'bold 16px Shantell Sans';
+ ctx.textAlign = 'center';
+ ctx.fillText('⚠️ DANGER ⚠️', canvas.width / 2, 30);
+ ctx.restore();
+ }
+ }
+ }
+
// Draw game over message
if (game.gameOver) {
ctx.fillStyle = 'rgba(0, 0, 0, 0.8)';
@@ -2831,7 +3353,8 @@ const restartGame = () => {
const initialPlayer = createPlayer(4, [], [], null, { row: 0, col: 0 }, 0, false, 0);
const initialGrid = generateGrid();
const initialLevel = createLevel(initialGrid, randomInt(4, 12), 1);
- let game = createGame(initialPlayer, initialLevel);
+ const initialMonster = createMonster();
+ let game = createGame(initialPlayer, initialLevel, false, null, [], initialMonster);
// Calculate initial probabilities
for (let row = 0; row < 4; row++) {
@@ -3241,6 +3764,7 @@ const init = async () => {
if (game.animationState.type === AnimationState.LEVEL_TRANSITION) {
const newLevel = game.animationState.newLevel;
const newPlayer = game.animationState.newPlayer;
+ const newMonster = game.animationState.newMonster;
// Calculate initial probabilities for the new level based on player's current buffs/curses
for (let row = 0; row < 4; row++) {
@@ -3264,7 +3788,8 @@ const init = async () => {
currentPosition: { row: 0, col: 0 },
lastPosition: null
},
- currentLevel: newLevel
+ currentLevel: newLevel,
+ monster: newMonster
};
// Update UI to reflect the new level and player state