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;
|
|
|
|
|
|
|
|
public sealed class Kal : Component
|
|
|
|
{
|
2024-10-30 02:19:02 +03:00
|
|
|
[RequireComponent] private ShrimpleCharacterController.ShrimpleCharacterController Controller { get; set; }
|
|
|
|
[RequireComponent] private AnimationHelper AnimationHelper { get; set; }
|
2024-10-29 18:52:47 +03:00
|
|
|
public SkinnedModelRenderer Renderer { get; set; }
|
2024-10-30 01:01:41 +03:00
|
|
|
[Property] public GameObject Body { get; set; }
|
2024-10-29 18:52:47 +03:00
|
|
|
public GameObject Camera { get; set; }
|
|
|
|
|
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; }
|
2024-10-30 02:19:02 +03:00
|
|
|
// [Sync] public bool IsDucking { get; set; }
|
|
|
|
// [Sync] public bool IsRunning { get; set; }
|
2024-10-30 01:01:41 +03:00
|
|
|
|
|
|
|
protected override void OnStart()
|
2024-10-29 18:52:47 +03:00
|
|
|
{
|
|
|
|
base.OnStart();
|
|
|
|
|
2024-10-30 02:19:02 +03:00
|
|
|
Controller = Components.Get<ShrimpleCharacterController.ShrimpleCharacterController>();
|
|
|
|
AnimationHelper = Components.Get<AnimationHelper>();
|
|
|
|
|
2024-10-29 18:52:47 +03:00
|
|
|
if ( !Network.IsOwner ) return;
|
|
|
|
|
|
|
|
Renderer = Components.Get<SkinnedModelRenderer>(FindMode.EverythingInSelfAndDescendants);
|
2024-10-30 02:19:02 +03:00
|
|
|
Camera = Scene.Camera.GameObject;
|
|
|
|
|
2024-10-29 18:52:47 +03:00
|
|
|
Camera.SetParent(GameObject);
|
|
|
|
var cameraComponent = Camera.Components.Create<CameraComponent>();
|
|
|
|
cameraComponent.ZFar = 32768f;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnFixedUpdate()
|
|
|
|
{
|
|
|
|
base.OnFixedUpdate();
|
|
|
|
|
2024-10-30 01:01:41 +03:00
|
|
|
if ( Network.IsOwner )
|
|
|
|
{
|
|
|
|
var wishDirection = Input.AnalogMove.Normal * Rotation.FromYaw( EyeAngles.yaw );
|
2024-10-30 02:19:02 +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 01:01:41 +03:00
|
|
|
Controller.WishVelocity = wishDirection * wishSpeed;
|
2024-10-30 02:19:02 +03:00
|
|
|
|
2024-10-30 01:01:41 +03:00
|
|
|
Controller.Move();
|
2024-10-29 18:52:47 +03:00
|
|
|
|
2024-10-30 01:01:41 +03:00
|
|
|
if ( Input.Pressed( "Jump" ) && Controller.IsOnGround )
|
|
|
|
{
|
2024-10-30 03:03:27 +03:00
|
|
|
Controller.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 02:19:02 +03:00
|
|
|
|
2024-10-30 01:01:41 +03:00
|
|
|
if ( !AnimationHelper.IsValid() ) return;
|
|
|
|
|
2024-10-30 02:19:02 +03:00
|
|
|
AnimationHelper.DuckLevel = IsDucking ? 1f : 0f;
|
2024-10-30 01:01:41 +03:00
|
|
|
}
|
|
|
|
|
2024-10-30 02:19:02 +03:00
|
|
|
Log.Info( AnimationHelper.HeadWeight );
|
|
|
|
|
|
|
|
AnimationHelper.WithWishVelocity(Controller.WishVelocity);
|
|
|
|
AnimationHelper.WithVelocity(Controller.Velocity);
|
|
|
|
AnimationHelper.IsGrounded = Controller.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
|
|
|
// Renderer.WorldRotation = Rotation.Slerp(Renderer.WorldRotation, Rotation.FromYaw(EyeAngles.yaw), Time.Delta * 5f);
|
|
|
|
var cameraOffset = Vector3.Up * CamOffsetZ + Vector3.Backward * CamOffsetX;
|
|
|
|
|
|
|
|
Camera.WorldRotation = EyeAngles.ToRotation();
|
|
|
|
Camera.LocalPosition = cameraOffset * Camera.WorldRotation;
|
|
|
|
}
|
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 01:01:41 +03:00
|
|
|
if(rotateDifference > 30f || Controller.Velocity.Length > 10f)
|
|
|
|
{
|
|
|
|
Body.WorldRotation = Rotation.Lerp(Body.WorldRotation, targetAngle, Time.Delta * 2f);
|
|
|
|
}
|
2024-10-29 18:52:47 +03:00
|
|
|
}
|
|
|
|
}
|