Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ...
- const HANDLER = {
- [ACTION.CONTAINER_SIZE_CHANGE]: (state, containerSize) => ({
- ...state,
- containerSize,
- ...getProjectors(containerSize, state.game.size)
- }),
- [ACTION.KEY_DOWN]: (state, key) => {
- if (MOVEMENT_KEYS.LEFT.includes(key)) {
- return { ...state, movement: MOVEMENT.LEFT }
- } else if (MOVEMENT_KEYS.RIGHT.includes(key)) {
- return { ...state, movement: MOVEMENT.RIGHT }
- }
- return state
- },
- [ACTION.KEY_UP]: (state, key) => {
- const newState = { ...state, movement: undefined }
- if (key === STOP_KEY) {
- if (state.stopTime) {
- return { ...newState, stopTime: undefined, time: state.time + Date.now() - state.stopTime}
- } else {
- return { ...newState, stopTime: Date.now() }
- }
- }
- return newState
- },
- [ACTION.TICK]: state => {
- if (state.stopTime) return state
- const time = Date.now()
- const newGame = getNewGameState(state.game, state.movement, time - state.time)
- const newState = { ...state, time }
- if (newGame.lives < 1) {
- return { ...newState, game: getGameStateFromLevel(LEVELS[state.level]) }
- } else if (newGame.blocks.length < 1) {
- const level = state.level === LEVELS.length ? state.level : state.level + 1
- localStorage.setItem('level', level)
- const game = getGameStateFromLevel(LEVELS[state.level])
- return {
- ...newState,
- level,
- game,
- ...getProjectors(state.containerSize, game.size)
- }
- }
- return { ...newState, game: newGame }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement