using System; using Sandbox.Citizen; using Sandbox.Network; using ShrimpleCharacterController; using SWB.Base; using SWB.Shared; using DamageInfo = SWB.Shared.DamageInfo; public sealed class Kal : Component, IPlayerBase { public ShrimpleCharacterController.ShrimpleCharacterController CharacterController { get; set; } public AnimationHelper AnimationHelper { get; set; } private RagdollController RagdollController { get; set; } public SkinnedModelRenderer BodyRenderer { get; set; } [Property] public GameObject Body { get; set; } [Property] public GameObject CameraPivot { get; set; } public CameraComponent Camera { get; set; } public IInventory Inventory { get; set; } public bool IsAlive { get; set; } = true; [Property] public int MaxHealth { get; set; } public int Health { get; set; } public int Kills { get; set; } public int Deaths { get; set; } [Sync] public NetDictionary Ammo { get; set; } = new(); public int AmmoCount( string ammoType ) { if ( Ammo.TryGetValue( ammoType, out var amount ) ) { return amount; } return 0; } public int TakeAmmo( string type, int amount ) { throw new NotImplementedException(); } public void TakeDamage( DamageInfo info ) { Health -= (int)info.Damage; } public void ShakeScreen( ScreenShake screenShake ) { return; } [Property] [Range(1f, 200f, 1f)] public float CamOffsetX { get; set; } [Property] [Range(1f, 200f, 1f)] public float CamOffsetZ { get; set; } [Property] [Range(50f, 200f, 10f)] public float WalkSpeed { get; set; } = 100f; [Property] [Range(100f, 500f, 20f)] public float RunSpeed { get; set; } = 300f; [Property] [Range(25f, 100f, 5f)] public float DuckSpeed { get; set; } = 50f; [Property] [Range(200f, 500f, 20f)] public float JumpStrength { get; set; } = 350f; [Sync] public Angles EyeAngles { get; set; } protected override void OnStart() { base.OnStart(); CharacterController = Components.Get(); AnimationHelper = Components.Get(); RagdollController = Components.Get(); if ( !Network.IsOwner ) return; BodyRenderer = Components.Get(FindMode.EverythingInSelfAndDescendants); Camera = Scene.Camera; Camera.GameObject.SetParent(GameObject); var cameraComponent = Camera.Components.Create(); cameraComponent.ZFar = 32768f; AddWeapon(); } void AddWeapon() { var weaponRegistry = Scene.Components.GetInChildren(); var weaponGo = weaponRegistry.Get( "Pistol" ); var weapon = weaponGo.Components.Get( true ); weaponGo.NetworkSpawn(); weaponGo.SetParent(GameObject); Inventory = Components.Create(); Inventory.Add(weaponGo, false); } protected override void OnFixedUpdate() { base.OnFixedUpdate(); if ( Network.IsOwner ) { var multiplier = RagdollController.Enabled ? 0f : 1f; var wishDirection = Input.AnalogMove.Normal * multiplier * Rotation.FromYaw( EyeAngles.yaw ); var isDucking = Input.Down( "Duck" ); var isRunning = Input.Down( "Run" ); var wishSpeed = isDucking ? DuckSpeed : isRunning ? RunSpeed : WalkSpeed; if ( Input.Pressed( "Slot1" ) ) { Inventory.SetActive("Pistol"); } if ( Input.Pressed( "Slot2" ) ) { Inventory.Disarm(); } CharacterController.WishVelocity = wishDirection * wishSpeed; CharacterController.Move(); if ( Input.Pressed( "Jump" ) && CharacterController.IsOnGround ) { CharacterController.Punch( Vector3.Up * JumpStrength ); AnimationHelper.TriggerJump(); } if ( !AnimationHelper.IsValid() ) return; AnimationHelper.DuckLevel = isDucking ? 1f : 0f; } AnimationHelper.WithWishVelocity(CharacterController.WishVelocity); AnimationHelper.WithVelocity(CharacterController.Velocity); AnimationHelper.IsGrounded = CharacterController.IsOnGround; } protected override void OnUpdate() { base.OnUpdate(); if ( Network.IsOwner ) { EyeAngles += Input.AnalogLook; EyeAngles = EyeAngles.WithPitch(MathX.Clamp(EyeAngles.pitch, -90f, 70f)); //Vector3.Up * (CamOffsetZ - Math.Abs(EyeAngles.pitch * .5f) ) + + cameraOffset * EyeAngles.ToRotation() var cameraOffset = Vector3.Backward * (CamOffsetX + EyeAngles.pitch * .5f) - Vector3.Left * 20f; Camera.WorldPosition = CameraPivot.WorldPosition + cameraOffset * EyeAngles.ToRotation(); Camera.WorldRotation = EyeAngles.ToRotation(); } RotateBody(); } void RotateBody() { if(Body is null) return; var targetAngle = new Angles(0, EyeAngles.yaw, 0).ToRotation(); float rotateDifference = Body.WorldRotation.Distance(targetAngle); if(rotateDifference > 30f || CharacterController.Velocity.Length > 10f) { Body.WorldRotation = Rotation.Lerp(Body.WorldRotation, targetAngle, Time.Delta * 2f); } } }