Start using new extension methods, code cleanup

Removed all of the different block property getter methods
This commit is contained in:
Norbi Peti 2021-05-10 22:45:07 +02:00
parent 2d99d1d478
commit 61184145a9
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 55 additions and 179 deletions

View file

@ -254,10 +254,10 @@ namespace TechbloxModdingAPI
/// </summary>
public float3 Position
{
get => MovementEngine.GetPosition(Id, InitData);
get => MovementEngine.GetPosition(this);
set
{
MovementEngine.MoveBlock(Id, InitData, value);
MovementEngine.MoveBlock(this, value);
if (blockGroup != null)
blockGroup.PosAndRotCalculated = false;
BlockEngine.UpdateDisplayedBlock(Id);
@ -269,10 +269,10 @@ namespace TechbloxModdingAPI
/// </summary>
public float3 Rotation
{
get => RotationEngine.GetRotation(Id, InitData);
get => RotationEngine.GetRotation(this);
set
{
RotationEngine.RotateBlock(Id, InitData, value);
RotationEngine.RotateBlock(this, value);
if (blockGroup != null)
blockGroup.PosAndRotCalculated = false;
BlockEngine.UpdateDisplayedBlock(Id);

View file

@ -70,75 +70,9 @@ namespace TechbloxModdingAPI.Blocks
: entitiesDB.QueryEntity<PaletteEntryEntityStruct>(index,
CommonExclusiveGroups.COLOUR_PALETTE_GROUP).Colour;
public OptionalRef<T> GetBlockInfo<T>(EGID blockID) where T : unmanaged, IEntityComponent
public OptionalRef<T> GetBlockInfo<T>(Block block) where T : unmanaged, IEntityComponent
{
return entitiesDB.TryQueryEntitiesAndIndex<T>(blockID, out uint index, out var array)
? new OptionalRef<T>(array, index)
: new OptionalRef<T>();
}
public ref T GetBlockInfoViewStruct<T>(EGID blockID) where T : struct, INeedEGID, IEntityViewComponent
{
if (entitiesDB.Exists<T>(blockID))
return ref entitiesDB.QueryEntity<T>(blockID);
T[] structHolder = new T[1]; //Create something that can be referenced
return ref structHolder[0]; //Gets a default value automatically
}
public U GetBlockInfo<T, U>(Block block, Func<T, U> getter,
U def = default) where T : unmanaged, IEntityComponent
{
if (entitiesDB.Exists<T>(block.Id))
return getter(entitiesDB.QueryEntity<T>(block.Id));
return GetBlockInitInfo(block, getter, def);
}
public U GetBlockInfoViewStruct<T, U>(Block block, Func<T, U> getter,
U def = default) where T : struct, IEntityViewComponent
{
if (entitiesDB.Exists<T>(block.Id))
return getter(entitiesDB.QueryEntity<T>(block.Id));
return GetBlockInitInfo(block, getter, def);
}
private U GetBlockInitInfo<T, U>(Block block, Func<T, U> getter, U def) where T : struct, IEntityComponent
{
if (block.InitData.Group == null) return def;
var initializer = new EntityInitializer(block.Id, block.InitData.Group, block.InitData.Reference);
if (initializer.Has<T>())
return getter(initializer.Get<T>());
return def;
}
public delegate void Setter<T, U>(ref T component, U value) where T : struct, IEntityComponent;
public void SetBlockInfoViewStruct<T, U>(Block block, Setter<T, U> setter, U value) where T : struct, IEntityViewComponent
{
if (entitiesDB.Exists<T>(block.Id))
setter(ref entitiesDB.QueryEntity<T>(block.Id), value);
else
SetBlockInitInfo(block, setter, value);
}
public void SetBlockInfo<T, U>(Block block, Setter<T, U> setter, U value) where T : unmanaged, IEntityComponent
{
if (entitiesDB.Exists<T>(block.Id))
setter(ref entitiesDB.QueryEntity<T>(block.Id), value);
else
SetBlockInitInfo(block, setter, value);
}
private void SetBlockInitInfo<T, U>(Block block, Setter<T, U> setter, U value)
where T : struct, IEntityComponent
{
if (block.InitData.Group != null)
{
var initializer = new EntityInitializer(block.Id, block.InitData.Group, block.InitData.Reference);
T component = initializer.Has<T>() ? initializer.Get<T>() : default;
ref T structRef = ref component;
setter(ref structRef, value);
initializer.Init(structRef);
}
return entitiesDB.QueryEntityOptional<T>(block);
}
public void UpdateDisplayedBlock(EGID id)
@ -153,7 +87,8 @@ namespace TechbloxModdingAPI.Blocks
internal void UpdatePrefab(Block block, ushort type, byte material, bool flipped)
{
uint pid = PrefabsID.GetOrCreatePrefabID(type, material, 0, flipped);
entitiesDB.QueryEntityOrDefault<GFXPrefabEntityStructGPUI>()
entitiesDB.QueryEntityOrDefault<GFXPrefabEntityStructGPUI>(block).prefabID = pid;
entitiesDB.QueryEntityOrDefault<PhysicsPrefabEntityStruct>(block) = new PhysicsPrefabEntityStruct(pid);
}
public bool BlockExists(EGID blockID)
@ -161,16 +96,6 @@ namespace TechbloxModdingAPI.Blocks
return entitiesDB.Exists<DBEntityStruct>(blockID);
}
public bool GetBlockInfoExists<T>(Block block) where T : struct, IEntityComponent
{
if (entitiesDB.Exists<T>(block.Id))
return true;
if (block.InitData.Group == null)
return false;
var init = new EntityInitializer(block.Id, block.InitData.Group, block.InitData.Reference);
return init.Has<T>();
}
public SimBody[] GetSimBodiesFromID(byte id)
{
var ret = new FasterList<SimBody>(4);

View file

@ -34,21 +34,12 @@ namespace TechbloxModdingAPI.Blocks
// implementations for Movement static class
internal float3 MoveBlock(EGID blockID, BlockEngine.BlockInitData data, float3 vector)
internal float3 MoveBlock(Block block, float3 vector)
{
if (!entitiesDB.Exists<PositionEntityStruct>(blockID))
{
if (data.Group == null) return float3.zero;
var init = new EntityInitializer(blockID, data.Group, data.Reference);
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);
ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntityOrDefault<PositionEntityStruct>(block);
ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntityOrDefault<GridRotationStruct>(block);
ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntityOrDefault<LocalTransformEntityStruct>(block);
ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntityOrDefault<UECSPhysicsEntityStruct>(block);
// main (persistent) position
posStruct.position = vector;
// placement grid position
@ -56,24 +47,21 @@ namespace TechbloxModdingAPI.Blocks
// rendered position
transStruct.position = vector;
// collision position
if (phyStruct.ID != EGID.Empty)
{ //It exists
FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity, new Translation
{
Value = posStruct.position
});
entitiesDB.QueryEntity<GridConnectionsEntityStruct>(blockID).isProcessed = false;
}
entitiesDB.QueryEntityOrDefault<GridConnectionsEntityStruct>(block).isProcessed = false;
return posStruct.position;
}
internal float3 GetPosition(EGID blockID, BlockEngine.BlockInitData data)
internal float3 GetPosition(Block block)
{
if (!entitiesDB.Exists<PositionEntityStruct>(blockID))
{
if (data.Group == null) return float3.zero;
var init = new EntityInitializer(blockID, data.Group, data.Reference);
return init.Has<PositionEntityStruct>() ? init.Get<PositionEntityStruct>().position : float3.zero;
}
ref PositionEntityStruct posStruct = ref this.entitiesDB.QueryEntity<PositionEntityStruct>(blockID);
return posStruct.position;
return entitiesDB.QueryEntityOrDefault<PositionEntityStruct>(block).position;
}
}
}

View file

@ -34,55 +34,38 @@ namespace TechbloxModdingAPI.Blocks
// implementations for Rotation static class
internal float3 RotateBlock(EGID blockID, BlockEngine.BlockInitData data, Vector3 vector)
internal float3 RotateBlock(Block block, Vector3 vector)
{
if (!entitiesDB.Exists<RotationEntityStruct>(blockID))
{
if (data.Group == null) return float3.zero;
var init = new EntityInitializer(blockID, data.Group, data.Reference);
init.GetOrCreate<RotationEntityStruct>().rotation = Quaternion.Euler(vector);
init.GetOrCreate<GridRotationStruct>().rotation = Quaternion.Euler(vector);
init.GetOrCreate<LocalTransformEntityStruct>().rotation = Quaternion.Euler(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
ref RotationEntityStruct rotStruct = ref this.entitiesDB.QueryEntityOrDefault<RotationEntityStruct>(block);
ref GridRotationStruct gridStruct = ref this.entitiesDB.QueryEntityOrDefault<GridRotationStruct>(block);
ref LocalTransformEntityStruct transStruct = ref this.entitiesDB.QueryEntityOrDefault<LocalTransformEntityStruct>(block);
ref UECSPhysicsEntityStruct phyStruct = ref this.entitiesDB.QueryEntityOrDefault<UECSPhysicsEntityStruct>(block);
// main (persistent) rotation
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
gridStruct.rotation = newRotation;
// rendered rotation
transStruct.rotation = newRotation;
// collision rotation
if (phyStruct.ID != EGID.Empty)
{ //It exists
FullGameFields._physicsWorld.EntityManager.SetComponentData(phyStruct.uecsEntity,
new Unity.Transforms.Rotation
{
Value = rotStruct.rotation
});
entitiesDB.QueryEntity<GridConnectionsEntityStruct>(blockID).isProcessed = false;
}
entitiesDB.QueryEntityOrDefault<GridConnectionsEntityStruct>(block).isProcessed = false;
return ((Quaternion)rotStruct.rotation).eulerAngles;
}
internal float3 GetRotation(EGID blockID, BlockEngine.BlockInitData data)
internal float3 GetRotation(Block block)
{
if (!entitiesDB.Exists<RotationEntityStruct>(blockID))
{
if (data.Group == null) return float3.zero;
var init = new EntityInitializer(blockID, data.Group, data.Reference);
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.QueryEntityOrDefault<RotationEntityStruct>(block);
return ((Quaternion) rotStruct.rotation).eulerAngles;
}
}

View file

@ -3,6 +3,7 @@ using Svelto.ECS;
using Svelto.DataStructures;
using Gamecraft.Wires;
using TechbloxModdingAPI.Engines;
using TechbloxModdingAPI.Utility;
namespace TechbloxModdingAPI.Blocks
{
@ -87,8 +88,8 @@ namespace TechbloxModdingAPI.Blocks
public ref PortEntityStruct GetPortByOffset(Block block, byte portNumber, bool input)
{
BlockPortsStruct bps = GetFromDbOrInitData<BlockPortsStruct>(block, block.Id, out bool exists);
if (!exists)
var bps = entitiesDB.QueryEntityOptional<BlockPortsStruct>(block);
if (!bps)
{
throw new BlockException("Block does not exist");
}
@ -208,8 +209,9 @@ namespace TechbloxModdingAPI.Blocks
public EGID MatchBlockInputToPort(Block block, byte portUsage, out bool exists)
{
BlockPortsStruct ports = GetFromDbOrInitData<BlockPortsStruct>(block, block.Id, out exists);
return new EGID(ports.firstInputID + portUsage, NamedExclusiveGroup<InputPortsGroup>.Group);
var ports = entitiesDB.QueryEntityOptional<BlockPortsStruct>(block);
exists = ports;
return new EGID(ports.Get().firstInputID + portUsage, NamedExclusiveGroup<InputPortsGroup>.Group);
}
public EGID MatchBlockInputToPort(EGID block, byte portUsage, out bool exists)
@ -226,8 +228,9 @@ namespace TechbloxModdingAPI.Blocks
public EGID MatchBlockOutputToPort(Block block, byte portUsage, out bool exists)
{
BlockPortsStruct ports = GetFromDbOrInitData<BlockPortsStruct>(block, block.Id, out exists);
return new EGID(ports.firstOutputID + portUsage, NamedExclusiveGroup<OutputPortsGroup>.Group);
var ports = entitiesDB.QueryEntityOptional<BlockPortsStruct>(block);
exists = ports;
return new EGID(ports.Get().firstOutputID + portUsage, NamedExclusiveGroup<OutputPortsGroup>.Group);
}
public EGID MatchBlockOutputToPort(EGID block, byte portUsage, out bool exists)
@ -385,29 +388,6 @@ namespace TechbloxModdingAPI.Blocks
return results.ToArray();
}
private ref T GetFromDbOrInitData<T>(Block block, EGID id, out bool exists) where T : unmanaged, IEntityComponent
{
T[] defRef = new T[1];
if (entitiesDB.Exists<T>(id))
{
exists = true;
return ref entitiesDB.QueryEntity<T>(id);
}
if (block == null || block.InitData.Group == null)
{
exists = false;
return ref defRef[0];
}
EntityInitializer initializer = new EntityInitializer(block.Id, block.InitData.Group, block.InitData.Reference);
if (initializer.Has<T>())
{
exists = true;
return ref initializer.Get<T>();
}
exists = false;
return ref defRef[0];
}
private EntityCollection<ChannelDataStruct> GetSignalStruct(uint signalID, out uint index, bool input = true)
{
ExclusiveGroup group = input