Browse Source

Implement init for position and rotation

tags/v2.2.0
NorbiPeti 4 years ago
parent
commit
d842df7681
4 changed files with 52 additions and 16 deletions
  1. +4
    -4
      GamecraftModdingAPI/Block.cs
  2. +3
    -0
      GamecraftModdingAPI/Blocks/ConsoleBlock.cs
  3. +17
    -2
      GamecraftModdingAPI/Blocks/MovementEngine.cs
  4. +28
    -10
      GamecraftModdingAPI/Blocks/RotationEngine.cs

+ 4
- 4
GamecraftModdingAPI/Block.cs 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);
}
}



+ 3
- 0
GamecraftModdingAPI/Blocks/ConsoleBlock.cs 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


+ 17
- 2
GamecraftModdingAPI/Blocks/MovementEngine.cs 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;
}


+ 28
- 10
GamecraftModdingAPI/Blocks/RotationEngine.cs 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;
}


Loading…
Cancel
Save