Example #1
0
        public static void CreateExplosive(int explosiveID, float3 spawnPos, uint ownerID)
        {
            #if UNITY_EDITOR
            debugGroupID += 1;
            #endif

            CreateVisualComponents(spawnPos, Parameters.GetExplosiveDataByID(explosiveID)); // temporary, see method

            SetComponents(explosiveID, spawnPos, Parameters.GetExplosiveDataByID(explosiveID), ownerID);
        }
Example #2
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            int count = ExplosiveQuery.CalculateEntityCount();

            if (count == 0)
            {
                return(inputDeps);
            }

            // get the explosive entities
            var entities = ExplosiveQuery.ToEntityArray(Allocator.TempJob, out JobHandle entHandle);

            entHandle.Complete();

            // Get all our ECS components we will need - this are minified jobs
            var explosives = ExplosiveQuery.ToComponentDataArray <Explosive>(Allocator.TempJob, out JobHandle expHandle);
            var positions  = ExplosiveQuery.ToComponentDataArray <Translation>(Allocator.TempJob, out JobHandle posHandle);
            var owners     = ExplosiveQuery.ToComponentDataArray <OwnerID>(Allocator.TempJob, out JobHandle ownerHandle);

            // make sure we have all our component data (minified jobs are done)
            expHandle.Complete();
            posHandle.Complete();
            ownerHandle.Complete();

            int           explosiveId;
            ExplosiveData data;

            // --------------------------
            // TODO: FIX GC: Adding to list is creating 21.9 KB a frame
            // THIS IS GENERATING GARBAGE
            // --------------------------
            List <ECSExplosiveData> tmpHitData = new List <ECSExplosiveData>();
            List <Collider>         explosiveHits;

            // for every explosion
            for (int i = 0; i < count; i++)
            {
                explosiveId = explosives[i].ID;
                data        = Parameters.GetExplosiveDataByID(explosiveId);

                // if we have hits
                ECSExplosiveData ecsData;
                // TODO: Check, is this going to apply to child colliders too? We don't want that
                int hitCount = Physics.OverlapSphereNonAlloc(positions[i].Value, data.radius, Colliders, HitMask);
                if (hitCount > 0)
                {
                    explosiveHits = new List <Collider>();
                    ecsData       = new ECSExplosiveData {
                        ExplosiveID = explosiveId, OwnerID = owners[i].Value, Position = positions[i].Value
                    };

                    // for every hit we have
                    for (int j = 0; j < Colliders.Length; j++)
                    {
                        // if we have a null collider - every collider after should be null as well
                        if (Colliders[j] == null)
                        {
                            break;
                        }

                        explosiveHits.Add(Colliders[j]);
                    }

                    // if we have hits, add it to the main array - may be an unneeded check
                    if (explosiveHits.Count > 0)
                    {
                        ecsData.Colliders = explosiveHits.ToArray();
                        tmpHitData.Add(ecsData);
                    }
                }
            }

            // if we have explosion hits
            if (tmpHitData.Count > 0)
            {
                onExplosionHitSystemFinish?.Invoke(tmpHitData.ToArray());
            }

            // batch destroy explosive entities
            EntityManager.DestroyEntity(entities);

            positions.Dispose();
            entities.Dispose();
            owners.Dispose();
            explosives.Dispose();

            return(inputDeps);
        }