Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import com.threerings.util.HashMap;
- import com.threerings.util.Log;
- import com.whirled.AvatarControl;
- import com.whirled.ControlEvent;
- import flash.display.DisplayObject;
- import flash.display.MovieClip;
- import flash.display.Scene;
- import flash.events.Event;
- public class ImpatientBody
- {
- public static var log:Log = Log.getLog(ImpatientBody);
- protected var _ctrl:AvatarControl;
- protected var _media:MovieClip;
- protected var _center:DisplayObject;
- protected var _mediaWidth:int;
- protected var _mediaHeight:int;
- protected var _scenes:HashMap;
- protected var _state:String;
- protected var _mode:String = "";
- protected var _playing:SceneList;
- protected var _sceneQueue:Array;
- public function ImpatientBody(param1:AvatarControl, param2:MovieClip, param3:int, param4:int = -1)
- {
- var scene:Scene = null;
- var bits:Array = null;
- var weight:int = 0;
- var number:int = 0;
- var wstr:String = null;
- var key:String = null;
- var type:String = null;
- var name:String = null;
- var list:SceneList = null;
- var cidx:int = 0;
- var mode:String = null;
- var ctrl:AvatarControl = param1;
- var media:MovieClip = param2;
- var width:int = param3;
- var height:int = param4;
- _scenes = new HashMap();
- _sceneQueue = new Array();
- super();
- _ctrl = ctrl;
- _ctrl.addEventListener(ControlEvent.APPEARANCE_CHANGED,appearanceChanged);
- _ctrl.addEventListener(ControlEvent.ACTION_TRIGGERED,function(param1:ControlEvent):void
- {
- triggerAction(param1.name);
- });
- _ctrl.addEventListener(ControlEvent.STATE_CHANGED,function(param1:ControlEvent):void
- {
- switchToState(param1.name);
- });
- _ctrl.addEventListener(Event.UNLOAD,function(param1:Event):void
- {
- shutdown();
- });
- _media = media;
- _media.addEventListener(Event.ADDED_TO_STAGE,handleAddRemove);
- _media.addEventListener(Event.REMOVED_FROM_STAGE,handleAddRemove);
- if(_media.stage != null)
- {
- _media.addEventListener(Event.ENTER_FRAME,onEnterFrame);
- }
- _mediaWidth = width;
- _mediaHeight = height;
- var states:Array = [];
- var actions:Array = [];
- for each(scene in _media.scenes)
- {
- bits = scene.name.split("_");
- if(bits.length < 2)
- {
- if(scene.name != "main")
- {
- log.warning("Invalid scene name [scene=" + scene.name + "].");
- }
- }
- else if(bits.length == 3 && String(bits[1]) == "to")
- {
- _scenes.put(scene.name.toLowerCase(),new SceneList(scene.name,scene));
- }
- else
- {
- weight = 1;
- number = 1;
- wstr = String(bits[bits.length - 1]);
- if(wstr.match("[0-9]+(:[0-9]+)"))
- {
- cidx = wstr.indexOf(":");
- if(cidx != -1)
- {
- number = int(wstr.substring(0,cidx));
- weight = int(wstr.substring(cidx + 1));
- }
- else
- {
- number = int(wstr);
- }
- bits.pop();
- }
- type = String(bits[0]);
- name = String(bits[1]);
- if(type == "action")
- {
- key = type + "_" + name;
- if(actions.indexOf(name) == -1)
- {
- actions.push(name);
- }
- }
- else if(type == "state")
- {
- if(bits.length < 3)
- {
- key = type + "_" + name;
- }
- else
- {
- mode = String(bits[2]);
- if(mode != "walking" && mode != "towalking" && mode != "fromwalking" && mode != "sleeping" && mode != "tosleeping" && mode != "fromsleeping")
- {
- log.warning("Invalid mode [scene=" + scene.name + ", mode=" + mode + "].");
- continue;
- }
- key = type + "_" + name + "_" + mode;
- }
- if(states.indexOf(name) == -1)
- {
- states.push(name);
- }
- }
- else
- {
- log.warning("Invalid type [scene=" + scene.name + "].");
- continue;
- }
- log.info("Registering scene " + key + " [weight=" + weight + ", num=" + number + "].");
- list = getScene(key);
- if(list == null)
- {
- _scenes.put(key.toLowerCase(),new SceneList(key,scene,weight));
- }
- else
- {
- list.addScene(scene,weight);
- }
- }
- }
- if(actions.length > 0)
- {
- _ctrl.registerActions(actions);
- }
- if(states.length > 1)
- {
- _ctrl.registerStates(states);
- }
- var startState:String = null;
- if(_ctrl.isConnected())
- {
- startState = _ctrl.getState();
- }
- if(startState == null)
- {
- startState = "default";
- }
- switchToState(startState);
- appearanceChanged(null);
- }
- public function switchToState(param1:String) : void
- {
- var _loc2_:SceneList = getScene("state_" + param1);
- if(_loc2_ == null)
- {
- return;
- }
- log.info("I\'m transitioning to \'" + param1 + "\'.");
- if(queueTransitions(_state,param1,true))
- {
- queueScene(_loc2_);
- }
- else
- {
- queueScene(_loc2_,true);
- }
- _state = param1;
- }
- public function triggerAction(param1:String) : void
- {
- var _loc2_:SceneList = getScene("action_" + param1);
- if(_loc2_ == null)
- {
- return;
- }
- log.info("I\'m triggering action \'" + param1 + "\'.");
- if(queueTransitions(_state,param1,true))
- {
- queueScene(_loc2_);
- }
- else
- {
- queueScene(_loc2_,true);
- }
- queueTransitions(param1,_state);
- queueScene(getScene("state_" + _state));
- }
- public function inTransition() : Boolean
- {
- return _sceneQueue.length > 0;
- }
- public function shutdown() : void
- {
- _media.removeEventListener(Event.ADDED_TO_STAGE,handleAddRemove);
- _media.removeEventListener(Event.REMOVED_FROM_STAGE,handleAddRemove);
- _media.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
- }
- protected function handleAddRemove(param1:Event) : void
- {
- if(param1.type == Event.ADDED_TO_STAGE)
- {
- _media.addEventListener(Event.ENTER_FRAME,onEnterFrame);
- onEnterFrame(param1);
- }
- else
- {
- _media.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
- }
- }
- protected function onEnterFrame(param1:Event) : void
- {
- if(_media == null || _playing == null)
- {
- return;
- }
- if(_media.currentScene.name != _playing.current.name)
- {
- if(_sceneQueue.length > 0)
- {
- _playing = _sceneQueue.shift() as SceneList;
- }
- else
- {
- _playing.updateScene();
- }
- _media.gotoAndPlay(1,_playing.current.name);
- _center = null;
- }
- if(_center == null)
- {
- _center = _media.getChildByName("center");
- if(_center == null)
- {
- _center = _media.getChildByName("ground");
- }
- if(_center != null)
- {
- _ctrl.setHotSpot(_center.x,_center.y,_mediaHeight);
- }
- }
- }
- protected function appearanceChanged(param1:ControlEvent) : void
- {
- var _loc2_:Number = _ctrl.getOrientation();
- if(_loc2_ < 180)
- {
- _media.x = _mediaWidth;
- _media.scaleX = -1;
- }
- else
- {
- _media.x = 0;
- _media.scaleX = 1;
- }
- var _loc3_:String = "";
- if(_ctrl.isMoving())
- {
- _loc3_ = "walking";
- }
- else if(_ctrl.isSleeping())
- {
- _loc3_ = "sleeping";
- }
- if(_mode == _loc3_)
- {
- return;
- }
- var _loc4_:SceneList = null;
- if(_mode == "")
- {
- _loc4_ = getScene("state_" + _state + "_to" + _loc3_);
- }
- else if(_loc3_ == "")
- {
- _loc4_ = getScene("state_" + _state + "_from" + _mode);
- }
- var _loc5_:String = "state_" + _state + (_loc3_ != ""?"_" + _loc3_:"");
- if(_loc4_ != null)
- {
- queueScene(_loc4_,true);
- queueScene(getScene(_loc5_),false);
- }
- else
- {
- queueScene(getScene(_loc5_),true);
- }
- _mode = _loc3_;
- }
- protected function queueTransitions(param1:String, param2:String, param3:Boolean = false) : Boolean
- {
- var _loc5_:SceneList = null;
- var _loc6_:SceneList = null;
- var _loc4_:SceneList = getScene(param1 + "_to_" + param2);
- if(_loc4_ != null)
- {
- queueScene(_loc4_,param3);
- return true;
- }
- _loc5_ = getScene(param1 + "_to_default");
- _loc6_ = getScene("default_to_" + param2);
- if(_loc5_ && _loc6_)
- {
- queueScene(_loc5_,param3);
- queueScene(_loc6_);
- return true;
- }
- return false;
- }
- protected function queueScene(param1:SceneList, param2:Boolean = false) : void
- {
- if(param1 == null)
- {
- return;
- }
- if(_playing == null || param2)
- {
- log.info("Switching immediately to " + param1.name + ".");
- _sceneQueue.length = 0;
- _playing = param1;
- _playing.updateScene();
- _sceneQueue.push(param1);
- }
- else
- {
- log.info("Queueing " + param1.name + ".");
- _sceneQueue.push(param1);
- }
- }
- protected function getScene(param1:String) : SceneList
- {
- return _scenes.get(param1.toLowerCase()) as SceneList;
- }
- }
- }
- import com.threerings.util.Random;
- import flash.display.Scene;
- class SceneList
- {
- public var name:String;
- protected var _curidx:int;
- protected var _scenes:Array;
- protected var _weights:Array;
- protected var _totalWeight:int = 0;
- protected var _rando:Random;
- function SceneList(param1:String, param2:Scene, param3:int = 1)
- {
- _scenes = new Array();
- _weights = new Array();
- _rando = new Random();
- super();
- this.name = param1;
- addScene(param2,param3);
- }
- public function get current() : Scene
- {
- return _scenes[_curidx] as Scene;
- }
- public function addScene(param1:Scene, param2:int = 1) : void
- {
- _scenes.push(param1);
- _weights.push(param2);
- _totalWeight = _totalWeight + param2;
- }
- public function updateScene() : void
- {
- var _loc1_:int = _rando.nextInt(_totalWeight);
- var _loc2_:int = 0;
- while(_loc2_ < _scenes.length)
- {
- if(_loc1_ < int(_weights[_loc2_]))
- {
- _curidx = _loc2_;
- return;
- }
- _loc1_ = _loc1_ - int(_weights[_loc2_]);
- _loc2_++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement