From 100256738c32963cad5a0a8d5947216b9d8351f2 Mon Sep 17 00:00:00 2001 From: Nebual Date: Tue, 8 Jun 2021 03:05:25 -0700 Subject: [PATCH] Add Numpad Input --- code/entities/NumpadInputEntity.cs | 64 ++++++++++++++++++++++++++++++ code/entities/WireOutputEntity.cs | 1 + code/tools/NumpadInput.cs | 23 +++++++++++ 3 files changed, 88 insertions(+) create mode 100755 code/entities/NumpadInputEntity.cs create mode 100755 code/tools/NumpadInput.cs diff --git a/code/entities/NumpadInputEntity.cs b/code/entities/NumpadInputEntity.cs new file mode 100755 index 0000000..9425018 --- /dev/null +++ b/code/entities/NumpadInputEntity.cs @@ -0,0 +1,64 @@ +using Sandbox; +using System; +using System.Collections.Generic; +using System.Linq; + +[Library( "ent_numpadinput", Title = "Numpad Input" )] +public partial class NumpadInputEntity : Prop, WireOutputEntity +{ + public Entity WireOwner; + public bool IsToggle { get; set; } = false; + WirePortData IWireEntity.WirePorts { get; } = new WirePortData(); + + private static Dictionary inputButtons = new Dictionary { + ["Forward"] = InputButton.Forward, // W + ["Back"] = InputButton.Back, // S + ["Left"] = InputButton.Left, // A + ["Right"] = InputButton.Right, // D + ["Attack1"] = InputButton.Attack1, // Mouse0 + ["Attack2"] = InputButton.Attack2, // Mouse1 + ["Reload"] = InputButton.Reload, // R + ["Drop"] = InputButton.Drop, // G + ["Jump"] = InputButton.Jump, // Spacebar + ["Run"] = InputButton.Run, // LShift and RShift + ["Walk"] = InputButton.Walk, // LAlt + ["Duck"] = InputButton.Duck, // LCtrl and RCtrl + ["Score"] = InputButton.Score, // Tab + ["Menu"] = InputButton.Menu, // Q + ["Flashlight"] = InputButton.Flashlight, // F + ["View"] = InputButton.View, // C + ["Voice"] = InputButton.Voice, // V + ["Slot1"] = InputButton.Slot1, + ["Slot2"] = InputButton.Slot2, + ["Slot3"] = InputButton.Slot3, + ["Slot4"] = InputButton.Slot4, + ["Slot5"] = InputButton.Slot5, + ["Slot6"] = InputButton.Slot6, + ["Slot7"] = InputButton.Slot7, + ["Slot8"] = InputButton.Slot8, + ["Slot9"] = InputButton.Slot9, + }; + + [Event.Tick.Server] + public void OnTick() + { + if ( !WireOwner.IsValid() || WireOwner is not SandboxPlayer ) { + return; + } + var input = ((SandboxPlayer)WireOwner).LastInput; + if ( input == null ) { + return; + } + foreach ( var name in inputButtons.Keys ) { + var inputButton = inputButtons[name]; + var newState = input.Down( inputButton ) ? 1 : 0; + if ( newState != ((WireOutputEntity)this).GetOutput( name ).value ) { + WireOutputEntity.WireTriggerOutput( this, name, newState ); + } + } + } + public string[] WireGetOutputs() + { + return inputButtons.Keys.ToArray(); + } +} diff --git a/code/entities/WireOutputEntity.cs b/code/entities/WireOutputEntity.cs index fa419dd..76af436 100755 --- a/code/entities/WireOutputEntity.cs +++ b/code/entities/WireOutputEntity.cs @@ -22,6 +22,7 @@ public interface WireOutputEntity : IWireEntity public static void WireTriggerOutput( WireOutputEntity ent, string outputName, int value ) { var output = ent.GetOutput( outputName ); + output.value = value; foreach ( var input in output.connected ) { if ( !input.entity.IsValid() ) continue; if ( input.entity is WireInputEntity inputEntity ) { diff --git a/code/tools/NumpadInput.cs b/code/tools/NumpadInput.cs new file mode 100755 index 0000000..b2ac095 --- /dev/null +++ b/code/tools/NumpadInput.cs @@ -0,0 +1,23 @@ +using System; +namespace Sandbox.Tools +{ + [Library( "tool_numpadinput", Title = "Numpad Input", Description = "Create Numpad Inputs!", Group = "construction" )] + public partial class NumpadInputTool : BaseWireTool + { + protected override Type GetEntityType() + { + return typeof( NumpadInputEntity ); + } + protected override string GetModel() + { + return "models/wirebox/katlatze/button.vmdl"; + } + protected override ModelEntity SpawnEntity( TraceResult tr ) + { + return new NumpadInputEntity { + Position = tr.EndPos, + WireOwner = Owner, + }; + } + } +}