Skip to content

Commit

Permalink
Sbox compatibility update
Browse files Browse the repository at this point in the history
  • Loading branch information
timmybo5 committed May 20, 2022
1 parent d385231 commit c14f637
Show file tree
Hide file tree
Showing 41 changed files with 304 additions and 128 deletions.
2 changes: 1 addition & 1 deletion code/ExamplePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public partial class ExamplePlayer : PlayerBase
public bool SupressPickupNotices { get; set; }
TimeSince timeSinceDropped;

public Clothing.Container Clothing = new();
public ClothingContainer Clothing = new();

public ExamplePlayer()
{
Expand Down
1 change: 0 additions & 1 deletion code/deathmatch_dep/ui/DeathmatchHud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public DeathmatchHud()

RootPanel.StyleSheet.Load("deathmatch_dep/ui/DeathmatchHud.scss");

RootPanel.AddChild<NameTags>();
RootPanel.AddChild<DamageIndicator>();

RootPanel.AddChild<InventoryBar>();
Expand Down
4 changes: 2 additions & 2 deletions code/deathmatch_dep/ui/InventoryBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public void ProcessClientInput(InputBuilder input)
//
// Fire pressed when we're open - select the weapon and close.
//
if (input.Down(InputButton.Attack1))
if (input.Down(InputButton.PrimaryAttack))
{
input.SuppressButton(InputButton.Attack1);
input.SuppressButton(InputButton.PrimaryAttack);
input.ActiveChild = SelectedWeapon;
IsOpen = false;
Sound.FromScreen("dm.ui_select");
Expand Down
8 changes: 5 additions & 3 deletions code/deathmatch_dep/ui/KillFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
using SWB_Base;
using System;

public partial class KillFeed : Sandbox.UI.KillFeed
public partial class KillFeed : Panel
{
public static KillFeed Current;

public KillFeed()
{
StyleSheet.Load("deathmatch_dep/ui/KillFeed.scss");
}

public override Panel AddEntry(long lsteamid, string left, long rsteamid, string right, string method)
public virtual Panel AddEntry(long lsteamid, string left, long rsteamid, string right, string method)
{
Log.Info($"{left} killed {right} using {method}");

Expand All @@ -27,7 +29,7 @@ public override Panel AddEntry(long lsteamid, string left, long rsteamid, string
// Temp solution ( get reference to kill weapon icon )
if (method.StartsWith("swb_"))
{
var killWeapon = Library.Create<WeaponBase>(method); // throws error when not found
var killWeapon = TypeLibrary.Create<WeaponBase>(method); // throws error when not found

if (!string.IsNullOrEmpty(killWeapon.Icon))
{
Expand Down
100 changes: 100 additions & 0 deletions code/deathmatch_dep/ui/NameTags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Linq;
using Sandbox;
using Sandbox.UI;
using Sandbox.UI.Construct;
using SWB_Base;

/// <summary>
/// When a player is within radius of the camera we add this to their entity.
/// We remove it again when they go out of range.
/// </summary>
internal class NameTagComponent : EntityComponent<PlayerBase>
{
NameTag NameTag;

protected override void OnActivate()
{
NameTag = new NameTag(Entity.Client?.Name ?? Entity.Name, Entity.Client?.PlayerId);
}

protected override void OnDeactivate()
{
NameTag?.Delete();
NameTag = null;
}

/// <summary>
/// Called for every tag, while it's active
/// </summary>
[Event.Frame]
public void FrameUpdate()
{
var tx = Entity.GetAttachment("hat") ?? Entity.Transform;
tx.Position += Vector3.Up * 10.0f;
tx.Rotation = Rotation.LookAt(-CurrentView.Rotation.Forward);

NameTag.Transform = tx;
}

/// <summary>
/// Called once per frame to manage component creation/deletion
/// </summary>
[Event.Frame]
public static void SystemUpdate()
{
// Check if deathmatch hud is active
var hasDeathmatchHud = Sandbox.Entity.All.OfType<DeathmatchHud>().Count() > 0;

if (!hasDeathmatchHud) return;

foreach (var player in Sandbox.Entity.All.OfType<PlayerBase>())
{
if (player.IsLocalPawn && player.IsFirstPersonMode)
{
var c = player.Components.Get<NameTagComponent>();
c?.Remove();
continue;
}

var shouldRemove = player.Position.Distance(CurrentView.Position) > 500;
shouldRemove = shouldRemove || player.LifeState != LifeState.Alive;
shouldRemove = shouldRemove || player.IsDormant;

if (shouldRemove)
{
var c = player.Components.Get<NameTagComponent>();
c?.Remove();
continue;
}

// Add a component if it doesn't have one
player.Components.GetOrCreate<NameTagComponent>();
}
}
}

/// <summary>
/// A nametag panel in the world
/// </summary>
public class NameTag : WorldPanel
{
public Panel Avatar;
public Label NameLabel;

internal NameTag(string title, long? steamid)
{
StyleSheet.Load("deathmatch_dep/ui/Nametags.scss");

if (steamid != null)
{
Avatar = Add.Panel("avatar");
Avatar.Style.SetBackgroundImage($"avatar:{steamid}");
}

NameLabel = Add.Label(title, "title");

// this is the actual size and shape of the world panel
PanelBounds = new Rect(-500, -100, 1000, 200);
}
}
22 changes: 22 additions & 0 deletions code/deathmatch_dep/ui/Nametags.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
NameTag {
justify-content: center;
align-items: center;
mix-blend-mode: lighten;

.title {
font-family: Roboto;
font-size: 30px;
white-space: nowrap;
color: #fa2;
mix-blend-mode: lighten;
text-shadow: 0px 0px 5px #f40;
}

.avatar {
width: 30px;
height: 30px;
margin-right: 15px;
border-radius: 100px;
background-size: cover;
}
}
2 changes: 1 addition & 1 deletion code/swb_base/AttachmentModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace SWB_Base
{
public class AttachmentModel : AnimEntity
public class AttachmentModel : AnimatedEntity
{
public AttachmentModel() { }

Expand Down
4 changes: 2 additions & 2 deletions code/swb_base/BulletBase.Physical.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ private void BulletHit(TraceResult tr)
{
var hitRotation = Rotation.From(new Angles(tr.Normal.z, tr.Normal.y, 0) * 90);

DebugOverlay.Circle(tr.EndPosition, hitRotation, bulletSize, IsServer ? Color.Red : Color.Blue, false, maxLifeTime);
DebugOverlay.Circle(tr.EndPosition, hitRotation, bulletSize, IsServer ? Color.Red : Color.Blue, maxLifeTime, false);

if (IsServer)
{
var distance = startPos.Distance(Position) / InchesPerMeter;
DebugOverlay.ScreenText(Rand.Int(40), distance.ToString(CultureInfo.InvariantCulture), maxLifeTime);
DebugOverlay.ScreenText(distance.ToString(CultureInfo.InvariantCulture), Rand.Int(40), maxLifeTime);
}
}

Expand Down
6 changes: 3 additions & 3 deletions code/swb_base/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace SWB_Base
{
internal class Commands
{
[ServerCmd("swb_editor_model", Help = "Opens the model editor")]
[ConCmd.Server("swb_editor_model", Help = "Opens the model editor")]
public static void OpenModelEditor()
{
Client client = ConsoleSystem.Caller;
Expand All @@ -16,7 +16,7 @@ public static void OpenModelEditor()
}
}

[ServerCmd("swb_editor_attachment", Help = "Opens the attachment editor")]
[ConCmd.Server("swb_editor_attachment", Help = "Opens the attachment editor")]
public static void OpenAttachmentEditor()
{
Client client = ConsoleSystem.Caller;
Expand All @@ -28,7 +28,7 @@ public static void OpenAttachmentEditor()
}
}

[ServerCmd("swb_attachment_equip", Help = "Equips an attachment by name")]
[ConCmd.Server("swb_attachment_equip", Help = "Equips an attachment by name")]
public static void EquipAttachmentCMD(string name, bool enabled)
{
Client client = ConsoleSystem.Caller;
Expand Down
27 changes: 26 additions & 1 deletion code/swb_base/PlayerBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using Sandbox;
using SWB_Base.UI;

Expand All @@ -7,6 +8,9 @@ namespace SWB_Base
public partial class PlayerBase : Player
{
public DamageInfo LastDamage;
private ScreenShakeStruct lastScreenShake;
private TimeSince timeSinceShake;
private float nextShake;

public override void TakeDamage(DamageInfo info)
{
Expand Down Expand Up @@ -34,6 +38,27 @@ public override void TakeDamage(DamageInfo info)
}
}

public override void PostCameraSetup(ref CameraSetup camSetup)
{
base.PostCameraSetup(ref camSetup);

if (timeSinceShake < lastScreenShake.Length && timeSinceShake > nextShake)
{
var random = new Random();
camSetup.Position += new Vector3(random.Float(0, lastScreenShake.Size), random.Float(0, lastScreenShake.Size), random.Float(0, lastScreenShake.Size));
camSetup.Rotation *= Rotation.From(new Angles(random.Float(0, lastScreenShake.Rotation), random.Float(0, lastScreenShake.Rotation), 0));

nextShake = timeSinceShake + lastScreenShake.Delay;
}
}

public virtual void ScreenShake(ScreenShakeStruct screenShake)
{
lastScreenShake = screenShake;
timeSinceShake = 0;
nextShake = 0;
}

public virtual bool Alive()
{
return Health > 0;
Expand Down
1 change: 1 addition & 0 deletions code/swb_base/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public ViewModelBase(WeaponBase weapon)
public override void PostCameraSetup(ref CameraSetup camSetup)
{
base.PostCameraSetup(ref camSetup);

camSetup.FieldOfView = weapon.FOV;
Rotation = camSetup.Rotation;
Position = camSetup.Position;
Expand Down
10 changes: 5 additions & 5 deletions code/swb_base/WeaponBase.Attack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ public virtual bool CanAttack(ClipInfo clipInfo, TimeSince lastAttackTime, Input
/// </summary>
public virtual bool CanPrimaryAttack()
{
return CanAttack(Primary, TimeSincePrimaryAttack, InputButton.Attack1);
return CanAttack(Primary, TimeSincePrimaryAttack, InputButton.PrimaryAttack);
}

/// <summary>
/// Checks if weapon can do the secondary attack
/// </summary>
public virtual bool CanSecondaryAttack()
{
return CanAttack(Secondary, TimeSinceSecondaryAttack, InputButton.Attack2);
return CanAttack(Secondary, TimeSinceSecondaryAttack, InputButton.SecondaryAttack);
}

/// <summary>
Expand Down Expand Up @@ -104,7 +104,7 @@ public virtual void Attack(ClipInfo clipInfo, bool isPrimary)
}

// Player anim
(Owner as AnimEntity).SetAnimParameter("b_attack", true);
(Owner as AnimatedEntity).SetAnimParameter("b_attack", true);

// Shoot effects
if (IsLocalPawn)
Expand Down Expand Up @@ -163,7 +163,7 @@ async Task AsyncAttack(ClipInfo clipInfo, bool isPrimary, float delay)
TimeSinceSecondaryAttack -= delay;

// Player anim
(Owner as AnimEntity).SetAnimParameter("b_attack", true);
(Owner as AnimatedEntity).SetAnimParameter("b_attack", true);

// Play pre-fire animation
ShootEffects(null, null, GetShootAnimation(clipInfo));
Expand Down Expand Up @@ -378,7 +378,7 @@ protected virtual void ShootEffects(string muzzleFlashParticle, string bulletEje
if (!string.IsNullOrEmpty(shootAnim))
{
ViewModelEntity?.SetAnimParameter(shootAnim, true);
CrosshairPanel?.CreateEvent("fire", (60f / Primary.RPM));
crosshair?.CreateEvent("fire", (60f / Primary.RPM));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions code/swb_base/WeaponBase.Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public override void Attack(ClipInfo clipInfo, bool isPrimary)
}

// Player anim
(Owner as AnimEntity).SetAnimParameter("b_attack", true);
(Owner as AnimatedEntity).SetAnimParameter("b_attack", true);

// Shoot effects
if (IsLocalPawn)
Expand Down Expand Up @@ -143,7 +143,7 @@ async Task AsyncAttack(ClipInfo clipInfo, bool isPrimary, float delay)
TimeSinceSecondaryAttack -= delay;

// Player anim
(Owner as AnimEntity).SetAnimParameter("b_attack", true);
(Owner as AnimatedEntity).SetAnimParameter("b_attack", true);

// Play pre-fire animation
ShootEffects(null, null, GetShootAnimation(clipInfo));
Expand Down
8 changes: 5 additions & 3 deletions code/swb_base/WeaponBase.Getters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ public virtual float GetRealSpread(float baseSpread = -1)
// Aiming
if (IsZooming && this is not WeaponBaseShotty)
floatMod /= 4;

if (Owner.GroundEntity == null) {

if (Owner.GroundEntity == null)
{
// Jumping
floatMod += 0.75f;
} else if (Owner.Velocity.Length > 100)
}
else if (Owner.Velocity.Length > 100)
{
// Moving
floatMod += 0.25f;
Expand Down
6 changes: 3 additions & 3 deletions code/swb_base/WeaponBase.Melee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public virtual void MeleeAttack(float damage, float force, string hitAnimation,
}

DoMeleeEffects(hitAnimation, sound);
(Owner as AnimEntity).SetAnimParameter("b_attack", true);
(Owner as AnimatedEntity).SetAnimParameter("b_attack", true);

if (!hitEntity || !IsServer) return;

Expand All @@ -112,12 +112,12 @@ private bool CanMelee(TimeSince lastAttackTime, float attackSpeed, InputButton i

public override bool CanPrimaryAttack()
{
return CanMelee(TimeSincePrimaryAttack, SwingSpeed, InputButton.Attack1);
return CanMelee(TimeSincePrimaryAttack, SwingSpeed, InputButton.PrimaryAttack);
}

public override bool CanSecondaryAttack()
{
return CanMelee(TimeSincePrimaryAttack, StabSpeed, InputButton.Attack2);
return CanMelee(TimeSincePrimaryAttack, StabSpeed, InputButton.SecondaryAttack);
}

public override void AttackPrimary()
Expand Down
2 changes: 1 addition & 1 deletion code/swb_base/WeaponBase.Shotty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class WeaponBaseShotty : WeaponBase
/// <summary>Duration of the reload insert animatio</summary>
public virtual float ShellReloadTimeInsert => -1;

/// <summary>The shell eject delay after firing</summary>
/// <summary>The shell eject delay after firing</summary>
public virtual float ShellEjectDelay => -1;

/// <summary>Animation for finishing the reload</summary>
Expand Down
Loading

0 comments on commit c14f637

Please sign in to comment.