• Advertisement
Sign in to follow this  

mouse and keyboard input architecture

This topic is 585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys.

 

I have a problem building input architecture.

 

I use heirarchical MVC, it looks like this:

World
    -> WorldModel
        -> Unit
            -> UnitModel
                -> Weapon
                    -> WeaponModel
                    -> WaponView
                    -> WeaponController
            -> UnitView
            -> UnitController
    -> WorldView
    -> WorldController

I don't know if this right understanding of concept.

 

My Input class looks like this

package com.fennec.delirium.utils
{
	import flash.display.DisplayObject;
	import flash.events.KeyboardEvent;

	public class Input
	{
		public static const instance:Input = new Input();
		
		private const _keys:Object = {};
		
		private var _owner:DisplayObject = null;
		
		public function Input()
		{
			if(instance)
			{
				throw new Error("use Input.instance!");
			}
		}

		public function init(owner:DisplayObject):void
		{
			_owner = owner;
			
			owner.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			owner.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}

		private function onKeyDown(event:KeyboardEvent):void
		{
			_keys[event.keyCode] = true;
		}

		private function onKeyUp(event:KeyboardEvent):void
		{
			_keys[event.keyCode] = false;
		}

		public function isKeyDown(keyCode:uint):Boolean
		{
			return _keys[keyCode];
		}

		public function get mouseX():Number
		{
			return _owner.mouseX;
		}
		
		public function get mouseY():Number
		{
			return _owner.mouseY;
		}
	}
}

Which doesn't fell right, because I think I may want to add some other input devices, like joystick. But for now it's the best thing I inveted so far.

 

The problem is that Unit's model position is in world coordinates, and mouse position is in screen coordinates, and I need them both to be from same source, so I could define Unit's face direction.

 

I want to avoid this singletone implementation for Input. Seems like I just need to make instance Input and pass it to UnitController but here's some troubles:

- I need two different owners for Keyboard + Mouse input: one for stage and one for world. This seems to violate incapsulation between responsibility layers.

- I need to pass Input from top level, which means WorldModel would know about Unit's Input, which also violates incapsulation.

 

I'm really stuck and need advice.

Share this post


Link to post
Share on other sites
Advertisement

in non-oo terms you want something that generally looks like this:

       GAME
         |
     TRANSLATION LAYER
         |
         |-------------------------------------------------------------------------------
         |         |                |                 |                          |
KEYBOARD MODULE   MOUSE MODULE    JOYSTICK MODULE   GAME CONTROLLER MODULE      ETC


the translation layer converts device coords into gameworld coords.

 

it might also possibly handle:

device calibration

mapping device keys/buttons to generic input commands (IE W=move forward in WASD, etc) 

which input device(s) are active

device output (optional rumble for gamepads)

and so on.

 

start with a list of data structures and methods. then sort them into groups based on functionality. the resulting groups should be a good indicator of the classes you'll want to define and use. IE the way to slice it up OO style. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement