Exemplo n.º 1
0
    public static void SphereOverlapAll(EntityManager entityManager,
                                        ref NativeList <Entity> hitColliHistoryEntityArray, int tick, int mask,
                                        Entity forceExcluded, Entity forceIncluded, sphere sphere,
                                        List <HitCollisionData.CollisionResult> results, List <Entity> hitCollisionOwners)
    {
        for (var i = 0; i < hitColliHistoryEntityArray.Length; i++)
        {
            var entity = hitColliHistoryEntityArray[i];

            if (!HitCollisionData.IsRelevant(entityManager, hitColliHistoryEntityArray[i], mask, forceExcluded, forceIncluded))
            {
                continue;
            }

            var collectionResult = new HitCollisionData.CollisionResult();

            var hit = HitCollisionData.SphereOverlapSingle(entityManager, entity, tick, sphere, ref collectionResult);

            if (hit)
            {
                var hitCollisionData = entityManager.GetComponentData <HitCollisionData>(hitColliHistoryEntityArray[i]);

                results.Add(collectionResult);
                hitCollisionOwners.Add(hitCollisionData.hitCollisionOwner);
            }
        }
    }
Exemplo n.º 2
0
    protected override void Update(Entity entity, HitCollisionHistory hitColliderHist)
    {
        var sampleTick = m_world.worldTime.tick;

        HitCollisionData.StoreBones(EntityManager, entity, hitColliderHist.collisiderParents, sampleTick);

        if (HitCollisionModule.ShowDebug.IntValue == 1)
        {
            var primColor   = Color.magenta;
            var boundsColor = Color.green;
            for (int i = 0; i < 20; i++)
            {
                HitCollisionData.DebugDrawTick(EntityManager, entity, sampleTick - i, primColor, boundsColor);
                primColor.a   -= 0.01f;
                boundsColor.a -= 0.01f;
            }
        }
    }
Exemplo n.º 3
0
    protected override void Initialize(ref EntityQuery group)
    {
        // We are not allowed to spawn prefabs while iterating ComponentGroup so we copy list of entities and characters.
        entityBuffer.Clear();
        characterBuffer.Clear();
        {
            var entityArray    = group.GetEntityArraySt();
            var characterArray = group.ToComponentArray <Character>();
            for (var i = 0; i < entityArray.Length; i++)
            {
                entityBuffer.Add(entityArray[i]);
                characterBuffer.Add(characterArray[i]);
            }
        }

        for (var i = 0; i < entityBuffer.Count; i++)
        {
            var charEntity = entityBuffer[i];
            var character  = characterBuffer[i];

            var characterRepAll = EntityManager.GetComponentData <CharacterReplicatedData>(charEntity);

            var heroTypeRegistry = m_resourceManager.GetResourceRegistry <HeroTypeRegistry>();
            var heroTypeAsset    = heroTypeRegistry.entries[characterRepAll.heroTypeIndex];
            character.heroTypeData = heroTypeAsset;

            var characterTypeAsset = heroTypeAsset.character;

            // Create main presentation
            var charPrefabGUID         = server ? characterTypeAsset.prefabServer : characterTypeAsset.prefabClient;
            var charPrefab             = m_resourceManager.GetSingleAssetResource(charPrefabGUID) as GameObject;
            var presentationGOE        = m_world.Spawn <GameObjectEntity>(charPrefab);
            var charPresentationEntity = presentationGOE.Entity;

            character.presentation = charPresentationEntity;

            var charPresentation = EntityManager.GetComponentObject <CharacterPresentationSetup>(charPresentationEntity);
            charPresentation.character = charEntity;
            character.presentations.Add(charPresentation);

            // Setup health
            var healthState = EntityManager.GetComponentData <HealthStateData>(charEntity);
            healthState.SetMaxHealth(heroTypeAsset.health);
            EntityManager.SetComponentData(charEntity, healthState);

            // Setup CharacterMoveQuery
            var moveQuery = EntityManager.GetComponentObject <CharacterMoveQuery>(charEntity);
            moveQuery.Initialize(heroTypeAsset.characterMovementSettings, charEntity);

            // Setup HitCollisionHistory
            if (EntityManager.HasComponent <HitCollisionData>(charPresentationEntity))
            {
                var hitCollisionData = EntityManager.GetComponentData <HitCollisionData>(charPresentationEntity);
                hitCollisionData.hitCollisionOwner = charEntity;
                EntityManager.SetComponentData(charPresentationEntity, hitCollisionData);
            }
            else
            {
                var hitCollisionData = new HitCollisionData
                {
                    hitCollisionOwner = charEntity,
                };
                EntityManager.AddComponentData(charPresentationEntity, hitCollisionData);
            }


            character.eyeHeight = heroTypeAsset.eyeHeight;


            // Setup abilities
            GameDebug.Assert(EntityManager.Exists(characterRepAll.abilityCollection), "behavior controller entity does not exist");
            var buffer = EntityManager.GetBuffer <EntityGroupChildren>(characterRepAll.abilityCollection);
            for (int j = 0; j < buffer.Length; j++)
            {
                var childEntity = buffer[j].entity;
                if (EntityManager.HasComponent <CharBehaviour>(childEntity))
                {
                    var charBehaviour = EntityManager.GetComponentData <CharBehaviour>(childEntity);
                    charBehaviour.character = charEntity;
                    EntityManager.SetComponentData(childEntity, charBehaviour);
                }
            }


            // Create items
            foreach (var itemEntry in heroTypeAsset.items)
            {
                var itemPrefabGuid = server ? itemEntry.itemType.prefabServer : itemEntry.itemType.prefabClient;

                if (!itemPrefabGuid.IsSet())
                {
                    continue;
                }

                var itemPrefab = m_resourceManager.GetSingleAssetResource(itemPrefabGuid) as GameObject;
                var itemGOE    = m_world.Spawn <GameObjectEntity>(itemPrefab);

                var itemCharPresentation = EntityManager.GetComponentObject <CharacterPresentationSetup>(itemGOE.Entity);
                itemCharPresentation.character            = charEntity;
                itemCharPresentation.attachToPresentation = charPresentationEntity;
                character.presentations.Add(itemCharPresentation);
            }
        }
    }
Exemplo n.º 4
0
    public static void Setup(EntityManager entityManager, Entity entity, List <Transform> parents,
                             float boundsRadius, float boundsHeightOffset, List <CapsuleCollider> capsuleColliders,
                             List <Transform> capsuleColliderParents, List <SphereCollider> sphereColliders,
                             List <Transform> sphereColliderParents, List <BoxCollider> boxColliders, List <Transform> boxColliderParents)
    {
        var coll = new HitCollisionData();

        if (entityManager.HasComponent <HitCollisionData>(entity))
        {
            coll = entityManager.GetComponentData <HitCollisionData>(entity);
        }
        coll.lastTick           = -1;
        coll.lastIndex          = -1;
        coll.boundsRadius       = boundsRadius;
        coll.boundsHeightOffset = boundsHeightOffset;
        if (entityManager.HasComponent <HitCollisionData>(entity))
        {
            entityManager.SetComponentData(entity, coll);
        }
        else
        {
            entityManager.AddComponentData(entity, coll);
        }

        // Setup history
        entityManager.AddBuffer <TransformHistory>(entity);
        var historyBuffer = entityManager.GetBuffer <TransformHistory>(entity);

        for (int i = 0; i < k_historyCount * k_maxColliderCount; i++)
        {
            historyBuffer.Add(new TransformHistory());
        }

        entityManager.AddBuffer <BoundsHistory>(entity);
        var boundsBuffer = entityManager.GetBuffer <BoundsHistory>(entity);

        for (int i = 0; i < k_historyCount; i++)
        {
            boundsBuffer.Add(new BoundsHistory());
        }


        // Primitives
        entityManager.AddBuffer <Capsule>(entity);
        var capsuleBuffer = entityManager.GetBuffer <Capsule>(entity);

        for (var i = 0; i < capsuleColliders.Count; i++)
        {
            var collider = capsuleColliders[i];
            var localPos = collider.center;
            var axis     = collider.direction == 0 ? Vector3.right :
                           collider.direction == 1 ? Vector3.up : Vector3.forward;

            var offset = 0.5f * axis * (collider.height - 2 * collider.radius);
            var prim   = new capsule()
            {
                p1     = localPos - offset,
                p2     = localPos + offset,
                radius = collider.radius,
            };

            var capsule = new Capsule();
            capsule.prim = primlib.transform(prim, collider.transform.localPosition,
                                             collider.transform.localRotation);

            var parent = capsuleColliderParents[i];
            capsule.transformIndex = parents.IndexOf(parent);
            capsule.info           = new HitCollInfo
            {
                type = HitCollType.Body
            };
            capsuleBuffer.Add(capsule);
        }

        entityManager.AddBuffer <Box>(entity);
        var boxBuffer = entityManager.GetBuffer <Box>(entity);

        for (var i = 0; i < boxColliders.Count; i++)
        {
            var collider = boxColliders[i];
            var prim     = new box
            {
                center   = collider.center,
                size     = collider.size,
                rotation = Quaternion.identity
            };

            var box = new Box();
            box.prim = primlib.transform(prim, collider.transform.localPosition,
                                         collider.transform.localRotation);

            var parent = boxColliderParents[i];
            box.transformIndex = parents.IndexOf(parent);
            box.info           = new HitCollInfo
            {
                type = HitCollType.Body
            };
            boxBuffer.Add(box);
        }

        entityManager.AddBuffer <Sphere>(entity);
        var sphereBuffer = entityManager.GetBuffer <Sphere>(entity);

        for (var i = 0; i < sphereColliders.Count; i++)
        {
            var collider = sphereColliders[i];
            var prim     = new sphere
            {
                center = collider.center,
                radius = collider.radius,
            };

            var sphere = new Sphere();
            sphere.prim = primlib.transform(prim, collider.transform.localPosition,
                                            collider.transform.localRotation);

            var parent = sphereColliderParents[i];
            sphere.transformIndex = parents.IndexOf(parent);
            sphere.info           = new HitCollInfo
            {
                type = HitCollType.Body
            };
            sphereBuffer.Add(sphere);
        }
    }
Exemplo n.º 5
0
    protected override void Initialize(ref EntityQuery group)
    {
        // We copy to list of incoming hitcollisions as it is not allowed to add entities while iterating componentarray
        var hitCollisionArray       = group.ToComponentArray <HitCollisionHistory>();
        var hitCollisionEntityArray = group.GetEntityArraySt();

        for (var iHitColl = 0; iHitColl < hitCollisionArray.Length; iHitColl++)
        {
            var hitCollision       = hitCollisionArray[iHitColl];
            var hitCollisionEntity = hitCollisionEntityArray[iHitColl];

            var externalSetup = hitCollision.settings.collisionSetup != null;
            var colliderSetup = externalSetup ? hitCollision.settings.collisionSetup.transform : hitCollision.transform;

            // TODO (mogensh) cache and reuse collision setup from each prefab - or find better serialization format

            // Find and disable all all colliders on collisionOwner
            var sourceColliders = new List <Collider>();
            RecursiveGetCollidersInChildren(colliderSetup.transform, sourceColliders);
            foreach (var collider in sourceColliders)
            {
                collider.enabled = false;
            }

            // Create collider collection
            if (m_systemRoot != null)
            {
                hitCollision.transform.SetParent(m_systemRoot.transform, false);
            }

            var uniqueParents          = new List <Transform>(16);
            var colliderParents        = new List <Transform>(16);
            var capsuleColliders       = new List <CapsuleCollider>(16);
            var capsuleColliderParents = new List <Transform>(16);
            var sphereColliders        = new List <SphereCollider>(16);
            var sphereColliderParents  = new List <Transform>(16);
            var boxColliders           = new List <BoxCollider>(16);
            var boxColliderParents     = new List <Transform>(16);

            for (var i = 0; i < sourceColliders.Count; i++)
            {
                var sourceCollider     = sourceColliders[i];
                var colliderParentBone = sourceCollider.transform.parent;
                if (externalSetup)
                {
                    var skeleton       = EntityManager.GetComponentObject <Skeleton>(hitCollisionEntity);
                    var ownerBoneIndex = skeleton.GetBoneIndex(colliderParentBone.name.GetHashCode());
                    colliderParentBone = skeleton.bones[ownerBoneIndex];
                }

                colliderParents.Add(colliderParentBone);

                if (!uniqueParents.Contains(colliderParentBone))
                {
                    uniqueParents.Add(colliderParentBone);
                }

                var capsuleCollider = sourceCollider as CapsuleCollider;
                if (capsuleCollider != null)
                {
                    capsuleColliderParents.Add(colliderParentBone);
                    capsuleColliders.Add(capsuleCollider);
                }
                else
                {
                    var boxCollider = sourceCollider as BoxCollider;
                    if (boxCollider != null)
                    {
                        boxColliders.Add(boxCollider);
                        boxColliderParents.Add(colliderParentBone);
                    }
                    else
                    {
                        var sphereCollider = sourceCollider as SphereCollider;
                        if (sphereCollider != null)
                        {
                            sphereColliders.Add(sphereCollider);
                            sphereColliderParents.Add(colliderParentBone);
                        }
                    }
                }
            }

            hitCollision.collisiderParents =
                new TransformAccessArray(uniqueParents.ToArray());
            HitCollisionData.Setup(EntityManager, hitCollisionEntity, uniqueParents,
                                   hitCollision.settings.boundsRadius, hitCollision.settings.boundsHeight, capsuleColliders,
                                   capsuleColliderParents, sphereColliders, sphereColliderParents, boxColliders, boxColliderParents);
        }
    }