Skip to content

Commit

Permalink
Merge from MusicBox
Browse files Browse the repository at this point in the history
  • Loading branch information
bakscratch committed Oct 20, 2022
1 parent 77b45e7 commit 7ad3aa0
Show file tree
Hide file tree
Showing 26 changed files with 308 additions and 20 deletions.
12 changes: 10 additions & 2 deletions .addon
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"MinPlayers": 1,
"RankType": "Best",
"LeaderboardType": "Ascending",
"PerMapRanking": false,
"PerMapRanking": true,
"MapSelect": "Tagged",
"MaxPlayers": 32,
"GameNetworkType": "Multiplayer",
Expand Down Expand Up @@ -56,7 +56,7 @@
}
]
},
"GameCategory": "None",
"GameCategory": "Retro",
"GameSettings": {
"pl_gametime": {
"DisplayType": "Integer",
Expand All @@ -78,6 +78,14 @@
"Minimum": 3,
"Maximum": 20
}
},
"DefaultLeaderboards": [
"Time"
],
"Compiler": {
"RootNamespace": "Sandbox",
"DefineConstants": "SANDBOX;ADDON;DEBUG",
"NoWarn": "1701;1702;1591;"
}
}
}
18 changes: 18 additions & 0 deletions code/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Platformer.UI;
using System.Collections.Generic;
using Platformer.Gamemodes;
using System.Threading.Tasks;

namespace Platformer;

Expand Down Expand Up @@ -115,4 +116,21 @@ public enum GameModes
Tag = 2,
Brawl = 3
}
public static async void SubmitScore( string bucket, Client client, int score )
{

var leaderboard = await Leaderboard.FindOrCreate( bucket, false );

await leaderboard.Value.Submit( client, score );

}

public static async Task<LeaderboardEntry?> GetScore( string bucket, Client client )
{

var leaderboard = await Leaderboard.FindOrCreate( bucket, false );

return await leaderboard.Value.GetScore( client.PlayerId );

}
}
17 changes: 12 additions & 5 deletions code/UI/Base/Chatbox/PlatformerChatBox.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

using Sandbox;
using Sandbox;
using Sandbox.UI;
using Sandbox.UI.Construct;
using System;

namespace Platformer.UI
{
Expand All @@ -13,6 +11,16 @@ public partial class PlatformerChatBox : Panel
public Panel Canvas { get; protected set; }
public TextEntry Input { get; protected set; }

public bool IsOpen
{
get => HasClass( "open" );
set
{
if ( IsOpen ) Close();
else Open();
}
}

public PlatformerChatBox()
{
Current = this;
Expand All @@ -27,14 +35,13 @@ public PlatformerChatBox()
Input.AcceptsFocus = true;
Input.AllowEmojiReplace = true;

Sandbox.Hooks.Chat.OnOpenChat += Open;

}

void Open()
{
AddClass( "open" );
Input.Focus();
Canvas.TryScrollToBottom();
}

void Close()
Expand Down
3 changes: 1 addition & 2 deletions code/UI/Base/Chatbox/PlatformerChatBox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ platformerchatentry {
transform: scale( 0 );
}

&:outro {
&.faded {
opacity: 0;
transition: all 2s ease-out;
}

image {
Expand Down
5 changes: 1 addition & 4 deletions code/UI/Base/Chatbox/PlatformerChatEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public override void Tick()
{
base.Tick();

if ( TimeSinceBorn > 10 )
{
Delete();
}
SetClass( "faded", TimeSinceBorn > 10f );
}
}
}
3 changes: 1 addition & 2 deletions code/hammer/Gameplay/CoopBlocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public void Tick()
{

Enabled = ForCoop ? !Platformer.CoopMode : Platformer.CoopMode;
Solid = ForCoop ? !Platformer.CoopMode : Platformer.CoopMode;

Collisions = ForCoop ? !Platformer.CoopMode : Platformer.CoopMode;

}
}
Expand Down
2 changes: 1 addition & 1 deletion code/hammer/Gameplay/WaitingBlocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Tick()
if ( Platformer.GameState == GameStates.Live )
{
Enabled = false;
Solid = false;
Collisions = false;
}
}
}
66 changes: 66 additions & 0 deletions code/hammer/Sound/MusicBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using SandboxEditor;
using Sandbox;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Platformer;

