using RobocraftX.Common; using RobocraftX.UECS; using Svelto.ECS; using Svelto.ECS.EntityStructs; using Unity.Mathematics; using UnityEngine; using GamecraftModdingAPI.Utility; using GamecraftModdingAPI.Engines; namespace GamecraftModdingAPI.Blocks { /// /// Engine which executes block movement actions /// public class RotationEngine : IApiEngine { public string Name { get; } = "GamecraftModdingAPIRotationGameEngine"; public EntitiesDB entitiesDB { set; private get; } public bool isRemovable => false; public bool IsInGame = false; public void Dispose() { IsInGame = false; } public void Ready() { IsInGame = true; } // implementations for Rotation static class internal float3 RotateBlock(EGID blockID, BlockEngine.BlockInitData data, Vector3 vector) { if (!entitiesDB.Exists(blockID)) { if (data.Group == null) return float3.zero; var init = new EntityComponentInitializer(blockID, data.Group); init.Init(new RotationEntityStruct {rotation = new Quaternion {eulerAngles = vector}}); init.Init(new GridRotationStruct {rotation = new Quaternion {eulerAngles = vector}}); init.Init(new LocalTransformEntityStruct {rotation = new Quaternion {eulerAngles = vector}}); return vector; } ref RotationEntityStruct rotStruct = ref this.entitiesDB.QueryEntity(blockID); ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity(blockID); ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity(blockID); ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity(blockID); // main (persistent) position Quaternion newRotation = rotStruct.rotation; newRotation.eulerAngles = vector; rotStruct.rotation = newRotation; // placement grid rotation Quaternion newGridRotation = gridStruct.rotation; newGridRotation.eulerAngles = vector; gridStruct.rotation = newGridRotation; // rendered position Quaternion newTransRotation = rotStruct.rotation; newTransRotation.eulerAngles = vector; transStruct.rotation = newTransRotation; // collision position FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Unity.Transforms.Rotation { Value = rotStruct.rotation }); entitiesDB.QueryEntity(blockID).isProcessed = false; return ((Quaternion)rotStruct.rotation).eulerAngles; } internal float3 GetRotation(EGID blockID, BlockEngine.BlockInitData data) { if (!entitiesDB.Exists(blockID)) { if (data.Group == null) return float3.zero; var init = new EntityComponentInitializer(blockID, data.Group); return init.Has() ? (float3) ((Quaternion) init.Get().rotation).eulerAngles : float3.zero; } ref RotationEntityStruct rotStruct = ref entitiesDB.QueryEntity(blockID); return ((Quaternion) rotStruct.rotation).eulerAngles; } } }