This belongs in the DOTS forum.
How I’m doing it:
The following is my control script.
void Start()
{
em = World.DefaultGameObjectInjectionWorld.EntityManager;
var settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, blobAssetStore);
settings.ConversionFlags = GameObjectConversionUtility.ConversionFlags.AssignName;
weaponEntity = GameObjectConversionUtility.ConvertGameObjectHierarchy(weaponMainLowPrefab, settings);
enemyEntity = GameObjectConversionUtility.ConvertGameObjectHierarchy(enemyPrefab, settings);
}
Entity CreateEnemy(int state, int X, int Y)
{
var instance2 = em.Instantiate(enemyEntity);
var actionThreshold = 1f;
var health = 100f;
switch (state)
{
case 0:
actionThreshold = 0.1f;
health = 100f;
break;
}
switch(mapStart)
{
case Starts.RegularGrowth:
health = 10f;
break;
}
var position = positionMapping[X, Y];
var we = new EnemyEntity()
{
actionThreshold = actionThreshold,
actionValue = 0,
health = health,
lerp = 0f,
mapCorner0 = mapCorners[0],
mapCorner1 = mapCorners[2],
newPosition = position,
oldPosition = position,
state = state,
weaponEntity = weaponEntity,
X = X,
Y = Y
};
em.SetComponentData(instance2, new Translation { Value = position });
em.SetComponentData(instance2, new Rotation { Value = quaternion.identity });
em.SetComponentData(instance2, we);
return instance2;
}
The prefabs must already have the components, with the [GenerateAuthoringComponent] tag above the struct, and a ConvertToEntity component (with ConvertAndDestroy selected).
The following is the enemy system.
var weaponCreation = new NativeQueue<WeaponCreationStruct>(Allocator.TempJob);
var parallel = weaponCreation.AsParallelWriter();
Entities
.WithName("EnemySystem")
.WithBurst(Unity.Burst.FloatMode.Default, Unity.Burst.FloatPrecision.Standard, true)
.ForEach((Entity entity, int nativeThreadIndex, ref Translation translation, ref Rotation rotation, ref EnemyEntity enemy, in LocalToWorld localToWorld) =>
{
parallel.Enqueue(new WeaponCreationStruct()
{
velocity = localToWorld.Forward * 55 /** velocity*/,
entity = enemy.weaponEntity,
weaponEntity = new WeaponEntity(WeaponType.MainLow, enemy.mapCorner0.x, enemy.mapCorner1.x, enemy.mapCorner0.y, enemy.mapCorner1.y),
position = front, // any lower and it collides with the player when the player moves
rotation = frontDir,
});
}).ScheduleParallel()
Dependency.Complete();
if (weaponCreation.Count > 0)
{
int count = weaponCreation.Count;
for (int i = 0; i < count; i++)
{
var current = weaponCreation.Dequeue();
var instance = EntityManager.Instantiate(current.entity);
EntityManager.SetComponentData(instance, new Translation { Value = current.position });
EntityManager.SetComponentData(instance, new Rotation { Value = current.rotation });
EntityManager.SetComponentData(instance, new Unity.Physics.PhysicsVelocity() { Linear = current.velocity });
EntityManager.SetComponentData(instance, current.weaponEntity);
}
}