parent
49baf89a5e
commit
faeae93892
16 changed files with 241 additions and 190 deletions
@ -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; |
@ -1,43 +1,7 @@ |
|||||||
export const MUNCHER_ACTION = 'MUNCHER_ACTION'; |
export const MUNCHER_ACTION = 'MUNCHER_ACTION'; |
||||||
export const LEFT = 'LEFT'; |
export const UPDATE = 'UPDATE'; |
||||||
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 moveLeft = () => ({ |
export const update = () => ({ |
||||||
type: MUNCHER_ACTION, |
type: MUNCHER_ACTION, |
||||||
action: LEFT |
action: UPDATE |
||||||
}); |
|
||||||
|
|
||||||
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 |
|
||||||
}); |
}); |
||||||
|
@ -0,0 +1,67 @@ |
|||||||
|
import { SETTINGS } from '../App.js'; |
||||||
|
|
||||||
|
import * as ScorebarActions from '../actions/board/scorebar.actions'; |
||||||
|
import * as MessageActions from '../actions/board/message.actions'; |
||||||
|
|
||||||
|
import TroggleLogic from './troggle.logic'; |
||||||
|
import MessageLogic from './message.logic'; |
||||||
|
import MuncherLogic from './muncher.logic'; |
||||||
|
import GridLogic from './grid.logic'; |
||||||
|
|
||||||
|
const level = 0; |
||||||
|
let dispatch; |
||||||
|
|
||||||
|
const BoardLogic = { |
||||||
|
setDispatch: (d) => { |
||||||
|
dispatch: d, |
||||||
|
GridLogic.setDispatch(d); |
||||||
|
MuncherLogic.setDispatch(d); |
||||||
|
}, |
||||||
|
|
||||||
|
munch() { |
||||||
|
const index = MuncherLogic.getY() * SETTINGS.GRID_WIDTH + MuncherLogic.getX(); |
||||||
|
|
||||||
|
if (GridLogic.getValues()[index].valid) { |
||||||
|
GridLogic.hideValue(index); |
||||||
|
|
||||||
|
if (GridLogic.isCompleted() === true) { |
||||||
|
dispatch(MessageActions.exclaim()); |
||||||
|
} |
||||||
|
} |
||||||
|
else { |
||||||
|
const msg = Values.getError(values[index].value, level); |
||||||
|
dispatch(MessageActions.show(msg)); |
||||||
|
} |
||||||
|
}, |
||||||
|
|
||||||
|
keyListener(e) { |
||||||
|
if (e.keyCode === 32 && GridLogic.isCompleted() === true) { |
||||||
|
this.nextLevel(); |
||||||
|
dispatch(MessageActions.hide()); |
||||||
|
} |
||||||
|
else if (e.keyCode === 32 && MessageLogic.isShowing() === true) { |
||||||
|
dispatch(MessageActions.hide()); |
||||||
|
} |
||||||
|
else if (e.keyCode === 32 && MessageLogic.isShowing() === false) { |
||||||
|
this.munch(); |
||||||
|
} |
||||||
|
else if (MessageLogic.isShowing() === false) { |
||||||
|
MuncherLogic.move(e); |
||||||
|
} |
||||||
|
}, |
||||||
|
|
||||||
|
nextLevel() { |
||||||
|
GridLogic.generateValues(level); |
||||||
|
// TroggleLogic.clearAll(this.props.dispatch);
|
||||||
|
// TroggleLogic.createTroggles(this.props.dispatch);
|
||||||
|
}, |
||||||
|
|
||||||
|
// 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();
|
||||||
|
// }
|
||||||
|
}; |
||||||
|
|
||||||
|
export default BoardLogic; |
@ -0,0 +1,27 @@ |
|||||||
|
import GridActions from '../actions/board/grid.actions'; |
||||||
|
import ValuesLogic from './values.logic'; |
||||||
|
import { SETTINGS } from '../App'; |
||||||
|
|
||||||
|
let values; |
||||||
|
let dispatch; |
||||||
|
|
||||||
|
const GridLogic = { |
||||||
|
setDispatch: d => dispatch = d, |
||||||
|
getValues: () => values, |
||||||
|
|
||||||
|
generateValues: (level) => { |
||||||
|
values = ValuesLogic.generate(SETTINGS.GRID_WIDTH * SETTINGS.GRID_HEIGHT, level); |
||||||
|
dispatch(GridActions.update()); |
||||||
|
}, |
||||||
|
|
||||||
|
isCompleted: (level) => { |
||||||
|
return ValuesLogic.checkComplete(values, level) |
||||||
|
}, |
||||||
|
|
||||||
|
hideValue: (index) => { |
||||||
|
values[index].show = false; |
||||||
|
dispatch(GridActions.update()); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
export default GridLogic; |
@ -0,0 +1,9 @@ |
|||||||
|
let show = false; |
||||||
|
|
||||||
|
const MessageLogic = { |
||||||
|
hide: () => show = false, |
||||||
|
show: () => show = true, |
||||||
|
isShowing: () => show |
||||||
|
}; |
||||||
|
|
||||||
|
export default MessageLogic; |
@ -0,0 +1,46 @@ |
|||||||
|
import * as MuncherActions from '../actions/board/muncher.actions'; |
||||||
|
import { SETTINGS } from '../App'; |
||||||
|
|
||||||
|
let x = 0; |
||||||
|
let y = 0; |
||||||
|
let dispatch; |
||||||
|
|
||||||
|
const MuncherLogic = { |
||||||
|
getX: () => x, |
||||||
|
getY: () => y, |
||||||
|
setDispatch: d => dispatch = d, |
||||||
|
|
||||||
|
move: (e) => { |
||||||
|
switch (e.keyCode) { |
||||||
|
case 37: |
||||||
|
if (x !== 0) { |
||||||
|
x -= 1; |
||||||
|
dispatch(MuncherActions.update()); |
||||||
|
} |
||||||
|
break; |
||||||
|
|
||||||
|
case 38: |
||||||
|
if (y !== 0) { |
||||||
|
y -= 1; |
||||||
|
dispatch(MuncherActions.update()); |
||||||
|
} |
||||||
|
break; |
||||||
|
|
||||||
|
case 39: |
||||||
|
if (x !== SETTINGS.GRID_WIDTH - 1) { |
||||||
|
x += 1; |
||||||
|
dispatch(MuncherActions.update()); |
||||||
|
} |
||||||
|
break; |
||||||
|
|
||||||
|
case 40: |
||||||
|
if (y !== SETTINGS.GRID_HEIGHT - 1) { |
||||||
|
y += 1; |
||||||
|
dispatch(MuncherActions.update()); |
||||||
|
} |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
export default MuncherLogic; |
@ -0,0 +1,20 @@ |
|||||||
|
const Immutable = require('immutable'); |
||||||
|
|
||||||
|
import * as GridActions from '../../actions/board/grid.actions'; |
||||||
|
import GridLogic from '../../logic/grid.logic.js'; |
||||||
|
|
||||||
|
const initial = []; |
||||||
|
|
||||||
|
const reducer = (state = initial, action) => { |
||||||
|
if (action.type !== GridActions.GRID_ACTION) { |
||||||
|
return state; |
||||||
|
} |
||||||
|
|
||||||
|
if (action.action === GridActions.UPDATE) { |
||||||
|
return Immutable.List(GridLogic.getValues()).toArray(); |
||||||
|
} |
||||||
|
|
||||||
|
return state; |
||||||
|
}; |
||||||
|
|
||||||
|
export default reducer; |
Loading…
Reference in new issue