1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- class SheepController {
- constructor() {
- this.sheep = null;
- this.proximity = 100;
- this.wanderRange = 50;
- this.wanderTimer = 5000;
- }
- init(systemObject, sheep) {
- this.system = systemObject;
- this.sheep = sheep;
- this.proximity = Math.floor(80 * Math.random() + 30);
- this.wanderRange = Math.floor(100 * Math.random() + 50);
- this.wanderTimer = Math.floor(15 * Math.random() + 5) * 1000;
- }
- reset() {
- }
- makeDecisions(player) {
- if(this.sheep.isRunningAway) {
- return;
- }
- let distance = Math.abs(this.system.distanceTo(this.sheep.position, player.position));
- if(distance < this.proximity) {
- clearTimeout(this.isIdle);
- this.isIdle = null;
- let deltaDistance = Math.floor(4 * Math.random() * (this.proximity - distance));
- let angleTo = this.system.angleTo(player.position, this.sheep.position);
- this.sheep.targetPosition.x = Math.floor(deltaDistance * Math.cos(angleTo)) + this.sheep.position.x;
- this.sheep.targetPosition.y = Math.floor(deltaDistance * Math.sin(angleTo)) + this.sheep.position.y;
- this.sheep.targetAngle = angleTo;
- this.sheep.isRunningAway = true;
- } else {
- if(distance < this.proximity * 2) {
- this.sheep.targetAngle = this.system.angleTo(this.sheep.position, player.position);
- }
- if(this.isIdle) {
- return;
- }
- this.isIdle = setTimeout(() => {this.pickNewDestination()}, this.wanderTimer);
- }
- }
- pickNewDestination() {
- this.isIdle = null;
- this.sheep.targetPosition.x = Math.floor(this.wanderRange * 2 * Math.random() - this.wanderRange) + this.sheep.position.x;
- this.sheep.targetPosition.y = Math.floor(this.wanderRange * 2 * Math.random() - this.wanderRange) + this.sheep.position.y;
- }
- contextMenu() {
- }
- debugDraw(context) {
- if(!localStorage['game.debug']) {
- return;
- }
- context.fillStyle = "red";
- context.strokeStyle = "red";
- context.lineWidth = 1;
- context.beginPath();
- context.moveTo(this.sheep.position.x, this.sheep.position.y);
- context.lineTo(this.sheep.targetPosition.x, this.sheep.targetPosition.y);
- context.stroke();
- context.beginPath();
- context.arc(this.sheep.position.x, this.sheep.position.y, this.proximity, 0, 2 * Math.PI);
- context.stroke();
- context.strokeStyle = "yellow";
- context.beginPath();
- context.arc(this.sheep.position.x, this.sheep.position.y, this.proximity * 2, 0, 2 * Math.PI);
- context.stroke();
- }
- }
|