using System.ComponentModel;
using System.Linq;
using Sandbox;
using Sandbox.Citizen;
[Icon("directions_run")]
public sealed class NoClip : Sandbox.Component, Sandbox.Component.ExecuteInEditor
{
[Sync] public Angles EyeAngles { get; set; }
[Sync] public Vector3 WishVelocity { get; set; }
///
/// Set to the speed you want the camera to move
///
[Property] public float Speed { get; set; } = 25;
///
/// Set to the GameObject you want the camera to follow
///
[Property] public GameObject Eyes { get; set; }
///
/// Leave null if you don't use the citizen animation helper
///
[Property] public CitizenAnimationHelper AnimationHelper { get; set; }
protected override void OnFixedUpdate()
{
CameraMovement();
if (!IsProxy)
{
UpdateAnimations();
Move();
Transform.Rotation = Rotation.Slerp(Transform.Rotation, new Angles(0, EyeAngles.yaw, 0).ToRotation(), Time.Delta * 5);
}
}
void Move()
{
WishVelocity = new Angles(EyeAngles.pitch, EyeAngles.yaw, 0).ToRotation() * Input.AnalogMove;
WishVelocity *= Speed;
GameObject.Transform.Position += WishVelocity;
}
void CameraMovement()
{
var camera = Scene.GetAllComponents().FirstOrDefault( x => x.IsMainCamera);
var ee = EyeAngles;
ee += Input.AnalogLook;
ee.roll = 0;
ee.pitch = ee.pitch.Clamp(-89, 89);
EyeAngles = ee;
camera.Transform.Position = Eyes.Transform.Position + Vector3.Up * 64;
camera.Transform.Rotation = new Angles(EyeAngles.pitch, EyeAngles.yaw, 0).ToRotation();
}
void UpdateAnimations()
{
if (AnimationHelper is null) return;
AnimationHelper.WithLook(EyeAngles.Forward);
AnimationHelper.WithWishVelocity(WishVelocity);
AnimationHelper.IsNoclipping = true;
var ShadowRenderType = IsProxy ? ModelRenderer.ShadowRenderType.On : ModelRenderer.ShadowRenderType.ShadowsOnly;
AnimationHelper.Target.RenderType = ShadowRenderType;
}
}