Skip to content

Commit b390e0b

Browse files
committed
Initial Wiremod for S&box: wire thrusters to buttons
0 parents  commit b390e0b

18 files changed

+634
-0
lines changed

.addon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"sharedassets": "*.*",
3+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Wireteam
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

code/entities/ButtonEntity.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Sandbox;
2+
using System;
3+
using System.Linq;
4+
5+
[Library( "ent_button", Title = "Button", Spawnable = true )]
6+
public partial class ButtonEntity : Prop, IUse, WireOutputEntity
7+
{
8+
public bool On { get; set; } = false;
9+
public bool IsToggle { get; set; } = false;
10+
11+
public override void Spawn()
12+
{
13+
base.Spawn();
14+
SetModel( "models/wirebox/katlatze/button.vmdl" );
15+
SetupPhysicsFromModel( PhysicsMotionType.Dynamic, false );
16+
}
17+
public bool IsUsable( Entity user )
18+
{
19+
return true;
20+
}
21+
22+
private bool ModelUsesMaterialGroups()
23+
{
24+
var model = GetModelName();
25+
if ( model == "models/wirebox/katlatze/button.vmdl" ) {
26+
return true;
27+
}
28+
return false;
29+
}
30+
31+
public bool OnUse( Entity user )
32+
{
33+
if ( user is Player player ) {
34+
On = !On;
35+
if ( ModelUsesMaterialGroups() ) {
36+
SetMaterialGroup( On ? 1 : 0 );
37+
}
38+
else {
39+
RenderColor = On ? Color32.Green : Color32.Red;
40+
}
41+
WireOutputEntity.WireTriggerOutput( this, "On", On ? 1 : 0 );
42+
}
43+
44+
return false;
45+
}
46+
47+
public string[] WireGetOutputs()
48+
{
49+
return new string[] { "On" };
50+
}
51+
}

code/entities/LampEntity.wire.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Sandbox;
2+
3+
public partial class LampEntity : WireInputEntity
4+
{
5+
public string[] WireGetInputs()
6+
{
7+
return new string[] { "On" };
8+
}
9+
10+
void WireInputEntity.HandleWireInput<T>( string inputName, T value )
11+
{
12+
if ( inputName == "On" ) {
13+
if ( typeof( T ) == typeof( int ) ) {
14+
Enabled = ((int)(object)value) != 0;
15+
}
16+
if ( typeof( T ) == typeof( float ) ) {
17+
Enabled = (float)(object)value != 0.0f;
18+
}
19+
}
20+
}
21+
}

code/entities/LightEntity.wire.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Sandbox;
2+
public partial class LightEntity : WireInputEntity
3+
{
4+
public string[] WireGetInputs()
5+
{
6+
return new string[] { "On" };
7+
}
8+
void WireInputEntity.HandleWireInput<T>( string inputName, T value )
9+
{
10+
if ( inputName == "On" ) {
11+
if ( typeof( T ) == typeof( int ) ) {
12+
Enabled = ((int)(object)value) != 0;
13+
}
14+
if ( typeof( T ) == typeof( float ) ) {
15+
Enabled = (float)(object)value != 0.0f;
16+
}
17+
}
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Sandbox;
2+
3+
public partial class ThrusterEntity : WireInputEntity
4+
{
5+
// todo: the vanilla ThrusterEntity is just on/off, so we'll probably want to just replace it entirely, rather than this extending...
6+
[Net]
7+
public float ForceMultiplier { get; set; } = 1.0f;
8+
9+
public string[] WireGetInputs()
10+
{
11+
return new string[] { "ForceMultiplier" };
12+
}
13+
14+
void WireInputEntity.HandleWireInput<T>( string inputName, T value )
15+
{
16+
if ( inputName == "ForceMultiplier" ) {
17+
if ( typeof( T ) == typeof( int ) ) {
18+
ForceMultiplier = ((int)(object)value);
19+
}
20+
if ( typeof( T ) == typeof( float ) ) {
21+
ForceMultiplier = (float)(object)value;
22+
}
23+
Enabled = ForceMultiplier != 0.0f;
24+
}
25+
}
26+
}

code/entities/WireInputEntity.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace Sandbox
5+
{
6+
public class WireInput
7+
{
8+
public int value;
9+
public Entity entity;
10+
public string inputName;
11+
public List<WireOutput> connected = new List<WireOutput>();
12+
13+
public WireInput( Entity entity, string inputName )
14+
{
15+
this.entity = entity;
16+
this.inputName = inputName;
17+
}
18+
}
19+
public interface WireInputEntity
20+
{
21+
protected static Dictionary<int, Dictionary<string, WireInput>> allInputs = new Dictionary<int, Dictionary<string, WireInput>>();
22+
23+
public int NetworkIdent { get; }
24+
public void WireTriggerInput<T>( string inputName, T value )
25+
{
26+
HandleWireInput( inputName, value );
27+
}
28+
public virtual void HandleWireInput<T>( string inputName, T value ) { }
29+
30+
public WireInput GetInput( string inputName )
31+
{
32+
if ( !allInputs.ContainsKey( this.NetworkIdent ) ) {
33+
InitializeInputs();
34+
}
35+
return allInputs[this.NetworkIdent][inputName];
36+
}
37+
public string[] GetInputNames()
38+
{
39+
if ( !allInputs.ContainsKey( this.NetworkIdent ) ) {
40+
InitializeInputs();
41+
}
42+
return allInputs[this.NetworkIdent].Keys.ToArray();
43+
}
44+
45+
protected void InitializeInputs()
46+
{
47+
allInputs[this.NetworkIdent] = new Dictionary<string, WireInput>();
48+
foreach ( var inputName in WireGetInputs() ) {
49+
allInputs[this.NetworkIdent][inputName] = new WireInput( (Entity)this, inputName );
50+
}
51+
}
52+
abstract public string[] WireGetInputs();
53+
}
54+
55+
}

code/entities/WireOutputEntity.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace Sandbox
5+
{
6+
public class WireOutput
7+
{
8+
public int value;
9+
public Entity entity;
10+
public string outputName;
11+
public List<WireInput> connected = new List<WireInput>();
12+
13+
public WireOutput( Entity entity, string newOutputName )
14+
{
15+
this.entity = entity;
16+
outputName = newOutputName;
17+
}
18+
}
19+
20+
public interface WireOutputEntity
21+
{
22+
protected static Dictionary<int, Dictionary<string, WireOutput>> allOutputs = new Dictionary<int, Dictionary<string, WireOutput>>();
23+
24+
public int NetworkIdent { get; }
25+
26+
public static void WireTriggerOutput( WireOutputEntity ent, string outputName, int value )
27+
{
28+
var output = ent.GetOutput( outputName );
29+
foreach ( var input in output.connected ) {
30+
if ( !input.entity.IsValid() ) continue;
31+
if ( input.entity is WireInputEntity inputEntity ) {
32+
inputEntity.WireTriggerInput( input.inputName, value );
33+
}
34+
}
35+
}
36+
public void WireConnect( WireInputEntity inputEnt, string outputName, string inputName )
37+
{
38+
GetOutput( outputName ).connected.Add( inputEnt.GetInput( inputName ) );
39+
}
40+
41+
public WireOutput GetOutput( string inputName )
42+
{
43+
if ( !allOutputs.ContainsKey( this.NetworkIdent ) ) {
44+
InitializeOutputs();
45+
}
46+
return allOutputs[this.NetworkIdent][inputName];
47+
}
48+
public string[] GetOutputNames()
49+
{
50+
if ( !allOutputs.ContainsKey( this.NetworkIdent ) ) {
51+
InitializeOutputs();
52+
}
53+
return allOutputs[this.NetworkIdent].Keys.ToArray();
54+
}
55+
56+
protected void InitializeOutputs()
57+
{
58+
allOutputs[this.NetworkIdent] = new Dictionary<string, WireOutput>();
59+
foreach ( var outputName in WireGetOutputs() ) {
60+
allOutputs[this.NetworkIdent][outputName] = new WireOutput( (Entity)this, outputName );
61+
}
62+
}
63+
abstract public string[] WireGetOutputs();
64+
}
65+
}

code/tools/Button.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
namespace Sandbox.Tools
3+
{
4+
[Library( "tool_button", Title = "Button", Description = "Create Buttons!", Group = "construction" )]
5+
public partial class ButtonTool : BaseTool
6+
{
7+
PreviewEntity previewModel;
8+
9+
protected override bool IsPreviewTraceValid( TraceResult tr )
10+
{
11+
if ( !base.IsPreviewTraceValid( tr ) )
12+
return false;
13+
14+
if ( tr.Entity is ButtonEntity )
15+
return false;
16+
17+
return true;
18+
}
19+
20+
public override void CreatePreviews()
21+
{
22+
if ( TryCreatePreview( ref previewModel, "models/wirebox/katlatze/button.vmdl" ) ) {
23+
previewModel.RelativeToNormal = false;
24+
}
25+
}
26+
27+
public override void Simulate()
28+
{
29+
if ( !Host.IsServer )
30+
return;
31+
32+
using ( Prediction.Off() ) {
33+
var input = Owner.Input;
34+
35+
if ( !input.Pressed( InputButton.Attack1 ) )
36+
return;
37+
38+
var startPos = Owner.EyePos;
39+
var dir = Owner.EyeRot.Forward;
40+
41+
var tr = Trace.Ray( startPos, startPos + dir * MaxTraceDistance )
42+
.Ignore( Owner )
43+
.Run();
44+
45+
if ( !tr.Hit )
46+
return;
47+
48+
if ( !tr.Entity.IsValid() )
49+
return;
50+
51+
CreateHitEffects( tr.EndPos );
52+
53+
if ( tr.Entity is ButtonEntity )
54+
return;
55+
56+
var ent = new ButtonEntity {
57+
Position = tr.EndPos,
58+
};
59+
60+
ent.SetModel( "models/wirebox/katlatze/button.vmdl" );
61+
62+
var attachEnt = tr.Body.IsValid() ? tr.Body.Entity : tr.Entity;
63+
64+
if ( attachEnt.IsValid() ) {
65+
ent.SetParent( tr.Body.Entity, tr.Body.PhysicsGroup.GetBodyBoneName( tr.Body ) );
66+
}
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)