package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; import flash.geom.Rectangle; import flash.utils.getTimer; import creatures.*; import hud.*; import world.*; import world.items.*; public class CreatureApp extends Sprite { private var environment:Environment; private var mouseLoc:Point = new Point(); private var hudPanel:HudPanel; private var hudTime:Number = getTimer(); public function CreatureApp() { environment = new Environment(); environment.bounds = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight*2); hudPanel = new HudPanel(); hudPanel.x = stage.stageWidth/2 - hudPanel.width/2; hudPanel.y = stage.stageHeight - 60; addChild(environment); addChild(hudPanel); stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown); stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp); addEventListener(Event.ENTER_FRAME, enterFrame); environment.addEventListener(Event.CHANGE, enviroChange); hudPanel.addEventListener(Event.INIT, reset); reset(); } private function enterFrame(evt:Event):void { updateHUDTimer(); } private function reset(evt:Event = null):void { init(); hudPanel.resetCount(); updateHUDCount(); hudTime = getTimer(); updateHUDTimer(); } private function init():void { environment.clearItems(); var dweller:Dweller; dweller = new SheepDweller(environment.getRandomValidLocation()); dweller.angle = Math.random()*Math.PI*2; environment.addItem(dweller); dweller = new SheepDweller(environment.getRandomValidLocation()); dweller.angle = Math.random()*Math.PI*2; environment.addItem(dweller); dweller = new ChupaovejaDweller(environment.getRandomValidLocation()); dweller.angle = Math.random()*Math.PI*2; environment.addItem(dweller); } private function mouseDown(evt:MouseEvent):void { stage.addEventListener(MouseEvent.MOUSE_MOVE, dragEnvironment); mouseLoc = new Point(stage.mouseX, stage.mouseY); } private function mouseUp(evt:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragEnvironment); } private function dragEnvironment(evt:MouseEvent):void { var change:Point = new Point(stage.mouseX - mouseLoc.x, (stage.mouseY - mouseLoc.y)*2); environment.location = environment.location.add(change); mouseLoc = new Point(stage.mouseX, stage.mouseY); } private function enviroChange(evt:Event):void { updateHUDCount(); } private function updateHUDCount():void { var items:Array = environment.items; var chipCount:int = 0; var sheepCount:int = 0; var item:Dweller; for each (item in items) { if (item is ChupaovejaDweller) { chipCount++; }else if (item is SheepDweller) { sheepCount++; } } hudPanel.setCount("chip", chipCount); hudPanel.setCount("sheep", sheepCount); } private function updateHUDTimer():void { if (environment.items.length) { var diff:Number = getTimer() - hudTime; hudPanel.setTime(diff); } } } }