home *** CD-ROM | disk | FTP | other *** search
/ Mobiclic 149 / MOBICLIC149.ISO / pc / DATA / DSS149 / DSS149_03 / DSS149_03.swf / scripts / dss149_03 / LabyrintheBase.as < prev    next >
Text File  |  2012-11-21  |  12KB  |  360 lines

  1. package dss149_03
  2. {
  3.    import com.milanpresse.engine.Engine;
  4.    import com.milanpresse.engine.initmovieclip.InteractiveMovie;
  5.    import com.milanpresse.engine.managers.KeyManager;
  6.    import com.milanpresse.engine.managers.TimelineManager;
  7.    import com.milanpresse.engine.typeargument.NoiseObject;
  8.    import flash.display.MovieClip;
  9.    import flash.events.Event;
  10.    import flash.events.KeyboardEvent;
  11.    import flash.geom.Point;
  12.    import flash.ui.Keyboard;
  13.    
  14.    public class LabyrintheBase
  15.    {
  16.        
  17.       
  18.       private var moduleConfig:ModuleConfig;
  19.       
  20.       private var _keyManager:KeyManager;
  21.       
  22.       private var gBlockKeys:Boolean = false;
  23.       
  24.       private var gPauseOn:Boolean = false;
  25.       
  26.       private var _e:Engine;
  27.       
  28.       private var _clip:MovieClip;
  29.       
  30.       private var _timelineMngr:TimelineManager;
  31.       
  32.       private var _functionEnd:Function;
  33.       
  34.       private var gDirection:String = "";
  35.       
  36.       private var gStopMove:Boolean = true;
  37.       
  38.       private var gKeyDown:Boolean = false;
  39.       
  40.       private var clipPerso:InteractiveMovie;
  41.       
  42.       private var nomDuClipPerso:String = "PERSO";
  43.       
  44.       private var clipPersoLabelRepos:String = "E1";
  45.       
  46.       private var clipPersoLabel_D:String = "E2";
  47.       
  48.       private var clipPersoLabel_G:String = "E3";
  49.       
  50.       private var clipPersoLabel_H:String = "E4";
  51.       
  52.       private var clipPersoLabel_B:String = "E5";
  53.       
  54.       private var clipPersoLabelGagne:String = "E6";
  55.       
  56.       private var clipLabyrinthe:MovieClip;
  57.       
  58.       private var nomDuClipLabyrinthe:String = "zone_lab";
  59.       
  60.       private var nomDuClipLabyrintheFin:String = "zone_fin";
  61.       
  62.       private var nomDesClipIntrusStatique:String = "TROU";
  63.       
  64.       private var nbIntrusStatique:int = 2;
  65.       
  66.       private var gVitessePerso:int = 5;
  67.       
  68.       public function LabyrintheBase(e:Engine, mainClip:MovieClip, tlnMgr:TimelineManager)
  69.       {
  70.          super();
  71.          this._e = e;
  72.          this._clip = mainClip;
  73.          this._timelineMngr = tlnMgr;
  74.          this.clipPerso = new InteractiveMovie(this._clip[this.nomDuClipPerso],this._timelineMngr);
  75.          this.clipLabyrinthe = this._clip[this.nomDuClipLabyrinthe];
  76.          this.moduleConfig = new ModuleConfig(this._e.config.XmlConfig.Module.Config.Params);
  77.          this.moduleConfig.toString();
  78.          this.gVitessePerso = this.moduleConfig.vitessePerso;
  79.          this.clipPerso.gotoAndStop(this.clipPersoLabelRepos);
  80.       }
  81.       
  82.       public function launch(functionEnd:Function) : void
  83.       {
  84.          this._functionEnd = functionEnd;
  85.          this._keyManager = new KeyManager(this._clip,this.testeKeyDown,this.testeKeyUp);
  86.          this.gStopMove = false;
  87.       }
  88.       
  89.       private function reset() : void
  90.       {
  91.          this.clipPerso.mc.x = this.clipPerso.mc.pOrigLoc.x;
  92.          this.clipPerso.mc.y = this.clipPerso.mc.pOrigLoc.y;
  93.          this.gStopMove = false;
  94.       }
  95.       
  96.       public function forget() : void
  97.       {
  98.          this.clipPerso.mc.parent.removeChild(this.clipPerso.mc);
  99.       }
  100.       
  101.       private function appliqueDirOpposee(myMc:MovieClip) : void
  102.       {
  103.          switch(myMc.pDirection)
  104.          {
  105.             case "H":
  106.                myMc.pDirectionOpposee = "B";
  107.                break;
  108.             case "G":
  109.                myMc.pDirectionOpposee = "D";
  110.                break;
  111.             case "B":
  112.                myMc.pDirectionOpposee = "H";
  113.                break;
  114.             case "D":
  115.                myMc.pDirectionOpposee = "G";
  116.          }
  117.       }
  118.       
  119.       private function persoEnterFrame(e:Event) : void
  120.       {
  121.          var newLoc:Array = null;
  122.          var nextLocH:int = 0;
  123.          var nextLocV:int = 0;
  124.          var myMc:MovieClip = null;
  125.          var i:int = 0;
  126.          if(this.gStopMove == false)
  127.          {
  128.             newLoc = [0,0];
  129.             switch(this.gDirection)
  130.             {
  131.                case "HAUT":
  132.                   newLoc[1] -= this.gVitessePerso;
  133.                   this.clipPerso.gotoAndStop(this.clipPersoLabel_H);
  134.                   break;
  135.                case "GAUCHE":
  136.                   newLoc[0] -= this.gVitessePerso;
  137.                   this.clipPerso.gotoAndStop(this.clipPersoLabel_G);
  138.                   break;
  139.                case "BAS":
  140.                   newLoc[1] += this.gVitessePerso;
  141.                   this.clipPerso.gotoAndStop(this.clipPersoLabel_B);
  142.                   break;
  143.                case "DROITE":
  144.                   newLoc[0] += this.gVitessePerso;
  145.                   this.clipPerso.gotoAndStop(this.clipPersoLabel_D);
  146.             }
  147.             nextLocH = this.clipPerso.mc.x + newLoc[0];
  148.             nextLocV = this.clipPerso.mc.y + newLoc[1];
  149.             if(this.testeHIT(this.clipPerso.mc,newLoc).touched == true)
  150.             {
  151.                trace("testeHIT(clipPerso.mc, newLoc).x",this.testeHIT(this.clipPerso.mc,newLoc).x);
  152.                trace("testeHIT(clipPerso.mc, newLoc).y",this.testeHIT(this.clipPerso.mc,newLoc).y);
  153.                this.clipPerso.gotoAndStop(this.clipPersoLabelRepos);
  154.             }
  155.             else
  156.             {
  157.                this.clipPerso.mc.x = nextLocH;
  158.                this.clipPerso.mc.y = nextLocV;
  159.                this.clipPerso.mc.bounds_obj = this.clipPerso.mc.getBounds(this._clip);
  160.                if(this.clipPerso.mc.hitTestObject(this._clip[this.nomDuClipLabyrintheFin]))
  161.                {
  162.                   trace("gagn├⌐ !!!");
  163.                   this._keyManager.killKeyManager();
  164.                   this.gKeyDown = false;
  165.                   this.clipPerso.gotoAndStop(this.clipPersoLabelGagne);
  166.                   this.clipPerso.mc.removeEventListener(Event.ENTER_FRAME,this.persoEnterFrame);
  167.                   this.gDirection = "";
  168.                   this._e.playNoise(new NoiseObject({"code":"B_GAGNE"}));
  169.                   this._functionEnd();
  170.                }
  171.                else
  172.                {
  173.                   for(i = 1; i <= this.nbIntrusStatique; i++)
  174.                   {
  175.                      myMc = this._clip[this.nomDesClipIntrusStatique + "_" + i];
  176.                      if(myMc.hitTestObject(this.clipPerso.mc.HIT))
  177.                      {
  178.                         this.gStopMove = true;
  179.                         this.clipPerso.gotoAndPlayUntilTheEnd("E6");
  180.                         this.clipPerso.mc.x = myMc.x + myMc.width / 2;
  181.                         this.clipPerso.mc.y = myMc.y + myMc.height / 2;
  182.                         this.gDirection = "";
  183.                         this._e.playNoise(new NoiseObject({
  184.                            "code":"B_TOMBE",
  185.                            "callback":this.finCHUTE
  186.                         }));
  187.                         break;
  188.                      }
  189.                   }
  190.                }
  191.             }
  192.          }
  193.       }
  194.       
  195.       private function finCHUTE() : void
  196.       {
  197.          this._e.playNoise(new NoiseObject({"code":"B_PERDU"}));
  198.          this.reset();
  199.       }
  200.       
  201.       private function testeHIT(myMc:MovieClip, newLoc:Array) : Object
  202.       {
  203.          var minX:int = 0;
  204.          var maxX:int = 0;
  205.          var minY:int = 0;
  206.          var maxY:int = 0;
  207.          var x:int = 0;
  208.          var y:int = 0;
  209.          var i:int = 0;
  210.          var j:int = 0;
  211.          myMc.bounds_objHIT = myMc.HIT.getBounds(this._clip);
  212.          switch(this.gDirection)
  213.          {
  214.             case "HAUT":
  215.                minY = myMc.bounds_objHIT.top - this.gVitessePerso;
  216.                maxY = myMc.bounds_objHIT.top;
  217.                minX = myMc.bounds_objHIT.left;
  218.                maxX = myMc.bounds_objHIT.right;
  219.                break;
  220.             case "GAUCHE":
  221.                minY = myMc.bounds_objHIT.top;
  222.                maxY = myMc.bounds_objHIT.bottom;
  223.                minX = myMc.bounds_objHIT.left - this.gVitessePerso;
  224.                maxX = myMc.bounds_objHIT.left;
  225.                break;
  226.             case "BAS":
  227.                minY = myMc.bounds_objHIT.bottom;
  228.                maxY = myMc.bounds_objHIT.bottom + this.gVitessePerso;
  229.                minX = myMc.bounds_objHIT.left;
  230.                maxX = myMc.bounds_objHIT.right;
  231.                break;
  232.             case "DROITE":
  233.                minY = myMc.bounds_objHIT.top;
  234.                maxY = myMc.bounds_objHIT.bottom;
  235.                minX = myMc.bounds_objHIT.right;
  236.                maxX = myMc.bounds_objHIT.right + this.gVitessePerso;
  237.          }
  238.          var touched:Boolean = false;
  239.          if(newLoc[0] !== 0)
  240.          {
  241.             for(i = minY; i <= maxY; i++)
  242.             {
  243.                for(j = minX; j <= maxX; j++)
  244.                {
  245.                   if(myMc.pCadre.hitTestPoint(j,i,true) == false)
  246.                   {
  247.                      x = j - 1;
  248.                      trace("├ºa touche plus x",x);
  249.                      touched = true;
  250.                      break;
  251.                   }
  252.                }
  253.             }
  254.          }
  255.          else
  256.          {
  257.             for(i = minX; i <= maxX; i++)
  258.             {
  259.                for(j = minY; j <= maxY; j++)
  260.                {
  261.                   if(myMc.pCadre.hitTestPoint(i,j,true) == false)
  262.                   {
  263.                      y = i - 1;
  264.                      trace("├ºa touche plus y",y);
  265.                      touched = true;
  266.                      break;
  267.                   }
  268.                }
  269.             }
  270.          }
  271.          return {
  272.             "touched":touched,
  273.             "x":x,
  274.             "y":y
  275.          };
  276.       }
  277.       
  278.       private function testeKeyDown(e:*) : void
  279.       {
  280.          var key:uint = 0;
  281.          if(this.gStopMove == false)
  282.          {
  283.             if(this.gPauseOn == false)
  284.             {
  285.                key = e.keyCode;
  286.                switch(key)
  287.                {
  288.                   case Keyboard.UP:
  289.                      trace("HAUT");
  290.                      this.gDirection = "HAUT";
  291.                      break;
  292.                   case Keyboard.LEFT:
  293.                      trace("GAUCHE");
  294.                      this.gDirection = "GAUCHE";
  295.                      break;
  296.                   case Keyboard.RIGHT:
  297.                      trace("DROITE");
  298.                      this.gDirection = "DROITE";
  299.                      break;
  300.                   case Keyboard.DOWN:
  301.                      trace("BAS");
  302.                      this.gDirection = "BAS";
  303.                }
  304.                if(this.gDirection !== "")
  305.                {
  306.                   if(this.clipPerso.mc.pOrigLoc == undefined)
  307.                   {
  308.                      this.clipPerso.mc.pOrigLoc = new Point(this.clipPerso.mc.x,this.clipPerso.mc.y);
  309.                      this.clipPerso.mc.pCadre = this.clipLabyrinthe;
  310.                   }
  311.                   this.clipPerso.mc.addEventListener(Event.ENTER_FRAME,this.persoEnterFrame);
  312.                }
  313.             }
  314.          }
  315.       }
  316.       
  317.       private function testeKeyUp(e:KeyboardEvent) : void
  318.       {
  319.          if(this.gStopMove == false)
  320.          {
  321.             if(this._keyManager.isKeyPressed(Keyboard.UP))
  322.             {
  323.                this.gDirection = "HAUT";
  324.             }
  325.             else if(this._keyManager.isKeyPressed(Keyboard.LEFT))
  326.             {
  327.                this.gDirection = "GAUCHE";
  328.             }
  329.             else if(this._keyManager.isKeyPressed(Keyboard.DOWN))
  330.             {
  331.                this.gDirection = "BAS";
  332.             }
  333.             else if(this._keyManager.isKeyPressed(Keyboard.RIGHT))
  334.             {
  335.                this.gDirection = "DROITE";
  336.             }
  337.             else
  338.             {
  339.                this.gKeyDown = false;
  340.                this.clipPerso.gotoAndStop(this.clipPersoLabelRepos);
  341.                this.clipPerso.mc.removeEventListener(Event.ENTER_FRAME,this.persoEnterFrame);
  342.                this.gDirection = "";
  343.             }
  344.          }
  345.       }
  346.       
  347.       public function sleepLab() : void
  348.       {
  349.          this.gStopMove = true;
  350.          this.gPauseOn = true;
  351.       }
  352.       
  353.       public function wakeLab() : void
  354.       {
  355.          this.gStopMove = false;
  356.          this.gPauseOn = false;
  357.       }
  358.    }
  359. }
  360.