Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.    import com.threerings.util.HashMap;
  4.    import com.threerings.util.Log;
  5.    import com.whirled.AvatarControl;
  6.    import com.whirled.ControlEvent;
  7.    import flash.display.DisplayObject;
  8.    import flash.display.MovieClip;
  9.    import flash.display.Scene;
  10.    import flash.events.Event;
  11.    
  12.    public class ImpatientBody
  13.    {
  14.      
  15.       public static var log:Log = Log.getLog(ImpatientBody);
  16.        
  17.      
  18.       protected var _ctrl:AvatarControl;
  19.      
  20.       protected var _media:MovieClip;
  21.      
  22.       protected var _center:DisplayObject;
  23.      
  24.       protected var _mediaWidth:int;
  25.      
  26.       protected var _mediaHeight:int;
  27.      
  28.       protected var _scenes:HashMap;
  29.      
  30.       protected var _state:String;
  31.      
  32.       protected var _mode:String = "";
  33.      
  34.       protected var _playing:SceneList;
  35.      
  36.       protected var _sceneQueue:Array;
  37.      
  38.       public function ImpatientBody(param1:AvatarControl, param2:MovieClip, param3:int, param4:int = -1)
  39.       {
  40.          var scene:Scene = null;
  41.          var bits:Array = null;
  42.          var weight:int = 0;
  43.          var number:int = 0;
  44.          var wstr:String = null;
  45.          var key:String = null;
  46.          var type:String = null;
  47.          var name:String = null;
  48.          var list:SceneList = null;
  49.          var cidx:int = 0;
  50.          var mode:String = null;
  51.          var ctrl:AvatarControl = param1;
  52.          var media:MovieClip = param2;
  53.          var width:int = param3;
  54.          var height:int = param4;
  55.          _scenes = new HashMap();
  56.          _sceneQueue = new Array();
  57.          super();
  58.          _ctrl = ctrl;
  59.          _ctrl.addEventListener(ControlEvent.APPEARANCE_CHANGED,appearanceChanged);
  60.          _ctrl.addEventListener(ControlEvent.ACTION_TRIGGERED,function(param1:ControlEvent):void
  61.          {
  62.             triggerAction(param1.name);
  63.          });
  64.          _ctrl.addEventListener(ControlEvent.STATE_CHANGED,function(param1:ControlEvent):void
  65.          {
  66.             switchToState(param1.name);
  67.          });
  68.          _ctrl.addEventListener(Event.UNLOAD,function(param1:Event):void
  69.          {
  70.             shutdown();
  71.          });
  72.          _media = media;
  73.          _media.addEventListener(Event.ADDED_TO_STAGE,handleAddRemove);
  74.          _media.addEventListener(Event.REMOVED_FROM_STAGE,handleAddRemove);
  75.          if(_media.stage != null)
  76.          {
  77.             _media.addEventListener(Event.ENTER_FRAME,onEnterFrame);
  78.          }
  79.          _mediaWidth = width;
  80.          _mediaHeight = height;
  81.          var states:Array = [];
  82.          var actions:Array = [];
  83.          for each(scene in _media.scenes)
  84.          {
  85.             bits = scene.name.split("_");
  86.             if(bits.length < 2)
  87.             {
  88.                if(scene.name != "main")
  89.                {
  90.                   log.warning("Invalid scene name [scene=" + scene.name + "].");
  91.                }
  92.             }
  93.             else if(bits.length == 3 && String(bits[1]) == "to")
  94.             {
  95.                _scenes.put(scene.name.toLowerCase(),new SceneList(scene.name,scene));
  96.             }
  97.             else
  98.             {
  99.                weight = 1;
  100.                number = 1;
  101.                wstr = String(bits[bits.length - 1]);
  102.                if(wstr.match("[0-9]+(:[0-9]+)"))
  103.                {
  104.                   cidx = wstr.indexOf(":");
  105.                   if(cidx != -1)
  106.                   {
  107.                      number = int(wstr.substring(0,cidx));
  108.                      weight = int(wstr.substring(cidx + 1));
  109.                   }
  110.                   else
  111.                   {
  112.                      number = int(wstr);
  113.                   }
  114.                   bits.pop();
  115.                }
  116.                type = String(bits[0]);
  117.                name = String(bits[1]);
  118.                if(type == "action")
  119.                {
  120.                   key = type + "_" + name;
  121.                   if(actions.indexOf(name) == -1)
  122.                   {
  123.                      actions.push(name);
  124.                   }
  125.                }
  126.                else if(type == "state")
  127.                {
  128.                   if(bits.length < 3)
  129.                   {
  130.                      key = type + "_" + name;
  131.                   }
  132.                   else
  133.                   {
  134.                      mode = String(bits[2]);
  135.                      if(mode != "walking" && mode != "towalking" && mode != "fromwalking" && mode != "sleeping" && mode != "tosleeping" && mode != "fromsleeping")
  136.                      {
  137.                         log.warning("Invalid mode [scene=" + scene.name + ", mode=" + mode + "].");
  138.                         continue;
  139.                      }
  140.                      key = type + "_" + name + "_" + mode;
  141.                   }
  142.                   if(states.indexOf(name) == -1)
  143.                   {
  144.                      states.push(name);
  145.                   }
  146.                }
  147.                else
  148.                {
  149.                   log.warning("Invalid type [scene=" + scene.name + "].");
  150.                   continue;
  151.                }
  152.                log.info("Registering scene " + key + " [weight=" + weight + ", num=" + number + "].");
  153.                list = getScene(key);
  154.                if(list == null)
  155.                {
  156.                   _scenes.put(key.toLowerCase(),new SceneList(key,scene,weight));
  157.                }
  158.                else
  159.                {
  160.                   list.addScene(scene,weight);
  161.                }
  162.             }
  163.          }
  164.          if(actions.length > 0)
  165.          {
  166.             _ctrl.registerActions(actions);
  167.          }
  168.          if(states.length > 1)
  169.          {
  170.             _ctrl.registerStates(states);
  171.          }
  172.          var startState:String = null;
  173.          if(_ctrl.isConnected())
  174.          {
  175.             startState = _ctrl.getState();
  176.          }
  177.          if(startState == null)
  178.          {
  179.             startState = "default";
  180.          }
  181.          switchToState(startState);
  182.          appearanceChanged(null);
  183.       }
  184.      
  185.       public function switchToState(param1:String) : void
  186.       {
  187.          var _loc2_:SceneList = getScene("state_" + param1);
  188.          if(_loc2_ == null)
  189.          {
  190.             return;
  191.          }
  192.          log.info("I\'m transitioning to \'" + param1 + "\'.");
  193.          if(queueTransitions(_state,param1,true))
  194.          {
  195.             queueScene(_loc2_);
  196.          }
  197.          else
  198.          {
  199.             queueScene(_loc2_,true);
  200.          }
  201.          _state = param1;
  202.       }
  203.      
  204.       public function triggerAction(param1:String) : void
  205.       {
  206.          var _loc2_:SceneList = getScene("action_" + param1);
  207.          if(_loc2_ == null)
  208.          {
  209.             return;
  210.          }
  211.          log.info("I\'m triggering action \'" + param1 + "\'.");
  212.          if(queueTransitions(_state,param1,true))
  213.          {
  214.             queueScene(_loc2_);
  215.          }
  216.          else
  217.          {
  218.             queueScene(_loc2_,true);
  219.          }
  220.          queueTransitions(param1,_state);
  221.          queueScene(getScene("state_" + _state));
  222.       }
  223.      
  224.       public function inTransition() : Boolean
  225.       {
  226.          return _sceneQueue.length > 0;
  227.       }
  228.      
  229.       public function shutdown() : void
  230.       {
  231.          _media.removeEventListener(Event.ADDED_TO_STAGE,handleAddRemove);
  232.          _media.removeEventListener(Event.REMOVED_FROM_STAGE,handleAddRemove);
  233.          _media.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
  234.       }
  235.      
  236.       protected function handleAddRemove(param1:Event) : void
  237.       {
  238.          if(param1.type == Event.ADDED_TO_STAGE)
  239.          {
  240.             _media.addEventListener(Event.ENTER_FRAME,onEnterFrame);
  241.             onEnterFrame(param1);
  242.          }
  243.          else
  244.          {
  245.             _media.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
  246.          }
  247.       }
  248.      
  249.       protected function onEnterFrame(param1:Event) : void
  250.       {
  251.          if(_media == null || _playing == null)
  252.          {
  253.             return;
  254.          }
  255.          if(_media.currentScene.name != _playing.current.name)
  256.          {
  257.             if(_sceneQueue.length > 0)
  258.             {
  259.                _playing = _sceneQueue.shift() as SceneList;
  260.             }
  261.             else
  262.             {
  263.                _playing.updateScene();
  264.             }
  265.             _media.gotoAndPlay(1,_playing.current.name);
  266.             _center = null;
  267.          }
  268.          if(_center == null)
  269.          {
  270.             _center = _media.getChildByName("center");
  271.             if(_center == null)
  272.             {
  273.                _center = _media.getChildByName("ground");
  274.             }
  275.             if(_center != null)
  276.             {
  277.                _ctrl.setHotSpot(_center.x,_center.y,_mediaHeight);
  278.             }
  279.          }
  280.       }
  281.      
  282.       protected function appearanceChanged(param1:ControlEvent) : void
  283.       {
  284.          var _loc2_:Number = _ctrl.getOrientation();
  285.          if(_loc2_ < 180)
  286.          {
  287.             _media.x = _mediaWidth;
  288.             _media.scaleX = -1;
  289.          }
  290.          else
  291.          {
  292.             _media.x = 0;
  293.             _media.scaleX = 1;
  294.          }
  295.          var _loc3_:String = "";
  296.          if(_ctrl.isMoving())
  297.          {
  298.             _loc3_ = "walking";
  299.          }
  300.          else if(_ctrl.isSleeping())
  301.          {
  302.             _loc3_ = "sleeping";
  303.          }
  304.          if(_mode == _loc3_)
  305.          {
  306.             return;
  307.          }
  308.          var _loc4_:SceneList = null;
  309.          if(_mode == "")
  310.          {
  311.             _loc4_ = getScene("state_" + _state + "_to" + _loc3_);
  312.          }
  313.          else if(_loc3_ == "")
  314.          {
  315.             _loc4_ = getScene("state_" + _state + "_from" + _mode);
  316.          }
  317.          var _loc5_:String = "state_" + _state + (_loc3_ != ""?"_" + _loc3_:"");
  318.          if(_loc4_ != null)
  319.          {
  320.             queueScene(_loc4_,true);
  321.             queueScene(getScene(_loc5_),false);
  322.          }
  323.          else
  324.          {
  325.             queueScene(getScene(_loc5_),true);
  326.          }
  327.          _mode = _loc3_;
  328.       }
  329.      
  330.       protected function queueTransitions(param1:String, param2:String, param3:Boolean = false) : Boolean
  331.       {
  332.          var _loc5_:SceneList = null;
  333.          var _loc6_:SceneList = null;
  334.          var _loc4_:SceneList = getScene(param1 + "_to_" + param2);
  335.          if(_loc4_ != null)
  336.          {
  337.             queueScene(_loc4_,param3);
  338.             return true;
  339.          }
  340.          _loc5_ = getScene(param1 + "_to_default");
  341.          _loc6_ = getScene("default_to_" + param2);
  342.          if(_loc5_ && _loc6_)
  343.          {
  344.             queueScene(_loc5_,param3);
  345.             queueScene(_loc6_);
  346.             return true;
  347.          }
  348.          return false;
  349.       }
  350.      
  351.       protected function queueScene(param1:SceneList, param2:Boolean = false) : void
  352.       {
  353.          if(param1 == null)
  354.          {
  355.             return;
  356.          }
  357.          if(_playing == null || param2)
  358.          {
  359.             log.info("Switching immediately to " + param1.name + ".");
  360.             _sceneQueue.length = 0;
  361.             _playing = param1;
  362.             _playing.updateScene();
  363.             _sceneQueue.push(param1);
  364.          }
  365.          else
  366.          {
  367.             log.info("Queueing " + param1.name + ".");
  368.             _sceneQueue.push(param1);
  369.          }
  370.       }
  371.      
  372.       protected function getScene(param1:String) : SceneList
  373.       {
  374.          return _scenes.get(param1.toLowerCase()) as SceneList;
  375.       }
  376.    }
  377. }
  378.  
  379. import com.threerings.util.Random;
  380. import flash.display.Scene;
  381.  
  382. class SceneList
  383. {
  384.    
  385.    
  386.    public var name:String;
  387.    
  388.    protected var _curidx:int;
  389.    
  390.    protected var _scenes:Array;
  391.    
  392.    protected var _weights:Array;
  393.    
  394.    protected var _totalWeight:int = 0;
  395.    
  396.    protected var _rando:Random;
  397.    
  398.    function SceneList(param1:String, param2:Scene, param3:int = 1)
  399.    {
  400.       _scenes = new Array();
  401.       _weights = new Array();
  402.       _rando = new Random();
  403.       super();
  404.       this.name = param1;
  405.       addScene(param2,param3);
  406.    }
  407.    
  408.    public function get current() : Scene
  409.    {
  410.       return _scenes[_curidx] as Scene;
  411.    }
  412.    
  413.    public function addScene(param1:Scene, param2:int = 1) : void
  414.    {
  415.       _scenes.push(param1);
  416.       _weights.push(param2);
  417.       _totalWeight = _totalWeight + param2;
  418.    }
  419.    
  420.    public function updateScene() : void
  421.    {
  422.       var _loc1_:int = _rando.nextInt(_totalWeight);
  423.       var _loc2_:int = 0;
  424.       while(_loc2_ < _scenes.length)
  425.       {
  426.          if(_loc1_ < int(_weights[_loc2_]))
  427.          {
  428.             _curidx = _loc2_;
  429.             return;
  430.          }
  431.          _loc1_ = _loc1_ - int(_weights[_loc2_]);
  432.          _loc2_++;
  433.       }
  434.    }
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement