mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
* Remove ComponentFamily, remove "game" components. The entire ComponentFamily system has been removed entirely. Components now require explicit registration into the IComponentFactory. This registration spawns the component as dummy and checks reads some data off it like name, netID... "Reference" types can also be registered using RegisterReference<TTarget, TInterface>(). These references allow you to get components from entities too, so you can reference by interfaces. This should allow us to inverse the direct fetching and provide a component API available from Shared. Note that these reference CANNOT be used to spawn a new component. Only a concrete registration can. Targeted component family messaging no longer exists. The messaging system still exist, but it's always to ALL components. The system should probably be killed completely later. To fill the gap, all cases where family-targeted messaging was used, it now uses references and simple interfaces. To reference components accross the network, the component now needs to define a "Net ID". These net IDs should be unique (but matching between client and server). I made a set of constants for these IDs too in NetIDs.cs Names are no longer used in netcode (replaced by net IDs). Should reduce bandwidth usage from string names. Because a LOT of code got in the way, most "game" content was cut. This includes (among others) items and interaction. This had to be reimplemented in content, and most of the code was terrible anyways. Also removed the messaging profiling system. It got in my way and I don't understand it (and I doubt anybody does). It's in the git history if somebody cares. Fixes #262, Fixes #250, Fixes #74. * Attempt to fix unit tests, not yet done. * Fix unit tests, properly. * Remove debug message. * Fix rendering on WearableAnimatedSprite. It wasn't being registered as ISpriteRenderableComponent because of a typo. The ComponentFactory now detects this error too.
176 lines
6.0 KiB
C#
176 lines
6.0 KiB
C#
using Lidgren.Network;
|
|
using SS14.Client.Interfaces.Input;
|
|
using SS14.Shared;
|
|
using SS14.Shared.GameObjects;
|
|
using SS14.Shared.GameObjects.Components;
|
|
using SS14.Shared.Interfaces.GameObjects;
|
|
using SS14.Shared.IoC;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System;
|
|
|
|
namespace SS14.Client.GameObjects
|
|
{
|
|
public class KeyBindingInputComponent : ClientComponent
|
|
{
|
|
public override string Name => "KeyBindingInput";
|
|
public override uint? NetID => NetIDs.KEY_BINDING_INPUT;
|
|
|
|
#region Delegates
|
|
|
|
public delegate void KeyEvent(bool state);
|
|
|
|
#endregion Delegates
|
|
|
|
private readonly Dictionary<BoundKeyFunctions, KeyEvent> _keyHandlers;
|
|
private readonly Dictionary<BoundKeyFunctions, bool> _keyStates;
|
|
|
|
private bool _enabled = true;
|
|
|
|
public KeyBindingInputComponent()
|
|
{
|
|
_keyStates = new Dictionary<BoundKeyFunctions, bool>();
|
|
_keyHandlers = new Dictionary<BoundKeyFunctions, KeyEvent>();
|
|
//Set up keystates
|
|
}
|
|
|
|
public override void OnRemove()
|
|
{
|
|
base.OnRemove();
|
|
|
|
var keyBindingManager = IoCManager.Resolve<IKeyBindingManager>();
|
|
keyBindingManager.BoundKeyDown -= KeyDown;
|
|
keyBindingManager.BoundKeyUp -= KeyUp;
|
|
}
|
|
|
|
public override void OnAdd(IEntity owner)
|
|
{
|
|
base.OnAdd(owner);
|
|
|
|
var keyBindingManager = IoCManager.Resolve<IKeyBindingManager>();
|
|
keyBindingManager.BoundKeyDown += KeyDown;
|
|
keyBindingManager.BoundKeyUp += KeyUp;
|
|
}
|
|
|
|
public override ComponentReplyMessage RecieveMessage(object sender, ComponentMessageType type,
|
|
params object[] list)
|
|
{
|
|
ComponentReplyMessage reply = base.RecieveMessage(sender, type, list);
|
|
|
|
if (sender == this)
|
|
return ComponentReplyMessage.Empty;
|
|
|
|
switch (type)
|
|
{
|
|
case ComponentMessageType.Die:
|
|
Disable();
|
|
break;
|
|
case ComponentMessageType.Live:
|
|
Enable();
|
|
break;
|
|
}
|
|
|
|
return reply;
|
|
}
|
|
|
|
public override void Shutdown()
|
|
{
|
|
base.Shutdown();
|
|
var keyBindingManager = IoCManager.Resolve<IKeyBindingManager>();
|
|
keyBindingManager.BoundKeyDown -= KeyDown;
|
|
keyBindingManager.BoundKeyUp -= KeyUp;
|
|
}
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
base.Update(frameTime);
|
|
if (_enabled)
|
|
UpdateKeys(frameTime);
|
|
}
|
|
|
|
private void Enable()
|
|
{
|
|
_enabled = true;
|
|
}
|
|
|
|
private void Disable()
|
|
{
|
|
_enabled = false;
|
|
|
|
//Remove all active key states and send keyup messages for them.
|
|
foreach (var state in _keyStates.ToList())
|
|
{
|
|
Owner.SendComponentNetworkMessage(this, NetDeliveryMethod.ReliableUnordered, state.Key, BoundKeyState.Up);
|
|
Owner.SendMessage(this, ComponentMessageType.BoundKeyChange, state.Key, BoundKeyState.Up);
|
|
_keyStates.Remove(state.Key);
|
|
}
|
|
}
|
|
|
|
public virtual void KeyDown(object sender, BoundKeyEventArgs e)
|
|
{
|
|
if (!_enabled || GetKeyState(e.Function))
|
|
return; //Don't repeat keys that are already down.
|
|
|
|
Owner.SendComponentNetworkMessage(this, NetDeliveryMethod.ReliableUnordered, e.Function, e.FunctionState);
|
|
SetKeyState(e.Function, true);
|
|
Owner.SendMessage(this, ComponentMessageType.BoundKeyChange, e.Function, e.FunctionState);
|
|
}
|
|
|
|
public virtual void KeyUp(object sender, BoundKeyEventArgs e)
|
|
{
|
|
if (!_enabled)
|
|
return;
|
|
Owner.SendComponentNetworkMessage(this, NetDeliveryMethod.ReliableUnordered, e.Function, e.FunctionState);
|
|
SetKeyState(e.Function, false);
|
|
Owner.SendMessage(this, ComponentMessageType.BoundKeyChange, e.Function, e.FunctionState);
|
|
}
|
|
|
|
protected void SetKeyState(BoundKeyFunctions k, bool state)
|
|
{
|
|
// Check to see if we have a keyhandler for the key that's been pressed. Discard invalid keys.
|
|
_keyStates[k] = state;
|
|
}
|
|
|
|
public bool GetKeyState(BoundKeyFunctions k)
|
|
{
|
|
if (_keyStates.Keys.Contains(k))
|
|
return _keyStates[k];
|
|
return false;
|
|
}
|
|
|
|
public virtual void UpdateKeys(float frameTime)
|
|
{
|
|
//Rate limit
|
|
/*TimeSpan timeSinceLastUpdate = entityManager.now - lastKeyUpdate;
|
|
if (timeSinceLastUpdate.TotalMilliseconds < 1000 / keyUpdateRateLimit)
|
|
return;*/
|
|
|
|
// So basically we check for active keys with handlers and execute them. This is a linq query.
|
|
// Get all of the active keys' handlers
|
|
var activeKeyHandlers =
|
|
from keyState in _keyStates
|
|
join handler in _keyHandlers on keyState.Key equals handler.Key
|
|
select new { evt = handler.Value, state = keyState.Value };
|
|
|
|
//Execute the bastards!
|
|
foreach (var keyHandler in activeKeyHandlers)
|
|
{
|
|
//If there's even one active, we set updateRequired so that this gets hit again next update
|
|
//updateRequired = true; // QUICKNDIRTY
|
|
KeyEvent k = keyHandler.evt;
|
|
k(keyHandler.state);
|
|
}
|
|
|
|
//Delete false states from the dictionary so they don't get reprocessed and fuck up other stuff.
|
|
foreach (var state in _keyStates.ToList())
|
|
{
|
|
if (!state.Value)
|
|
_keyStates.Remove(state.Key);
|
|
else
|
|
Owner.SendMessage(this, ComponentMessageType.BoundKeyRepeat, state.Key, BoundKeyState.Repeat);
|
|
}
|
|
//lastKeyUpdate = entityManager.now;
|
|
}
|
|
}
|
|
}
|