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 Attachments = new(); protected override void OnAwake() { Tags.Add( TagsHelper.Weapon ); Attachments = Components.GetAll( 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() { if ( IsProxy ) return; CreateUI(); } protected override void OnDisabled() { if ( IsProxy ) return; IsReloading = false; IsScoping = false; IsAiming = false; IsCustomizing = false; DestroyUI(); } [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(); CreateModels(); // Attachments (load for clients joining late) 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(); } ); } } protected override void OnUpdate() { if ( Owner is null ) return; // UpdateModels(); Owner.AnimationHelper.HoldType = HoldType; 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() { WorldModelRenderer = Components.Create(); 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( resourceID ); if ( sound is null ) return; Sound.Play( sound, WorldPosition ); } }