123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- function Ship(game, controller) {
- this.id = 0;
- this.position = {x: 400, y: 300};
- this.velocity = {x: 0.1, y: 0.1};
- this.controller = controller;
- this.acceleration = {x: 0, y: 0, speed: 0.08};
- this.width = 20;
- this.height = 20;
- this.maxSpeed = 24;
- this.turnRate = Math.PI / 64;
- this.moveToTarget = false;
- this.targetDestination = {x: 400, y: 400};
- this.targetAngle = 0;
- this.angleRadians = 0;
- this.shipColor = "#DD3333";
- this.playerName = "Player";
- this.spriteCanvas = null;
- this.spriteContext = null;
- this.isAccelerating = false;
- this.home = null;
- this.spawn = null;
- this.isDead = false;
- this.touching = null;
- this.lastHit = 0;
- this.teamId = 0;
- this.health = 5;
- this.maxHealth = 5;
- this.shotDelay = 10;
- this.init = function(systemColor, homeSystem) {
- this.teamId = homeSystem.teamId;
- this.controller.init();
- this.shipColor = systemColor;
- this.home = homeSystem;
- this.respawn();
- }
- this.update = function(delta) {
- this.shotDelay = Math.max(0, this.shotDelay - 1);
- if(this.isDead) {
- return;
- }
- if(this.health <= 0) {
- this.die();
- return;
- }
- this.acceleration.x = 0;
- this.acceleration.y = 0;
- if(this.isAccelerating) {
- this.angleRadians = this.targetAngle;
- this.acceleration.x = this.acceleration.speed * Math.cos(this.angleRadians);
- this.acceleration.y = this.acceleration.speed * Math.sin(this.angleRadians);
- if(this.touching != null) {
- this.touching = null;
- var liftoffSpeed = 2.0;
- this.velocity.x = liftoffSpeed * Math.cos(this.angleRadians);
- this.velocity.y = liftoffSpeed * Math.sin(this.angleRadians);
- }
- }
- this.velocity.x = Math.max(Math.min(this.velocity.x, this.maxSpeed), -this.maxSpeed);
- this.velocity.y = Math.max(Math.min(this.velocity.y, this.maxSpeed), -this.maxSpeed);
- var nearestSystem = game.getNearestSystem(game, this);
- if(nearestSystem != null) {
- nearestSystem.addGravity(this);
- for(var index in nearestSystem.planets) {
- var planet = nearestSystem.planets[index];
- planet.addGravity(this);
- }
- } else {
- throw new Error("Ship lost in space", ship);
- }
-
- this.velocity.x += this.acceleration.x;
- this.velocity.y += this.acceleration.y;
- if(this.touching != null) {
- var angle = game.calculateAngleTo(this.touching, this) + Math.PI;
- var dist = game.calculateDistanceTo(this.touching, this);
- var magnitude = 0;
- if(this.touching.radius + (this.width / 2) > dist) {
- this.angleRadians = angle;
- this.targetAngle = angle;
- magnitude = this.touching.radius - dist;
- var landedVector = {x: 0, y: 0};
- landedVector.x = (magnitude + (this.width / 2)) * Math.cos(angle);
- landedVector.y = (magnitude + (this.height / 2)) * Math.sin(angle);
- this.position.x += landedVector.x;
- this.position.y += landedVector.y;
- this.velocity.x = 0;
- this.velocity.y = 0;
- this.touching.claim(this);
- }
- } else {
- this.position.x += this.velocity.x * delta;
- this.position.y += this.velocity.y * delta;
- }
- if(this.position.x > 62880) {
- this.position.x = -62880;
- } else if (this.position.x < -62880) {
- this.position.x = 62880;
- }
- if(this.position.y > 62880) {
- this.position.y = -62880;
- } else if (this.position.y < -62880) {
- this.position.y = 62880;
- }
- }
- this.control = function() {
- this.controller.makeDecisions(this);
- }
- this.setSpawn = function(planet) {
- this.spawn = planet;
- }
- this.respawn = function() {
- let self = this;
- if(this.spawn == null || this.spawn.teamId != this.teamId) {
- var homePlanets = [];
- for(var index in game.planets) {
- var planet = game.planets[index];
- if(planet.teamId == this.teamId && planet.type == "habitable") {
- homePlanets.push(planet);
- }
- }
- this.setSpawn(homePlanets[parseInt(homePlanets.length * Math.random())]);
- }
- this.position.x = this.spawn.position.x;
- this.position.y = this.spawn.position.y;
- if(!this.spawn.canSpawnShip()) {
- this.spawn = null;
- setTimeout(function() {self.respawn();}, 1000);
- return;
- }
- var angle = (2 * Math.PI) * Math.random();
- this.position.x = this.spawn.position.x + (this.spawn.radius * Math.cos(angle));
- this.position.y = this.spawn.position.y + (this.spawn.radius * Math.sin(angle));
- this.health = this.maxHealth;
- this.angleRadians = angle;
- this.isAccelerating = false;
- this.isDead = false;
- this.controller.reset(this);
- this.shotDelay = 0;
- this.spawn.spawnShip();
- }
- this.die = function() {
- this.health = 0;
- if(!this.isDead) {
- this.velocity.x /= 4;
- this.velocity.y /= 4;
- //spawn debris
- for(var i = 0; i < 5; i++) {
- this.targetAngle = (2 * Math.PI) * Math.random();
- game.spawnBullet(this);
- }
- var ship = this;
- setTimeout(function() {ship.respawn();}, 3000);
- this.isDead = true;
- }
- }
- this.idle = function() {
- this.isAccelerating = false;
- }
- this.toggleAccelerate = function() {
- if(this.isAccelerating) {
- this.idle();
- } else {
- this.accelerate();
- }
- }
- this.accelerate = function() {
- this.isAccelerating = true;
- }
- this.shoot = function() {
- if(this.isDead) {
- return;
- }
- if(this.shotDelay == 0) {
- game.spawnBullet(this);
- this.shotDelay = 5;
- }
- };
- this.turnLeft = function() {
- if(this.touching != null) {
- return;
- }
- this.targetAngle -= this.turnRate;
- };
- this.turnRight = function() {
- if(this.touching != null) {
- return;
- }
- this.targetAngle += this.turnRate;
- };
- this.preRender = function(context, angle) {
- if(angle == null) {
- angle = 0;
- }
- context.fillStyle = this.shipColor;
- context.strokeStyle = this.shipColor;
- 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.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;
- }
- var truePos = {x: 0, y: 0};
- truePos.x = this.position.x;
- truePos.y = this.position.y;
- context.save();
- context.translate(truePos.x, truePos.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.lineWidth = 1;
- context.beginPath();
- context.arc(0, 0, this.width / 2, 0, 2 * Math.PI);
- context.stroke();*/
- context.restore();
- context.save();
- context.translate(truePos.x, truePos.y);
- context.font = "16px sans-serif";
- context.textAlign = "center";
- context.fillStyle = "#DDDDDD";
- context.strokeStyle = "#333333";
- context.lineWidth = 2;
- var textMetrics = context.measureText("Player " + this.id);
- var textWidth = textMetrics.width;
- context.fillText("Player " + this.id, 0, 0);
-
- context.restore();
- this.controller.debugDraw(this, context);
- }
- this.takeDamage = function(damageAmount, ownerId) {
- this.isTakingDamage = true;
- this.health -= damageAmount;
- this.lastHit = ownerId;
- }
- this.checkHit = function(ship) {
- var distance = Math.sqrt(Math.pow(ship.position.x - this.position.x, 2) + Math.pow(ship.position.y - this.position.y, 2));
- if(this.id != ship.id && ship.width / 2 >= distance) {
- //ship.takeDamage(1);
- this.takeDamage(1);
- this.lastHit = ship.id;
-
- }
- }
- };
|