Skip to content

Commit

Permalink
Add Numpad Input
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebual committed Jun 8, 2021
1 parent 3a1dd15 commit 1002567
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
64 changes: 64 additions & 0 deletions code/entities/NumpadInputEntity.cs
Original file line number Diff line number Diff line change
@@ -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<string, InputButton> inputButtons = new Dictionary<string, InputButton> {
["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();
}
}
1 change: 1 addition & 0 deletions code/entities/WireOutputEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
23 changes: 23 additions & 0 deletions code/tools/NumpadInput.cs
Original file line number Diff line number Diff line change
@@ -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,
};
}
}
}

0 comments on commit 1002567

Please sign in to comment.