Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. // #region game core
  2. // ...
  3. const getStateAfterMoveProcessing = (state, movement, distance) => {
  4. const newTail = getNewTail(state.snake, distance)
  5. const oldHead = getLastElement(state.snake)
  6. const newHead = oldHead.add(state.direction.scaleBy(distance))
  7. const newDirection = getNewDirection(state.direction, movement)
  8. if (!state.direction.equalTo(newDirection)) {
  9. const { x: oldX, y: oldY } = oldHead
  10. const [
  11. oldXRounded,
  12. oldYRounded,
  13. newXRounded,
  14. newYRounded
  15. ] = [oldX, oldY, newHead.x, newHead.y].map(Math.round)
  16. const getStateWithBrokenSnake = (old, oldRounded, newRounded, getBreakpoint) => {
  17. const breakpointComponent = oldRounded + (newRounded > oldRounded ? 0.5 : -0.5)
  18. const breakpoint = getBreakpoint(breakpointComponent)
  19. const vector = newDirection.scaleBy(distance - Math.abs(old - breakpointComponent))
  20. const head = breakpoint.add(vector)
  21. return {
  22. ...state,
  23. direction: newDirection,
  24. snake: [...newTail, breakpoint, head]
  25. }
  26. }
  27. if (oldXRounded !== newXRounded) {
  28. return getStateWithBrokenSnake(oldX, oldXRounded, newXRounded, x => new Vector(x, oldY))
  29. }
  30. if (oldYRounded !== newYRounded) {
  31. return getStateWithBrokenSnake(oldY, oldYRounded, newYRounded, y => new Vector(oldX, y))
  32. }
  33. }
  34. return {
  35. ...state,
  36. snake: [...newTail, newHead]
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement