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.
74 lines
2.2 KiB
74 lines
2.2 KiB
import { SETTINGS } from '../App';
|
|
|
|
const TroggleLogic = {
|
|
move: function(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 };
|
|
},
|
|
|
|
create: function() {
|
|
// 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 TroggleAI;
|
|
|