(function() { "use strict"; var Structure = function(scene, spec, position) { this._id = tine.newId(); this.scene = scene; this.display = null; this.structure = true; // i'm structure this.position = position; this.imageId = spec.image; this.team = spec.team; this.cost = spec.cost; this.minDistance = spec.min_distance; this.dropTime = spec.drop_time; this.life = spec.life; this.width = spec.width; this.height = spec.height; this.extractionRate = 1/spec.extraction_rate; // rate resources/second this.marines = spec.marines; this.elites = spec.elites; this.zerglingRate = 1/spec.zergling_rate; this.hydraliskRate = 1/spec.hydralisk_rate; this.maxGuardians = spec.max_guardians; this.status = 'spawning'; // ground this.lastAttacker = null; this.creatures = []; this.guardians = []; this.defenders = []; this.attackers = []; this.idleGuardians = []; this.idleSoldiers = []; this.memory = new b3.Blackboard(); this.shouldDestroy = !!(spec.destroy_in); this.destroyTime = spec.destroy_in; this.extractionTime = 0; this.zerglingTime = 0; this.hydraliskTime = 0; this._draw(); this._drop(); } var p = Structure.prototype // -------------------------------------------------------------------------- // INTERNAL // -------------------------------------------------------------------------- // Draw object p._draw = function() { var color = (this.team==='player')?'#FC9C65':'#130B07'; if (this.imageId) { this.display = game.create.bitmap(this.imageId, { regX:'center', regY:game.canvas.height }); } else { this.display = new createjs.Shape(); this.display.regX = 0; this.display.regY = game.canvas.height; this.display.graphics .f(color) .r(-this.width/2, 0, this.width, this.height) .ef() // DEBUG // .s('grey') // .dc(0, this.height, this.minDistance) } var position = this.position||0; this.position = (position<0)? 360+position%360 : position%360; this.display.rotation = position; } // Drop the structure p._drop = function() { var radius = this.scene.planet.radius; var self = this; var shape = game.create.shape(); shape.graphics .f('#BEBDBD') .de(-this.width/2, -this.height/4, this.width, this.height/4); // .dc(0, 0, this.width); shape.regY = radius-5; shape.rotation = this.position; this.scene.addObject(shape, 'planetfg'); createjs.Tween.get(this.display) .to({regY: (radius+this.height)}, this.dropTime, createjs.Ease.cubicIn) .call(function() { self._finishDrop(); }); if (this.dropTime > 0 && this.team === 'player') { game.sound.playSfx('fall'); } createjs.Tween.get(shape) .to({alpha: 0}, this.dropTime*0.9, createjs.Ease.linear) .call(function() { self.scene.removeObject(shape); }); } // Spawn marine (if any) and realocate it p._finishDrop = function() { if (this.dropTime > 0 && this.team === 'player') { this.scene.humanPlayed = true; this.scene.addLandingEffect(this.position, this); } this.status = 'ground'; var r = this.scene.planet.radius; var c = this.scene.planet.circumference; var ratio = 360/c; var spawn_area = Math.max(this.width, game.data.spawn_area)*ratio; var spawn_move_area = Math.max(this.width, game.data.spawn_move_area)*ratio; for (var i=0; i= this.scene.maxEnemies) return; } var r = this.scene.planet.radius; var c = this.scene.planet.circumference; var ratio = 360/c; var spawn_area = Math.max(this.width, game.data.spawn_area)*ratio; var spawn_move_area = Math.max(this.width, game.data.spawn_move_area)*ratio; if (!pos1) { pos1 = this.position + Math.random()*spawn_area*2 - spawn_area; } var pos2 = this.position + Math.random()*spawn_move_area*2 - spawn_move_area; var c = this.scene.addCreature(type, pos1); if (this.guardians.length >= this.maxGuardians || role==='soldier' || Math.random() > this.scene.guardianChance) { c.role = 'soldier'; } else { this.guardians.push(c); c.attachedTo = this; } this.creatures.push(c); c.display.alpha = 0; createjs.Tween.get(c.display).to({alpha:1}, game.data.zerg_spawn_time); c.goTo(pos2); } // -------------------------------------------------------------------------- // ENTITY INTERACTION // -------------------------------------------------------------------------- p.$damage = function(damage, creature) { if (this.life <= 0) return; this.scene.addAttackEffect(this.position, damage); this.life -= damage; if (this.life <= 0) { var self = this; this.scene.addDestroyEffect(this.position); createjs.Tween.get(this.display) .to({alpha:0}, 500) .call(function() { self.scene.removeStructure(self); }); return true; } this.lastAttacker = creature; return false; } p.update = function() { // Auto destroy update if (this.shouldDestroy && this.life > 0) { this.destroyTime -= game.time.fdelta; if (this.destroyTime < 0) { this.life = 0; var self = this; createjs.Tween.get(this.display) .to({alpha:0}, 500) .call(function() { self.scene.removeStructure(self); }); } } if (this.life <= 0 || this.status==='spawning') return; // Zerling Spawn if (this.zerglingRate) { this.zerglingTime += game.time.fdelta; while (this.zerglingTime >= this.zerglingRate) { this._spawnZerg('zergling', this.position); this.zerglingTime -= this.zerglingRate; } } // Hydralisk Spawn if (this.hydraliskRate) { this.hydraliskTime += game.time.fdelta; while (this.hydraliskTime >= this.hydraliskRate) { this._spawnZerg('hydralisk', this.position); this.hydraliskTime -= this.hydraliskRate; } } // Extraction if (this.extractionRate) { this.extractionTime += game.time.fdelta; while (this.extractionTime >= this.extractionRate) { this.extractionTime -= this.extractionRate; if (this.scene.planet.resources === 0) break; this.scene.player.resources += 1; this.scene.player.totalResources += 1; this.scene.planet.resources -= 1; } } // Update view and IA if (this.team === 'enemy') { this.defenders = []; this.attackers = []; this.idleSoldiers = []; this.idleGuardians = []; for (var i=0; i