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.
 
 

120 lines
3.5 KiB

import { SETTINGS } from '../App';
import * as TroggleActions from '../actions/board/troggle.actions';
const troggleMoveTimers = [];
const troggleCreateTimers = [];
const TroggleLogic = {
frozen: false,
clearAll(dispatch) {
dispatch(TroggleActions.clearAll());
troggleMoveTimers.forEach((timer) => {
clearTimeout(timer);
});
troggleCreateTimers.forEach((timer) => {
clearTimeout(timer);
});
},
createTroggle(index, dispatch) {
dispatch(TroggleActions.create(index));
const ref = this.moveTroggle.bind(this, index, dispatch);
troggleMoveTimers[index] = setTimeout(ref, 1000);
},
createTroggles(dispatch, level) {
// const count = Math.min(Math.ceil((level + 1) / 2), 5);
const count = 3;
for (let index = 0; index < count; index++) {
const ref = this.createTroggle.bind(this, index, dispatch);
troggleCreateTimers[index] = setTimeout(ref, (index) * 1000);
// troggleCreateTimers[index] = setTimeout(ref, (index + 1) * 5000);
}
},
moveTroggle(index, dispatch) {
if (this.frozen === false) {
dispatch(TroggleActions.move(index));
}
const ref = this.moveTroggle.bind(this, index, dispatch);
troggleMoveTimers[index] = setTimeout(ref, 1000);
},
getMoveCoords(currX, currY, muncherX, muncherY) {
// Randomize movement with boolean flags.
const moveToAttack = Boolean(Math.round(Math.random()));
const moveAlongXAxis = Boolean(Math.round(Math.random()));
const moveInPositiveDirection = Boolean(Math.round(Math.random()));
let newX = currX;
let newY = currY;
// DO NOT CONSOLIDATE. Attack first, ask questions later.
if (moveAlongXAxis === true) {
if (currX >= SETTINGS.GRID_WIDTH - 1 ||
(moveToAttack && currX > muncherX)) {
newX = currX - 1;
}
else if (currX <= 0 ||
(moveToAttack && currX < muncherX)) {
newX = currX + 1;
}
else if (moveInPositiveDirection) {
newX = currX + 1;
}
else if (!moveInPositiveDirection) {
newX = currX - 1;
}
}
else {
if (currY >= SETTINGS.GRID_HEIGHT - 1 ||
(moveToAttack && currY > muncherY)) {
newY = currY - 1;
}
else if (currY <= 0 ||
(moveToAttack && currY < muncherY)) {
newY = currY + 1;
}
else if (moveInPositiveDirection) {
newY = currY + 1;
}
else if (!moveInPositiveDirection) {
newY = currY - 1;
}
}
return { x: newX, y: newY };
},
getStartingCoords() {
// Start outside grid at a randomized location.
const enterOnXAxis = Boolean(Math.round(Math.random()));
const enterFromPositive = Boolean(Math.round(Math.random()));
let x = -1;
let y = -1;
if (enterFromPositive === true) {
x = SETTINGS.GRID_WIDTH;
y = SETTINGS.GRID_HEIGHT;
}
if (enterOnXAxis === true) {
y = Math.round(Math.random() * (SETTINGS.GRID_HEIGHT - 1));
}
else {
x = Math.round(Math.random() * (SETTINGS.GRID_WIDTH - 1));
}
return { x: x, y: y }
}
};
export default TroggleLogic;