// direction choosing functions ------------------------------------------------ function getUserDir(keys: Keys, state: SnakeGameState): Vec2 { // get direction based on user input const { snake } = state; let dir = getSnakeDir(snake); const keyDirMap = { w: vec2(0, -1), s: vec2(0, +1), a: vec2(-1, 0), d: vec2(+1, 0), }; for (const [key, newDir] of Object.entries(keyDirMap)) { // vec2.dot === 0 prevents choosing to move backwards if (keys.isKeyPressed(key) && Vec2.dot(dir, newDir) === 0) { dir = newDir; break; } } return dir; } function getWallRiderDir(state: SnakeGameState): Vec2 { // basic wall rider "AI" that will ride the wall forever // as long as it can turn left (note y-axis is flipped) const { snake } = state; let dir = getSnakeDir(snake); const nextSquare = getSnakeNextSquare(snake, dir); if (isOutOfBounds(nextSquare)) { dir = dir.rot90(); } return dir; }