Files
vela-snake-m/src/common/snake.js
T

129 lines
5.0 KiB
JavaScript

// Shared geometry: 32px tiles wholly inside the 368px circular arena.
const ARENA = { cell: 32, size: 11, offset: 57, radius: 184, cells: [], rows: [], columns: [] }
for (let y = 0; y < ARENA.size; y++) {
ARENA.rows[y] = []
ARENA.columns[y] = []
}
for (let y = 0; y < ARENA.size; y++) for (let x = 0; x < ARENA.size; x++) {
const dx = Math.abs((x + 0.5) * ARENA.cell - 176) + ARENA.cell / 2
const dy = Math.abs((y + 0.5) * ARENA.cell - 176) + ARENA.cell / 2
if (dx * dx + dy * dy <= ARENA.radius * ARENA.radius) {
ARENA.cells.push({ x, y })
ARENA.rows[y].push(x)
ARENA.columns[x].push(y)
}
}
function initialSnake() {
return [
{ x: 6, y: 7 },
{ x: 5, y: 7 },
{ x: 4, y: 7 }
]
}
// Advance the snake one cell in `dir` ({x,y} unit vector). Returns { snake, ate, alive }.
function step(snake, dir, food, N, mode) {
const head = snake[0]
let nx = head.x + dir.x
let ny = head.y + dir.y
if (typeof N === 'object') {
const row = N.rows[ny]
if (!row || row.indexOf(nx) === -1) {
if (mode !== 'wrap') return { snake, ate: false, alive: false }
const span = dir.x ? N.rows[head.y] : N.columns[head.x]
const opposite = dir.x + dir.y > 0 ? span[0] : span[span.length - 1]
if (dir.x) nx = opposite
else ny = opposite
}
} else if (mode === 'wrap') {
nx = (nx + N) % N
ny = (ny + N) % N
} else if (nx < 0 || ny < 0 || nx >= N || ny >= N) {
return { snake, ate: false, alive: false }
}
const ate = !!food && (nx === food.x && ny === food.y)
// tail cell vacates unless we grew, so exclude it from self-collision check
const body = ate ? snake : snake.slice(0, snake.length - 1)
for (let i = 0; i < body.length; i++) {
if (body[i].x === nx && body[i].y === ny) {
return { snake, ate: false, alive: false }
}
}
const next = [{ x: nx, y: ny }].concat(snake)
if (!ate) next.pop()
return { snake: next, ate, alive: true }
}
// Reject a 180° reversal; otherwise accept the new direction.
function turn(current, desired) {
if (current.x + desired.x === 0 && current.y + desired.y === 0) return current
return desired
}
// Classify every segment as a texture piece (head/body/corner/tail + compass side),
// so the page can render each cell with the right PNG. Returns one name per segment.
// Sides are screen compass (y grows down): e/w/n/s = right/left/up/down.
function renderPieces(snakeCells, dir, N) {
const D = { '1,0': 'e', '-1,0': 'w', '0,-1': 'n', '0,1': 's' }
const opp = { e: 'w', w: 'e', n: 's', s: 'n' }
const delta = d => N && d === N - 1 ? -1 : N && d === 1 - N ? 1 : d
// Circular seams join the endpoints of this row/column, not the bounding grid.
const side = (s, neighbor) => {
let dx = neighbor.x - s.x, dy = neighbor.y - s.y
if (N && typeof N === 'object') {
if (Math.abs(dx) > 1) dx = dx > 0 ? -1 : 1
if (Math.abs(dy) > 1) dy = dy > 0 ? -1 : 1
} else { dx = delta(dx); dy = delta(dy) }
return D[dx + ',' + dy]
}
return snakeCells.map((s, i) => {
if (i === 0) return 'head_' + D[dir.x + ',' + dir.y]
const prev = snakeCells[i - 1]
const next = snakeCells[i + 1]
const from = side(s, prev) // side toward head
if (!next) return 'tail_' + opp[from] // tip points away from body
const to = side(s, next) // side toward tail tip
if ((from === 'e' || from === 'w') && (to === 'e' || to === 'w')) return 'body_h'
if ((from === 'n' || from === 's') && (to === 'n' || to === 's')) return 'body_v'
// corner: name = the two sides it connects; pairs are alphabetically sorted
const pair = [from, to].sort()
if (pair[0] === 'e' && pair[1] === 'n') return 'corner_en'
if (pair[0] === 'n' && pair[1] === 'w') return 'corner_nw'
if (pair[0] === 'e' && pair[1] === 's') return 'corner_es'
return 'corner_sw'
})
}
// Rotate a direction 90°: clockwise order right->down->left->up. Always ±90°, never a reversal.
function rotateDir(dir, clockwise) {
const order = [{ x: 1, y: 0 }, { x: 0, y: 1 }, { x: -1, y: 0 }, { x: 0, y: -1 }]
let i = 0
for (let k = 0; k < 4; k++) {
if (order[k].x === dir.x && order[k].y === dir.y) { i = k; break }
}
return clockwise ? order[(i + 1) % 4] : order[(i + 3) % 4]
}
// Random empty cell, avoiding the snake.
function spawnFood(snake, N) {
const occupied = new Set(snake.map(s => s.x + ',' + s.y))
if (typeof N === 'object') {
const empty = N.cells.filter(c => !occupied.has(c.x + ',' + c.y))
return empty.length ? empty[Math.floor(Math.random() * empty.length)] : null
}
if (occupied.size >= N * N) return null
// Select an empty-cell rank uniformly; at most N*N checks, even almost full.
let rank = Math.floor(Math.random() * (N * N - occupied.size))
for (let y = 0; y < N; y++) {
for (let x = 0; x < N; x++) {
if (!occupied.has(x + ',' + y)) {
if (rank === 0) return { x, y }
rank--
}
}
}
return null
}
module.exports = { ARENA, initialSnake, step, turn, renderPieces, rotateDir, spawnFood }