Skip to content

Commit

Permalink
Updated Music Box Tweaker to have more control
Browse files Browse the repository at this point in the history
  • Loading branch information
bakscratch committed Oct 27, 2022
1 parent 91b3999 commit c18a4ee
Showing 1 changed file with 62 additions and 15 deletions.
77 changes: 62 additions & 15 deletions code/hammer/Sound/MusicBoxTweaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,47 @@
using Sandbox;
using System.ComponentModel.DataAnnotations;
using System;
using System.Collections.Generic;

namespace Platformer;

[Library( "plat_musicboxtweaker", Description = "Music Box Tweaker" )]
[EditorSprite( "materials/editor/musicboxtweaker/musicboxtweaker.vmat" )]
[Display( Name = "Music Box Tweaker", GroupName = "Platformer", Description = "Platformer Soundscape" ), Category( "Sound" ), Icon( "speaker" )]
[HammerEntity]
[BoundsHelper( "mins", "maxs", false, true )]
[BoundsHelper( "outermins", "outermaxs", false, true )]
[BoundsHelper( "innermins", "innermaxs", false, true )]
partial class MusicBoxTweaker : ModelEntity
{
[Net, Property, FGDType( "target_destination" )] public string TargetMusicBox { get; set; } = "";
[Net] public float Volume { get; set; } = 1;

[Property( "mins", Title = "Tweaker mins" )]
[Property( "outermins", Title = "Tweaker Outer Mins" )]
[Net]
public Vector3 Mins { get; set; } = new Vector3( -32, -32, 0 );
[DefaultValue( "-64 -64 -64" )]
public Vector3 Mins { get; set; } = new Vector3( -64, -64, -64 );

[Property( "maxs", Title = "Tweaker maxs" )]
[Property( "outermaxs", Title = "Tweaker Outer Maxs" )]
[Net]
public Vector3 Maxs { get; set; } = new Vector3( 32, 32, 64 );
[DefaultValue( "64 64 64" )]
public Vector3 Maxs { get; set; } = new Vector3( 64, 64, 64 );

[Property( "innermins", Title = "Tweaker Inner Mins" )]
[Net]
[DefaultValue( "-32 -32 -32" )]
public Vector3 InMins { get; set; } = new Vector3( -32, -32, -32 );

[Property( "innermaxs", Title = "Tweaker Inner Maxs" )]
[Net]
[DefaultValue( "32 32 32" )]
public Vector3 InMaxs { get; set; } = new Vector3( 32, 32, 32 );

[Net]
public BBox Outer { get; private set; }

[Net]
public BBox Inner { get; private set; }

private MusicBox MusicBox;
private Vector3[] DirectionLut = new Vector3[]
{
Expand All @@ -40,6 +60,11 @@ public override void Spawn()
base.Spawn();

Transmit = TransmitType.Always;

Outer = new BBox( Position + Mins, Position + Maxs );
Inner = new BBox( Position + InMins, Position + InMaxs );


}

[Event.Frame]
Expand All @@ -54,17 +79,18 @@ public void OnFrame()
pos = Local.Pawn.Position;
}

var bbox = new BBox( Position + Mins, Position + Maxs );
var bbox = Outer;
var playerBbox = new BBox( pos - new Vector3( 8, 8, 0 ), pos + new Vector3( 8, 8, 64 ) );

if ( !bbox.Overlaps( playerBbox ) )
return;

var dist = ShortestDistanceToSurface( bbox, pos ) - 8.0f;
var vol = Math.Max( dist.LerpInverse( 0f, 64f ), 0.1f );
var dist = ShortestDistanceToSurface( bbox, pos );
var vol = dist.Clamp( 0, 1 );

if ( BasePlayerController.Debug )
{

DebugOverlay.Box( bbox, Color.Green );
DebugOverlay.Text( vol.ToString(), bbox.Center, 0, 3000 );
}
Expand All @@ -76,21 +102,42 @@ private float ShortestDistanceToSurface( BBox bbox, Vector3 position )
{
var result = float.MaxValue;
var point = Vector3.Zero;


foreach ( var dir in DirectionLut )
{
var closetsPoint = bbox.ClosestPoint( position + dir * 10000 );
var dist = Vector3.DistanceBetween( closetsPoint, position + new Vector3( 0, 0, 48 ) );
if( dist < result )
var outerclosetsPoint = bbox.ClosestPoint( position + new Vector3( 0, 0, 48 ) + dir * 10000 );
var dist2 = Vector3.DistanceBetween( outerclosetsPoint, position + new Vector3( 0, 0, 48 ) );
if ( dist2 < result )
{
point = closetsPoint;
result = dist;
point = outerclosetsPoint;
result = dist2;
}
}

var innerclosetsPoint = Inner.ClosestPoint( position + new Vector3( 0, 0, 48 ) );
var outerclosetsPoint1 = Outer.ClosestPoint( position + new Vector3( 0, 0, 48 ) );
var maxdist = Vector3.DistanceBetween( innerclosetsPoint, point );
var dist = result / maxdist;
if ( dist < result )
{
result = dist;
}

if ( BasePlayerController.Debug )
{
DebugOverlay.Sphere( point, 3f, Color.Red, 0, false );
DebugOverlay.Line( point, position + new Vector3(0,0,48), 0f, false );
DebugOverlay.Text( result.ToString(), bbox.Center, 0, 3000 );
DebugOverlay.Sphere( Inner.Center, 3f, Color.Red, 0, false );
DebugOverlay.Sphere( innerclosetsPoint, 3f, Color.Green, 0, false );
DebugOverlay.Sphere( outerclosetsPoint1, 3f, Color.Blue, 0, false );
DebugOverlay.Sphere( point, 3f, Color.Cyan, 0, false );

DebugOverlay.Line( innerclosetsPoint, Inner.Center, 0f, false );
DebugOverlay.Line( outerclosetsPoint1, innerclosetsPoint, 0f, false );
DebugOverlay.Line( outerclosetsPoint1, point, 0f, false );

DebugOverlay.Box( Outer, Color.Green );
DebugOverlay.Box( Inner, Color.Yellow );
}

return result;
Expand Down

0 comments on commit c18a4ee

Please sign in to comment.