function Character(game) { this.id = 0; this.position = {x: 340, y: 320}; this.targetPosition = {x: 300, y: 320}; this.width = 20; this.height = 20; this.maxSpeed = 2; this.targetAngle = 0; this.angleRadians = 0; this.color = "cornflowerblue"; this.playerName = "Character"; this.spriteCanvas = null; this.spriteContext = null; this.isDead = false; this.health = 5; this.maxHealth = 5; this.seen = []; this.brain = function(){}; this.init = function(intelligence) { this.brain = intelligence; //this.controller.init(this); this.color = "cornflowerblue"; //this.respawn(); } this.update = function(delta) { if(this.isDead) { return; } if(this.health <= 0) { this.die(); return; } if(this.waitHere-- > 0) { return; } this.angleRadians = 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; } } this.brain(this); } this.control = function(input) { this.controller.makeDecisions(this); } this.respawn = function() { this.position.x = 0; this.position.y = 0; } this.die = function() { this.health = 0; if(!this.isDead) { var player = this; setTimeout(function() {player.respawn();}, 3000); this.isDead = true; } } this.walkUp = function() { this.targetPosition.y = this.position.y - this.maxSpeed; } this.walkDown = function() { this.targetPosition.y = this.position.y + this.maxSpeed; } this.walkLeft = function() { this.targetPosition.x = this.position.x - this.maxSpeed; } this.walkRight = function() { this.targetPosition.x = this.position.x + this.maxSpeed; } this.walkTowards = function(newPosition) { this.targetPosition.x = parseInt(newPosition.x); this.targetPosition.y = parseInt(newPosition.y); } this.preRender = function(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.rect(-(this.width / 2), -(this.height / 2), this.width, this.height); context.fill(); context.restore(); } this.draw = function(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); 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.restore(); } this.trigger = function(eventId) { if(this.seen.indexOf(eventId) == -1) { this.seen.push(eventId); return true; } return false; } };