Skip to content

Instantly share code, notes, and snippets.

@adicuco
Created March 4, 2019 19:33
Show Gist options
  • Save adicuco/c4e01469c608c72e3dd4a4c189ea77cd to your computer and use it in GitHub Desktop.
Save adicuco/c4e01469c608c72e3dd4a4c189ea77cd to your computer and use it in GitHub Desktop.
HBDA
const constats = {
MIN_DISTANCE: 2,
MIN_VELOCITY: 3,
MAX_VELOCITY: 2,
MIN_TRAVEL_TIME: 2,
MAX_TRAVEL_TIME: 4
};
const followTarget = () => {
let vector = {
x: target.x - position.x,
y: target.y - position.y
};
const velocity =
constats.MIN_VELOCITY + (Math.random() * constats.MAX_VELOCITY - constats.MIN_VELOCITY);
const travelTime =
constats.MIN_TRAVEL_TIME +
(Math.random() * constats.MAX_TRAVEL_TIME - constats.MIN_TRAVEL_TIME);
const distanceToTarget = Math.sqrt(Math.pow(vector.x, 2) + Math.pow(vector.y, 2));
const unitVector = {
x: vector.x / distanceToTarget,
y: vector.y / distanceToTarget
};
newPosition.x = unitVector.x * velocity * travelTime;
newPosition.y = unitVector.y * velocity * travelTime;
newPositions.push(newPosition);
};
currentPositions = [{ x: 1, y: 2 }, { x: 3, y: 2 }, { x: 5, y: 2 }];
newPositions = [];
while (time === ok) {
for (i = 0; i < currentPositions.length; i++) {
currentPosition = currentPositions[i];
x = currentPosition.x;
y = currentPosition.y;
targetX = generateTargetX();
targetY = generateTargetY();
let optionalDistance = true;
const inRangeNodes = getInrangeNodes(x, y);
for (let j = 0; j < inRangeNodes.length; j++) {
if (distance(currentPosition, inRangeNodes[j]) < constats.MIN_DISTANCE) {
optionalDistance = false;
break;
}
}
if (optionalDistance && inRangeNodes.length > 0) {
followTarget();
} else {
adjustPosition();
}
}
currentPositions = newPositions;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment