snake moving, changing directions, wall collision working

This commit is contained in:
2025-10-16 13:30:26 +01:00
parent 20ac6b9349
commit f37605d8d2
2 changed files with 88 additions and 1 deletions
+44
View File
@@ -9,6 +9,7 @@ export function useSnake(
) {
const { containerRef } = rendererManager;
const [snakeBody, setSnakeBody] = useState<SnakeBody>([]);
const snakeBodyRef = useRef<SnakeBody>([]);
const [direction, setDirection] = useState<Direction>("up");
const directionRef = useRef<Direction>("up");
@@ -23,6 +24,7 @@ export function useSnake(
];
setSnakeBody(initialBody);
snakeBodyRef.current = initialBody;
setDirection("up");
directionRef.current = "up";
renderSnake(initialBody);
@@ -30,6 +32,7 @@ export function useSnake(
const clearSnake = () => {
setSnakeBody([]);
snakeBodyRef.current = [];
setDirection("up");
directionRef.current = "up";
};
@@ -66,13 +69,54 @@ export function useSnake(
}
};
const moveSnake = (): "continue" | "wall-collision" | "self-collision" => {
const body = snakeBodyRef.current;
if (body.length === 0) return "continue";
const head = body[0];
const dir = directionRef.current;
let newR = head.r;
let newC = head.c;
switch (dir) {
case "up":
newR -= 1;
break;
case "down":
newR += 1;
break;
case "left":
newC -= 1;
break;
case "right":
newC += 1;
break;
}
if (newR < 0 || newR >= rows || newC < 0 || newC >= cols) {
return "wall-collision";
}
const newHead = { r: newR, c: newC };
const newBody = [newHead, ...body.slice(0, -1)];
setSnakeBody(newBody);
snakeBodyRef.current = newBody;
renderSnake(newBody);
return "continue";
};
return {
snakeBody,
snakeBodyRef,
direction,
directionRef,
initSnake,
clearSnake,
changeDirection,
moveSnake,
renderSnake,
};
}