Actually moving the camera

master
D4VID 4 months ago
parent 2a5e4e29d2
commit 3129ba0210

@ -32,5 +32,8 @@
<Reference Include="LogicLog"> <Reference Include="LogicLog">
<HintPath>$(LogicWorldGameLocation)\Logic_World_Data\Managed\LogicLog.dll</HintPath> <HintPath>$(LogicWorldGameLocation)\Logic_World_Data\Managed\LogicLog.dll</HintPath>
</Reference> </Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>$(LogicWorldGameLocation)\Logic_World_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -1,7 +1,7 @@
ID: D4VID_CameraRoll ID: D4VID_CameraRoll
Name: CameraRoll Name: CameraRoll
Author: D4VID Author: D4VID
Version: 0.0.1 Version: 0.1.0
Priority: 0 Priority: 0
ClientOnly: true ClientOnly: true
Dependencies: Dependencies:

@ -12,10 +12,15 @@ namespace CameraRoll.Client {
public override string TextID => Id; public override string TextID => Id;
/// <summary> /// <summary>
/// Locked mouse means you look around when you move it. /// True locks mouse so you look around when you move it.
/// False would display the cursor to be able to click on UI elements. /// False displays the cursor to be able to click on UI elements.
/// </summary> /// </summary>
public override bool MouseLocked => true; public override bool MouseLocked => false;
/// <summary>
/// Don't display the hotbar with components as the user cannot place them
/// </summary>
public override bool ShowHotbarWhileStateActive => false;
/// <summary> /// <summary>
/// This list gets printed on the game's top left UI panel which displays the currently available keybindings /// This list gets printed on the game's top left UI panel which displays the currently available keybindings
@ -33,6 +38,7 @@ namespace CameraRoll.Client {
/// </summary> /// </summary>
public override void OnEnter() { public override void OnEnter() {
CameraRollClientMod.Logger.Info("CameraRollGameState enter"); CameraRollClientMod.Logger.Info("CameraRollGameState enter");
CameraRollTool.OnEnter();
} }
/// <summary> /// <summary>
@ -50,6 +56,8 @@ namespace CameraRoll.Client {
} else if (CustomInput.DownThisFrame(CameraRollTrigger.Clear)) { } else if (CustomInput.DownThisFrame(CameraRollTrigger.Clear)) {
CameraRollTool.Clear(); CameraRollTool.Clear();
} }
CameraRollTool.Update();
} }
/// <summary> /// <summary>

@ -1,28 +1,130 @@
using System.Collections.Generic;
using LogicLog; using LogicLog;
using UnityEngine;
namespace CameraRoll.Client.Tool { namespace CameraRoll.Client.Tool {
public class CameraRollTool { public class CameraRollTool {
private static ILogicLogger _logger = null!; private static ILogicLogger _logger = null!;
private static Camera _cam;
/// <summary>
/// How fast does it interpolate between points
/// TODO: Make this editable per point
/// </summary>
private const float InterpolationSpeed = 0.005f;
/// <summary>
/// If the track is playing/animating/moving the camera
/// </summary>
private static bool _playing;
// Backup of the original (local) position and rotation of the camera
private static Vector3 _originalPosition;
private static Quaternion _originalRotation;
/// <summary>
/// Which point the camera is currently heading towards
/// </summary>
private static int _pointIndex;
/// <summary>
/// Progress of the linear interpolation (0.0f - 1.0f)
/// </summary>
private static float _interpolationState;
private static readonly List<GameObject> PathPoints = new List<GameObject>();
public static void Init(ILogicLogger logger) { public static void Init(ILogicLogger logger) {
_logger = logger; _logger = logger;
PathPoints.Clear();
_playing = false;
}
public static void OnEnter() {
_cam = Camera.main;
} }
public static void PlacePoint() { public static void PlacePoint() {
_logger.Info("PlacePoint"); _logger.Info("PlacePoint");
GameObject point = new GameObject($"PathPoint #{PathPoints.Count}") {
transform = {
position = _cam.transform.position,
rotation = _cam.transform.rotation
}
};
point.AddComponent<PathPoint>();
PathPoints.Add(point);
} }
public static void Play() { public static void Play() {
_logger.Info("Play"); if (_playing) return;
_originalPosition = _cam.transform.localPosition;
_originalRotation = _cam.transform.localRotation;
_pointIndex = 0;
_interpolationState = 0.0f;
_playing = true;
} }
public static void Stop() { public static void Stop() {
_logger.Info("Stop"); if (!_playing) return;
_playing = false;
_cam.transform.localPosition = _originalPosition;
_cam.transform.localRotation = _originalRotation;
} }
public static void Clear() { public static void Clear() {
_logger.Info("Clear"); foreach (GameObject point in PathPoints) {
Object.Destroy(point);
}
PathPoints.Clear();
}
public static void Update() {
if (_playing) {
if (_pointIndex >= PathPoints.Count) {
// Reached the end
_logger.Info("Reached the end of track");
Stop();
return;
}
if (_pointIndex == 0) {
// Snap to the first defined point
_cam.transform.position = PathPoints[0].transform.position;
_pointIndex++;
_interpolationState = 0.0f;
return;
}
_interpolationState += InterpolationSpeed;
Vector3 prevPos = GetPos(_pointIndex - 1);
Vector3 posDiff = GetPos(_pointIndex) - prevPos;
_cam.transform.position = prevPos + posDiff * _interpolationState;
Vector3 prevRot = GetRot(_pointIndex - 1);
Vector3 rotDiff = GetRot(_pointIndex) - prevRot;
_cam.transform.eulerAngles = prevRot + rotDiff * _interpolationState;
// TODO: interpolate across the shorter direction of rotation
if (_interpolationState >= 1.0f) {
_pointIndex++;
_interpolationState = 0.0f;
}
}
}
private static Vector3 GetPos(int index) => PathPoints[index].transform.position;
private static Vector3 GetRot(int index) => PathPoints[index].transform.eulerAngles;
public static void OnExit() {
Clear();
_playing = false;
} }
} }
} }

@ -0,0 +1,27 @@
using UnityEngine;
namespace CameraRoll.Client.Tool {
public class PathPoint : MonoBehaviour {
private const float AxisLength = 0.1f;
void Start() {
CreateAxis(Vector3.right, Color.red);
CreateAxis(Vector3.up, Color.green);
CreateAxis(Vector3.forward, Color.blue);
}
void CreateAxis(Vector3 dir, Color color) {
GameObject line = new GameObject("Axis");
line.transform.SetParent(transform, false);
LineRenderer lr = line.AddComponent<LineRenderer>();
lr.useWorldSpace = false;
lr.positionCount = 2;
lr.SetPosition(0, dir.normalized * -AxisLength);
lr.SetPosition(1, dir.normalized * AxisLength);
lr.material = new Material(Shader.Find("Unlit/Color"));
lr.material.color = color;
lr.startWidth = 0.01f;
lr.endWidth = 0.01f;
}
}
}
Loading…
Cancel
Save