mouse and keyboard input architecture

Started by
0 comments, last by Norman Barrows 7 years, 7 months ago

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.

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.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

This topic is closed to new replies.

Advertisement