Skip to content

Commit

Permalink
Refactor Button tool into BaseWireTool
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebual committed Jun 8, 2021
1 parent 4b26be2 commit 3a1dd15
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 62 deletions.
71 changes: 71 additions & 0 deletions code/tools/BaseWireTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
namespace Sandbox.Tools
{
public abstract partial class BaseWireTool : BaseTool
{
PreviewEntity previewModel;

abstract protected Type GetEntityType();
abstract protected string GetModel();
abstract protected ModelEntity SpawnEntity( TraceResult tr );

protected override bool IsPreviewTraceValid( TraceResult tr )
{
if ( !base.IsPreviewTraceValid( tr ) ) {
return false;
}
if ( tr.Entity.GetType().IsSubclassOf( GetEntityType() ) ) {
return false;
}

return true;
}

public override void CreatePreviews()
{
if ( TryCreatePreview( ref previewModel, GetModel() ) ) {
previewModel.RelativeToNormal = false;
}
}

public override void Simulate()
{
if ( !Host.IsServer ) {
return;
}

using ( Prediction.Off() ) {
var input = Owner.Input;

if ( !input.Pressed( InputButton.Attack1 ) )
return;

var startPos = Owner.EyePos;
var dir = Owner.EyeRot.Forward;

var tr = Trace.Ray( startPos, startPos + dir * MaxTraceDistance )
.Ignore( Owner )
.Run();

if ( !tr.Hit || !tr.Entity.IsValid() ) {
return;
}

CreateHitEffects( tr.EndPos );

if ( tr.Entity.GetType().IsSubclassOf( GetEntityType() ) ) {
return;
}

var ent = SpawnEntity( tr );
ent.SetModel( GetModel() );

var attachEnt = tr.Body.IsValid() ? tr.Body.Entity : tr.Entity;

if ( attachEnt.IsValid() ) {
ent.SetParent( tr.Body.Entity, tr.Body.PhysicsGroup.GetBodyBoneName( tr.Body ) );
}
}
}
}
}
66 changes: 9 additions & 57 deletions code/tools/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,21 @@
namespace Sandbox.Tools
{
[Library( "tool_button", Title = "Button", Description = "Create Buttons!", Group = "construction" )]
public partial class ButtonTool : BaseTool
public partial class ButtonTool : BaseWireTool
{
PreviewEntity previewModel;

protected override bool IsPreviewTraceValid( TraceResult tr )
protected override Type GetEntityType()
{
if ( !base.IsPreviewTraceValid( tr ) )
return false;

if ( tr.Entity is ButtonEntity )
return false;

return true;
return typeof( ButtonEntity );
}

public override void CreatePreviews()
protected override string GetModel()
{
if ( TryCreatePreview( ref previewModel, "models/wirebox/katlatze/button.vmdl" ) ) {
previewModel.RelativeToNormal = false;
}
return "models/wirebox/katlatze/button.vmdl";
}

public override void Simulate()
protected override ModelEntity SpawnEntity( TraceResult tr )
{
if ( !Host.IsServer )
return;

using ( Prediction.Off() ) {
var input = Owner.Input;

if ( !input.Pressed( InputButton.Attack1 ) )
return;

var startPos = Owner.EyePos;
var dir = Owner.EyeRot.Forward;

var tr = Trace.Ray( startPos, startPos + dir * MaxTraceDistance )
.Ignore( Owner )
.Run();

if ( !tr.Hit )
return;

if ( !tr.Entity.IsValid() )
return;

CreateHitEffects( tr.EndPos );

if ( tr.Entity is ButtonEntity )
return;

var ent = new ButtonEntity {
Position = tr.EndPos,
};

ent.SetModel( "models/wirebox/katlatze/button.vmdl" );

var attachEnt = tr.Body.IsValid() ? tr.Body.Entity : tr.Entity;

if ( attachEnt.IsValid() ) {
ent.SetParent( tr.Body.Entity, tr.Body.PhysicsGroup.GetBodyBoneName( tr.Body ) );
}
}
return new ButtonEntity {
Position = tr.EndPos,
};
}
}
}
1 change: 1 addition & 0 deletions code/tools/Wire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public override void Simulate()
var outputName = wireOutputProp.GetOutputNames()[0];
var inputName = wireInputProp.GetInputNames()[0];

//Log.Info("Wiring " + wireInputProp + "'s " + inputName + " to " + wireOutputProp + "'s " + outputName);
wireOutputProp.WireConnect( wireInputProp, outputName, inputName );
WireOutputEntity.WireTriggerOutput( wireOutputProp, outputName, wireOutputProp.GetOutput( outputName ).value );
Reset();
Expand Down
4 changes: 2 additions & 2 deletions models/wirebox/katlatze/button.vmdl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
[
{
_class = "RenderMeshFile"
filename = "models/katlatze/button.fbx"
filename = "models/wirebox/katlatze/button.fbx"
import_translation = [ 0.0, 0.0, 0.0 ]
import_rotation = [ 0.0, 0.0, 0.0 ]
import_scale = 0.1
Expand Down Expand Up @@ -98,7 +98,7 @@
recenter_on_parent_bone = false
offset_origin = [ 0.0, 0.0, 0.0 ]
offset_angles = [ 0.0, 0.0, 0.0 ]
filename = "models/katlatze/button.fbx"
filename = "models/wirebox/katlatze/button.fbx"
import_scale = 0.1
faceMergeAngle = 10.0
maxHullVertices = 0
Expand Down
4 changes: 2 additions & 2 deletions models/wirebox/katlatze/spoon.vmdl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
recenter_on_parent_bone = false
offset_origin = [ 0.0, 0.0, 0.0 ]
offset_angles = [ 0.0, 0.0, 0.0 ]
filename = "models/katlatze/spoon.dmx"
filename = "models/wirebox/katlatze/spoon.dmx"
import_scale = 1.0
faceMergeAngle = 25.0
maxHullVertices = 0
Expand All @@ -81,7 +81,7 @@
[
{
_class = "RenderMeshFile"
filename = "models/katlatze/spoon.dmx"
filename = "models/wirebox/katlatze/spoon.dmx"
import_translation = [ 0.0, 0.0, 0.0 ]
import_rotation = [ 0.0, 0.0, 0.0 ]
import_scale = 1.0
Expand Down
2 changes: 1 addition & 1 deletion unpack-assets.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
xcopy /E models\wirebox\ ..\..\..\models\wirebox\
xcopy /E /Y models\wirebox\ ..\..\..\models\wirebox\

0 comments on commit 3a1dd15

Please sign in to comment.