Advertisement
Guest User

InstrumentApplierAspect

a guest
Mar 28th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  © 2017 nexters.com
  4. //
  5. ////////////////////////////////////////////////////////////////////////////////
  6.  
  7. package ru.nexters.ie2.view.scene.aspects {
  8.     import flash.utils.Dictionary;
  9.    
  10.     import protobuf.Dictionary_;
  11.    
  12.     import ru.nexters.ie2.controller.aspects.AspectControllerBase;
  13.     import ru.nexters.ie2.model.process.ProcessCheckResult;
  14.     import ru.nexters.ie2.model.process.ProcessGraph;
  15.     import ru.nexters.ie2.model.process.ProcessWrapper;
  16.     import ru.nexters.ie2.view.components.drag.InstrumentData;
  17.     import ru.nexters.ie2.view.scene.Entity;
  18.    
  19.     /**
  20.      * @author                  v.proskurin
  21.      * @version                 1.0
  22.      * @playerversion           Flash 10
  23.      * @langversion             3.0
  24.      * @date                    Mar 27, 2017
  25.      */
  26.     public class InstrumentApplierAspect extends AspectControllerBase {
  27.        
  28.         //------------------------------------------------------------------------------
  29.         //  Class constants
  30.         //------------------------------------------------------------------------------
  31.        
  32.         //------------------------------------------------------------------------------
  33.         //  Class variables
  34.         //------------------------------------------------------------------------------
  35.        
  36.         //------------------------------------------------------------------------------
  37.         //  Class methods
  38.         //------------------------------------------------------------------------------
  39.        
  40.         //--------------------------------------------------------------------------
  41.         //  Constructor
  42.         //--------------------------------------------------------------------------
  43.        
  44.         public function InstrumentApplierAspect() {
  45.             super();
  46.         }
  47.        
  48.         //--------------------------------------------------------------------------
  49.         //  Variables
  50.         //--------------------------------------------------------------------------
  51.        
  52.         //--------------------------------------------------------------------------
  53.         //  Properties
  54.         //--------------------------------------------------------------------------
  55.        
  56.         //--------------------------------------------------------------------------
  57.         //  Public methods
  58.         //--------------------------------------------------------------------------
  59.        
  60.         override public function init(target:Entity):void {
  61.             super.init(target);
  62.            
  63.             _target.signals.onApplyInstrument.add(apply);
  64.         }
  65.        
  66.         override public function dispose():void{
  67.             _target.signals.onApplyInstrument.remove(apply);
  68.         }
  69.        
  70.        
  71.         //--------------------------------------------------------------------------
  72.         //  Protected methods
  73.         //--------------------------------------------------------------------------
  74.        
  75.         //--------------------------------------------------------------------------
  76.         //  Private methods
  77.         //--------------------------------------------------------------------------
  78.        
  79.         private function apply(instrument:InstrumentData):void {
  80.             switch (instrument.type) {
  81.                 case InstrumentData.SETUP:
  82.                     addProcess(instrument);
  83.                 break;
  84.                 case InstrumentData.COLLECT:
  85.                     removeProcesses(instrument);
  86.                 break;
  87.             }
  88.         }
  89.        
  90.         private function addProcess(instrument:InstrumentData):void {
  91.             const context:ProcessGraph = ProcessGraph.instance;
  92.            
  93.             const processId:Dictionary_ = instrument.data;
  94.             const pw:ProcessWrapper = ProcessWrapper.fromProtoAndObjects(processId.prototype.as32,[_target.source]);
  95.             const result:ProcessCheckResult = pw.canBeAdded(context);
  96.             if (result.isOk) {
  97.                 context.addProcess(pw);
  98.                 instrument.dispatch(true);
  99.             } else if (result.error == ProcessCheckResult.REQS_NOT_SATISFIED) {
  100.                 instrument.dispatch(false);
  101.             }
  102.         }
  103.        
  104.         private function removeProcesses(instrument:InstrumentData):void {
  105.             const context:ProcessGraph = ProcessGraph.instance;
  106.             const processes:Dictionary = _target.source.allProcesses;
  107.            
  108.             var status:Boolean;
  109.             for each (var process:ProcessWrapper in processes) {
  110.                 const result:ProcessCheckResult = process.canBeRemoved(context, false);
  111.                 if (result.isOk) {
  112.                     _target.scene.source.sceneEvents.dropCreated.dispatch(process.drop, _target.position);
  113.                     context.requestRemove(process);
  114.                     status = true;
  115.                 }
  116.             }
  117.            
  118.             instrument.dispatch(status);
  119.         }
  120.        
  121.         //--------------------------------------------------------------------------
  122.         //  Event handlers
  123.         //--------------------------------------------------------------------------
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement