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.
56 lines
1.5 KiB
56 lines
1.5 KiB
require('../../sass/board/muncher.scss');
|
|
|
|
import { Component } from 'react';
|
|
import { connect } from 'react-redux';
|
|
import * as MuncherActions from '../../actions/board/muncher.actions';
|
|
import { SETTINGS } from '../../App';
|
|
|
|
export class Muncher extends Component {
|
|
keydown(e) {
|
|
const x = this.props.x;
|
|
const y = this.props.y;
|
|
|
|
switch (e.keyCode) {
|
|
case 32:
|
|
this.props.onMunch(x, y);
|
|
break;
|
|
case 37:
|
|
if (x !== 0) {
|
|
this.props.dispatch(MuncherActions.moveLeft());
|
|
}
|
|
break;
|
|
|
|
case 38:
|
|
if (y !== 0) {
|
|
this.props.dispatch(MuncherActions.moveUp());
|
|
}
|
|
break;
|
|
|
|
case 39:
|
|
if (x !== SETTINGS.GRID_WIDTH - 1) {
|
|
this.props.dispatch(MuncherActions.moveRight());
|
|
}
|
|
break;
|
|
|
|
case 40:
|
|
if (y !== SETTINGS.GRID_HEIGHT - 1) {
|
|
this.props.dispatch(MuncherActions.moveDown());
|
|
}
|
|
break;
|
|
}
|
|
};
|
|
|
|
render() {
|
|
const classname = ['muncher', 'x' + this.props.x, 'y' + this.props.y];
|
|
|
|
return (
|
|
<div className={classname.join(' ')} tabIndex='1' onKeyDown={this.keydown.bind(this)}></div>
|
|
);
|
|
};
|
|
};
|
|
|
|
const select = (state) => {
|
|
return state.muncher;
|
|
}
|
|
|
|
export default connect(select)(Muncher);
|
|
|