diff --git a/GamecraftModdingAPI/FlyCam.cs b/GamecraftModdingAPI/FlyCam.cs index 15a6aa5..be9955f 100644 --- a/GamecraftModdingAPI/FlyCam.cs +++ b/GamecraftModdingAPI/FlyCam.cs @@ -1,26 +1,119 @@ using GamecraftModdingAPI.Players; +using GamecraftModdingAPI.Utility; + +using RobocraftX.Physics; using Svelto.ECS.EntityStructs; +using Techblox.FlyCam; using Unity.Mathematics; +using UnityEngine; namespace GamecraftModdingAPI { public class FlyCam { - private static FlyCamEngine Engine; + private static FlyCamEngine Engine = new FlyCamEngine(); public uint Id { get; } public FlyCam(uint id) => Id = id; - public unsafe float3 Position + /// + /// The local player's camera. + /// + public static FlyCam LocalCamera => new FlyCam(Player.LocalPlayer.Id); + + /// + /// The current position of the camera. + /// + public float3 Position { - get => Engine.GetComponent(Id).Map(pos => &pos->position); - set => Engine.GetComponent(Id).Map(pos => &pos->position).Set(value); + get => Engine.GetComponent(Id).Get().position; + set + { + Engine.GetComponent(Id).Get().position = value; + Engine.GetComponent(Id).Get().position = value; + } + } + + /// + /// The current rotation of the camera. + /// + public float3 Rotation + { + get => ((Quaternion) Engine.GetComponent(Id).Get().rotation).eulerAngles; + set + { + Engine.GetComponent(Id).Get().rotation = Quaternion.Euler(value); + Engine.GetComponent(Id).Get().rotation = Quaternion.Euler(value); + } + } + + /// + /// The current direction the camera is moving. + /// + public float3 MovementDirection + { + get => Engine.GetComponent(Id).Get().movementDirection; + set => Engine.GetComponent(Id).Get().movementDirection = value; + } + + /// + /// Whether the camera (player) is sprinting. + /// + public bool Sprinting + { + get => Engine.GetComponent(Id).Get().sprinting; + set => Engine.GetComponent(Id).Get().sprinting = value; + } + + /// + /// The speed setting of the camera. + /// + public float Speed + { + get => Engine.GetComponent(Id).Get().speed; + set => Engine.GetComponent(Id).Get().speed = value; + } + + /// + /// The multiplier setting to use when sprinting. + /// + public float SpeedSprintMultiplier + { + get => Engine.GetComponent(Id).Get().speedSprintMultiplier; + set => Engine.GetComponent(Id).Get().speedSprintMultiplier = value; + } + + /// + /// The acceleration setting of the camera. + /// + public float Acceleration + { + get => Engine.GetComponent(Id).Get().acceleration; + set => Engine.GetComponent(Id).Get().acceleration = value; + } + + /// + /// The current velocity of the camera. + /// + public float3 Velocity + { + get => Engine.GetComponent(Id).Get().velocity; + set => Engine.GetComponent(Id).Get().velocity = value; + } + + /// + /// The current angular velocity of the camera. + /// + public float3 AngularVelocity + { + get => Engine.GetComponent(Id).Get().angularVelocity; + set => Engine.GetComponent(Id).Get().angularVelocity = value; } public static void Init() { - Engine = new FlyCamEngine(); + GameEngineManager.AddGameEngine(Engine); } } } \ No newline at end of file diff --git a/GamecraftModdingAPI/OptionalRef.cs b/GamecraftModdingAPI/OptionalRef.cs deleted file mode 100644 index a1b09cd..0000000 --- a/GamecraftModdingAPI/OptionalRef.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using GamecraftModdingAPI.Blocks; -using Svelto.DataStructures; -using Svelto.ECS; - -namespace GamecraftModdingAPI -{ - public struct OptionalRef where T : unmanaged - { - private bool exists; - private NB array; - private uint index; - - private unsafe T* pointer; - - public OptionalRef(NB array, uint index) - { - exists = true; - this.array = array; - this.index = index; - unsafe - { - pointer = null; - } - } - - public OptionalRef(ref T value) - { - unsafe - { - fixed(T* p = &value) pointer = p; - } - - exists = true; - array = default; - index = default; - } - - public ref T Get() - { - unsafe - { - if (pointer != null && exists) - return ref *pointer; - } - - if (exists) - return ref array[index]; - throw new InvalidOperationException("Calling Get() on an empty OptionalRef"); - } - - public T Value - { - get - { - unsafe - { - if (pointer != null && exists) - return *pointer; - } - - if (exists) - return array[index]; - return default; - } - } - - public unsafe void Set(T value) //Can't use properties because it complains that you can't set struct members - { - if (pointer != null && exists) - *pointer = value; - } - - public bool Exists => exists; - - public static implicit operator T(OptionalRef opt) => opt.Value; - - public static implicit operator bool(OptionalRef opt) => opt.exists; - - public delegate ref TR Mapper(ref T component) where TR : unmanaged; - public unsafe delegate TR* PMapper(T* component) where TR : unmanaged; - - /*public OptionalRef Map(Mapper mapper) where TR : unmanaged => - exists ? new OptionalRef(ref mapper(ref Get())) : new OptionalRef();*/ - - /*public OptionalRef Map(Expression> expression) where TR : unmanaged - { - if (expression.Body.NodeType == ExpressionType.MemberAccess) - Console.WriteLine(((MemberExpression) expression.Body).Member); - }*/ - - public unsafe OptionalRef Map(PMapper mapper) where TR : unmanaged => - exists ? new OptionalRef(ref *mapper(pointer)) : new OptionalRef(); - } -} \ No newline at end of file diff --git a/GamecraftModdingAPI/Player.cs b/GamecraftModdingAPI/Player.cs index 0bed6a2..8b204d0 100644 --- a/GamecraftModdingAPI/Player.cs +++ b/GamecraftModdingAPI/Player.cs @@ -414,6 +414,11 @@ namespace GamecraftModdingAPI return playerEngine.GetSelectedBlocks(Id); } + /// + /// The camera of this player used when building. + /// + public FlyCam BuildCamera => new FlyCam(Id); + public bool Equals(Player other) { if (ReferenceEquals(null, other)) return false; diff --git a/GamecraftModdingAPI/Utility/OptionalRef.cs b/GamecraftModdingAPI/Utility/OptionalRef.cs new file mode 100644 index 0000000..4ac293d --- /dev/null +++ b/GamecraftModdingAPI/Utility/OptionalRef.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using GamecraftModdingAPI.Blocks; +using Svelto.DataStructures; +using Svelto.ECS; + +namespace GamecraftModdingAPI +{ + public struct OptionalRef where T : unmanaged + { + private bool exists; + private NB array; + private uint index; + + public OptionalRef(NB array, uint index) + { + exists = true; + this.array = array; + this.index = index; + } + + public OptionalRef(ref T value) + { + exists = true; + array = default; + index = default; + } + + public ref T Get(T def = default) + { + if (exists) + return ref array[index]; + return ref CompRefCache._default; + } + + public bool Exists => exists; + + public static implicit operator T(OptionalRef opt) => opt.Get(); + + public static implicit operator bool(OptionalRef opt) => opt.exists; + + /*public delegate ref TR Mapper(ref T component) where TR : unmanaged; + public unsafe delegate TR* PMapper(T* component) where TR : unmanaged; + + public unsafe OptionalRef Map(PMapper mapper) where TR : unmanaged => + exists ? new OptionalRef(ref *mapper(pointer)) : new OptionalRef();*/ + + /// + /// Creates an instance of a struct T that can be referenced. + /// + /// The struct type to cache + private struct CompRefCache where T : unmanaged + { + public static T _default; + } + } +} \ No newline at end of file