root/Scripts/Amoeba.as

46
11
	{
11
	{
12
		private var stageRef:Stage;
12
		private var stageRef:Stage;
13
		private var key:KeyObject;
13
		private var key:KeyObject;
14
		private var GameMap:Array = new Array();
15
		private var bAmoebaLoaded:Boolean;
16
		private var bActive:Boolean;
17
		
18
		// Movement vars
19
		public var bMoving:Boolean;
20
		public var iDir:int;
21
		//private var bBrake:Boolean;
22
		private  var mUp:int  = 1;
23
		private  var mDown:int = 2;
24
		private  var mRight:int = 3;
25
		private  var mLeft:int = 4;
26
		private  var mNone:int = 0;
14
 
27
 
15
		public function Amoeba(stageRef:Stage)
28
 		public function Amoeba()
16
		{
29
		{
30
			bAmoebaLoaded = false;
31
			bActive = false;
32
		}
33
		
34
		public function IsReady():Boolean
35
	   { return bAmoebaLoaded;}
36
	   
37
	   public function Activate():void
38
	   {bActive = true;}
39
 		
40
		public function CreateAmoeba(stageRef:Stage)
41
		{
17
			trace("Amoeba: Constructor");
42
			trace("Amoeba: Constructor");
18
			this.stageRef = stageRef;
43
			this.stageRef = stageRef;
44
			
45
			bMoving = false;
46
			//bBrake = false;
47
			iDir= mNone;
48
			
19
			key = new KeyObject(stageRef);
49
			key = new KeyObject(stageRef);
50
			this.name="nAmoeba";
20
			
51
			
21
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);			
52
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);			
22
			//addEventListener(Event.EXIT_FRAME, removeSelf, false, 0, true);			
53
			//addEventListener(Event.EXIT_FRAME, removeSelf, false, 0, true);			
54
			bAmoebaLoaded = true;
23
 
55
 
24
		}
56
		}
25
		
57
		
58
		public function SetMap(gamemap:Array)
59
		{
60
			this.GameMap = gamemap;
61
		}
62
		
26
		public function loop(e:Event) : void
63
		public function loop(e:Event) : void
27
		{
64
		{
28
			trace("Amoeba: Loop");
65
			if(bActive)
29
			if (key.isDown(Keyboard.LEFT))
66
			{
30
				x -= 2;
67
				MoveInput();
31
			else if (key.isDown(Keyboard.RIGHT))
68
				ColisionDetection();
32
				x += 2;
69
				Move();
33
 
70
			}
34
			if (key.isDown(Keyboard.UP))
35
				y -= 2;
36
			else if (key.isDown(Keyboard.DOWN))
37
				y += 2;							
38
		}
71
		}
72
		private function ColisionDetection()
73
		{
74
			if(bMoving)
75
			{
76
				//  the next cell 
77
				var nextCell:int 
78
				var nextX:int;
79
				var nextY:int;
80
				//trace("----:)---:(----")
81
//				trace("- Pos: x: "+ x + " y: "+ y );
82
				// in the direction i'm going
83
				switch(iDir)
84
				{
85
					case mLeft: 	nextX=(x-5)/25; nextY = y/25; 	/*trace("- Est: x: "+ (x-5) + " y: "+ y );	*/break;
86
					case mRight: 	nextX=(x+5+25)/25; nextY = y/25; 	/*trace("- Est: x: "+ (x+5+25) + " y: "+ y );		*/break;
87
					case mUp: 		nextX= x/25; 	nextY = (y-5)/25;  /*trace("- Est: x: "+ x + " y: "+ (y-5) );		*/break;
88
					case mDown: 	nextX= x/25; 	nextY = (y+5+25)/25; /*trace("- Est: x: "+ x + " y: "+ (y+5+25) );		*/break;
89
					default: trace("Error: ColDec wrong Amoeba dir: " + iDir); break;
90
				}
91
				//trace("- Dir: "+ GetDir() +" at: " + nextX + ", " + nextY);
92
				
93
				// Safety correction:
94
				if(nextX < 0) {trace("Error: ColDect trying to go, x:" + nextX); nextX=0;}
95
				if(nextX > 21) {trace("Error: ColDect trying to go, x:" + nextX); nextX=21;}
96
				if(nextY < 0) {trace("Error: ColDect trying to go, y:" + nextY); nextY=0;}
97
				if(nextY > 15) {trace("Error: ColDect trying to go, y:" + nextY); nextY=21;}
98
				
99
				nextCell= GameMap[nextY][nextX];
100
				//trace("- Next Cell Val: " + nextCell );
101
				
102
				// is not passable
103
				if(nextCell == 0 || nextCell == 1 ||nextCell == 2 ||nextCell == 3 ||nextCell == 4 )
104
				{
105
					//trace("Stop");
106
					// stop when i reach it
107
					bMoving = false;
108
					// Fix the extra pixels
109
					switch(iDir)
110
					{
111
						case mLeft: 	x=(nextX+1)*25; break;
112
						case mRight: 	x=(nextX-1)*25; break;
113
						case mUp: 		y=(nextY+1)*25; break;
114
						case mDown: 	y=(nextY-1)*25; break;
115
						default: trace("Error: ColDec wrong Amoeba dir: " + iDir); break;
116
					}
117
					iDir=mNone;
118
					
119
				}
120
				
121
			}
122
		}
39
		
123
		
124
		private function MoveInput():void
125
		{
126
			// Getting Directions
127
			if(!bMoving)
128
			{
129
				bMoving = true;
130
				if 		(key.isDown(Keyboard.LEFT)) 	iDir = mLeft;
131
				else if (key.isDown(Keyboard.RIGHT))	iDir = mRight;	 
132
				else if (key.isDown(Keyboard.UP))		iDir = mUp;
133
				else if (key.isDown(Keyboard.DOWN))		iDir = mDown;
134
				else bMoving = false;
135
			}
136
		}
40
		
137
		
138
		private function Move():void
139
		{		
140
			// Locomotor action!
141
			if(bMoving)
142
			{
143
				switch(iDir)
144
				{
145
					case mLeft: 	x -= 5; break;
146
					case mRight: 	x += 5; break;
147
					case mUp: 		y -= 5; break;
148
					case mDown: 	y += 5; break;
149
					default: trace("Error: wrong Amoeba dir: " + iDir); break;
150
				}
151
			} 
152
		}
153
154
		
155
		
41
		public function removeSelf() : void
156
		public function removeSelf() : void
42
		{
157
		{
43
			trace("Amoeba: Destructor");
158
			trace("Amoeba: Destructor");
...
...
47
 
162
 
48
			if (stageRef.contains(this)) stageRef.removeChild(this);
163
			if (stageRef.contains(this)) stageRef.removeChild(this);
49
		}
164
		}
165
		
166
		public function GetDir():String
167
		{
168
			if(iDir==mUp) return "Up";
169
			else if(iDir==mDown) return "Down";
170
			else if(iDir==mRight) return "Right";
171
			else if(iDir==mLeft) return "Left";
172
			else if(iDir==mNone) return "Stopped";
173
			else return ("Unknown: "+ iDir);
174
		}
50
 
175
 
51
	}
176
	}
52
 
177