1. import flash.events.Event;
  2. import flash.events.MouseEvent;
  3. import flash.desktop.NativeApplication;
  4. import flash.desktop.SystemIdleMode;
  5. import flash.system.Capabilities;
  6. import flash.utils.getTimer;
  7. import audio.*;
  8.  
  9. const numRealOptions:int = 4;
  10. const numFakeOptions:int = 3;
  11. const minDistance:Number = 90;
  12. var timeRemaining:Number=0;
  13. var secElapsed:Number;
  14. var lT:Number;
  15. var score:Number;
  16. var fakeMovementPhase:int = 0;
  17. var realMovementPhase:int = 0;
  18. var numReal:int = 0;
  19. var numFake:int = 0;
  20. var activeReal:Array = new Array();
  21. var activeFake:Array = new Array();
  22. var spawnDelay:Number=0;
  23. var activeToasts:Array = new Array();
  24.  
  25. stage.addEventListener(Event.RESIZE, resized);
  26.  
  27. init();
  28. stop();
  29. function init():void {
  30.     resized(null);
  31.     SFX.initSystem();
  32.     cmdExit.addEventListener(MouseEvent.CLICK, kill);
  33.     cmdGo.addEventListener(MouseEvent.CLICK, startGame);
  34. }
  35.  
  36. function startGame(e:MouseEvent):void {
  37.     cmdGo.visible = false;
  38.     lT = getTimer();
  39.     timeRemaining = 120;
  40.     score = 0;
  41.     killSpots();
  42.     spawnDelay = 0;
  43.     addEventListener(Event.ENTER_FRAME, processGame);
  44. }
  45.  
  46. function killSpots():void {
  47.     for(var s:int=0; s<activeReal.length; s++) {
  48.         if(activeReal[s].parent!=null) activeReal[s].parent.removeChild(activeReal[s]);
  49.         activeReal[s].removeEventListener(MouseEvent.MOUSE_DOWN, realClicked);
  50.         activeReal[s] = null;
  51.     }
  52.     activeReal.length = 0;
  53.     for(s=0; s<activeFake.length; s++) {
  54.         if(activeFake[s].parent!=null) activeFake[s].parent.removeChild(activeFake[s]);
  55.         activeFake[s].removeEventListener(MouseEvent.MOUSE_DOWN, fakeClicked);
  56.         activeFake[s] = null;
  57.     }
  58.     activeFake.length = 0;
  59.     for(s=0; s<activeToasts.length; s++) {
  60.         if(activeToasts[s].parent!=null) activeToasts[s].parent.removeChild(activeToasts[s]);
  61.         activeToasts[s] = null;
  62.     }
  63.     activeToasts.length = 0;
  64. }
  65.  
  66. function realClicked(e:MouseEvent):void {
  67.     SFX.play("good");
  68.     MovieClip(e.target).parent.removeChild(MovieClip(e.target));
  69.     score+=1+(1-e.target.alpha);
  70.     score += Math.abs(e.target.x-stage.stageWidth/2)/stage.stageWidth/4;
  71.     score += Math.abs(e.target.y-stage.stageHeight/2)/stage.stageHeight/4;
  72.     var newToast:Toast = new Toast();
  73.     newToast.alpha = 0.5;
  74.     newToast.x = stage.mouseX;
  75.     newToast.y = stage.mouseY;
  76.     newToast.mouseEnabled = false;
  77.     newToast.gotoAndStop(1);
  78.     stage.addChild(newToast);
  79.     activeToasts.push(newToast);
  80. }
  81.  
  82. function fakeClicked(e:MouseEvent):void {
  83.     SFX.play("bad");
  84.     MovieClip(e.target).parent.removeChild(MovieClip(e.target));
  85.     score-=2;
  86.     if(score<0) {
  87.         score = 0;
  88.     }
  89.     var newToast:Toast = new Toast();
  90.     newToast.alpha = 0.5;
  91.     newToast.x = stage.mouseX;
  92.     newToast.y = stage.mouseY;
  93.     newToast.mouseEnabled = false;
  94.     newToast.gotoAndStop(2);
  95.     stage.addChild(newToast);
  96.     activeToasts.push(newToast);
  97. }
  98.  
  99. function processGame(e:Event):void {
  100.     secElapsed = (getTimer()-lT)/1000;
  101.     lT = getTimer();
  102.     timeRemaining -= secElapsed;
  103.     if(timeRemaining<=0) {
  104.         timeRemaining = 0;
  105.         cmdGo.visible = true;
  106.         killSpots();
  107.         removeEventListener(Event.ENTER_FRAME, processGame);
  108.     }
  109.     txtTime.text = "TIME: "+Math.ceil(timeRemaining);
  110.     txtScore.text = "SCORE: "+Math.ceil(score*100)/10;
  111.     if(timeRemaining>0) {
  112.         if(spawnDelay<=0) {
  113.             processSpawning();
  114.         } else {
  115.             spawnDelay-=secElapsed;
  116.         }
  117.         processSpots();
  118.     }
  119. }
  120.  
  121. function processSpawning():void {
  122.     if(score>100) {
  123.         numReal = 1;
  124.         numFake = 10;
  125.         fakeMovementPhase = 4;
  126.         realMovementPhase = 3;
  127.     } else if(score>80) {
  128.         numReal = 1;
  129.         numFake = 10;
  130.         fakeMovementPhase = 3;
  131.         realMovementPhase = 3;
  132.     } else if(score>60) {
  133.         numReal = 1;
  134.         numFake = 10;
  135.         fakeMovementPhase = 2;
  136.         realMovementPhase = 3;
  137.     } else if(score>40) {
  138.         numReal = 1;
  139.         numFake = 10;
  140.         fakeMovementPhase = 2;
  141.         realMovementPhase = 2;
  142.     } else if(score>30) {
  143.         numReal = 2;
  144.         numFake = 8;
  145.         fakeMovementPhase = 1;
  146.         realMovementPhase = 2;
  147.     } else if(score>20) {
  148.         numReal = 2;
  149.         numFake = 5;
  150.         fakeMovementPhase = 1;
  151.         realMovementPhase = 1;
  152.     } else if(score>10) {
  153.         numReal = 2;
  154.         numFake = 3;
  155.         fakeMovementPhase = 0;
  156.         realMovementPhase = 1;
  157.     } else {
  158.         numReal = 2;
  159.         numFake = 1;
  160.         fakeMovementPhase = 0;
  161.         realMovementPhase = 0;
  162.     }
  163.     for(var s:int=0; s<activeReal.length; s++) {
  164.         activeReal[s].life -= secElapsed;
  165.         if(activeReal[s].life<=0) {
  166.             activeReal[s].parent.removeChild(activeReal[s]);
  167.         }
  168.         if(activeReal[s].parent == null) {
  169.             activeReal[s].removeEventListener(MouseEvent.MOUSE_DOWN, realClicked);
  170.             activeReal[s] = null;
  171.             activeReal.splice(s,1); s--;
  172.         }
  173.     }
  174.     for(s=0; s<activeFake.length; s++) {
  175.         activeFake[s].life -= secElapsed;
  176.         if(activeFake[s].life<=0) {
  177.             activeFake[s].parent.removeChild(activeFake[s]);
  178.         }
  179.         if(activeFake[s].parent == null) {
  180.             activeFake[s].removeEventListener(MouseEvent.MOUSE_DOWN, fakeClicked);
  181.             activeFake[s] = null;
  182.             activeFake.splice(s,1); s--;
  183.         }
  184.     }
  185.     var attempts:int = 0;
  186.     if(activeReal.length<numReal) {
  187.         var newReal:GaborSpot = new GaborSpot();
  188.         newReal.x = 100 + Math.random()*(stage.stageWidth-200);
  189.         newReal.y = 100 + Math.random()*(stage.stageHeight-200);
  190.         while(collisionCheck(newReal.x,newReal.y) && attempts<10) {
  191.             attempts++;
  192.         }
  193.         if(attempts==10) {
  194.             newReal = null;
  195.         } else {
  196.             newReal.gotoAndStop(1+Math.floor(Math.random()*numRealOptions));
  197.             newReal.addEventListener(MouseEvent.MOUSE_DOWN, realClicked);
  198.             newReal.alpha = 0;
  199.             newReal.scaleX = newReal.scaleY = 1;
  200.             newReal.rotation = Math.random()*360;
  201.             newReal.life = 4+Math.random()*3;
  202.             stage.addChild(newReal);
  203.             activeReal.push(newReal);
  204.             spawnDelay = 0.5 + Math.random()*realMovementPhase;
  205.         }
  206.     }
  207.     if(activeFake.length<numFake) {
  208.         var newFake:FakeGabor = new FakeGabor();
  209.         newFake.x = 100 + Math.random()*(stage.stageWidth-200);
  210.         newFake.y = 100 + Math.random()*(stage.stageHeight-200);
  211.         while(collisionCheck(newFake.x,newFake.y) && attempts<10) {
  212.             attempts++;
  213.         }
  214.         if(attempts==10) {
  215.             newFake = null;
  216.         } else {
  217.             newFake.gotoAndStop(1+Math.floor(Math.random()*numFakeOptions));
  218.             newFake.addEventListener(MouseEvent.MOUSE_DOWN, fakeClicked);
  219.             newFake.alpha = 0;
  220.             newFake.scaleX = newFake.scaleY = 1;
  221.             newFake.rotation = Math.random()*360;
  222.             newFake.life = 4+Math.random()*3;
  223.             stage.addChild(newFake);
  224.             activeFake.push(newFake);
  225.             spawnDelay = 0.5 + Math.random()*realMovementPhase;
  226.         }
  227.     }
  228. }
  229.  
  230. function collisionCheck(xx:Number, yy:Number):Boolean {
  231.     if(Math.abs(stage.stageWidth/2 - xx)<minDistance) {
  232.         if(Math.abs(stage.stageHeight/2 - yy)<minDistance) {
  233.             return true;
  234.         }
  235.     }
  236.     for(var s:int=0; s<activeReal.length; s++) {
  237.         if(activeReal[s].parent != null) {
  238.             if(Math.abs(activeReal[s].x - xx)<minDistance) {
  239.                 if(Math.abs(activeReal[s].y - yy)<minDistance) {
  240.                     return true;
  241.                 }
  242.             }
  243.         }
  244.     }
  245.     for(s=0; s<activeFake.length; s++) {
  246.         if(activeFake[s].parent != null) {
  247.             if(Math.abs(activeFake[s].x - xx)<minDistance) {
  248.                 if(Math.abs(activeFake[s].y - yy)<minDistance) {
  249.                     return true;
  250.                 }
  251.             }
  252.         }
  253.     }
  254.     return false;
  255. }
  256.  
  257. function gravity(val):Number {
  258.     if(secElapsed==0) return val;
  259.     return val * limit((1/60)/secElapsed,1/(val*1.1));
  260. }
  261.  
  262. function limit(num:Number, to:Number):Number {
  263.     if(num>to) {
  264.         num = to;
  265.     }
  266.     return num;
  267. }
  268.  
  269. function processSpots():void {
  270.     var stageScale:Number = ((stage.stageWidth/800)+(stage.stageHeight/480))/2;
  271.     for(var s:int=0; s<activeReal.length; s++) {
  272.         if(activeReal[s].parent != null) {
  273.             switch(realMovementPhase) {
  274.                 case 3:
  275.                     activeReal[s].alpha += (0.2-activeReal[s].alpha) * gravity(0.05);
  276.                     activeReal[s].scaleX = activeReal[s].scaleY = 0.7 * stageScale;
  277.                     break;
  278.                 case 2:
  279.                     activeReal[s].alpha += (0.5-activeReal[s].alpha) * gravity(0.05);
  280.                     activeReal[s].scaleX = activeReal[s].scaleY = .8 * stageScale;
  281.                     break;
  282.                 case 1:
  283.                     activeReal[s].alpha += (0.6-activeReal[s].alpha) * gravity(0.1);
  284.                     activeReal[s].scaleX = activeReal[s].scaleY = 0.9 * stageScale;
  285.                     break;
  286.                 case 0:
  287.                     activeReal[s].alpha += (1-activeReal[s].alpha) * gravity(0.5);
  288.                     activeReal[s].scaleX = activeReal[s].scaleY = 1 * stageScale;
  289.                     break;
  290.             }
  291.         }
  292.     }
  293.     for(s=0; s<activeFake.length; s++) {
  294.         if(activeFake[s].parent != null) {
  295.             switch(fakeMovementPhase) {
  296.                 case 4:
  297.                     activeFake[s].alpha += (0.8+Math.random()*.2-activeFake[s].alpha) * gravity(0.2);
  298.                     activeFake[s].scaleX = activeFake[s].scaleY = 0.8 * stageScale;
  299.                     activeFake[s].x += Math.cos(activeFake[s].rotation) * secElapsed*2;
  300.                     activeFake[s].y += Math.sin(activeFake[s].rotation) * secElapsed*2;
  301.                     activeFake[s].rotation += -0.1 + Math.random()*0.2;
  302.                     if(activeFake[s].x < -100) {
  303.                         activeFake[s].x += stage.stageWidth+200;
  304.                     }
  305.                     if(activeFake[s].x > stage.stageWidth+100) {
  306.                         activeFake[s].x -= stage.stageWidth+200;
  307.                     }
  308.                     if(activeFake[s].y < -100) {
  309.                         activeFake[s].y += stage.stageHeight+200;
  310.                     }
  311.                     if(activeFake[s].y > stage.stageHeight+100) {
  312.                         activeFake[s].y -= stage.stageHeight+200;
  313.                     }
  314.                 case 3:
  315.                     activeFake[s].alpha += (0.8+Math.random()*.2-activeFake[s].alpha) * gravity(0.2);
  316.                     activeFake[s].scaleX = activeFake[s].scaleY = 0.8 * stageScale;
  317.                     activeFake[s].x += Math.cos(activeFake[s].rotation) * secElapsed*2;
  318.                     activeFake[s].y += Math.sin(activeFake[s].rotation) * secElapsed*2;
  319.                     if(activeFake[s].x < -100) {
  320.                         activeFake[s].x += stage.stageWidth+200;
  321.                     }
  322.                     if(activeFake[s].x > stage.stageWidth+100) {
  323.                         activeFake[s].x -= stage.stageWidth+200;
  324.                     }
  325.                     if(activeFake[s].y < -100) {
  326.                         activeFake[s].y += stage.stageHeight+200;
  327.                     }
  328.                     if(activeFake[s].y > stage.stageHeight+100) {
  329.                         activeFake[s].y -= stage.stageHeight+200;
  330.                     }
  331.                     break;
  332.                 case 2:
  333.                     activeFake[s].alpha += (0.8+Math.random()*.2-activeFake[s].alpha) * gravity(0.2);
  334.                     activeFake[s].scaleX = activeFake[s].scaleY = 0.8 * stageScale;
  335.                     break;
  336.                 case 1:
  337.                     activeFake[s].alpha += (0.9-activeFake[s].alpha) * gravity(0.1);
  338.                     activeFake[s].scaleX = activeFake[s].scaleY = 0.7 * stageScale;
  339.                     break;
  340.                 case 0:
  341.                     activeFake[s].alpha += (1-activeFake[s].alpha) * gravity(0.05);
  342.                     activeFake[s].scaleX = activeFake[s].scaleY = 0.7 * stageScale;
  343.                     break;
  344.             }
  345.         }
  346.     }
  347.     for(s=0; s<activeToasts.length; s++) {
  348.         activeToasts[s].alpha -= secElapsed;
  349.         activeToasts[s].scaleX = activeToasts[s].scaleY = stageScale;
  350.         if(activeToasts[s].alpha<=0) {
  351.             activeToasts[s].parent.removeChild(activeToasts[s]);
  352.             activeToasts[s] = null;
  353.             activeToasts.splice(s,1);
  354.             s--;
  355.         }
  356.     }
  357. }
  358.  
  359. function kill(e:MouseEvent):void {
  360.     NativeApplication.nativeApplication.exit();
  361. }
  362.  
  363. function resized(e:Event):void {
  364.     focusDot.x = cmdGo.x = stage.stageWidth/2;
  365.     focusDot.y = cmdGo.y = stage.stageHeight/2;
  366.     cmdExit.x = 0;
  367.     cmdExit.y = stage.stageHeight;
  368. }