@tdurtschi/bug
Version:
Bug is a simulation that models a weevil walking around in an HTML canvas.
118 lines • 4.65 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Climb = void 0;
var BugBehavior_1 = require("./BugBehavior");
var walk_1 = require("./walk");
var stats_1 = require("../../../util/stats");
var Victor = require("victor");
var turnAround_1 = require("./turnAround");
var pause_1 = require("./pause");
var Climb = (function (_super) {
__extends(Climb, _super);
function Climb() {
return _super !== null && _super.apply(this, arguments) || this;
}
Climb.prototype.do = function () {
var bug = this.bug;
if (!bug.climbingOn) {
bug.finishBehavior();
return;
}
var currentBranch = bug.climbingOn.branch;
var branchPosition = bug.climbingOn.plant.getAbsolutePos(currentBranch);
var branchOffset = bug.pos.clone().subtract(branchPosition);
var angleBetweenBranchAndDirection = Math.abs(currentBranch.node.direction() - bug.direction.direction());
var isGoingDown = angleBetweenBranchAndDirection > 3;
if (isGoingDown) {
var directionDelta = Math.abs(branchOffset.direction() - currentBranch.node.direction());
if (directionDelta > 0.3) {
if (currentBranch.parent) {
bug.climbingOn.branch = currentBranch.parent;
bug.pos = bug.climbingOn.plant
.getAbsolutePos(bug.climbingOn.branch)
.add(bug.climbingOn.branch.node);
bug.direction = bug.climbingOn.branch.node
.clone()
.norm()
.multiplyScalar(-1);
}
else {
endClimbing(bug);
}
}
}
else if (branchOffset.magnitude() >= currentBranch.node.magnitude()) {
var nextBranch = getNextBranch(currentBranch);
if (nextBranch) {
climbBranch(bug, nextBranch);
}
else {
bug.queueBehavior(new pause_1.Pause(bug, stats_1.randInt(50, 300)));
bug.queueBehavior(new turnAround_1.TurnAround(bug));
bug.queueBehavior(new Climb(bug));
bug.finishBehavior();
}
}
walk_1.walk(bug);
if (bug.bugInstinct.IsTiredOfClimbing) {
console.log("Bug got tired of climbin");
bug.finishBehavior();
}
};
return Climb;
}(BugBehavior_1.BugBehavior));
exports.Climb = Climb;
var getNextBranch = function (currentBranch) {
return stats_1.randBool() && currentBranch.left
? currentBranch.left
: currentBranch.right;
};
var endClimbing = function (bug) {
bug.direction = new Victor(stats_1.randBool() ? 1 : -1, 0);
var sizeOffset = bug.direction.x > 0 ? bug.size.x + 1 : -(bug.size.x + 1);
var newX = bug.climbingOn.plant.pos.x + sizeOffset;
bug.pos = new Victor(newX, bug.climbingOn.plant.pos.y);
bug.climbingOn = undefined;
bug.zIndexChanged.next();
bug.finishBehavior();
};
var climbBranch = function (bug, branch, direction) {
if (direction === void 0) { direction = Direction.UP; }
var offset;
if (direction === Direction.UP) {
if (!branch.parent) {
offset = new Victor(bug.size.x / 3, 0).rotate(branch.node.direction());
}
else {
offset = new Victor(Math.sin(Math.abs(branch.node.direction() - branch.parent.node.direction()) / 2) * bug.size.x, 0).rotate(branch.node.direction());
}
}
else {
offset = branch.node;
}
bug.climbingOn.branch = branch;
bug.pos = bug.climbingOn.plant.getAbsolutePos(branch).add(offset);
bug.direction = branch.node
.clone()
.norm()
.multiplyScalar(direction === Direction.UP ? 1 : -1);
};
var Direction;
(function (Direction) {
Direction[Direction["UP"] = 0] = "UP";
Direction[Direction["DOWN"] = 1] = "DOWN";
})(Direction || (Direction = {}));
//# sourceMappingURL=climb.js.map