Implement init for position and rotation

This commit is contained in:
Norbi Peti 2020-07-15 22:46:48 +02:00 committed by NGnius (Graham)
parent 7336fe8353
commit 5e335e78ff
4 changed files with 52 additions and 16 deletions

View file

@ -209,10 +209,10 @@ namespace GamecraftModdingAPI
/// </summary> /// </summary>
public float3 Position public float3 Position
{ {
get => Exists ? MovementEngine.GetPosition(Id) : float3.zero; get => MovementEngine.GetPosition(Id, InitData);
set set
{ {
if (Exists) MovementEngine.MoveBlock(Id, value); MovementEngine.MoveBlock(Id, InitData, value);
} }
} }
@ -221,10 +221,10 @@ namespace GamecraftModdingAPI
/// </summary> /// </summary>
public float3 Rotation public float3 Rotation
{ {
get => Exists ? RotationEngine.GetRotation(Id) : float3.zero; get => RotationEngine.GetRotation(Id, InitData);
set set
{ {
if (Exists) RotationEngine.RotateBlock(Id, value); RotationEngine.RotateBlock(Id, InitData, value);
} }
} }

View file

@ -22,6 +22,9 @@ namespace GamecraftModdingAPI.Blocks
// custom console block properties // custom console block properties
/// <summary>
/// Setting a nonexistent command will crash the game when switching to simulation
/// </summary>
public string Command public string Command
{ {
get get

View file

@ -35,8 +35,17 @@ namespace GamecraftModdingAPI.Blocks
// implementations for Movement static class // implementations for Movement static class
public float3 MoveBlock(EGID blockID, float3 vector) internal float3 MoveBlock(EGID blockID, BlockEngine.BlockInitData data, float3 vector)
{ {
if (!entitiesDB.Exists<PositionEntityStruct>(blockID))
{
if (data.Group == null) return float3.zero;
var init = new EntityComponentInitializer(blockID, data.Group);
init.Init(new PositionEntityStruct {position = vector});
init.Init(new GridRotationStruct {position = vector});
init.Init(new LocalTransformEntityStruct {position = vector});
return vector;
}
ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID); ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID);
ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity<GridRotationStruct>(blockID); ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity<GridRotationStruct>(blockID);
ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID); ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID);
@ -56,8 +65,14 @@ namespace GamecraftModdingAPI.Blocks
return posStruct.position; return posStruct.position;
} }
public float3 GetPosition(EGID blockID) internal float3 GetPosition(EGID blockID, BlockEngine.BlockInitData data)
{ {
if (!entitiesDB.Exists<PositionEntityStruct>(blockID))
{
if (data.Group == null) return float3.zero;
var init = new EntityComponentInitializer(blockID, data.Group);
return init.Has<PositionEntityStruct>() ? init.Get<PositionEntityStruct>().position : float3.zero;
}
ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID); ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID);
return posStruct.position; return posStruct.position;
} }

View file

@ -35,23 +35,32 @@ namespace GamecraftModdingAPI.Blocks
// implementations for Rotation static class // implementations for Rotation static class
public float3 RotateBlock(EGID blockID, Vector3 vector) internal float3 RotateBlock(EGID blockID, BlockEngine.BlockInitData data, Vector3 vector)
{ {
if (!entitiesDB.Exists<RotationEntityStruct>(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<RotationEntityStruct>(blockID); ref RotationEntityStruct rotStruct = ref this.entitiesDB.QueryEntity<RotationEntityStruct>(blockID);
ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity<GridRotationStruct>(blockID); ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity<GridRotationStruct>(blockID);
ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID); ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID);
ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity<UECSPhysicsEntityStruct>(blockID); ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntity<UECSPhysicsEntityStruct>(blockID);
// main (persistent) position // main (persistent) position
Quaternion newRotation = (Quaternion)rotStruct.rotation; Quaternion newRotation = rotStruct.rotation;
newRotation.eulerAngles += vector; newRotation.eulerAngles = vector;
rotStruct.rotation = (quaternion)newRotation; rotStruct.rotation = newRotation;
// placement grid rotation // placement grid rotation
Quaternion newGridRotation = (Quaternion)gridStruct.rotation; Quaternion newGridRotation = gridStruct.rotation;
newGridRotation.eulerAngles += vector; newGridRotation.eulerAngles = vector;
gridStruct.rotation = (quaternion)newGridRotation; gridStruct.rotation = newGridRotation;
// rendered position // rendered position
Quaternion newTransRotation = (Quaternion)rotStruct.rotation; Quaternion newTransRotation = rotStruct.rotation;
newTransRotation.eulerAngles += vector; newTransRotation.eulerAngles = vector;
transStruct.rotation = newTransRotation; transStruct.rotation = newTransRotation;
// collision position // collision position
FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Unity.Transforms.Rotation FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Unity.Transforms.Rotation
@ -63,8 +72,17 @@ namespace GamecraftModdingAPI.Blocks
} }
public float3 GetRotation(EGID blockID) internal float3 GetRotation(EGID blockID, BlockEngine.BlockInitData data)
{ {
if (!entitiesDB.Exists<RotationEntityStruct>(blockID))
{
if (data.Group == null) return float3.zero;
var init = new EntityComponentInitializer(blockID, data.Group);
return init.Has<RotationEntityStruct>()
? (float3) ((Quaternion) init.Get<RotationEntityStruct>().rotation).eulerAngles
: float3.zero;
}
ref RotationEntityStruct rotStruct = ref entitiesDB.QueryEntity<RotationEntityStruct>(blockID); ref RotationEntityStruct rotStruct = ref entitiesDB.QueryEntity<RotationEntityStruct>(blockID);
return ((Quaternion) rotStruct.rotation).eulerAngles; return ((Quaternion) rotStruct.rotation).eulerAngles;
} }