using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Transforms;
using Random = UnityEngine.Random;
public class EntitySpawnerSystem : SystemBase
{
private EndSimulationEntityCommandBufferSystem _commandBufferSystem;
private float _elapsedTime = 0.0f;
private int _entityCount = 0;
protected override void OnCreate()
{
_commandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
if (_entityCount > 1500)
return;
EntityCommandBuffer commandBuffer = _commandBufferSystem.CreateCommandBuffer();
_elapsedTime += Time.DeltaTime;
if (_elapsedTime >= 0.0001f)
{
_entityCount++;
_elapsedTime = 0.0f;
Entities.ForEach((ref BurstFireData burstFireData) =>
{
Entity spawnedEntity = commandBuffer.Instantiate(burstFireData.bulletPrefabEntity);
commandBuffer.SetComponent(spawnedEntity,
new Translation() { Value = new float3(Random.Range(-1f, 1f), 3, Random.Range(-1f, 1f)) });
commandBuffer.SetComponent(spawnedEntity,
new PhysicsVelocity() { Linear = new float3(0, Random.Range(10, 25), 0) });
}).WithoutBurst().Run();
}
}
}