229 lines
5.2 KiB
C#
Raw Permalink Normal View History

2024-10-30 19:01:58 +03:00
using SWB.Base.Attachments;
using SWB.Shared;
using System.Collections.Generic;
using System.Linq;
namespace SWB.Base;
[Group( "SWB" )]
[Title( "Weapon" )]
public partial class Weapon : Component, IInventoryItem
{
public IPlayerBase Owner { get; private set; }
public SkinnedModelRenderer WorldModelRenderer { get; private set; }
public WeaponSettings Settings { get; private set; }
public List<Attachment> Attachments = new();
protected override void OnAwake()
{
Tags.Add( TagsHelper.Weapon );
Attachments = Components.GetAll<Attachment>( FindMode.EverythingInSelf ).OrderBy( att => att.Name ).ToList();
Settings = WeaponSettings.Instance;
InitialPrimaryStats = StatsModifier.FromShootInfo( Primary );
InitialSecondaryStats = StatsModifier.FromShootInfo( Primary );
}
protected override void OnDestroy()
{
}
protected override void OnEnabled()
{
2024-10-30 19:48:58 +03:00
if ( Owner.IsValid ) Owner.AnimationHelper.HoldType = HoldType;
2024-10-30 19:01:58 +03:00
if ( IsProxy ) return;
CreateUI();
}
protected override void OnDisabled()
{
2024-10-30 19:48:58 +03:00
if ( Owner.IsValid ) Owner.AnimationHelper.HoldType = AnimationHelper.HoldTypes.None;
2024-10-30 19:01:58 +03:00
if ( IsProxy ) return;
2024-10-30 19:48:58 +03:00
2024-10-30 19:01:58 +03:00
IsReloading = false;
IsScoping = false;
IsAiming = false;
IsCustomizing = false;
DestroyUI();
2024-10-30 19:48:58 +03:00
2024-10-30 19:01:58 +03:00
}
[Broadcast]
public void OnCarryStart()
{
if ( !IsValid ) return;
GameObject.Enabled = true;
}
[Broadcast]
public void OnCarryStop()
{
if ( !IsValid ) return;
GameObject.Enabled = false;
}
public bool CanCarryStop()
{
return TimeSinceDeployed > 0;
}
public void OnDeploy()
{
var delay = 0f;
if ( Primary.Ammo == 0 && !string.IsNullOrEmpty( DrawEmptyAnim ) )
{
delay = DrawEmptyTime;
}
else if ( !string.IsNullOrEmpty( DrawAnim ) )
{
delay = DrawTime;
}
TimeSinceDeployed = -delay;
// Sound
if ( DeploySound is not null )
PlaySound( DeploySound.ResourceId );
// Start drawing
// Boltback
if ( InBoltBack )
AsyncBoltBack( delay );
}
protected override void OnStart()
{
Owner = Components.GetInAncestors<IPlayerBase>();
CreateModels();
// Attachments (load for clients joining late)
2024-10-30 19:48:58 +03:00
// if ( IsProxy )
// {
// // Log.Info( "Checking -> " + Network.Owner.DisplayName + "'s " + DisplayName + " for attachments" );
// Attachments.ForEach( att =>
// {
// // Log.Info( "[" + att.Name + "] equipped ->" + att.Equipped );
// if ( att is not null && att.Equipped )
// att.Equip();
// } );
// }
2024-10-30 19:01:58 +03:00
}
protected override void OnUpdate()
{
if ( Owner is null ) return;
2024-10-30 19:48:58 +03:00
// UpdateModels()
2024-10-30 19:01:58 +03:00
if ( !IsProxy )
{
// if ( IsDeploying ) return;
// Customization
if ( WeaponSettings.Instance.Customization && !IsScoping && !IsAiming && Input.Pressed( InputButtonHelper.Menu ) && Attachments.Count > 0 )
{
if ( !IsCustomizing )
OpenCustomizationMenu();
else
CloseCustomizationMenu();
IsCustomizing = !IsCustomizing;
}
// Don't cancel reload when customizing
if ( IsCustomizing && !IsReloading ) return;
if ( Scoping )
{
if ( IsAiming && !IsScoping )
OnScopeStart();
else if ( !IsAiming && IsScoping )
OnScopeEnd();
}
ResetBurstFireCount( Primary, InputButtonHelper.PrimaryAttack );
ResetBurstFireCount( Secondary, InputButtonHelper.SecondaryAttack );
BarrelHeatCheck();
// var shouldTuck = ShouldTuck();
if ( CanPrimaryShoot() )
{
if ( IsReloading && ShellReloading && ShellReloadingShootCancel )
CancelShellReload();
TimeSincePrimaryShoot = 0;
Shoot( Primary, true );
}
else if ( CanSecondaryShoot())
{
TimeSinceSecondaryShoot = 0;
Shoot( Secondary, false );
}
else if ( Input.Down( InputButtonHelper.Reload ) )
{
if ( ShellReloading )
OnShellReload();
else
Reload();
}
if ( IsReloading && TimeSinceReload >= 0 )
{
if ( ShellReloading )
OnShellReloadFinish();
else
OnReloadFinish();
}
}
}
void CreateModels()
2024-10-30 19:59:34 +03:00
{
2024-10-30 19:01:58 +03:00
WorldModelRenderer = Components.Create<SkinnedModelRenderer>();
WorldModelRenderer.Model = WorldModel;
WorldModelRenderer.AnimationGraph = WorldModel.AnimGraph;
WorldModelRenderer.CreateBoneObjects = true;
ModelUtil.ParentToBone( GameObject, Owner.BodyRenderer, "hold_R" );
Network.ClearInterpolation();
Log.Info("START WEAPON");
}
// Temp fix until https://github.com/Facepunch/sbox-issues/issues/5247 is fixed
// void ResetViewModelAnimations()
// {
// ViewModelRenderer?.Set( Primary.ShootAnim, false );
// ViewModelRenderer?.Set( Primary.ShootEmptyAnim, false );
// ViewModelRenderer?.Set( Primary.ShootAimedAnim, false );
//
// if ( Secondary is not null )
// {
// ViewModelRenderer?.Set( Secondary.ShootAnim, false );
// ViewModelRenderer?.Set( Secondary.ShootEmptyAnim, false );
// ViewModelRenderer?.Set( Secondary.ShootAimedAnim, false );
// }
//
// ViewModelRenderer?.Set( ReloadAnim, false );
// ViewModelRenderer?.Set( ReloadEmptyAnim, false );
// ViewModelRenderer?.Set( DrawAnim, false );
// ViewModelRenderer?.Set( DrawEmptyAnim, false );
// }
[Broadcast]
void PlaySound( int resourceID )
{
if ( !IsValid ) return;
var sound = ResourceLibrary.Get<SoundEvent>( resourceID );
if ( sound is null ) return;
Sound.Play( sound, WorldPosition );
}
}