Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. // ...
  2. const HANDLER = {
  3. [ACTION.CONTAINER_SIZE_CHANGE]: (state, containerSize) => ({
  4. ...state,
  5. containerSize,
  6. ...getProjectors(containerSize, state.game.size)
  7. }),
  8. [ACTION.KEY_DOWN]: (state, key) => {
  9. if (MOVEMENT_KEYS.LEFT.includes(key)) {
  10. return { ...state, movement: MOVEMENT.LEFT }
  11. } else if (MOVEMENT_KEYS.RIGHT.includes(key)) {
  12. return { ...state, movement: MOVEMENT.RIGHT }
  13. }
  14. return state
  15. },
  16. [ACTION.KEY_UP]: (state, key) => {
  17. const newState = { ...state, movement: undefined }
  18. if (key === STOP_KEY) {
  19. if (state.stopTime) {
  20. return { ...newState, stopTime: undefined, time: state.time + Date.now() - state.stopTime}
  21. } else {
  22. return { ...newState, stopTime: Date.now() }
  23. }
  24. }
  25. return newState
  26. },
  27. [ACTION.TICK]: state => {
  28. if (state.stopTime) return state
  29.  
  30. const time = Date.now()
  31. const newGame = getNewGameState(state.game, state.movement, time - state.time)
  32. const newState = { ...state, time }
  33. if (newGame.lives < 1) {
  34. return { ...newState, game: getGameStateFromLevel(LEVELS[state.level]) }
  35. } else if (newGame.blocks.length < 1) {
  36. const level = state.level === LEVELS.length ? state.level : state.level + 1
  37. localStorage.setItem('level', level)
  38. const game = getGameStateFromLevel(LEVELS[state.level])
  39. return {
  40. ...newState,
  41. level,
  42. game,
  43. ...getProjectors(state.containerSize, game.size)
  44. }
  45. }
  46. return { ...newState, game: newGame }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement