|
| 1 | +package |
| 2 | +{ |
| 3 | + import flash.display.Sprite; |
| 4 | + import flash.events.ErrorEvent; |
| 5 | + import flash.events.Event; |
| 6 | + import flash.events.EventDispatcher; |
| 7 | + import flash.events.KeyboardEvent; |
| 8 | + import flash.events.MouseEvent; |
| 9 | + import flash.geom.Point; |
| 10 | + import flash.text.TextField; |
| 11 | + import flash.ui.KeyboardType; |
| 12 | + |
| 13 | + public class Game extends Sprite |
| 14 | + { |
| 15 | + //debug элементы |
| 16 | + public var score:Number = 0; |
| 17 | + public var anCounter:Number = new Number(); |
| 18 | + public var cursor:Sprite = new Sprite(); |
| 19 | + public var debugText:TextField = new TextField(); |
| 20 | + public var debugHero:GameObject = new GameObject(); |
| 21 | + public var debugBackground:Sprite = new Sprite(); |
| 22 | + //mustbe |
| 23 | + private static var _instance:Game = new Game(); |
| 24 | + public static function get Instance():Game//делаем синглетон |
| 25 | + { |
| 26 | + trace("getInstance"); |
| 27 | + return _instance; |
| 28 | + } |
| 29 | + public function Game():void |
| 30 | + { |
| 31 | + trace("Constructor"); |
| 32 | + if(_instance) |
| 33 | + throw new Error("Use Instance Field"); |
| 34 | + Initialize(); |
| 35 | + } |
| 36 | + |
| 37 | + private function Initialize():void |
| 38 | + { |
| 39 | + trace("initialization"); |
| 40 | + |
| 41 | + this.addEventListener(KeyboardEvent.KEY_DOWN,InputManager.Instance.KeyDownListener); |
| 42 | + this.addEventListener(KeyboardEvent.KEY_UP,InputManager.Instance.KeyUpListener); |
| 43 | + |
| 44 | + ResourceLoader.Instance.LoadXML("Config.xml"); |
| 45 | + ResourceLoader.Instance.LoadXML("GraphicsSet.xml"); |
| 46 | + ResourceLoader.Instance.LoadXML("Level.xml"); |
| 47 | + this.debugBackground.graphics.beginFill(0x000000); |
| 48 | + this.debugBackground.graphics.drawRect(-20,-20,640,480); |
| 49 | + this.debugText = new TextField; |
| 50 | + this.debugText.textColor = 0xFFFFFF; |
| 51 | + this.debugText.x = 0; |
| 52 | + this.debugText.y = 0; |
| 53 | + this.cursor.graphics.beginFill(0xff00ff); |
| 54 | + this.cursor.graphics.drawCircle(1,1,2); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + public function Loop(e:Event):void |
| 59 | + { |
| 60 | + this.anCounter++; |
| 61 | + trace("loop"); |
| 62 | + Render(); |
| 63 | + InputProcessing(); |
| 64 | + AIProcessing(); |
| 65 | + SceneUpdate(); |
| 66 | + Scoring(); |
| 67 | + } |
| 68 | + |
| 69 | + private function Render():void |
| 70 | + { |
| 71 | + if(anCounter % 100 == 0) |
| 72 | + GameObjectPool.Instance.AlternateBitmapData(); |
| 73 | + this.addChild(this.debugBackground); |
| 74 | + this.addChild(this.debugHero); |
| 75 | + this.addChild(this.cursor); |
| 76 | + this.addChild(this.debugText); |
| 77 | + for each(var object:GameObject in GameObjectPool.Instance.Objects) |
| 78 | + this.addChild(object); |
| 79 | + trace("Render"); |
| 80 | + } |
| 81 | + |
| 82 | + private function InputProcessing():void |
| 83 | + { |
| 84 | + this.cursor.x = this.stage.mouseX; |
| 85 | + this.cursor.y = this.stage.mouseY; |
| 86 | + trace("Input Processing"); |
| 87 | + } |
| 88 | + |
| 89 | + private function AIProcessing():void |
| 90 | + { |
| 91 | + if(anCounter % 200 == 0) |
| 92 | + for each(var obj:GameObject in GameObjectPool.Instance.Objects) |
| 93 | + if(obj.type == "alien1" || obj.type == "alien2" || obj.type == "alien0") |
| 94 | + obj.startFollowing(obj.x,obj.y+5); |
| 95 | + trace("Ai Processing"); |
| 96 | + } |
| 97 | + |
| 98 | + private function SceneUpdate():void |
| 99 | + { |
| 100 | + for each(var object:GameObject in GameObjectPool.Instance.Objects) |
| 101 | + object.updatePosition(); |
| 102 | + |
| 103 | + |
| 104 | + trace("Scene Update"); |
| 105 | + } |
| 106 | + |
| 107 | + private function Scoring():void |
| 108 | + { |
| 109 | + score ++; |
| 110 | + this.debugText.text = this.score.toString(); |
| 111 | + } |
| 112 | + |
| 113 | + private function Uninitialize():void |
| 114 | + { |
| 115 | + trace("Uninitialize"); |
| 116 | + } |
| 117 | + |
| 118 | + private function Close():void |
| 119 | + { |
| 120 | + trace("bye"); |
| 121 | + }; |
| 122 | + } |
| 123 | +} |
0 commit comments