Implement init for position and rotation

This commit is contained in:
Norbi Peti 2020-07-15 22:46:48 +02:00
parent 3592c6f464
commit d842df7681
4 changed files with 52 additions and 16 deletions

View file

@ -209,10 +209,10 @@ namespace GamecraftModdingAPI
/// </summary>
public float3 Position
{
get => Exists ? MovementEngine.GetPosition(Id) : float3.zero;
get => MovementEngine.GetPosition(Id, InitData);
set
{
if (Exists) MovementEngine.MoveBlock(Id, value);
MovementEngine.MoveBlock(Id, InitData, value);
}
}
@ -221,10 +221,10 @@ namespace GamecraftModdingAPI
/// </summary>
public float3 Rotation
{
get => Exists ? RotationEngine.GetRotation(Id) : float3.zero;
get => RotationEngine.GetRotation(Id, InitData);
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
/// <summary>
/// Setting a nonexistent command will crash the game when switching to simulation
/// </summary>
public string Command
{
get

View file

@ -35,8 +35,17 @@ namespace GamecraftModdingAPI.Blocks
// 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 GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntity<GridRotationStruct>(blockID);
ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntity<LocalTransformEntityStruct>(blockID);
@ -56,8 +65,14 @@ namespace GamecraftModdingAPI.Blocks
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);
return posStruct.position;
}

View file

@ -35,23 +35,32 @@ namespace GamecraftModdingAPI.Blocks
// 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 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);
// main (persistent) position
Quaternion newRotation = (Quaternion)rotStruct.rotation;
newRotation.eulerAngles += vector;
rotStruct.rotation = (quaternion)newRotation;
Quaternion newRotation = rotStruct.rotation;
newRotation.eulerAngles = vector;
rotStruct.rotation = newRotation;
// placement grid rotation
Quaternion newGridRotation = (Quaternion)gridStruct.rotation;
newGridRotation.eulerAngles += vector;
gridStruct.rotation = (quaternion)newGridRotation;
Quaternion newGridRotation = gridStruct.rotation;
newGridRotation.eulerAngles = vector;
gridStruct.rotation = newGridRotation;
// rendered position
Quaternion newTransRotation = (Quaternion)rotStruct.rotation;
newTransRotation.eulerAngles += vector;
Quaternion newTransRotation = rotStruct.rotation;
newTransRotation.eulerAngles = vector;
transStruct.rotation = newTransRotation;
// collision position
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);
return ((Quaternion) rotStruct.rotation).eulerAngles;
}