|
|
@ -26,127 +26,115 @@ const troggleCreateTimers = []; |
|
|
|
let listener = null; |
|
|
|
let listener = null; |
|
|
|
|
|
|
|
|
|
|
|
export default class Board extends Component { |
|
|
|
export default class Board extends Component { |
|
|
|
|
|
|
|
refocus() { |
|
|
|
|
|
|
|
this.props.dispatch(MessageActions.hide()); |
|
|
|
|
|
|
|
ReactDOM.findDOMNode(this.refs.muncher).focus(); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
componentDidMount() { |
|
|
|
componentDidMount() { |
|
|
|
listener = this.keydown.bind(this); |
|
|
|
ReactDOM.findDOMNode(this.refs.muncher).focus(); |
|
|
|
window.addEventListener('keydown', listener); |
|
|
|
listener = this.refocus.bind(this); |
|
|
|
|
|
|
|
window.addEventListener('click', listener); |
|
|
|
this.nextLevel(); |
|
|
|
this.nextLevel(); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() { |
|
|
|
componentWillUnmount() { |
|
|
|
this.clearTroggleTimers(); |
|
|
|
// this.clearTroggleTimers();
|
|
|
|
window.removeEventListener('keydown', listener); |
|
|
|
window.removeEventListener('keydown', listener); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
nextLevel() { |
|
|
|
nextLevel() { |
|
|
|
this.props.dispatch(BoardActions.nextLevel()); |
|
|
|
this.props.dispatch(BoardActions.nextLevel()); |
|
|
|
this.props.dispatch(TroggleActions.clearAll()); |
|
|
|
// this.props.dispatch(TroggleActions.clearAll());
|
|
|
|
this.clearTroggleTimers(); |
|
|
|
// this.clearTroggleTimers();
|
|
|
|
this.createTroggles(); |
|
|
|
// this.createTroggles();
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clearTroggleTimers() { |
|
|
|
|
|
|
|
troggleMoveTimers.forEach((timer) => { |
|
|
|
|
|
|
|
clearTimeout(timer); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
troggleCreateTimers.forEach((timer) => { |
|
|
|
|
|
|
|
clearTimeout(timer); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
createTroggle(index) { |
|
|
|
// clearTroggleTimers() {
|
|
|
|
const newCoords = TroggleAI.create(); |
|
|
|
// troggleMoveTimers.forEach((timer) => {
|
|
|
|
this.props.dispatch(TroggleActions.create(newCoords.x, newCoords.y)); |
|
|
|
// clearTimeout(timer);
|
|
|
|
troggleMoveTimers.push(setTimeout(this.moveTroggle.bind(this, index), 1000)); |
|
|
|
// });
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// troggleCreateTimers.forEach((timer) => {
|
|
|
|
|
|
|
|
// clearTimeout(timer);
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// createTroggle(index) {
|
|
|
|
|
|
|
|
// const newCoords = TroggleAI.create();
|
|
|
|
|
|
|
|
// this.props.dispatch(TroggleActions.create(newCoords.x, newCoords.y));
|
|
|
|
|
|
|
|
// troggleMoveTimers.push(setTimeout(this.moveTroggle.bind(this, index), 1000));
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// createTroggles() {
|
|
|
|
|
|
|
|
// const troggleCount = 3; //Math.min(Math.ceil(this.props.board.level / 2), 5);
|
|
|
|
|
|
|
|
// for (let i = 0; i < troggleCount; i++) {
|
|
|
|
|
|
|
|
// setTimeout(this.createTroggle.bind(this, i), (i + 1) * 5000);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// moveTroggle(index) {
|
|
|
|
|
|
|
|
// const newCoords = TroggleAI.move(
|
|
|
|
|
|
|
|
// this.props.troggles[index].x,
|
|
|
|
|
|
|
|
// this.props.troggles[index].y,
|
|
|
|
|
|
|
|
// this.props.muncher.x,
|
|
|
|
|
|
|
|
// this.props.muncher.y
|
|
|
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// if (newCoords.x === this.props.muncher.x && newCoords.y === this.props.muncher.y) {
|
|
|
|
|
|
|
|
// this.props.dispatch(MessageActions.show("You've been eaten by a troggle!"));
|
|
|
|
|
|
|
|
// this.props.dispatch(TroggleActions.clearAll());
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// else {
|
|
|
|
|
|
|
|
// this.props.dispatch(TroggleActions.move(index, newCoords.x, newCoords.y));
|
|
|
|
|
|
|
|
// clearTimeout(troggleMoveTimers[index]);
|
|
|
|
|
|
|
|
// troggleMoveTimers[index] = setTimeout(this.moveTroggle.bind(this, index), 1000);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
messageNext() { |
|
|
|
|
|
|
|
this.props.dispatch(MessageActions.hide()); |
|
|
|
|
|
|
|
ReactDOM.findDOMNode(this.refs.muncher).focus(); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
createTroggles() { |
|
|
|
muncherNext(x, y) { |
|
|
|
const troggleCount = 3; //Math.min(Math.ceil(this.props.board.level / 2), 5);
|
|
|
|
const index = y * SETTINGS.GRID_WIDTH + x; |
|
|
|
for (let i = 0; i < troggleCount; i++) { |
|
|
|
|
|
|
|
setTimeout(this.createTroggle.bind(this, i), (i + 1) * 5000); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
moveTroggle(index) { |
|
|
|
if (this.props.values[index].valid) { |
|
|
|
const newCoords = TroggleAI.move( |
|
|
|
this.props.dispatch(BoardActions.hideValue(index)); |
|
|
|
this.props.troggles[index].x, |
|
|
|
this.props.dispatch(ScorebarActions.munchSucceeded()); |
|
|
|
this.props.troggles[index].y, |
|
|
|
|
|
|
|
this.props.muncher.x, |
|
|
|
|
|
|
|
this.props.muncher.y |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (newCoords.x === this.props.muncher.x && newCoords.y === this.props.muncher.y) { |
|
|
|
|
|
|
|
this.props.dispatch(MessageActions.show('Eaten by a troggle!')); |
|
|
|
|
|
|
|
this.props.dispatch(TroggleActions.clearAll()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
this.props.dispatch(TroggleActions.move(index, newCoords.x, newCoords.y)); |
|
|
|
const msg = Values.getError(this.props.values[index].value, this.props.level); |
|
|
|
clearTimeout(troggleMoveTimers[index]); |
|
|
|
this.props.dispatch(MessageActions.show(msg)); |
|
|
|
troggleMoveTimers[index] = setTimeout(this.moveTroggle.bind(this, index), 1000); |
|
|
|
this.props.dispatch(ScorebarActions.munchFailed()); |
|
|
|
} |
|
|
|
ReactDOM.findDOMNode(this.refs.message).focus(); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Keydown listener for spacebar, since it is bound to munch event and
|
|
|
|
|
|
|
|
// message hide event. Couldn't find a more modular way to do this.
|
|
|
|
|
|
|
|
keydown(e) { |
|
|
|
|
|
|
|
if (e.keyCode !== 32) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (this.props.message.hidden === false) { |
|
|
|
// if (Values.checkComplete(this.props.values, this.props.level)) {
|
|
|
|
this.props.dispatch(MessageActions.hide()); |
|
|
|
// this.props.dispatch(MessageActions.exclaim());
|
|
|
|
this.props.dispatch(MuncherActions.unfreeze()); |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
if (Values.checkComplete(this.props.board.values, this.props.board.level)) { |
|
|
|
|
|
|
|
console.warn("NEXT LEVEL") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
const index = this.props.muncher.y * SETTINGS.GRID_HEIGHT + this.props.muncher.x |
|
|
|
|
|
|
|
const { board, dispatch } = this.props; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (board.values[index].valid) { |
|
|
|
|
|
|
|
dispatch(BoardActions.hideValue(index)); |
|
|
|
|
|
|
|
dispatch(ScorebarActions.munchSucceeded()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
const msg = Values.getError(board.values[index].value, board.level); |
|
|
|
|
|
|
|
dispatch(MessageActions.show(msg)); |
|
|
|
|
|
|
|
dispatch(ScorebarActions.munchFailed()); |
|
|
|
|
|
|
|
dispatch(MuncherActions.freeze()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Values.checkComplete(this.props.board.values, board.level)) { |
|
|
|
|
|
|
|
dispatch(MessageActions.exclaim()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
render() { |
|
|
|
render() { |
|
|
|
const { board, message, troggles } = this.props; |
|
|
|
// const troggleElements = [];
|
|
|
|
const troggleElements = []; |
|
|
|
// for (let i = 0; i < troggles.length; i++) {
|
|
|
|
for (let i = 0; i < troggles.length; i++) { |
|
|
|
// troggleElements[i] = <Troggle x={troggles[i].x} y={troggles[i].y} key={i} />;
|
|
|
|
troggleElements.push(<Troggle x={troggles[i].x} y={troggles[i].y} key={i} />); |
|
|
|
// }
|
|
|
|
} |
|
|
|
// {troggleElements}
|
|
|
|
|
|
|
|
|
|
|
|
return (<div className='board'> |
|
|
|
return (<div className='board'> |
|
|
|
<Scorebar /> |
|
|
|
<Scorebar /> |
|
|
|
<Titlebar title={board.title} /> |
|
|
|
<Titlebar title={this.props.title} /> |
|
|
|
<Message hidden={message.hidden} message={message.message} /> |
|
|
|
<Message ref='message' next={this.messageNext.bind(this)} /> |
|
|
|
<Muncher /> |
|
|
|
<Grid values={this.props.values} /> |
|
|
|
{troggleElements} |
|
|
|
<Muncher ref='muncher' next={this.muncherNext.bind(this)} /> |
|
|
|
<Grid values={board.values} /> |
|
|
|
|
|
|
|
</div>); |
|
|
|
</div>); |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const select = (state) => { |
|
|
|
const select = (state) => { |
|
|
|
return { |
|
|
|
return state.board; |
|
|
|
board: state.board, |
|
|
|
|
|
|
|
muncher: state.muncher, |
|
|
|
|
|
|
|
message: state.message, |
|
|
|
|
|
|
|
troggles: state.troggles |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export default connect(select)(Board); |
|
|
|
export default connect(select)(Board); |
|
|
|