PageRenderTime 25ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 0ms app.codeStats 0ms

/src/GameInput.as

https://bitbucket.org/HopeSky/mars_nd2d
ActionScript | 253 lines | 182 code | 34 blank | 37 comment | 8 complexity | 8228813f73b2b7c6dcda514ead5b5aff MD5 | raw file
  1// Stage3d game input routines version 1.2
  2//
  3package
  4{
  5
  6import flash.display.Stage;
  7import flash.ui.Keyboard;
  8import flash.events.*;
  9
 10public class GameInput
 11{
 12	// the current state of the mouse
 13	public var mouseIsDown:Boolean = false;
 14	public var mouseClickX:int = 0;
 15	public var mouseClickY:int = 0;
 16	public var mouseX:int = 0;
 17	public var mouseY:int = 0;
 18
 19	// the current state of the keyboard controls
 20	public var pressing:Object = 
 21	{ up:0, down:0, left:0, right:0, fire:0, 
 22	strafeLeft:0, strafeRight:0, 
 23	key0:0, key1:0, key2:0, key3:0, key4:0,
 24	key5:0,	key6:0, key7:0, key8:0, key9:0 };
 25
 26	// if mouselook is on, this is added to the chase camera
 27	public var cameraAngleX:Number = 0;
 28	public var cameraAngleY:Number = 0;
 29	public var cameraAngleZ:Number = 0;
 30
 31	// if this is true, dragging the mouse changes the camera angle
 32	public var mouseLookMode:Boolean = true;
 33
 34	// the game's main stage
 35	public var stage:Stage;
 36	
 37	// for click events to be sent to the game
 38	private var _clickfunc:Function = null;
 39	
 40	// class constructor
 41	public function GameInput(
 42		theStage:Stage, 
 43		clickfunc:Function = null)
 44	{
 45		stage = theStage;
 46		// get keypresses and detect the game losing focus
 47		stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
 48		stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
 49		stage.addEventListener(Event.DEACTIVATE, lostFocus);
 50		stage.addEventListener(Event.ACTIVATE, gainFocus);
 51		stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);   
 52		stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);   
 53		stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove); 
 54		
 55		// handle clicks in game
 56		_clickfunc = clickfunc;
 57	}
 58
 59	private function mouseMove(e:MouseEvent):void   
 60	{
 61		mouseX = e.stageX;
 62		mouseY = e.stageY;
 63		if (mouseIsDown && mouseLookMode)
 64		{
 65			cameraAngleY = 90 * ((mouseX - mouseClickX)
 66				/ stage.width);
 67			cameraAngleX = 90 * ((mouseY - mouseClickY) 
 68				/ stage.height);
 69		}
 70	}
 71
 72	private function mouseDown(e:MouseEvent):void   
 73	{   
 74		trace('mouseDown at '+e.stageX+','+e.stageY);
 75		mouseClickX = e.stageX;
 76		mouseClickY = e.stageY;
 77		mouseIsDown = true;
 78		if (_clickfunc != null) _clickfunc();
 79	}	
 80
 81	private function mouseUp(e:MouseEvent):void   
 82	{   
 83		trace('mouseUp at '+e.stageX+','+e.stageY+' drag distance:'+
 84			(e.stageX-mouseClickX)+','+(e.stageY-mouseClickY));
 85		mouseIsDown = false;
 86		if (mouseLookMode)
 87		{	// reset camera angle
 88			cameraAngleX = cameraAngleY = cameraAngleZ = 0;
 89		}
 90	}	
 91
 92	private function keyPressed(event:KeyboardEvent):void 
 93	{
 94		// qwer 81 87 69 82
 95		// asdf 65 83 68 70
 96		// left right 37 39
 97		// up down 38 40
 98		// 0123456789 = 48 to 57
 99		// zxcv = 90 88 67 86
100
101		// trace("keyPressed " + event.keyCode);
102		
103		if (event.ctrlKey || 
104			event.altKey || 
105			event.shiftKey)
106			pressing.fire = true;
107
108		switch(event.keyCode)
109		{
110			/*
111			case 81: // Q
112				pressing.strafeLeft = true;
113			break;
114			case 69: // E
115				pressing.strafeRight = true;
116			break;
117			*/
118
119			case Keyboard.UP:
120			case 87: // W
121			case 90: // Z
122				pressing.up = true;
123			break;
124			
125			case Keyboard.DOWN:
126			case 83: // S
127				pressing.down = true;
128			break;
129			
130			case Keyboard.LEFT:
131			case 65: // A
132			case 81: // Q
133				pressing.left = true;
134			break;
135			
136			case Keyboard.RIGHT:
137			case 68: // D
138				pressing.right = true;
139			break;
140			
141			case Keyboard.SPACE:
142			case Keyboard.SHIFT:
143			case Keyboard.CONTROL:
144			case Keyboard.ENTER:
145			// case 90: // z
146			case 88: // x
147			case 67: // c
148				pressing.fire = true;
149			break;
150
151			case 48: pressing.key0 = true; break;
152			case 49: pressing.key1 = true; break;
153			case 50: pressing.key2 = true; break;
154			case 51: pressing.key3 = true; break;
155			case 52: pressing.key4 = true; break;
156			case 53: pressing.key5 = true; break;
157			case 54: pressing.key6 = true; break;
158			case 55: pressing.key7 = true; break;
159			case 56: pressing.key8 = true; break;
160			case 57: pressing.key9 = true; break;
161			
162		}
163	}
164
165	private function gainFocus(event:Event):void 
166	{
167		trace("Game received keyboard focus.");
168	}
169
170	// if the game loses focus, don't keep keys held down
171	private function lostFocus(event:Event):void 
172	{
173		trace("Game lost keyboard focus.");
174		pressing.up = false;
175		pressing.down = false;
176		pressing.left = false;
177		pressing.right = false;
178		pressing.strafeLeft = false;
179		pressing.strafeRight = false;
180		pressing.fire = false;
181		pressing.key0 = false;
182		pressing.key1 = false;
183		pressing.key2 = false;
184		pressing.key3 = false;
185		pressing.key4 = false;
186		pressing.key5 = false;
187		pressing.key6 = false;
188		pressing.key7 = false;
189		pressing.key8 = false;
190		pressing.key9 = false;
191	}
192
193	private function keyReleased(event:KeyboardEvent):void 
194	{
195		switch(event.keyCode)
196		{
197			/*
198			case 81: // Q
199				pressing.strafeLeft = false;
200			break;
201			case 69: // E
202				pressing.strafeRight = false;
203			break;
204			*/
205
206			case Keyboard.UP:
207			case 87: // W
208			case 90: // Z
209				pressing.up = false;
210			break;
211
212			case Keyboard.DOWN:
213			case 83: // S
214				pressing.down = false;
215			break;
216
217			case Keyboard.LEFT:
218			case 65: // A
219			case 81: // Q
220				pressing.left = false;
221			break;
222
223			case Keyboard.RIGHT:
224			case 68: // D
225				pressing.right = false;
226			break;
227
228			case Keyboard.SPACE:
229			case Keyboard.SHIFT:
230			case Keyboard.CONTROL:
231			case Keyboard.ENTER:
232			// case 90: // z
233			case 88: // x
234			case 67: // c
235				pressing.fire = false;
236			break;
237
238			case 48: pressing.key0 = false; break;
239			case 49: pressing.key1 = false; break;
240			case 50: pressing.key2 = false; break;
241			case 51: pressing.key3 = false; break;
242			case 52: pressing.key4 = false; break;
243			case 53: pressing.key5 = false; break;
244			case 54: pressing.key6 = false; break;
245			case 55: pressing.key7 = false; break;
246			case 56: pressing.key8 = false; break;
247			case 57: pressing.key9 = false; break;
248			
249		}
250	}
251
252} // end class
253} // end package