[Library( "plat_musicbox", Description = "Music Box" )]
[EditorSprite( "materials/editor/musicbox/musicbox.vmat" )]
[Display( Name = "Music Box", GroupName = "Platformer", Description = "Platformer Soundscape" ), Category( "Sound" ), Icon( "speaker" )]
[HammerEntity]
partial class MusicBox : Entity
{
/// <summary>
/// Name of the sound to play.
/// </summary>
[Property( "soundName" ), FGDType( "sound" )]
[Net] public string SoundName { get; set; }

/// <summary>
/// Name of the sound to play.
/// </summary>
[Property( "bgsoundName" ), FGDType( "sound" )]
[Net] public string BGSoundName { get; set; }

[Net] public float Volume { get; set; }

public Sound PlayingSound { get; protected set; }
public Sound BGPlayingSound { get; protected set; }


public override void Spawn()
{
base.Spawn();

Transmit = TransmitType.Always;
}

public override void ClientSpawn()
{
// OnStartSound();
}

[Event.Tick.Client]
public void Tick()
{
if ( PlayingSound.Index <= 0 )
{
OnStartSound();
}
}

[ClientRpc]
protected void OnStartSound()
{
PlayingSound = Sound.FromScreen( SoundName ).SetVolume( 1 );
BGPlayingSound = Sound.FromScreen( BGSoundName ).SetVolume( .1f );
}

[ClientRpc]
public void UpdateVolume(float vol)
{
PlayingSound.SetVolume( 1 - vol );
BGPlayingSound.SetVolume( vol );
}
}
99 changes: 99 additions & 0 deletions code/hammer/Sound/MusicBoxTweaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

using SandboxEditor;
using Sandbox;
using System.ComponentModel.DataAnnotations;
using System;

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 )]
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" )]
[Net]
public Vector3 Mins { get; set; } = new Vector3( -32, -32, 0 );

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

private MusicBox MusicBox;
private Vector3[] DirectionLut = new Vector3[]
{
Vector3.Up,
Vector3.Down,
Vector3.Left,
Vector3.Right,
Vector3.Forward,
Vector3.Backward
};

public override void Spawn()
{
base.Spawn();

Transmit = TransmitType.Always;
}

[Event.Frame]
public void OnFrame()
{
MusicBox ??= FindByName( TargetMusicBox ) as MusicBox;
if ( !MusicBox.IsValid() ) return;

var pos = CurrentView.Position;
if ( Local.Pawn.IsValid() )
{
pos = Local.Pawn.Position;
}

var bbox = new BBox( Position + Mins, Position + Maxs );
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 );

if ( BasePlayerController.Debug )
{
DebugOverlay.Box( bbox, Color.Green );
DebugOverlay.Text( vol.ToString(), bbox.Center, 0, 3000 );
}

MusicBox.UpdateVolume( vol );
}

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 )
{
point = closetsPoint;
result = dist;
}
}

if ( BasePlayerController.Debug )
{
DebugOverlay.Sphere( point, 3f, Color.Red, 0, false );
DebugOverlay.Line( point, position + new Vector3(0,0,48), 0f, false );
}

return result;
}

}
57 changes: 57 additions & 0 deletions code/hammer/Sound/MusicBoxTweakerRadius.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

using SandboxEditor;
using Sandbox;
using System.ComponentModel.DataAnnotations;
using System;

namespace Platformer;

[Library( "plat_musicboxtweakerradius", Description = "Music Box Tweaker Radius" )]
[EditorSprite( "editor/ent_logic.vmat" )]
[Display( Name = "Music Box Tweaker", GroupName = "Platformer", Description = "Platformer Soundscape" ), Category( "Sound" ), Icon( "speaker" )]
[HammerEntity]
[Sphere( "radius" )]
partial class MusicBoxTweakerRadius : ModelEntity
{
[Net, Property, FGDType( "target_destination" )] public string TargetMusicBox { get; set; } = "";
[Net] public float Volume { get; set; } = 1;
[Net, Property]
public float Radius { get; set; } = 128.0f;

private MusicBox MusicBox;

public override void Spawn()
{
base.Spawn();

Transmit = TransmitType.Always;
}

[Event.Frame]
public void OnFrame()
{
MusicBox ??= FindByName( TargetMusicBox ) as MusicBox;
if ( !MusicBox.IsValid() ) return;

var pos = CurrentView.Position;
if ( Local.Pawn.IsValid() )
{
pos = Local.Pawn.Position;
}

var dist = Position.Distance( pos );
if ( dist > Radius )
return;

var vol = (Radius - dist).LerpInverse( 0, 64f );
vol = Math.Max( 0.1f, vol );

MusicBox.UpdateVolume( vol );

if ( BasePlayerController.Debug )
{
DebugOverlay.Text( vol.ToString(), Position );
}
}

}
3 changes: 2 additions & 1 deletion code/player/Player.Timer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ public async Task CompleteCourseAsync()
{
BestTime = TimeSinceStart;

_ = await GameServices.UpdateLeaderboard( Client.PlayerId, BestTime );
Platformer.SubmitScore( "Time", Client, BestTime.CeilToInt() );
}
}
}


public void ResetTimer()
{
TimerState = TimerState.InStartZone;
Expand Down
2 changes: 1 addition & 1 deletion code/player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public override void OnKilled()

BaseGamemode.Instance.DoPlayerKilled( this );

BecomeRagdollOnClient( Velocity, lastDamage.Flags, lastDamage.Position, lastDamage.Force, GetHitboxBone( lastDamage.HitboxIndex ) );
BecomeRagdollOnClient( Velocity, lastDamage.Flags, lastDamage.Position, lastDamage.Force, lastDamage.BoneIndex );
}

private TimeUntil TimeUntilCanUse;
Expand Down
Loading

0 comments on commit 7ad3aa0

Please sign in to comment.