Skip to content

Commit ad0e779

Browse files
root
1 parent 4b80350 commit ad0e779

28 files changed

+916
-0
lines changed

Config.xml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Config>
2+
<Title>Lera suicide</Title>
3+
<Width>640</Width>
4+
<Height>480</Height>
5+
<FrameRate>100</FrameRate>
6+
</Config>

Game.as

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}

GameGraphics.as

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package
2+
{
3+
import flash.display.Bitmap;
4+
import flash.display.BitmapData;
5+
6+
public class GameGraphics
7+
{
8+
public var type:String = new String;
9+
public var bitmap:Bitmap = new Bitmap;
10+
public function GameGraphics()
11+
{}
12+
}
13+
}

GameObject.as

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package
2+
{
3+
import flash.display.Sprite;
4+
import flash.events.Event;
5+
6+
public class GameObject extends Sprite
7+
{
8+
public var isDead:Boolean = false;
9+
public var isRemoved:Boolean = false;
10+
public var type:String = new String;
11+
private var targetX:Number = 0;
12+
private var targetY:Number = 0;
13+
14+
public function GameObject()
15+
{
16+
targetX = this.x;
17+
}
18+
19+
public function startFollowing(_x:int,_y:int):void
20+
{
21+
targetX = _x;
22+
targetY = _y;
23+
}
24+
25+
public function follow():void
26+
{
27+
if(this.x < this.targetX) this.x++;
28+
else if(this.x > this.targetX) this.x--;
29+
if(this.y < this.targetY) this.y++;
30+
else if(this.y > this.targetY) this.y--;
31+
}
32+
33+
public function stopFollowing():void
34+
{
35+
targetX = this.x;
36+
}
37+
38+
public function updatePosition():void
39+
{
40+
follow();
41+
if((this.type == "bullet") && this.y < 10)
42+
{
43+
GameObjectPool.Instance.Search("bullet").x = -20;
44+
GameObjectPool.Instance.Search("bullet").y = -20;
45+
Game.Instance.removeChild(GameObjectPool.Instance.Search("bullet"));
46+
GameObjectPool.Instance.Objects.pop();
47+
}
48+
else if((this.type == "alien0") ||(this.type == "alien1") ||(this.type == "alien2"))
49+
if(GameObjectPool.Instance.Search("bullet") != null)
50+
if(this.hitTestObject(GameObjectPool.Instance.Search("bullet")))
51+
{
52+
this.isDead = true;
53+
GameObjectPool.Instance.Search("bullet").x = -20;
54+
GameObjectPool.Instance.Search("bullet").y = -20;
55+
Game.Instance.removeChild(GameObjectPool.Instance.Search("bullet"));
56+
GameObjectPool.Instance.Objects.pop();
57+
}
58+
if((this.type == "alien0") ||(this.type == "alien1") ||(this.type == "alien2"))
59+
if(this.y >= 300)
60+
Game.Instance.removeEventListener(Event.ENTER_FRAME,Game.Instance.Loop);
61+
}
62+
63+
public function startFire():void
64+
{
65+
var object:GameObject = new GameObject();
66+
object.x = this.x;
67+
object.y = this.y
68+
object.type = "bullet";
69+
object.startFollowing(this.x,0);
70+
GameObjectPool.Instance.Objects.push(object);
71+
}
72+
73+
public function stopFire():void
74+
{
75+
}
76+
77+
}
78+
}

GameObjectPool.as

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package
2+
{
3+
import flash.events.Event;
4+
public class GameObjectPool
5+
{
6+
public var Objects:Array = new Array();
7+
public var animation:Boolean = true;
8+
9+
private static var _instance:GameObjectPool = new GameObjectPool();
10+
11+
public static function get Instance():GameObjectPool//делаем синглетон
12+
{
13+
trace("getInstance");
14+
return _instance;
15+
}
16+
17+
public function Search(name:String):GameObject
18+
{
19+
var response:GameObject;
20+
for each(var object:GameObject in Objects)
21+
{
22+
if(object.type == name)
23+
{
24+
response = object;
25+
break;
26+
}
27+
}
28+
return response;
29+
}
30+
31+
public function GameObjectPool():void
32+
{
33+
trace("Constructor");
34+
if(_instance)
35+
throw new Error("Use Instance Field");
36+
}
37+
38+
public function ApplyBitmapData():void
39+
{
40+
for each(var object:GameObject in Objects)
41+
{
42+
object.graphics.beginBitmapFill(GraphicsPool.Instance.SearchBitmap(object.type).bitmapData);
43+
object.graphics.drawRect(0,0,GraphicsPool.Instance.SearchBitmap(object.type).width,GraphicsPool.Instance.SearchBitmap(object.type).height);
44+
}
45+
Game.Instance.addEventListener(Event.ENTER_FRAME,Game.Instance.Loop);
46+
}
47+
48+
public function AlternateBitmapData():void
49+
{
50+
for each(var object:GameObject in Objects)
51+
{
52+
if (!object.isDead)
53+
{
54+
if(animation)
55+
{
56+
object.graphics.beginBitmapFill(GraphicsPool.Instance.SearchBitmap(object.type).bitmapData);
57+
object.graphics.drawRect(0,0,GraphicsPool.Instance.SearchBitmap(object.type).width,GraphicsPool.Instance.SearchBitmap(object.type).height);
58+
animation = false;
59+
}
60+
else
61+
{
62+
object.graphics.beginBitmapFill(GraphicsPool.Instance.SearchBitmap(object.type+"1").bitmapData);
63+
object.graphics.drawRect(0,0,GraphicsPool.Instance.SearchBitmap(object.type+"1").width,GraphicsPool.Instance.SearchBitmap(object.type+"1").height);
64+
animation = true;
65+
}
66+
}
67+
else if(!object.isRemoved)
68+
{
69+
object.graphics.beginBitmapFill(GraphicsPool.Instance.SearchBitmap("aliendead").bitmapData);
70+
object.graphics.drawRect(0,0,GraphicsPool.Instance.SearchBitmap("aliendead").width,GraphicsPool.Instance.SearchBitmap("aliendead").height);
71+
object.isRemoved = true;
72+
}
73+
else
74+
{
75+
object.graphics.clear();
76+
Game.Instance.removeChild(object);
77+
}
78+
}
79+
}
80+
}
81+
}

GraphicsPool.as

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package
2+
{
3+
import flash.display.Bitmap;
4+
5+
public class GraphicsPool
6+
{
7+
public var Graphics:Array = new Array();
8+
9+
private static var _instance:GraphicsPool = new GraphicsPool();
10+
11+
public static function get Instance():GraphicsPool//делаем синглетон
12+
{
13+
trace("getInstance");
14+
return _instance;
15+
}
16+
public function GraphicsPool():void
17+
{
18+
trace("Constructor");
19+
if(_instance)
20+
throw new Error("Use Instance Field");
21+
}
22+
23+
public function SearchBitmap(request:String):Bitmap
24+
{
25+
var response:GameGraphics;
26+
for each(var object:GameGraphics in this.Graphics)
27+
{
28+
if (object.type == ("app:/"+request+".png"))
29+
{
30+
response = object;
31+
break;
32+
}
33+
}
34+
return response.bitmap;
35+
}
36+
37+
38+
39+
40+
41+
}
42+
}

0 commit comments

Comments
 (0)