Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #region game core
- // ...
- const getStateAfterFoodProcessing = (state) => {
- const headSegment = new Segment(
- getLastElement(getWithoutLastElement(state.snake)),
- getLastElement(state.snake)
- )
- if (!headSegment.isPointInside(state.food)) return state
- const [tailEnd, beforeTailEnd, ...restOfSnake] = state.snake
- const tailSegment = new Segment(beforeTailEnd, tailEnd)
- const newTailEnd = tailEnd.add(tailSegment.getVector().normalize())
- const snake = [newTailEnd, beforeTailEnd, ...restOfSnake]
- const food = getFood(state.width, state.height, snake)
- return {
- ...state,
- snake,
- score: state.score + 1,
- food
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement