Exemplo n.º 1
0
        private void Spawn(ComponentArray <Spawn> spawns, int count, int target)
        {
            List <Spawn> inactiveSpawns = new List <Spawn>();

            for (int i = 0; i < spawns.Length; i++)
            {
                var spawn = spawns[i];
                if (spawn.spawning)
                {
                    count++;
                }
                else
                {
                    inactiveSpawns.Add(spawn);
                }
            }

            while (count < target)
            {
                var index = Random.Range(0, inactiveSpawns.Count);
                var spawn = inactiveSpawns[index];
                inactiveSpawns.RemoveAt(index);
                spawn.TimeLeft = spawn.TimeToTrigger;
                spawn.spawning = true;
                count++;
            }
        }
Exemplo n.º 2
0
        public void RemoveComponent(int entityId, Type type)
        {
            Entity ent = FindEntity(entityId);

            if (ent == null)
            {
                return;
            }

            int comTypeId = ComponentTypeManager.GetTypeId(type);

            ComponentArray entComArray = componentsArrayOfEntity[entityId];
            Component      com         = entComArray[comTypeId];

            if (com != null)
            {
                entComArray[comTypeId] = null;

                /*ComponentArray typeComArray = typeConponnetsArray[comTypeId];
                 * typeComArray.Remove(com);*/

                systemManager.OnRemoveComponent(ent, com);

                BitSet bits = componentBitsOfEntity[entityId];
                bits[comTypeId] = false;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets a <see cref="IComponentArray"/> of a component type.
        /// </summary>
        /// <typeparam name="T">The type of component.</typeparam>
        /// <param name="create">Create array if one doesnt exist.</param>
        /// <returns>A <see cref="IComponentArray"/>.</returns>
        private ComponentArray <T> GetComponentArray <T>(bool create) where T : IComponent
        {
            Type            componentType = typeof(T);
            IComponentArray array;

            if (!_componentDatabase.TryGetValue(componentType, out array) && create)
            {
                array = new ComponentArray <T>();
                _componentDatabase.Add(componentType, array);
            }

            return((ComponentArray <T>)array);
        }
Exemplo n.º 4
0
    public static void SetupFanComponents(ref ComponentArray <Fan> fanComponents)
    {
        s_SourceJoints.SetTransforms(null);
        s_FanJoints.SetTransforms(null);

        for (var i = 0; i < fanComponents.Length; i++)
        {
            foreach (var fanData in fanComponents[i].fanDatas)
            {
                AddFanJoint(fanData);
            }
        }
    }
Exemplo n.º 5
0
        public void RemoveAllComponents(int entityId)
        {
            ComponentArray entComArray = componentsArrayOfEntity[entityId];

            for (int i = 0; i < entComArray.GetCapacity(); ++i)
            {
                Component com = entComArray[i];
                if (com != null)
                {
                    RemoveComponent(entityId, com.GetType());
                }
            }
        }
Exemplo n.º 6
0
        public void AddComponent(int entityId, Component component)
        {
            Entity ent = FindEntity(entityId);

            if (ent == null)
            {
                return;
            }

            int comTypeId = ComponentTypeManager.GetTypeId(component.GetType());

            ComponentArray entComArray = null;

            if (entityId < componentsArrayOfEntity.Count)
            {
                entComArray = componentsArrayOfEntity[entityId];
            }
            else
            {
                entComArray = new ComponentArray();
                componentsArrayOfEntity[entityId] = entComArray;
            }

            Component com = entComArray[comTypeId];

            if (com == null)
            {
                com = component;
                entComArray[comTypeId] = com;

                /*ComponentArray typeComArray = null;
                 * if (comTypeId < typeConponnetsArray.Count)
                 *      typeComArray = typeConponnetsArray[comTypeId];
                 * else
                 * {
                 *      typeComArray = new ComponentArray();
                 *      typeConponnetsArray[comTypeId] = typeComArray;
                 * }
                 * typeComArray.Add(com);*/

                BitSet bits = componentBitsOfEntity[entityId];
                bits[comTypeId] = true;

                systemManager.OnAddComponent(ent, com);
            }
            else
            {
                //throw new Exception("EntityManager.AddComponent: repeated component " + component.GetType());
            }
        }
Exemplo n.º 7
0
    public static void SetupTwistComponents(ref ComponentArray <Twist> twistComponents)
    {
        s_SetupIndex = 0;
        s_TwistIndex = 0;
        s_SourceJoints.SetTransforms(null);
        s_TwistJoints.SetTransforms(null);

        for (var i = 0; i < twistComponents.Length; i++)
        {
            for (var j = 0; j < twistComponents[i].twistChains.Count; j++)
            {
                AddTwistChain(twistComponents[i].twistChains[j]);
            }
        }
    }
Exemplo n.º 8
0
    public static void SetupTranslateScaleComponents(ref ComponentArray <TranslateScale> translateScaleComponents)
    {
        s_DriverIndex = 0;
        s_DrivenIndex = 0;
        s_SourceJoints.SetTransforms(null);
        s_DrivenJoints.SetTransforms(null);

        for (var i = 0; i < translateScaleComponents.Length; i++)
        {
            for (var j = 0; j < translateScaleComponents[i].chains.Count; j++)
            {
                AddTranslateScaleChains(translateScaleComponents[i].chains[j]);
            }
        }
    }
Exemplo n.º 9
0
        public Component GetComponent(int entityId, Type type)
        {
            Entity ent = FindEntity(entityId);

            if (ent == null)
            {
                return(null);
            }

            int comTypeId = ComponentTypeManager.GetTypeId(type);

            ComponentArray entComArray = componentsArrayOfEntity[entityId];
            Component      com         = entComArray[comTypeId];

            return(com);
        }
Exemplo n.º 10
0
    public static void PrepareColliders(ref ComponentArray <HitCollisionHistory> collections, int tick, int mask, Entity forceExcluded, Entity forceIncluded, ray ray, float rayDist, float radius)
    {
        Profiler.BeginSample("HitCollisionHistory.PrepareColliders [SphereCast]");

        for (var i = 0; i < collections.Length; i++)
        {
            var collection = collections[i];
            if (!IsRelevant(collection, mask, forceExcluded, forceIncluded))
            {
                collection.DisableHitCollision();
                continue;
            }

            var stateIndex = collection.GetStateIndex(tick);

            Profiler.BeginSample("-capsule test");
            var boundCenter = collection.boundsCenterBuffer[stateIndex];

            var rayEnd            = ray.origin + ray.direction * rayDist;
            var closestPointOnRay = coll.ClosestPointOnLineSegment(ray.origin, rayEnd, boundCenter);
            var dist      = math.distance(closestPointOnRay, boundCenter);
            var boundsHit = dist < collection.settings.boundsRadius + radius;

            Profiler.EndSample();

            if (boundsHit)
            {
                collection.EnableCollisionForIndex(stateIndex);
            }
            else
            {
                collection.DisableHitCollision();
            }

            if (HitCollisionModule.ShowDebug.IntValue > 0)
            {
                DebugPrimitiveModule.ClearChannel(HitCollisionModule.PrimDebugChannel);
                DebugPrimitiveModule.CreateCapsulePrimitive(HitCollisionModule.PrimDebugChannel,
                                                            ray.origin + ray.direction * radius, ray.origin + ray.direction * (rayDist - radius), radius, Color.yellow, 5);
                DebugPrimitiveModule.CreateSpherePrimitive(HitCollisionModule.PrimDebugChannel, boundCenter,
                                                           collection.settings.boundsRadius,
                                                           boundsHit ? Color.yellow : Color.gray, 5);
            }
        }

        Profiler.EndSample();
    }
Exemplo n.º 11
0
    int FindPlayerControlling(ref ComponentArray <PlayerState> players, Entity entity)
    {
        if (entity == Entity.Null)
        {
            return(-1);
        }

        for (int i = 0, c = players.Length; i < c; ++i)
        {
            var playerState = players[i];
            if (playerState.controlledEntity == entity)
            {
                return(i);
            }
        }
        return(-1);
    }
Exemplo n.º 12
0
    public static void PrepareColliders(ref ComponentArray <HitCollisionHistory> collections, int tick, int mask, Entity forceExcluded, Entity forceIncluded, sphere sphere)
    {
        Profiler.BeginSample("HitCollisionHistory.PrepareColliders [Sphere]");

        for (var i = 0; i < collections.Length; i++)
        {
            var collection = collections[i];
            if (!IsRelevant(collection, mask, forceExcluded, forceIncluded))
            {
                collection.DisableHitCollision();
                continue;
            }

            var stateIndex = collection.GetStateIndex(tick);

            var boundsCenter = collection.boundsCenterBuffer[stateIndex];
            var boundsRadius = collection.settings.boundsRadius;
            var dist         = math.distance(sphere.center, boundsCenter);

            var boundsHit = dist < sphere.radius + boundsRadius;

            if (boundsHit)
            {
                collection.EnableCollisionForIndex(stateIndex);
            }
            else
            {
                collection.DisableHitCollision();
            }

            if (HitCollisionModule.ShowDebug.IntValue > 0)
            {
                DebugPrimitiveModule.ClearChannel(HitCollisionModule.PrimDebugChannel);
                DebugPrimitiveModule.CreateSpherePrimitive(HitCollisionModule.PrimDebugChannel, sphere.center, sphere.radius,
                                                           Color.yellow, 5);
                DebugPrimitiveModule.CreateSpherePrimitive(HitCollisionModule.PrimDebugChannel, boundsCenter,
                                                           boundsRadius,
                                                           boundsHit ? Color.yellow : Color.gray, 5);
            }
        }

        Profiler.EndSample();
    }
Exemplo n.º 13
0
    public static void PrepareColliders(ref ComponentArray <HitCollisionHistory> collections, int tick, int mask, Entity forceExcluded, Entity forceIncluded, ray ray, float rayDist)
    {
        Profiler.BeginSample("HitCollisionHistory.PrepareColliders [Ray]");

        // Rollback
        for (var i = 0; i < collections.Length; i++)
        {
            var collection = collections[i];
            if (!IsRelevant(collection, mask, forceExcluded, forceIncluded))
            {
                collection.DisableHitCollision();
                continue;
            }

            var stateIndex = collection.GetStateIndex(tick);

            Profiler.BeginSample("-raycast");

            var sphere    = primlib.sphere(collection.boundsCenterBuffer[stateIndex], collection.settings.boundsRadius);
            var boundsHit = coll.RayCast(sphere, ray, rayDist);

            Profiler.EndSample();

            if (boundsHit)
            {
                collection.EnableCollisionForIndex(stateIndex);
            }
            else
            {
                collection.DisableHitCollision();
            }

            if (HitCollisionModule.ShowDebug.IntValue > 0)
            {
                DebugPrimitiveModule.ClearChannel(HitCollisionModule.PrimDebugChannel);
                DebugPrimitiveModule.CreateLinePrimitive(HitCollisionModule.PrimDebugChannel, ray.origin, ray.origin + ray.direction * rayDist, Color.yellow, 5);
                DebugPrimitiveModule.CreateSpherePrimitive(HitCollisionModule.PrimDebugChannel, sphere.center, sphere.radius,
                                                           boundsHit ? Color.yellow : Color.gray, 5);
            }
        }

        Profiler.EndSample();
    }
Exemplo n.º 14
0
        private int UpdateSpawners(ComponentArray <Spawn> spawns, float deltaTime)
        {
            int spawnCount = 0;

            for (int i = 0; i < spawns.Length; i++)
            {
                var spawn = spawns[i];

                if (spawn.TimeLeft > 0.0)
                {
                    spawn.TimeLeft -= deltaTime;
                }
                else if (spawn.TimeLeft < 0.0)
                {
                    GameObject.Instantiate(spawn.Prefab, spawn.gameObject.transform.position, Quaternion.identity);
                    spawn.TimeLeft = 0.0f;
                    spawn.spawning = false;
                    spawnCount++;
                }
            }

            return(spawnCount);
        }
Exemplo n.º 15
0
    static void HandleRequests(EntityManager entityManager, int queryCount, Query[] queries, Result[] results, ComponentArray <HitCollisionHistory> hitCollisionArray,
                               int environmentMask, int hitCollisionLayer, RaycastHit[] raycastHitBuffer)
    {
//		GameDebug.Log("HandleRequests id:" + startId + " to " + endId + "  index:" + startId%c_bufferSize + " to " + endId%c_bufferSize);

        // First perform collision test against environment.New endpoint (if environment collision is found) is
        // used when testing agains hitcollision. If no hitcollision found damage will be applied to environment
        var rayTestCount = 0;

        for (var i = 0; i < queryCount; i++)
        {
            var query = queries[i];
            if (query.testAgainsEnvironment == 0)
            {
                continue;
            }
            rayTestCount++;
        }

        Profiler.BeginSample("-create ray commands");

        var rayCommands =
            new NativeArray <RaycastCommand>(rayTestCount, Allocator.TempJob);
        var rayResults = new NativeArray <RaycastHit>(rayTestCount, Allocator.TempJob);

        var rayTestIndex = 0;

        for (var i = 0; i < queryCount; i++)
        {
            var query = queries[i];
            if (query.testAgainsEnvironment == 0)
            {
                continue;
            }

            rayCommands[rayTestIndex] =
                new RaycastCommand(query.origin, query.direction, query.distance, environmentMask);

            rayTestIndex++;
        }

        Profiler.EndSample();

        Profiler.BeginSample("-excute ray commands");
        var handle =
            RaycastCommand.ScheduleBatch(rayCommands, rayResults, 10);

        handle.Complete();
        Profiler.EndSample();

        // Test collision with hitCollision
        rayTestIndex = 0;
        for (var i = 0; i < queryCount; i++)
        {
            var query = queries[i];

            // Handle raytest result
            var      environmentHit      = false;
            var      environmentPoint    = Vector3.zero;
            var      environmentNormal   = Vector3.zero;
            Collider environmentCollider = null;
            if (query.testAgainsEnvironment == 1)
            {
                environmentCollider = rayResults[rayTestIndex].collider;
                var impact = environmentCollider != null;
                if (impact)
                {
                    environmentHit    = true;
                    environmentPoint  = rayResults[rayTestIndex].point;
                    environmentNormal = rayResults[rayTestIndex].normal;

                    // query distance is adjusted so followup tests only are done before environment hit point
                    query.distance = rayResults[rayTestIndex].distance;
                }

                if (showDebug.IntValue > 0)
                {
                    Debug.DrawLine(query.origin, query.origin + query.direction * query.distance,
                                   impact ? Color.red : Color.green, debugDuration.FloatValue);
                }

                rayTestIndex++;
            }

            var result = new Result();

            if (query.sphereCastRadius == 0)
            {
                HitCollisionHistory.PrepareColliders(entityManager, ref hitCollisionArray, query.hitCollisionTestTick, query.sphereCastMask,
                                                     query.sphereCastExcludeOwner, Entity.Null, ray(query.origin, query.direction), query.distance);

                // HitCollision test
                var count = Physics.RaycastNonAlloc(query.origin, query.direction, raycastHitBuffer, query.distance,
                                                    1 << hitCollisionLayer);
                if (count > 0)
                {
                    var closestIndex = GetClosestHit(raycastHitBuffer, count, query.origin);
                    result.hit       = 1;
                    result.collider  = raycastHitBuffer[closestIndex].collider;
                    result.hitPoint  = raycastHitBuffer[closestIndex].point;
                    result.hitNormal = raycastHitBuffer[closestIndex].normal;
                }
                else
                {
                    result.hitCollisionOwner = Entity.Null;
                }
            }
            else
            {
                HitCollisionHistory.PrepareColliders(entityManager, ref hitCollisionArray, query.hitCollisionTestTick, query.sphereCastMask,
                                                     query.sphereCastExcludeOwner, Entity.Null, ray(query.origin, query.direction), query.distance,
                                                     query.sphereCastRadius);

                var count = Physics.SphereCastNonAlloc(query.origin, query.sphereCastRadius, query.direction,
                                                       raycastHitBuffer, query.distance, 1 << hitCollisionLayer);

                if (count > 0)
                {
                    var closestIndex = GetClosestHit(raycastHitBuffer, count, query.origin);

                    result.hit       = 1;
                    result.collider  = raycastHitBuffer[closestIndex].collider;
                    result.hitPoint  = raycastHitBuffer[closestIndex].point;
                    result.hitNormal = raycastHitBuffer[closestIndex].normal;
                }
                else
                {
                    result.hitCollisionOwner = Entity.Null;
                }
            }

            // If no hitCollision found we use environment hit results
            if (result.hit == 0 && environmentHit)
            {
                result.hit       = 1;
                result.hitPoint  = environmentPoint;
                result.hitNormal = environmentNormal;
                result.collider  = environmentCollider;
            }

            // Flag result as ready
            results[i] = result;
        }

        rayCommands.Dispose();
        rayResults.Dispose();
    }
Exemplo n.º 16
0
// Public methods
///////////////////////////

    public static ComponentArray <T> As <T>(this ComponentArray ca)
    {
        return((ComponentArray <T>)ca);
    }
Exemplo n.º 17
0
 public ComponentTuple(EntityManager entityManager)
 {
     _entityIndex    = 0;
     group           = entityManager.GetComponentGroup(typeof(T1));
     componentArray1 = group.GetComponent <T1>();
 }
Exemplo n.º 18
0
 public void TestCloneableComponent()
 {
     using var managedArray = new ComponentArray(typeof(ManagedComponent));
     managedArray.Add(default);