(function() { "use strict"; var Creature = function(scene, spec, position) { this._id = tine.newId(); this.scene = scene; this.creature = true; this.position = position; this.imageId = spec.image; this.team = spec.team; this.life = spec.life; this.totalLife = spec.life; this.attack = spec.attack; this.attackRate = 1/spec.attack_rate; // rate attacks/second this.awareDistance = spec.aware_distance this.attackDistance = spec.attack_distance this.speed = spec.speed; this.height = spec.height; this.width = spec.width; this.order = 'none'; //'attack', 'defend' this.role = 'guardian';//'soldier' this.memory = new b3.Blackboard(); this.attackTime = this.attackRate; this.attachedTo = null; this.memory.set('boring', null); this.memory.set('target', null); this.memory.set('position', null); this.memory.set('nearestEnemy', null); this.memory.set('nearestEnemyDistance', null); this._draw(); } var p = Creature.prototype // -------------------------------------------------------------------------- // INTERNAL // -------------------------------------------------------------------------- p._draw = function() { var radius = this.scene.planet.radius; var color = (this.team==='player'?'#FCD265':'#D86060'); var movearea = (this.team==='player'? game.data.random_move_area_player: game.data.random_move_area_enemy); if (this.imageId) { this.display = game.create.bitmap(this.imageId, { regX:'center', regY: radius+this.height+4 }); } else { this.display = new createjs.Shape(); this.display.regX = 0; this.display.regY = radius+this.height; this.display.graphics .f(color) .r(-this.width/2, 0, this.width, this.height) .ef() // DEBUG .s('green') .dc(0, this.height, this.awareDistance) .s('red') .dc(0, this.height, this.attackDistance) .s('lightgrey') .dc(0, this.height, movearea) .es() } this.display.rotation = this.position; } p._updateMemory = function() { var circumference = this.scene.planet.circumference; // Structures and Creatures of the other team; var structures = this.scene.enemyStructures; var creatures = this.scene.enemyCreatures; if (this.team === 'enemy') { structures = this.scene.playerStructures; creatures = this.scene.playerCreatures; } var nearestEnemy = null; var nearestDist = 100000000000; // Verify nearest creature for (var i=0; i 0) { var dist = distance(this.position, c.position, circumference); if (dist < nearestDist) { nearestDist = dist; nearestEnemy = c; } } } // Verify nearest structure for (var i=0; i 0 && c.status === 'ground') { var dist = distance(this.position, c.position, circumference); if (dist < nearestDist) { nearestDist = dist; nearestEnemy = c; } } } this.memory.set('nearestEnemy', nearestEnemy); this.memory.set('nearestEnemyDistance', nearestDist); // update boring var boring = tine.clip(this.memory.get('boring')+game.time.fdelta, 0, 100); this.memory.set('boring', boring); } // -------------------------------------------------------------------------- // INTERFACE // -------------------------------------------------------------------------- p.goTo = function(position) { this.targetPosition = position; } p.move = function(direction) { if (direction > 0) { this.display.scaleX = -1; } else { this.display.scaleX = 1; } var position = this.position + this.speed*game.time.fdelta*direction; position = (position<0)? 360+position%360 : position%360; this.position = position; this.display.rotation = position; } p.update = function() { if (this.life <= 0) return; this._updateMemory(); game.ai.unit.tick(this, this.memory); // if (this._attack()) return; // if (this._move()) return; } // -------------------------------------------------------------------------- // 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.addKillEffect(this.position, this); createjs.Tween.get(this.display) .to({alpha:0}, 500) .call(function() { self.scene.removeCreature(self); }); return true; } if (this.status !== 'attacking') { this.status = 'attacking'; this.attackTargetPosition = creature.position; } } window.Creature = Creature; })();