diff options
author | elioat <{ID}+{username}@users.noreply.github.com> | 2024-12-19 17:12:44 -0500 |
---|---|---|
committer | elioat <{ID}+{username}@users.noreply.github.com> | 2024-12-19 17:12:44 -0500 |
commit | 97f98d4f223147ade7f0351ded18136d56c967cb (patch) | |
tree | 0665da7343e53f3925951d077f4c0ed82ea49595 /html/plains | |
parent | 8a8ad9ab33524769b5327be78cfaadb426cf859d (diff) | |
download | tour-97f98d4f223147ade7f0351ded18136d56c967cb.tar.gz |
*
Diffstat (limited to 'html/plains')
-rw-r--r-- | html/plains/game.js | 100 |
1 files changed, 39 insertions, 61 deletions
diff --git a/html/plains/game.js b/html/plains/game.js index 5b23e5b..7c73a19 100644 --- a/html/plains/game.js +++ b/html/plains/game.js @@ -12,30 +12,32 @@ const worldToGrid = (x, y) => ({ y: Math.floor(y / CONFIG.display.grid.size) }); +// Helper function to create a villager object +const createVillager = (cellX, cellY) => ({ + x: (cellX * CONFIG.display.grid.size) + (CONFIG.display.grid.size / 2), + y: (cellY * CONFIG.display.grid.size) + (CONFIG.display.grid.size / 2), + color: CONFIG.world.villagers.colors[Math.floor(Math.random() * CONFIG.world.villagers.colors.length)], + shape: CONFIG.world.villagers.shapes[Math.floor(Math.random() * CONFIG.world.villagers.shapes.length)], + status: 'lost', + cellX, + cellY, + bobSpeed: 0.005 + Math.random() * 0.005, + bobAmplitude: 2 + Math.random() * 2, + lostBobSpeed: 0.005 + Math.random() * 0.005, + lostBobAmplitude: 2 + Math.random() * 2 +}); + const generateVillagers = () => { const villagers = []; const occupiedCells = new Set(); const gridSize = CONFIG.display.grid.size; const villageSize = CONFIG.world.village.size; const worldSize = CONFIG.display.grid.worldSize; - - // place one villager near the village, so that hopefully the player can find them easily + + // Place one villager near the village const nearVillageX = villageSize + Math.floor(Math.random() * 2); const nearVillageY = villageSize + Math.floor(Math.random() * 2); - - villagers.push({ - x: (nearVillageX * gridSize) + (gridSize / 2), - y: (nearVillageY * gridSize) + (gridSize / 2), - color: CONFIG.world.villagers.colors[Math.floor(Math.random() * CONFIG.world.villagers.colors.length)], - shape: CONFIG.world.villagers.shapes[Math.floor(Math.random() * CONFIG.world.villagers.shapes.length)], - status: 'lost', - cellX: nearVillageX, - cellY: nearVillageY, - bobSpeed: 0.005 + Math.random() * 0.005, - bobAmplitude: 2 + Math.random() * 2, - lostBobSpeed: 0.005 + Math.random() * 0.005, - lostBobAmplitude: 2 + Math.random() * 2 - }); + villagers.push(createVillager(nearVillageX, nearVillageY)); occupiedCells.add(`${nearVillageX},${nearVillageY}`); while (villagers.length < CONFIG.world.villagers.total) { @@ -43,66 +45,42 @@ const generateVillagers = () => { const cellY = villageSize + Math.floor(Math.random() * (worldSize - villageSize)); const cellKey = `${cellX},${cellY}`; - // try not to put villagers into trees...but this doesn't always work if (occupiedCells.has(cellKey) || state.collisionMap.has(cellKey)) { continue; } - // look...sometimes copy and paste is easy and good - villagers.push({ - x: (cellX * gridSize) + (gridSize / 2), - y: (cellY * gridSize) + (gridSize / 2), - color: CONFIG.world.villagers.colors[Math.floor(Math.random() * CONFIG.world.villagers.colors.length)], - shape: CONFIG.world.villagers.shapes[Math.floor(Math.random() * CONFIG.world.villagers.shapes.length)], - status: 'lost', - cellX, - cellY, - bobSpeed: 0.005 + Math.random() * 0.005, - bobAmplitude: 2 + Math.random() * 2, - lostBobSpeed: 0.005 + Math.random() * 0.005, - lostBobAmplitude: 2 + Math.random() * 2 - }); + villagers.push(createVillager(cellX, cellY)); occupiedCells.add(cellKey); } - + return villagers; }; +// Refactored drawVillagerShape function const drawVillagerShape = (ctx, x, y, shape, size) => { ctx.beginPath(); - - switch (shape) { - case 'square': - ctx.rect(x - size/2, y - size/2, size, size); - break; - - case 'triangle': - ctx.moveTo(x, y - size/2); - ctx.lineTo(x + size/2, y + size/2); - ctx.lineTo(x - size/2, y + size/2); - break; - - case 'pentagon': + const shapes = { + square: () => ctx.rect(x - size / 2, y - size / 2, size, size), + triangle: () => { + ctx.moveTo(x, y - size / 2); + ctx.lineTo(x + size / 2, y + size / 2); + ctx.lineTo(x - size / 2, y + size / 2); + }, + pentagon: () => { for (let i = 0; i < 5; i++) { - const angle = (i * 2 * Math.PI / 5) - Math.PI/2; - const px = x + Math.cos(angle) * size/2; - const py = y + Math.sin(angle) * size/2; - if (i === 0) ctx.moveTo(px, py); - else ctx.lineTo(px, py); + const angle = (i * 2 * Math.PI / 5) - Math.PI / 2; + ctx.lineTo(x + Math.cos(angle) * size / 2, y + Math.sin(angle) * size / 2); } - break; - - case 'hexagon': + }, + hexagon: () => { for (let i = 0; i < 6; i++) { const angle = (i * 2 * Math.PI / 6); - const px = x + Math.cos(angle) * size/2; - const py = y + Math.sin(angle) * size/2; - if (i === 0) ctx.moveTo(px, py); - else ctx.lineTo(px, py); + ctx.lineTo(x + Math.cos(angle) * size / 2, y + Math.sin(angle) * size / 2); } - break; - } - + } + }; + + if (shapes[shape]) shapes[shape](); ctx.closePath(); }; @@ -152,7 +130,7 @@ const CONFIG = { lookRadius: 0.4 }, equipment: { - swordUnlockCount: 5, // The number of villagers you need to rescue to unlock the sword + swordUnlockCount: 8, // The number of villagers you need to rescue to unlock the sword unlockAnimation: { duration: 1500, glowColor: 'rgba(255, 215, 0, 0.6)', |