|
| 1 | +namespace Sandbox; |
| 2 | + |
| 3 | +public abstract partial class Connection |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Build the <see cref="UserCommand"/> for this <see cref="Connection"/>. |
| 7 | + /// </summary> |
| 8 | + internal void BuildUserCommand( ref UserCommand cmd ) |
| 9 | + { |
| 10 | + cmd.Actions = Sandbox.Input.Actions; |
| 11 | + } |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Clear pending pressed and released actions for all connections in the Update context. |
| 15 | + /// </summary> |
| 16 | + internal static void ClearUpdateContextInput() |
| 17 | + { |
| 18 | + foreach ( var connection in All ) |
| 19 | + { |
| 20 | + connection.Input.ClearUpdateContext(); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Clear pending pressed and released actions for all connections in the Fixed Update context. |
| 26 | + /// </summary> |
| 27 | + internal static void ClearFixedUpdateContextInput() |
| 28 | + { |
| 29 | + foreach ( var connection in All ) |
| 30 | + { |
| 31 | + connection.Input.ClearFixedUpdateContext(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + internal struct InputState |
| 36 | + { |
| 37 | + internal struct Context |
| 38 | + { |
| 39 | + public ulong Pressed; |
| 40 | + public ulong Released; |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Clear the state of this input context. |
| 44 | + /// </summary> |
| 45 | + public void Clear() |
| 46 | + { |
| 47 | + Released = 0; |
| 48 | + Pressed = 0; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private UserCommand _lastUserCommand; |
| 53 | + |
| 54 | + public ulong Actions; |
| 55 | + |
| 56 | + private Context _fixedUpdateContext; |
| 57 | + private Context _updateContext; |
| 58 | + |
| 59 | + public Context GetCurrentContext() |
| 60 | + { |
| 61 | + var isFixedUpdate = Game.ActiveScene?.IsFixedUpdate ?? false; |
| 62 | + return isFixedUpdate ? _fixedUpdateContext : _updateContext; |
| 63 | + } |
| 64 | + |
| 65 | + public void ApplyUserCommand( in UserCommand cmd ) |
| 66 | + { |
| 67 | + var commandNumberDelta = cmd.CommandNumber - _lastUserCommand.CommandNumber; |
| 68 | + |
| 69 | + // Drop duplicates or commands that are too far behind (wrap-aware) |
| 70 | + if ( commandNumberDelta is 0 or > 0x7FFFFFFF ) |
| 71 | + return; |
| 72 | + |
| 73 | + var pressed = (~_lastUserCommand.Actions) & cmd.Actions; |
| 74 | + var released = _lastUserCommand.Actions & ~cmd.Actions; |
| 75 | + |
| 76 | + if ( pressed != 0 ) |
| 77 | + { |
| 78 | + _fixedUpdateContext.Pressed |= pressed; |
| 79 | + _updateContext.Pressed |= pressed; |
| 80 | + } |
| 81 | + |
| 82 | + if ( released != 0 ) |
| 83 | + { |
| 84 | + _fixedUpdateContext.Released |= released; |
| 85 | + _updateContext.Released |= released; |
| 86 | + } |
| 87 | + |
| 88 | + Actions = cmd.Actions; |
| 89 | + |
| 90 | + _lastUserCommand = cmd; |
| 91 | + } |
| 92 | + |
| 93 | + public void ClearFixedUpdateContext() |
| 94 | + { |
| 95 | + _fixedUpdateContext.Clear(); |
| 96 | + } |
| 97 | + |
| 98 | + public void ClearUpdateContext() |
| 99 | + { |
| 100 | + _updateContext.Clear(); |
| 101 | + } |
| 102 | + |
| 103 | + public void Clear() |
| 104 | + { |
| 105 | + _lastUserCommand = default; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + internal InputState Input; |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Action is currently pressed down for this <see cref="Connection"/>. |
| 113 | + /// </summary> |
| 114 | + public bool Down( [InputAction] string action ) |
| 115 | + { |
| 116 | + // If we're the host, just use our local input instead. |
| 117 | + if ( IsHost ) |
| 118 | + return Sandbox.Input.Down( action ); |
| 119 | + |
| 120 | + if ( string.IsNullOrWhiteSpace( action ) ) |
| 121 | + return false; |
| 122 | + |
| 123 | + var index = Sandbox.Input.GetActionIndex( action ); |
| 124 | + if ( index == -1 ) |
| 125 | + return false; |
| 126 | + |
| 127 | + var mask = 1UL << index; |
| 128 | + return (Input.Actions & mask) != 0; |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Action was pressed for this <see cref="Connection"/> within the current update context. |
| 133 | + /// </summary> |
| 134 | + public bool Pressed( [InputAction] string action ) |
| 135 | + { |
| 136 | + // If we're the host, just use our local input instead. |
| 137 | + if ( IsHost ) |
| 138 | + return Sandbox.Input.Pressed( action ); |
| 139 | + |
| 140 | + if ( string.IsNullOrWhiteSpace( action ) ) |
| 141 | + return false; |
| 142 | + |
| 143 | + var index = Sandbox.Input.GetActionIndex( action ); |
| 144 | + if ( index == -1 ) |
| 145 | + return false; |
| 146 | + |
| 147 | + var mask = 1UL << index; |
| 148 | + var context = Input.GetCurrentContext(); |
| 149 | + |
| 150 | + return (context.Pressed & mask) != 0; |
| 151 | + } |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Action was released for this <see cref="Connection"/> within the current update context. |
| 155 | + /// </summary> |
| 156 | + public bool Released( [InputAction] string action ) |
| 157 | + { |
| 158 | + // If we're the host, just use our local input instead. |
| 159 | + if ( IsHost ) |
| 160 | + return Sandbox.Input.Released( action ); |
| 161 | + |
| 162 | + if ( string.IsNullOrWhiteSpace( action ) ) |
| 163 | + return false; |
| 164 | + |
| 165 | + var index = Sandbox.Input.GetActionIndex( action ); |
| 166 | + if ( index == -1 ) |
| 167 | + return false; |
| 168 | + |
| 169 | + var mask = 1UL << index; |
| 170 | + var context = Input.GetCurrentContext(); |
| 171 | + |
| 172 | + return (context.Released & mask) != 0; |
| 173 | + } |
| 174 | +} |
0 commit comments