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.