velox_test/code/LocalCar.cs
2025-06-14 18:21:37 +07:00

67 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
using VeloX;
namespace Sandbox
{
internal class LocalCar : Component, Component.INetworkListener
{
void INetworkListener.OnDisconnected( Connection channel )
{
if ( Car?.ConnectionID == channel.Id )
DestroyGameObject();
}
[Property] VeloXCar Car { get; set; }
[Property] public Vector3 CameraOffset { get; set; }
public float blendSpeed = 10f;
private Angles EyeAngles = new( 0, 180, 0 );
private Rotation targetRotation;
protected override void OnUpdate()
{
if ( !Car.IsDriver )
return;
UpdateEyeAngles();
CameraSetup();
}
private void UpdateEyeAngles()
{
Angles input = Input.AnalogLook;
Angles eyeAngles = EyeAngles;
eyeAngles += input.WithPitch( -input.pitch );
eyeAngles.roll = 0f;
eyeAngles.pitch = eyeAngles.pitch.Clamp( 0f - 89, 89 );
EyeAngles = eyeAngles;
}
protected void CameraSetup()
{
var cam = Scene.Camera;
var zoffset = Vector3.Up * CameraOffset.z;
Vector3 targetPos = WorldPosition + zoffset + WorldRotation * EyeAngles * CameraOffset;
float blendFactor = blendSpeed * Time.Delta;
blendFactor = MathF.Min( blendFactor, 1f );
cam.WorldPosition = Vector3.Lerp( cam.WorldPosition, targetPos, blendFactor );
Vector3 lookDirection = WorldPosition + zoffset - cam.WorldPosition;
if ( lookDirection != Vector3.Zero )
targetRotation = Rotation.LookAt( lookDirection, Vector3.Up );
cam.WorldRotation = targetRotation;
}
}
}