class Sheep { constructor(game, controller) { this.id = 0; this.position = { x: 0, y: 0 }; this.targetPosition = { x: 0, y: 0 }; this.velocity = { x: 0.0, y: 0.0 }; this.controller = controller; this.acceleration = { x: 0, y: 0, speed: 0.08 }; this.width = 30; this.height = 30; this.maxSpeed = 2; this.turnRate = Math.PI / 64; this.targetAngle = 0; this.angleRadians = 0; this.color = "white"; this.name = "Sheep"; this.spriteCanvas = null; this.spriteContext = null; this.isAccelerating = false; this.isDead = false; this.health = 5; this.maxHealth = 5; this.seen = []; } init(systemObject) { this.system = systemObject; this.controller.init(this); this.color = "white"; this.controller.init(systemObject, this); this.respawn(); } update(delta) { if (this.isDead) { return; } if (this.health <= 0) { this.die(); return; } this.angleRadians = this.system.angleTo(this.position, this.targetPosition); if (this.targetPosition.x != this.position.x) { if (Math.abs(this.position.x - this.targetPosition.x) < this.maxSpeed) { this.position.x = this.targetPosition.x; this.targetAngle = this.angleRadians; } else { this.targetAngle = this.angleRadians; this.position.x += this.maxSpeed * Math.cos(this.angleRadians) * delta; } } if (this.targetPosition.y != this.position.y) { if (Math.abs(this.position.y - this.targetPosition.y) < this.maxSpeed) { this.position.y = this.targetPosition.y; this.targetAngle = this.angleRadians; } else { this.targetAngle = this.angleRadians; this.position.y += (this.maxSpeed) * Math.sin(this.angleRadians) * delta; } } if(this.isRunningAway) { if(this.targetPosition.x == this.position.x && this.targetPosition.y == this.position.y) { setTimeout(() => { this.isRunningAway = false; }, 300); } } } control(player) { this.controller.makeDecisions(player); } respawn() { this.position.x = Math.floor(200 * Math.random() - 100); this.position.y = Math.floor(200 * Math.random() - 100); this.targetPosition.x = this.position.x; this.targetPosition.y = this.position.y; this.targetAngle = Math.floor(360 * Math.random()); } die() { this.health = 0; if (!this.isDead) { var sheep = this; setTimeout(function () { sheep.respawn(); }, 3000); this.isDead = true; } } walkUp() { this.targetPosition.y = this.position.y - this.maxSpeed; } walkDown() { this.targetPosition.y = this.position.y + this.maxSpeed; } walkLeft() { this.targetPosition.x = this.position.x - this.maxSpeed; } walkRight() { this.targetPosition.x = this.position.x + this.maxSpeed; } walkTowards(newPosition) { this.targetPosition.x = parseInt(newPosition.x); this.targetPosition.y = parseInt(newPosition.y); } preRender(context, angle) { if (angle == null) { angle = 0; } context.fillStyle = this.color; context.strokeStyle = this.color; context.lineWidth = 4; context.save(); context.translate(this.width / 2, this.height / 2); context.rotate(angle); // context.beginPath(); // context.moveTo(this.width / 2, 0); // context.lineTo(-(this.width / 2), -(this.height / 3)); // context.lineTo(-(this.width / 2), this.height / 3); // context.lineTo(this.width / 2, 0); // context.fill(); context.beginPath(); context.arc(0, 0, this.width / 4, 0, 2 * Math.PI); context.fill(); context.beginPath(); context.arc(this.width / 6, 0, this.width / 6, 0, 2 * Math.PI); context.fill(); context.restore(); } draw(context) { if (this.spriteContext == null) { this.spriteCanvas = document.createElement("canvas"); this.spriteCanvas.width = this.width; this.spriteCanvas.height = this.height; this.spriteContext = this.spriteCanvas.getContext("2d"); this.preRender(this.spriteContext); } if (this.isDead) { return; } context.save(); context.translate(this.position.x, this.position.y); context.rotate(this.targetAngle); if (this.isAccelerating) { context.fillStyle = "#7777FF"; context.beginPath(); context.moveTo(-this.width - 5, 0); context.lineTo(-this.width / 2, 3); context.lineTo(-this.width / 2, -3); context.lineTo(-this.width - 5, 0); context.fill(); } context.drawImage(this.spriteCanvas, 0, 0, this.spriteCanvas.width, this.spriteCanvas.height, -this.spriteCanvas.width / 2, -this.spriteCanvas.height / 2, this.width, this.height); context.strokeStyle = "#00FF00"; if (this.isTakingDamage) { context.strokeStyle = "#FF0000"; this.isTakingDamage = false; } context.restore(); this.controller.debugDraw(context); } trigger(eventId) { if (this.seen.indexOf(eventId) == -1) { this.seen.push(eventId); return true; } return false; } }