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.
 
 

69 lines
1.8 KiB

require('../../sass/board/muncher.scss');
import { Component } from 'react';
import { connect } from 'react-redux';
import * as Actions from '../../actions/board/muncher.actions';
import { SETTINGS } from '../../App';
let listener = null;
export class Muncher extends Component {
keydown(e) {
const x = this.props.x;
const y = this.props.y;
switch (e.keyCode) {
case 32:
this.props.munch();
break;
case 37:
if (this.props.x === 0) {
return;
}
this.props.dispatch(Actions.moveLeft());
break;
case 38:
if (this.props.y === 0) {
return;
}
this.props.dispatch(Actions.moveUp());
break;
case 39:
if (this.props.x === SETTINGS.GRID_WIDTH - 1) {
return;
}
this.props.dispatch(Actions.moveRight());
break;
case 40:
if (this.props.y === SETTINGS.GRID_HEIGHT - 1) {
return;
}
this.props.dispatch(Actions.moveDown());
break;
}
};
componentDidMount() {
listener = this.keydown.bind(this);
window.addEventListener('keydown', listener);
};
componentWillUnmount() {
window.removeEventListener('keydown', listener);
};
render() {
const classname = ['muncher', 'x' + this.props.x, 'y' + this.props.y];
return (
<div className={classname.join(' ')}></div>
);
};
};
const select = (state) => {
return state.muncher;
};
export default connect(select)(Muncher);