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.
145 lines
4.2 KiB
145 lines
4.2 KiB
import SETTINGS from '../../AppSettings';
|
|
|
|
import * as TroggleActions from '../../actions/board/troggle.actions';
|
|
import MuncherCtrl from './muncher.controller';
|
|
import BoardCtrl from './board.controller';
|
|
|
|
const troggles = [];
|
|
const troggleMoveTimers = [];
|
|
const troggleCreateTimers = [];
|
|
|
|
let frozen = false;
|
|
let dispatch;
|
|
|
|
const TroggleCtrl = {
|
|
setDispatch: (d) => dispatch = d,
|
|
|
|
getTroggles: () => troggles,
|
|
|
|
freeze: () => frozen = true,
|
|
unfreeze: () => frozen = false,
|
|
isFrozen: () => frozen,
|
|
|
|
clearTroggles() {
|
|
dispatch(TroggleActions.clearAll());
|
|
|
|
troggles.length = 0;
|
|
|
|
troggleMoveTimers.forEach((timer) => {
|
|
clearTimeout(timer);
|
|
});
|
|
|
|
troggleCreateTimers.forEach((timer) => {
|
|
clearTimeout(timer);
|
|
});
|
|
},
|
|
|
|
createTroggle(index) {
|
|
const coords = this.getStartingCoords();
|
|
troggles[index] = { x: coords.x, y: coords.y };
|
|
dispatch(TroggleActions.create(index, coords.x, coords.y));
|
|
|
|
const ref = this.moveTroggle.bind(this, index);
|
|
troggleMoveTimers[index] = setTimeout(ref, 1000);
|
|
},
|
|
|
|
createTroggles(level) {
|
|
const count = Math.min(Math.ceil((level + 1) / 2), 5);
|
|
let wait = 0;
|
|
|
|
for (let index = 0; index < count; index++) {
|
|
const ref = this.createTroggle.bind(this, index);
|
|
wait = wait + Math.random() * 10000;
|
|
|
|
// troggleCreateTimers[index] = setTimeout(ref, 1000);
|
|
troggleCreateTimers[index] = setTimeout(ref, wait);
|
|
}
|
|
},
|
|
|
|
moveTroggle(index) {
|
|
if (frozen === false) {
|
|
const coords = this.getMoveCoords(troggles[index].x, troggles[index].y);
|
|
troggles[index].x = coords.x;
|
|
troggles[index].y = coords.y;
|
|
|
|
BoardCtrl.checkCollision();
|
|
dispatch(TroggleActions.update(index, coords.x, coords.y));
|
|
}
|
|
|
|
const ref = this.moveTroggle.bind(this, index);
|
|
troggleMoveTimers[index] = setTimeout(ref, 1000);
|
|
},
|
|
|
|
getMoveCoords(currX, currY) {
|
|
// Randomize movement with boolean flags. Aggression can be controlled here.
|
|
const moveToAttack = Boolean(Math.random() > 0.35);
|
|
const moveAlongXAxis = Boolean(Math.random() > 0.5);
|
|
const moveInPositiveDirection = Boolean(Math.random() > 0.5);
|
|
|
|
let newX = currX;
|
|
let newY = currY;
|
|
let muncherX = MuncherCtrl.getX();
|
|
let muncherY = MuncherCtrl.getY();
|
|
|
|
// 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 TroggleCtrl;
|
|
|