package world.items.behaviors { import flash.display.Sprite; import flash.geom.Point; import com.senocular.utils.Range; import world.*; import world.items.*; public class Hunter extends Wonderer { protected var lifespan:int; protected var lifespanPeriod:Range = new Range(300, 1000); protected var gestationLimit:int = 1000; protected var gestation:int = gestationLimit; protected var gestationIncrement:Range = new Range(100, 400); public function Hunter(owner:Dweller = null) { super(owner); lifespan = lifespanPeriod.getRandom(); } public override function step():void { super.step(); if (owner && owner.environment) { live(); reproduce(); } } protected function live():void { lifespan--; if (lifespan < 0) { owner.environment.removeItem(owner); } } protected function reproduce():void { gestation = (gestation < gestationLimit) ? gestation + 1 : gestationLimit; if (gestation < 0) { gestation = gestationLimit; var dest:Point = new Point(); var currLoc:Point = owner.location; var dir:Number = owner.angle + Math.PI; dest.x = currLoc.x + Math.cos(dir)*owner.size; dest.y = currLoc.y + Math.sin(dir)*owner.size; dest = owner.environment.validateLocation(dest); if (dest) { var baby:Dweller = owner.environment.addItem(this.owner.clone()); if (baby) { baby.location = dest; baby.angle = dir; gestation = gestationLimit; } } } } protected override function move():void { if (_isWalking) { var dest:Point = new Point(); var currLoc:Point = owner.location; if (_isTurning || _isStuck) { owner.angle = owner.angle + owner.turnSpeed; } dest.x = currLoc.x + Math.cos(owner.angle)*owner.walkSpeed; dest.y = currLoc.y + Math.sin(owner.angle)*owner.walkSpeed; dest = owner.environment.validateLocation(dest, owner); if (dest){ owner.location = dest; isStuck = false; }else{ var prey:Dweller = owner.environment.getLastCollision() as SheepDweller; if (prey && prey.behavior is Grazer) { var preyLoc:Point = prey.location; owner.angle = owner.environment.angleBetween(currLoc, preyLoc); owner.setBehavior(new Attack(prey, this)); prey.setBehavior(new Tremble()); lifespan = lifespanPeriod.getRandom(); gestation -= gestationIncrement.getRandom(); }else{ isStuck = true; } } } } } }