You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

142 lines
4.3 KiB

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';
import Message from './message.component';
import Muncher from './muncher.component';
import Troggle from './troggle.component';
import Values from '../../reducers/Values';
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 * as TroggleActions from '../../actions/board/troggle.actions';
const exclamations = [
'Congratulations!',
'Yippee!',
'Woohoo!',
'Nice work!',
'Great job!',
'Boom!',
'All finished!',
'Shazam!'
];
const troggleMoveTimers = [];
const troggleCreateTimers = [];
// const toggleMoveTimeout = function() {
// this.props.dispatch(TroggleActions.move());
// };
const continuations = {
muncher: null,
message: null
};
let listener = null;
export default class Board extends Component {
nextLevel() {
this.props.dispatch(BoardActions.nextLevel());
const troggleCount = 1; //Math.min(Math.ceil(this.props.board.level / 2), 5);
for (let i = 0; i < troggleCount; i++) {
this.props.dispatch(TroggleActions.create());
troggleMoveTimers.push(setTimeout(this.moveTroggle.bind(this, i), 1000));
}
// toggleTimeout.call(this);
};
componentDidMount() {
listener = this.keydown.bind(this);
window.addEventListener('keydown', listener);
this.nextLevel();
};
componentWillUnmount() {
window.removeEventListener('keydown', listener);
// destroy all troggle timers
};
moveTroggle(index) {
this.props.dispatch(TroggleActions.move(index));
clearTimeout(troggleMoveTimers[index]);
troggleMoveTimers[index] = setTimeout(this.moveTroggle.bind(this, index), 1000);
};
// 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) {
this.props.dispatch(MessageActions.hide());
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.display(msg));
dispatch(ScorebarActions.munchFailed());
dispatch(MuncherActions.freeze());
}
if (Values.checkComplete(this.props.board.values, board.level)) {
const msg = exclamations[Math.floor(Math.random() * exclamations.length)];
dispatch(MessageActions.display(msg));
}
}
};
render() {
const { board, message, troggles } = this.props;
const troggleElements = [];
for (let i = 0; i < troggles.length; i++) {
troggleElements.push(<Troggle x={troggles[i].x} y={troggles[i].y} key={i} />);
}
return (<div className='board'>
<Scorebar />
<Titlebar title={board.title} />
<Message hidden={message.hidden} message={message.message} />
<Muncher />
{troggleElements}
<Grid values={board.values} />
</div>);
};
};
const select = (state) => {
return {
board: state.board,
muncher: state.muncher,
message: state.message,
troggles: state.troggles
};
}
export default connect(select)(Board);