NorbiPeti
3592c6f464
Newly created blocks use the initializer to set properties, allowing the user to set per-block properties
181 lines
No EOL
5.6 KiB
C#
181 lines
No EOL
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
using Gamecraft.Wires;
|
|
using RobocraftX.Blocks;
|
|
using RobocraftX.Common;
|
|
using RobocraftX.GUI.Hotbar.Colours;
|
|
using RobocraftX.Physics;
|
|
using RobocraftX.Scene.Simulation;
|
|
using Svelto.DataStructures;
|
|
using Svelto.ECS;
|
|
|
|
using GamecraftModdingAPI.Engines;
|
|
|
|
namespace GamecraftModdingAPI.Blocks
|
|
{
|
|
/// <summary>
|
|
/// Engine for executing general block actions
|
|
/// </summary>
|
|
public partial class BlockEngine : IApiEngine
|
|
{
|
|
public string Name { get; } = "GamecraftModdingAPIBlockGameEngine";
|
|
|
|
public EntitiesDB entitiesDB { set; private get; }
|
|
|
|
public bool isRemovable => false;
|
|
|
|
internal bool Synced = true;
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
|
|
public void Ready()
|
|
{
|
|
}
|
|
|
|
public Block[] GetConnectedBlocks(EGID blockID)
|
|
{
|
|
if (!BlockExists(blockID)) return new Block[0];
|
|
Stack<EGID> cubeStack = new Stack<EGID>();
|
|
FasterList<EGID> cubes = new FasterList<EGID>(10);
|
|
var coll = entitiesDB.QueryEntities<GridConnectionsEntityStruct>();
|
|
foreach (var (ecoll, _) in coll)
|
|
foreach (ref var conn in ecoll)
|
|
conn.isProcessed = false;
|
|
|
|
ConnectedCubesUtility.TreeTraversal.GetConnectedCubes(entitiesDB, blockID, cubeStack, cubes,
|
|
(in GridConnectionsEntityStruct g) => { return false; });
|
|
|
|
var ret = new Block[cubes.count];
|
|
for (int i = 0; i < cubes.count; i++)
|
|
ret[i] = new Block(cubes[i]);
|
|
return ret;
|
|
}
|
|
|
|
public void SetBlockColorFromPalette(ref ColourParameterEntityStruct color)
|
|
{
|
|
ref var paletteEntry = ref entitiesDB.QueryEntity<PaletteEntryEntityStruct>(color.indexInPalette,
|
|
CommonExclusiveGroups.COLOUR_PALETTE_GROUP);
|
|
color.paletteColour = paletteEntry.Colour;
|
|
}
|
|
|
|
public ref T GetBlockInfo<T>(EGID blockID) where T : struct, IEntityComponent
|
|
{
|
|
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 : struct, IEntityComponent
|
|
{
|
|
if (entitiesDB.Exists<T>(block.Id))
|
|
return getter(entitiesDB.QueryEntity<T>(block.Id));
|
|
if (block.InitData.Group == null) return def;
|
|
var initializer = new EntityComponentInitializer(block.Id, block.InitData.Group);
|
|
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 SetBlockInfo<T, U>(Block block, Setter<T, U> setter, U value) where T : struct, IEntityComponent
|
|
{
|
|
if (entitiesDB.Exists<T>(block.Id))
|
|
setter(ref entitiesDB.QueryEntity<T>(block.Id), value);
|
|
if (block.InitData.Group != null)
|
|
{
|
|
var initializer = new EntityComponentInitializer(block.Id, block.InitData.Group);
|
|
ref T structRef = ref (new T[1])[0]; //A reference for a default value for struct
|
|
setter(ref structRef, value);
|
|
initializer.Init(structRef);
|
|
}
|
|
}
|
|
|
|
public bool BlockExists(EGID blockID)
|
|
{
|
|
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 EntityComponentInitializer(block.Id, block.InitData.Group);
|
|
return init.Has<T>();
|
|
}
|
|
|
|
public SimBody[] GetSimBodiesFromID(byte id)
|
|
{
|
|
var ret = new FasterList<SimBody>(4);
|
|
if (!entitiesDB.HasAny<ObjectIdEntityStruct>(CommonExclusiveGroups.BUILD_OBJID_BLOCK_GROUP))
|
|
return new SimBody[0];
|
|
var oids = entitiesDB.QueryEntities<ObjectIdEntityStruct>(CommonExclusiveGroups.BUILD_OBJID_BLOCK_GROUP);
|
|
var connections = entitiesDB.QueryMappedEntities<GridConnectionsEntityStruct>(CommonExclusiveGroups.BUILD_OBJID_BLOCK_GROUP);
|
|
foreach (ref ObjectIdEntityStruct oid in oids)
|
|
{
|
|
if (oid.objectId != id) continue;
|
|
var rid = connections.Entity(oid.ID.entityID).machineRigidBodyId;
|
|
foreach (var rb in ret)
|
|
{
|
|
if (rb.Id.entityID == rid)
|
|
goto DUPLICATE; //Multiple Object Identifiers on one rigid body
|
|
}
|
|
ret.Add(new SimBody(rid));
|
|
DUPLICATE: ;
|
|
}
|
|
return ret.ToArray();
|
|
}
|
|
|
|
public ObjectIdentifier[] GetObjectIDsFromID(byte id, bool sim)
|
|
{
|
|
var ret = new FasterList<ObjectIdentifier>(4);
|
|
if (!entitiesDB.HasAny<ObjectIdEntityStruct>(CommonExclusiveGroups.BUILD_OBJID_BLOCK_GROUP))
|
|
return new ObjectIdentifier[0];
|
|
var oids = entitiesDB.QueryEntities<ObjectIdEntityStruct>(CommonExclusiveGroups.BUILD_OBJID_BLOCK_GROUP);
|
|
foreach (ref ObjectIdEntityStruct oid in oids)
|
|
if (sim ? oid.simObjectId == id : oid.objectId == id)
|
|
ret.Add(new ObjectIdentifier(oid.ID));
|
|
return ret.ToArray();
|
|
}
|
|
|
|
public SimBody[] GetConnectedSimBodies(uint id)
|
|
{
|
|
var joints = entitiesDB.QueryEntities<JointEntityStruct>(MachineSimulationGroups.JOINTS_GROUP);
|
|
var list = new FasterList<SimBody>(4);
|
|
foreach (var joint in joints)
|
|
{
|
|
if (joint.jointState == JointState.Broken) continue;
|
|
if (joint.connectedEntityA == id) list.Add(new SimBody(joint.connectedEntityB));
|
|
else if (joint.connectedEntityB == id) list.Add(new SimBody(joint.connectedEntityA));
|
|
}
|
|
|
|
return list.ToArray();
|
|
}
|
|
|
|
public EGID? FindBlockEGID(uint id)
|
|
{
|
|
var groups = entitiesDB.FindGroups<DBEntityStruct>();
|
|
foreach (ExclusiveGroupStruct group in groups)
|
|
{
|
|
if (entitiesDB.Exists<DBEntityStruct>(id, group))
|
|
return new EGID(id, group);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
#if DEBUG
|
|
public EntitiesDB GetEntitiesDB()
|
|
{
|
|
return entitiesDB;
|
|
}
|
|
#endif
|
|
}
|
|
} |