import flash.events.Event;
import flash.events.MouseEvent;
import flash.desktop.NativeApplication;
import flash.desktop.SystemIdleMode;
import flash.system.Capabilities;
import flash.utils.getTimer;
import audio.*;
const numRealOptions:int = 4;
const numFakeOptions:int = 3;
const minDistance:Number = 90;
var timeRemaining:Number=0;
var secElapsed:Number;
var lT:Number;
var score:Number;
var fakeMovementPhase:int = 0;
var realMovementPhase:int = 0;
var numReal:int = 0;
var numFake:int = 0;
var activeReal:Array = new Array();
var activeFake:Array = new Array();
var spawnDelay:Number=0;
var activeToasts:Array = new Array();
stage.addEventListener(Event.RESIZE, resized);
init();
stop();
function init():void {
resized(null);
SFX.initSystem();
cmdExit.addEventListener(MouseEvent.CLICK, kill);
cmdGo.addEventListener(MouseEvent.CLICK, startGame);
}
function startGame(e:MouseEvent):void {
cmdGo.visible = false;
lT = getTimer();
timeRemaining = 120;
score = 0;
killSpots();
spawnDelay = 0;
addEventListener(Event.ENTER_FRAME, processGame);
}
function killSpots():void {
for(var s:int=0; s<activeReal.length; s++) {
if(activeReal[s].parent!=null) activeReal[s].parent.removeChild(activeReal[s]);
activeReal[s].removeEventListener(MouseEvent.MOUSE_DOWN, realClicked);
activeReal[s] = null;
}
activeReal.length = 0;
for(s=0; s<activeFake.length; s++) {
if(activeFake[s].parent!=null) activeFake[s].parent.removeChild(activeFake[s]);
activeFake[s].removeEventListener(MouseEvent.MOUSE_DOWN, fakeClicked);
activeFake[s] = null;
}
activeFake.length = 0;
for(s=0; s<activeToasts.length; s++) {
if(activeToasts[s].parent!=null) activeToasts[s].parent.removeChild(activeToasts[s]);
activeToasts[s] = null;
}
activeToasts.length = 0;
}
function realClicked(e:MouseEvent):void {
SFX.play("good");
MovieClip(e.target).parent.removeChild(MovieClip(e.target));
score+=1+(1-e.target.alpha);
score += Math.abs(e.target.x-stage.stageWidth/2)/stage.stageWidth/4;
score += Math.abs(e.target.y-stage.stageHeight/2)/stage.stageHeight/4;
var newToast:Toast = new Toast();
newToast.alpha = 0.5;
newToast.x = stage.mouseX;
newToast.y = stage.mouseY;
newToast.mouseEnabled = false;
newToast.gotoAndStop(1);
stage.addChild(newToast);
activeToasts.push(newToast);
}
function fakeClicked(e:MouseEvent):void {
SFX.play("bad");
MovieClip(e.target).parent.removeChild(MovieClip(e.target));
score-=2;
if(score<0) {
score = 0;
}
var newToast:Toast = new Toast();
newToast.alpha = 0.5;
newToast.x = stage.mouseX;
newToast.y = stage.mouseY;
newToast.mouseEnabled = false;
newToast.gotoAndStop(2);
stage.addChild(newToast);
activeToasts.push(newToast);
}
function processGame(e:Event):void {
secElapsed = (getTimer()-lT)/1000;
lT = getTimer();
timeRemaining -= secElapsed;
if(timeRemaining<=0) {
timeRemaining = 0;
cmdGo.visible = true;
killSpots();
removeEventListener(Event.ENTER_FRAME, processGame);
}
txtTime.text = "TIME: "+Math.ceil(timeRemaining);
txtScore.text = "SCORE: "+Math.ceil(score*100)/10;
if(timeRemaining>0) {
if(spawnDelay<=0) {
processSpawning();
} else {
spawnDelay-=secElapsed;
}
processSpots();
}
}
function processSpawning():void {
if(score>100) {
numReal = 1;
numFake = 10;
fakeMovementPhase = 4;
realMovementPhase = 3;
} else if(score>80) {
numReal = 1;
numFake = 10;
fakeMovementPhase = 3;
realMovementPhase = 3;
} else if(score>60) {
numReal = 1;
numFake = 10;
fakeMovementPhase = 2;
realMovementPhase = 3;
} else if(score>40) {
numReal = 1;
numFake = 10;
fakeMovementPhase = 2;
realMovementPhase = 2;
} else if(score>30) {
numReal = 2;
numFake = 8;
fakeMovementPhase = 1;
realMovementPhase = 2;
} else if(score>20) {
numReal = 2;
numFake = 5;
fakeMovementPhase = 1;
realMovementPhase = 1;
} else if(score>10) {
numReal = 2;
numFake = 3;
fakeMovementPhase = 0;
realMovementPhase = 1;
} else {
numReal = 2;
numFake = 1;
fakeMovementPhase = 0;
realMovementPhase = 0;
}
for(var s:int=0; s<activeReal.length; s++) {
activeReal[s].life -= secElapsed;
if(activeReal[s].life<=0) {
activeReal[s].parent.removeChild(activeReal[s]);
}
if(activeReal[s].parent == null) {
activeReal[s].removeEventListener(MouseEvent.MOUSE_DOWN, realClicked);
activeReal[s] = null;
activeReal.splice(s,1); s--;
}
}
for(s=0; s<activeFake.length; s++) {
activeFake[s].life -= secElapsed;
if(activeFake[s].life<=0) {
activeFake[s].parent.removeChild(activeFake[s]);
}
if(activeFake[s].parent == null) {
activeFake[s].removeEventListener(MouseEvent.MOUSE_DOWN, fakeClicked);
activeFake[s] = null;
activeFake.splice(s,1); s--;
}
}
var attempts:int = 0;
if(activeReal.length<numReal) {
var newReal:GaborSpot = new GaborSpot();
newReal.x = 100 + Math.random()*(stage.stageWidth-200);
newReal.y = 100 + Math.random()*(stage.stageHeight-200);
while(collisionCheck(newReal.x,newReal.y) && attempts<10) {
attempts++;
}
if(attempts==10) {
newReal = null;
} else {
newReal.gotoAndStop(1+Math.floor(Math.random()*numRealOptions));
newReal.addEventListener(MouseEvent.MOUSE_DOWN, realClicked);
newReal.alpha = 0;
newReal.scaleX = newReal.scaleY = 1;
newReal.rotation = Math.random()*360;
newReal.life = 4+Math.random()*3;
stage.addChild(newReal);
activeReal.push(newReal);
spawnDelay = 0.5 + Math.random()*realMovementPhase;
}
}
if(activeFake.length<numFake) {
var newFake:FakeGabor = new FakeGabor();
newFake.x = 100 + Math.random()*(stage.stageWidth-200);
newFake.y = 100 + Math.random()*(stage.stageHeight-200);
while(collisionCheck(newFake.x,newFake.y) && attempts<10) {
attempts++;
}
if(attempts==10) {
newFake = null;
} else {
newFake.gotoAndStop(1+Math.floor(Math.random()*numFakeOptions));
newFake.addEventListener(MouseEvent.MOUSE_DOWN, fakeClicked);
newFake.alpha = 0;
newFake.scaleX = newFake.scaleY = 1;
newFake.rotation = Math.random()*360;
newFake.life = 4+Math.random()*3;
stage.addChild(newFake);
activeFake.push(newFake);
spawnDelay = 0.5 + Math.random()*realMovementPhase;
}
}
}
function collisionCheck(xx:Number, yy:Number):Boolean {
if(Math.abs(stage.stageWidth/2 - xx)<minDistance) {
if(Math.abs(stage.stageHeight/2 - yy)<minDistance) {
return true;
}
}
for(var s:int=0; s<activeReal.length; s++) {
if(activeReal[s].parent != null) {
if(Math.abs(activeReal[s].x - xx)<minDistance) {
if(Math.abs(activeReal[s].y - yy)<minDistance) {
return true;
}
}
}
}
for(s=0; s<activeFake.length; s++) {
if(activeFake[s].parent != null) {
if(Math.abs(activeFake[s].x - xx)<minDistance) {
if(Math.abs(activeFake[s].y - yy)<minDistance) {
return true;
}
}
}
}
return false;
}
function gravity(val):Number {
if(secElapsed==0) return val;
return val * limit((1/60)/secElapsed,1/(val*1.1));
}
function limit(num:Number, to:Number):Number {
if(num>to) {
num = to;
}
return num;
}
function processSpots():void {
var stageScale:Number = ((stage.stageWidth/800)+(stage.stageHeight/480))/2;
for(var s:int=0; s<activeReal.length; s++) {
if(activeReal[s].parent != null) {
switch(realMovementPhase) {
case 3:
activeReal[s].alpha += (0.2-activeReal[s].alpha) * gravity(0.05);
activeReal[s].scaleX = activeReal[s].scaleY = 0.7 * stageScale;
break;
case 2:
activeReal[s].alpha += (0.5-activeReal[s].alpha) * gravity(0.05);
activeReal[s].scaleX = activeReal[s].scaleY = .8 * stageScale;
break;
case 1:
activeReal[s].alpha += (0.6-activeReal[s].alpha) * gravity(0.1);
activeReal[s].scaleX = activeReal[s].scaleY = 0.9 * stageScale;
break;
case 0:
activeReal[s].alpha += (1-activeReal[s].alpha) * gravity(0.5);
activeReal[s].scaleX = activeReal[s].scaleY = 1 * stageScale;
break;
}
}
}
for(s=0; s<activeFake.length; s++) {
if(activeFake[s].parent != null) {
switch(fakeMovementPhase) {
case 4:
activeFake[s].alpha += (0.8+Math.random()*.2-activeFake[s].alpha) * gravity(0.2);
activeFake[s].scaleX = activeFake[s].scaleY = 0.8 * stageScale;
activeFake[s].x += Math.cos(activeFake[s].rotation) * secElapsed*2;
activeFake[s].y += Math.sin(activeFake[s].rotation) * secElapsed*2;
activeFake[s].rotation += -0.1 + Math.random()*0.2;
if(activeFake[s].x < -100) {
activeFake[s].x += stage.stageWidth+200;
}
if(activeFake[s].x > stage.stageWidth+100) {
activeFake[s].x -= stage.stageWidth+200;
}
if(activeFake[s].y < -100) {
activeFake[s].y += stage.stageHeight+200;
}
if(activeFake[s].y > stage.stageHeight+100) {
activeFake[s].y -= stage.stageHeight+200;
}
case 3:
activeFake[s].alpha += (0.8+Math.random()*.2-activeFake[s].alpha) * gravity(0.2);
activeFake[s].scaleX = activeFake[s].scaleY = 0.8 * stageScale;
activeFake[s].x += Math.cos(activeFake[s].rotation) * secElapsed*2;
activeFake[s].y += Math.sin(activeFake[s].rotation) * secElapsed*2;
if(activeFake[s].x < -100) {
activeFake[s].x += stage.stageWidth+200;
}
if(activeFake[s].x > stage.stageWidth+100) {
activeFake[s].x -= stage.stageWidth+200;
}
if(activeFake[s].y < -100) {
activeFake[s].y += stage.stageHeight+200;
}
if(activeFake[s].y > stage.stageHeight+100) {
activeFake[s].y -= stage.stageHeight+200;
}
break;
case 2:
activeFake[s].alpha += (0.8+Math.random()*.2-activeFake[s].alpha) * gravity(0.2);
activeFake[s].scaleX = activeFake[s].scaleY = 0.8 * stageScale;
break;
case 1:
activeFake[s].alpha += (0.9-activeFake[s].alpha) * gravity(0.1);
activeFake[s].scaleX = activeFake[s].scaleY = 0.7 * stageScale;
break;
case 0:
activeFake[s].alpha += (1-activeFake[s].alpha) * gravity(0.05);
activeFake[s].scaleX = activeFake[s].scaleY = 0.7 * stageScale;
break;
}
}
}
for(s=0; s<activeToasts.length; s++) {
activeToasts[s].alpha -= secElapsed;
activeToasts[s].scaleX = activeToasts[s].scaleY = stageScale;
if(activeToasts[s].alpha<=0) {
activeToasts[s].parent.removeChild(activeToasts[s]);
activeToasts[s] = null;
activeToasts.splice(s,1);
s--;
}
}
}
function kill(e:MouseEvent):void {
NativeApplication.nativeApplication.exit();
}
function resized(e:Event):void {
focusDot.x = cmdGo.x = stage.stageWidth/2;
focusDot.y = cmdGo.y = stage.stageHeight/2;
cmdExit.x = 0;
cmdExit.y = stage.stageHeight;
}