Initial commit

This commit is contained in:
gamer147
2023-08-02 19:47:53 -04:00
commit 285a130a81
33 changed files with 1699 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
using LuckerGame.Entities;
using Sandbox;
namespace LuckerGame.Components.Lucker.Cameras;
public abstract class AbstractCamera : EntityComponent<Entities.Lucker>
{
protected Vector3 CameraPosition { get; set; }
protected Rotation CameraRotation { get; set; }
protected float FieldOfView { get; set; }
protected IEntity FirstPersonViewer { get; set; }
protected Transform SoundSource { get; set; }
/// <summary>
/// Handles any input-independent camera updates (ie following a pawn)
/// </summary>
protected abstract void UpdateCameraParameters();
/// <summary>
/// Handles any input dependent camera updates (ie moving around a top down cam)
/// </summary>
public abstract void BuildInput();
/// <summary>
/// Applies Camera parameters to the static Camera
/// </summary>
public virtual void Update()
{
UpdateCameraParameters();
Camera.Position = CameraPosition;
Camera.Rotation = CameraRotation;
Camera.FieldOfView = FieldOfView;
Camera.FirstPersonViewer = FirstPersonViewer;
Sound.Listener = SoundSource;
}
}

View File

@@ -0,0 +1,47 @@
using Sandbox;
using System;
using System.Linq;
namespace LuckerGame.Components.Lucker.Cameras;
/// <summary>
/// A top down camera that can be
/// </summary>
public partial class RTSCamera : AbstractCamera, ISingletonComponent
{
private const float MaxDistance = 400f;
private const float MinDistance = 200f;
private const float Speed = 4f;
protected float WheelSpeed => 5f;
protected Vector2 DistanceClamp => new( MinDistance, MaxDistance );
protected float CameraDistance = MinDistance;
protected float TargetDistance = MinDistance;
private Vector3 CameraPositionOffset = Vector3.Zero;
protected override void UpdateCameraParameters()
{
CameraPosition = Entity.Position + CameraPositionOffset;
CameraRotation = Rotation.FromPitch( -270 );
FieldOfView = 70f;
FirstPersonViewer = null;
CameraPosition = CameraPosition.WithZ( CameraDistance );
SoundSource = new Transform { Position = CameraPosition };
}
public override void BuildInput()
{
// Zooming in and out
var wheel = Input.MouseWheel;
if ( wheel != 0 )
{
TargetDistance -= wheel * WheelSpeed;
TargetDistance = TargetDistance.Clamp( DistanceClamp.x, DistanceClamp.y );
}
CameraDistance = CameraDistance.LerpTo( TargetDistance, Time.Delta * 10f );
// Moving
CameraPositionOffset += Input.AnalogMove * Speed;
}
}

View File

@@ -0,0 +1,97 @@
/*using Sandbox;
using System;
using System.Linq;
namespace LuckerGame.Components.Lucker.Cameras;
/// <summary>
/// A top downish camera that follows an entity
/// </summary>
public partial class TopDownCamera : AbstractCamera, ISingletonComponent
{
protected float WheelSpeed => 30f;
protected Vector2 CameraDistance => new( 125, 1000 );
protected Vector2 PitchClamp => new( 30, 60 );
float OrbitDistance = 400f;
float TargetOrbitDistance = 400f;
Angles OrbitAngles = Angles.Zero;
protected static Vector3 IntersectPlane( Vector3 pos, Vector3 dir, float z )
{
float a = (z - pos.z) / dir.z;
return new( dir.x * a + pos.x, dir.y * a + pos.y, z );
}
protected static Rotation LookAt( Vector3 targetPosition, Vector3 position )
{
var targetDelta = (targetPosition - position);
var direction = targetDelta.Normal;
return Rotation.From( new Angles(
((float)Math.Asin( direction.z )).RadianToDegree() * -1.0f,
((float)Math.Atan2( direction.y, direction.x )).RadianToDegree(),
0.0f ) );
}
public override void Update()
{
var pawn = Entity;
if ( !pawn.IsValid() )
return;
Camera.Position = pawn.Position;
Vector3 targetPos;
Camera.Position += Vector3.Up * (pawn.CollisionBounds.Center.z * pawn.Scale);
Camera.Rotation = Rotation.From( OrbitAngles );
targetPos = Camera.Position + Camera.Rotation.Backward * OrbitDistance;
Camera.Position = targetPos;
Camera.FieldOfView = 70f;
Camera.FirstPersonViewer = null;
Sound.Listener = new()
{
Position = pawn.AimRay.Position,
Rotation = pawn.EyeRotation
};
}
public override void BuildInput()
{
var wheel = Input.MouseWheel;
if ( wheel != 0 )
{
TargetOrbitDistance -= wheel * WheelSpeed;
TargetOrbitDistance = TargetOrbitDistance.Clamp( CameraDistance.x, CameraDistance.y );
}
OrbitDistance = OrbitDistance.LerpTo( TargetOrbitDistance, Time.Delta * 10f );
// Sets pawn to face where the camera faces
if ( Input.UsingController || Input.Down( "attack2" ) )
{
OrbitAngles.yaw += Input.AnalogLook.yaw;
OrbitAngles.pitch += Input.AnalogLook.pitch;
OrbitAngles = OrbitAngles.Normal;
Entity.ViewAngles = OrbitAngles.WithPitch( 0f );
}
// Sets pawn to face to point that the mouse is over
else
{
// Get ray from direction vector from mouse pointer through screen
var direction = Screen.GetDirection( Mouse.Position, Camera.FieldOfView, Camera.Rotation, Screen.Size );
var hitPos = IntersectPlane( Camera.Position, direction, Entity.EyePosition.z );
Entity.ViewAngles = (hitPos - Entity.EyePosition).EulerAngles;
}
OrbitAngles.pitch = OrbitAngles.pitch.Clamp( PitchClamp.x, PitchClamp.y );
Entity.InputDirection = Input.AnalogMove;
}
}*/

View File

@@ -0,0 +1,15 @@
using Sandbox;
namespace LuckerGame.EntityComponents.Lucker;
/// <summary>
/// A component for capturing and passing around a client's input for an attached Lucker
/// </summary>
public class LuckerClientInput : EntityComponent<Entities.Lucker>
{
[ClientInput]
public Vector3 InputDirection { get; set; }
[ClientInput]
public Angles ViewAngles { get; set; }
}