Start working on FlyCam and create an overcomplicated struct

Just some native code that's totally unnecessary
This commit is contained in:
Norbi Peti 2021-04-26 03:12:22 +02:00
parent eb7a09ed22
commit 55b38f1678
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 153 additions and 0 deletions

View file

@ -0,0 +1,26 @@
using GamecraftModdingAPI.Players;
using Svelto.ECS.EntityStructs;
using Unity.Mathematics;
namespace GamecraftModdingAPI
{
public class FlyCam
{
private static FlyCamEngine Engine;
public uint Id { get; }
public FlyCam(uint id) => Id = id;
public unsafe float3 Position
{
get => Engine.GetComponent<PositionEntityStruct>(Id).Map(pos => &pos->position);
set => Engine.GetComponent<PositionEntityStruct>(Id).Map(pos => &pos->position).Set(value);
}
public static void Init()
{
Engine = new FlyCamEngine();
}
}
}

View file

@ -0,0 +1,99 @@
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<T> where T : unmanaged
{
private bool exists;
private NB<T> array;
private uint index;
private unsafe T* pointer;
public OptionalRef(NB<T> 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<T> opt) => opt.Value;
public static implicit operator bool(OptionalRef<T> opt) => opt.exists;
public delegate ref TR Mapper<TR>(ref T component) where TR : unmanaged;
public unsafe delegate TR* PMapper<TR>(T* component) where TR : unmanaged;
/*public OptionalRef<TR> Map<TR>(Mapper<TR> mapper) where TR : unmanaged =>
exists ? new OptionalRef<TR>(ref mapper(ref Get())) : new OptionalRef<TR>();*/
/*public OptionalRef<TR> Map<TR>(Expression<Func<T, TR>> expression) where TR : unmanaged
{
if (expression.Body.NodeType == ExpressionType.MemberAccess)
Console.WriteLine(((MemberExpression) expression.Body).Member);
}*/
public unsafe OptionalRef<TR> Map<TR>(PMapper<TR> mapper) where TR : unmanaged =>
exists ? new OptionalRef<TR>(ref *mapper(pointer)) : new OptionalRef<TR>();
}
}

View file

@ -0,0 +1,28 @@
using GamecraftModdingAPI.Engines;
using Svelto.ECS;
using Techblox.FlyCam;
namespace GamecraftModdingAPI.Players
{
public class FlyCamEngine : IApiEngine
{
public void Ready()
{
}
public EntitiesDB entitiesDB { get; set; }
public void Dispose()
{
}
public string Name => "TechbloxModdingAPIFlyCamEngine";
public bool isRemovable => false;
public OptionalRef<T> GetComponent<T>(uint id) where T : unmanaged, IEntityComponent
{
if (entitiesDB.TryQueryEntitiesAndIndex<T>(id, Techblox.FlyCam.FlyCam.Group, out uint index, out var array))
return new OptionalRef<T>(array, index);
return new OptionalRef<T>();
}
}
}