TechbloxModdingAPI/GamecraftModdingAPI/Blocks/MovementEngine.cs

81 lines
3 KiB
C#
Raw Normal View History

2020-05-13 14:52:21 +00:00
using RobocraftX.Common;
2019-12-17 01:55:52 +00:00
using RobocraftX.UECS;
using Svelto.ECS;
using Svelto.ECS.EntityStructs;
using Unity.Transforms;
using Unity.Mathematics;
using GamecraftModdingAPI.Utility;
2020-05-12 00:28:26 +00:00
using GamecraftModdingAPI.Engines;
2019-12-17 01:55:52 +00:00
namespace GamecraftModdingAPI.Blocks
{
/// <summary>
/// Engine which executes block movement actions
/// </summary>
2019-12-19 20:42:50 +00:00
public class MovementEngine : IApiEngine
2019-12-17 01:55:52 +00:00
{
public string Name { get; } = "GamecraftModdingAPIMovementGameEngine";
public EntitiesDB entitiesDB { set; private get; }
2019-12-17 01:55:52 +00:00
2020-05-12 00:28:26 +00:00
public bool isRemovable => false;
public bool IsInGame = false;
2019-12-17 01:55:52 +00:00
public void Dispose()
{
IsInGame = false;
}
public void Ready()
{
IsInGame = true;
}
// implementations for Movement static class
internal float3 MoveBlock(EGID blockID, BlockEngine.BlockInitData data, float3 vector)
2019-12-17 01:55:52 +00:00
{
if (!entitiesDB.Exists<PositionEntityStruct>(blockID))
{
if (data.Group == null) return float3.zero;
var init = new EntityInitializer(blockID, data.Group);
init.GetOrCreate<PositionEntityStruct>().position = vector;
init.GetOrCreate<GridRotationStruct>().position = vector;
init.GetOrCreate<LocalTransformEntityStruct>().position = vector;
return vector;
}
ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID);
ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity<GridRotationStruct>(blockID);
ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID);
ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity<UECSPhysicsEntityStruct>(blockID);
2019-12-17 01:55:52 +00:00
// main (persistent) position
posStruct.position = vector;
2019-12-17 01:55:52 +00:00
// placement grid position
gridStruct.position = vector;
2019-12-17 01:55:52 +00:00
// rendered position
transStruct.position = vector;
2019-12-17 01:55:52 +00:00
// collision position
FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Translation
{
Value = posStruct.position
});
entitiesDB.QueryEntity<GridConnectionsEntityStruct>(blockID).isProcessed = false;
2019-12-17 01:55:52 +00:00
return posStruct.position;
}
internal float3 GetPosition(EGID blockID, BlockEngine.BlockInitData data)
2019-12-17 01:55:52 +00:00
{
if (!entitiesDB.Exists<PositionEntityStruct>(blockID))
{
if (data.Group == null) return float3.zero;
var init = new EntityInitializer(blockID, data.Group);
return init.Has<PositionEntityStruct>() ? init.Get<PositionEntityStruct>().position : float3.zero;
}
ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID);
return posStruct.position;
2019-12-17 01:55:52 +00:00
}
}
}