package world.items.behaviors { import flash.display.Sprite; import flash.geom.Point; import world.*; import world.items.*; public class Wonderer extends Behavior { protected var decideKeepTurn:Number = .8; protected var decideKeepStraight:Number = .9; protected var decideWalks:Number = .98; protected var decideStands:Number = .97; protected var _isWalking:Boolean = false; protected var _isTurning:Boolean = false; protected var _isStuck:Boolean = false; public function get isWalking():Boolean { return _isWalking; } public function set isWalking(b:Boolean):void { if (_isWalking != b) { _isWalking = b; updateAnimation(); } } public function get isTurning():Boolean { return _isTurning; } public function set isTurning(b:Boolean):void { if (_isTurning != b) { _isTurning = b; } } public function get isStuck():Boolean { return _isStuck; } public function set isStuck(b:Boolean):void { if (_isStuck != b) { _isStuck = b; } } public function Wonderer(owner:Dweller = null) { super(owner); } public override function step():void { if (owner && owner.environment) { decisions(); move(); } } public override function updateAnimation():void { if (owner) { owner.setSkeletonAnimation(_isWalking ? Dweller.WALK_ANIM : Dweller.STAND_ANIM); } } protected 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{ isStuck = true; } } } protected function decisions():void { if (_isWalking) { if (Math.random() > decideWalks) { isWalking = false; }else{ if (_isTurning) { if (Math.random() > decideKeepTurn) { isTurning = false; } }else{ if (Math.random() > decideKeepStraight) { isTurning = true; owner.turnSpeed *= (Math.random() < .5) ? 1 : -1; } } } }else{ if (Math.random() > decideStands) { isWalking = true; } } } } }