diff --git a/actions/board/grid.actions.js b/actions/board/grid.actions.js new file mode 100644 index 0000000..9413657 --- /dev/null +++ b/actions/board/grid.actions.js @@ -0,0 +1,11 @@ +export const GRID_ACTION = 'GRID_ACTION'; +export const UPDATE = 'UPDATE'; + +const GridActions = { + update: () => ({ + type: GRID_ACTION, + action: UPDATE + }) +}; + +export default GridActions; diff --git a/actions/board/muncher.actions.js b/actions/board/muncher.actions.js index 6ba10cc..4acb013 100644 --- a/actions/board/muncher.actions.js +++ b/actions/board/muncher.actions.js @@ -1,43 +1,7 @@ export const MUNCHER_ACTION = 'MUNCHER_ACTION'; -export const LEFT = 'LEFT'; -export const RIGHT = 'RIGHT'; -export const UP = 'UP'; -export const DOWN = 'DOWN'; -export const MUNCH = 'MUNCH'; -export const FREEZE = 'FREEZE'; -export const UNFREEZE = 'UNFREEZE'; +export const UPDATE = 'UPDATE'; -export const moveLeft = () => ({ +export const update = () => ({ type: MUNCHER_ACTION, - action: LEFT -}); - -export const moveRight = () => ({ - type: MUNCHER_ACTION, - action: RIGHT -}); - -export const moveUp = () => ({ - type: MUNCHER_ACTION, - action: UP -}); - -export const moveDown = () => ({ - type: MUNCHER_ACTION, - action: DOWN -}); - -export const munch = (x, y) => ({ - type: MUNCHER_ACTION, - action: MUNCH -}); - -export const freeze = () => ({ - type: MUNCHER_ACTION, - action: FREEZE -}); - -export const unfreeze = () => ({ - type: MUNCHER_ACTION, - action:UNFREEZE + action: UPDATE }); diff --git a/components/board/board.component.js b/components/board/board.component.js index ef7acbe..e7dc273 100644 --- a/components/board/board.component.js +++ b/components/board/board.component.js @@ -2,7 +2,7 @@ require('../../sass/board/board.scss'); import { Component } from 'react'; import { connect } from 'react-redux'; -import { getState } from 'react-redux'; + import Scorebar from './scorebar.component'; import Titlebar from './titlebar.component'; import Grid from './grid.component'; @@ -10,82 +10,33 @@ import Message from './message.component'; import Muncher from './muncher.component'; import Troggles from './troggles.component'; -import ValuesLogic from '../../logic/values.logic.js'; -import TroggleLogic from '../../logic/troggle.logic.js'; -import { SETTINGS } from '../../App'; - -import * as BoardActions from '../../actions/board/board.actions'; -import * as ScorebarActions from '../../actions/board/scorebar.actions'; -import * as MessageActions from '../../actions/board/message.actions'; -import * as MuncherActions from '../../actions/board/muncher.actions'; +import BoardLogic from '../../logic/board.logic'; -let nextLevelFlag = false; +let listener; -export default class Board extends Component { +export class Board extends Component { componentDidMount() { - this.nextLevel(); - }; - - nextLevel() { - this.props.dispatch(BoardActions.nextLevel()); - TroggleLogic.clearAll(this.props.dispatch); - TroggleLogic.createTroggles(this.props.dispatch); - }; + BoardLogic.setDispatch(this.props.dispatch); + BoardLogic.nextLevel(); - messageNext() { - if (nextLevelFlag === true) { - this.nextLevel(); - nextLevelFlag = false; - } - this.props.dispatch(MessageActions.hide()); - ReactDOM.findDOMNode(this.refs.muncher).focus(); + listener = BoardLogic.keyListener.bind(BoardLogic); + window.addEventListener('keydown', listener); }; - muncherNext(x, y) { - const index = y * SETTINGS.GRID_WIDTH + x; - - if (this.props.values[index].valid) { - this.props.dispatch(BoardActions.hideValue(index)); - this.props.dispatch(ScorebarActions.munchSucceeded()); - - // State will not have been updated; temporary state created for checking. - const tmp = this.props.values.slice(0); - tmp[index].show = false; - if (ValuesLogic.checkComplete(tmp, this.props.level)) { - nextLevelFlag = true; - this.props.dispatch(MessageActions.exclaim()); - ReactDOM.findDOMNode(this.refs.message).focus(); - } - } - else { - const msg = ValuesLogic.getError(this.props.values[index].value, this.props.level); - this.props.dispatch(MessageActions.show(msg)); - this.props.dispatch(ScorebarActions.munchFailed()); - ReactDOM.findDOMNode(this.refs.message).focus(); - } - // - // if (troggles[i].x === muncher.x && troggles[i].y === muncher.y) { - // this.props.dispatch(MessageActions.show("You've been eaten by a troggle!")); - // this.props.dispatch(ScorebarActions.munchFailed()); - // TroggleLogic.frozen = true; - // ReactDOM.findDOMNode(this.refs.message).focus(); - // } + componentWillUnmount() { + window.removeEventListener('keydown', listener); }; render() { return (