kakozuzo/Code/Kal.cs

171 lines
5.4 KiB
C#
Raw Normal View History

2024-10-30 13:15:49 +03:00
using System;
2024-10-29 18:52:47 +03:00
using Sandbox.Citizen;
2024-10-29 19:54:53 +03:00
using Sandbox.Network;
2024-10-29 18:52:47 +03:00
using ShrimpleCharacterController;
2024-10-30 19:01:58 +03:00
using SWB.Base;
using SWB.Shared;
using DamageInfo = SWB.Shared.DamageInfo;
2024-10-29 18:52:47 +03:00
2024-10-30 19:01:58 +03:00
public sealed class Kal : Component, IPlayerBase
2024-10-29 18:52:47 +03:00
{
2024-10-30 19:01:58 +03:00
public ShrimpleCharacterController.ShrimpleCharacterController CharacterController { get; set; }
public AnimationHelper AnimationHelper { get; set; }
private RagdollController RagdollController { get; set; }
public SkinnedModelRenderer BodyRenderer { get; set; }
2024-10-30 01:01:41 +03:00
[Property] public GameObject Body { get; set; }
2024-10-30 13:15:49 +03:00
[Property] public GameObject CameraPivot { get; set; }
2024-10-30 19:01:58 +03:00
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<string, int> 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;
}
2024-10-30 01:01:41 +03:00
[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;
2024-10-30 02:19:02 +03:00
2024-10-30 01:01:41 +03:00
[Sync] public Angles EyeAngles { get; set; }
protected override void OnStart()
2024-10-29 18:52:47 +03:00
{
base.OnStart();
2024-10-30 19:01:58 +03:00
CharacterController = Components.Get<ShrimpleCharacterController.ShrimpleCharacterController>();
2024-10-30 02:19:02 +03:00
AnimationHelper = Components.Get<AnimationHelper>();
2024-10-30 14:10:44 +03:00
RagdollController = Components.Get<RagdollController>();
2024-10-30 02:19:02 +03:00
2024-10-29 18:52:47 +03:00
if ( !Network.IsOwner ) return;
2024-10-30 02:19:02 +03:00
2024-10-30 19:01:58 +03:00
BodyRenderer = Components.Get<SkinnedModelRenderer>(FindMode.EverythingInSelfAndDescendants);
Camera = Scene.Camera;
Camera.GameObject.SetParent(GameObject);
2024-10-29 18:52:47 +03:00
var cameraComponent = Camera.Components.Create<CameraComponent>();
2024-10-30 19:01:58 +03:00
2024-10-29 18:52:47 +03:00
cameraComponent.ZFar = 32768f;
2024-10-30 19:48:58 +03:00
AddWeapon();
}
void AddWeapon()
{
var weaponRegistry = Scene.Components.GetInChildren<WeaponRegistry>();
var weaponGo = weaponRegistry.Get( "Pistol" );
var weapon = weaponGo.Components.Get<Weapon>( true );
weaponGo.NetworkSpawn();
weaponGo.SetParent(GameObject);
2024-10-30 19:01:58 +03:00
2024-10-30 19:48:58 +03:00
Inventory = Components.Create<Inventory>();
Inventory.Add(weaponGo, false);
2024-10-29 18:52:47 +03:00
}
protected override void OnFixedUpdate()
{
base.OnFixedUpdate();
2024-10-30 19:48:58 +03:00
2024-10-30 01:01:41 +03:00
if ( Network.IsOwner )
{
2024-10-30 14:10:44 +03:00
var multiplier = RagdollController.Enabled ? 0f : 1f;
var wishDirection = Input.AnalogMove.Normal * multiplier * Rotation.FromYaw( EyeAngles.yaw );
2024-10-30 13:33:29 +03:00
var isDucking = Input.Down( "Duck" );
var isRunning = Input.Down( "Run" );
var wishSpeed = isDucking ? DuckSpeed :
isRunning ? RunSpeed : WalkSpeed;
2024-10-29 18:52:47 +03:00
2024-10-30 19:48:58 +03:00
if ( Input.Pressed( "Slot1" ) )
{
2024-10-30 19:59:34 +03:00
Inventory?.SetActive("Pistol");
2024-10-30 19:48:58 +03:00
}
if ( Input.Pressed( "Slot2" ) )
{
2024-10-30 19:59:34 +03:00
Inventory?.Disarm();
2024-10-30 19:48:58 +03:00
}
2024-10-30 19:01:58 +03:00
CharacterController.WishVelocity = wishDirection * wishSpeed;
2024-10-30 13:15:49 +03:00
2024-10-30 19:01:58 +03:00
CharacterController.Move();
2024-10-29 18:52:47 +03:00
2024-10-30 19:01:58 +03:00
if ( Input.Pressed( "Jump" ) && CharacterController.IsOnGround )
2024-10-30 01:01:41 +03:00
{
2024-10-30 19:01:58 +03:00
CharacterController.Punch( Vector3.Up * JumpStrength );
2024-10-30 02:19:02 +03:00
AnimationHelper.TriggerJump();
2024-10-30 01:01:41 +03:00
}
2024-10-30 13:15:49 +03:00
2024-10-30 01:01:41 +03:00
if ( !AnimationHelper.IsValid() ) return;
2024-10-30 13:33:29 +03:00
AnimationHelper.DuckLevel = isDucking ? 1f : 0f;
2024-10-30 01:01:41 +03:00
}
2024-10-30 13:33:29 +03:00
2024-10-30 19:01:58 +03:00
AnimationHelper.WithWishVelocity(CharacterController.WishVelocity);
AnimationHelper.WithVelocity(CharacterController.Velocity);
AnimationHelper.IsGrounded = CharacterController.IsOnGround;
2024-10-29 18:52:47 +03:00
}
protected override void OnUpdate()
{
base.OnUpdate();
2024-10-30 01:01:41 +03:00
if ( Network.IsOwner )
{
EyeAngles += Input.AnalogLook;
2024-10-30 03:03:27 +03:00
EyeAngles = EyeAngles.WithPitch(MathX.Clamp(EyeAngles.pitch, -90f, 70f));
2024-10-30 01:01:41 +03:00
2024-10-30 13:15:49 +03:00
//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();
2024-10-30 01:01:41 +03:00
}
2024-10-29 18:52:47 +03:00
2024-10-30 01:01:41 +03:00
RotateBody();
}
void RotateBody()
{
if(Body is null) return;
2024-10-29 18:52:47 +03:00
2024-10-30 01:01:41 +03:00
var targetAngle = new Angles(0, EyeAngles.yaw, 0).ToRotation();
float rotateDifference = Body.WorldRotation.Distance(targetAngle);
2024-10-29 18:52:47 +03:00
2024-10-30 19:01:58 +03:00
if(rotateDifference > 30f || CharacterController.Velocity.Length > 10f)
2024-10-30 01:01:41 +03:00
{
Body.WorldRotation = Rotation.Lerp(Body.WorldRotation, targetAngle, Time.Delta * 2f);
}
2024-10-29 18:52:47 +03:00
}
}