Пример #1
0
        void OnTriggerEnter(Collider other)
        {
            if (!enabled || !_canGenerateCollisions || _checkedColliders.Contains(other))
            {
                return;
            }
            _checkedColliders.Add(other);
            var hitEntity = EntityController.GetEntity(UnityToEntityBridge.GetEntityId(other));
            var entity    = EntityController.GetEntity(EntityID);

            if (hitEntity == null || hitEntity.Id == EntityID)
            {
                return;
            }
            if (!CollisionCheckSystem.IsValidCollision(entity, _limitToEnemy, hitEntity, other, out var sourceNode, out var targetNode))
            {
                return;
            }
            var position  = transform.position;
            var hitPnt    = other.ClosestPointOnBounds(position);
            var hitNormal = (hitPnt - position).normalized;

#if DEBUG
            DebugExtension.DrawPoint(hitPnt, Color.yellow, 1.5f, 4f);
#endif
            hitEntity.Post(new CollisionEvent(entity, sourceNode, targetNode, hitPnt, hitNormal));
            entity.Post(new PerformedCollisionEvent(sourceNode, targetNode, hitPnt, hitNormal));
        }
        void OnCollisionEnter(Collision collision)
        {
            if (!enabled || !_canGenerateCollisions || _checkedColliders.Contains(collision.collider))
            {
                return;
            }
            var other = collision.collider;

            _checkedColliders.Add(other);
            var hitEntity = EntityController.GetEntity(UnityToEntityBridge.GetEntityId(other));
            var entity    = EntityController.GetEntity(EntityID);

            if (hitEntity == null || hitEntity.Id == EntityID)
            {
                return;
            }
            if (!CollisionCheckSystem.IsValidCollision(entity, _limitToEnemy, hitEntity, other, out var sourceNode, out var targetNode))
            {
                return;
            }
            var collisionPnt = collision.contacts[0];
            var hitPnt       = collisionPnt.point;
            var hitNormal    = collisionPnt.normal;

#if DEBUG
            DebugExtension.DrawPoint(hitPnt, Color.magenta, 1.5f, 4f);
#endif
            hitEntity.Post(new CollisionEvent(entity, sourceNode, targetNode, hitPnt, hitNormal));
            entity.Post(new PerformedCollisionEvent(sourceNode, targetNode, hitPnt, hitNormal));
        }
Пример #3
0
        public static void TestSerializeComponent(int id, string typeName)
        {
            var dict = EntityController.GetEntity(id).GetAllComponents();

            if (dict == null)
            {
                Debug.LogFormat("{0} has no components", id);
                return;
            }
            System.Type type     = null;
            IComponent  instance = null;

            foreach (var cRef in dict)
            {
                if (cRef.Array.ArrayType.Name == typeName)
                {
                    type     = cRef.Array.ArrayType;
                    instance = cRef.Get() as IComponent;
                }
            }
            if (type == null || instance == null)
            {
                Debug.LogFormat("{0} doesn't have component {1}", id, typeName);
                return;
            }
            TestSerializeComponent(instance);
        }
Пример #4
0
        public void Handle(MoveComplete arg)
        {
            var target = EntityController.GetEntity(arg.Target);

            Owner.PostAdvance(target, arg.MoveTarget, target.GetRotation(), StateEvent);
            target.Destroy();
        }
Пример #5
0
 public Entity GetTarget(Entity source, Vector3 dir, float range, TargetType targeting)
 {
     //if (GameOptions.ArenaCombat) {
     //    return FindArenaActorInRange(source, range, targeting);
     //}
     var sourcePos = source.GetPosition();
     var ray = new Ray(sourcePos, dir);
     var rayLimit = Physics.RaycastNonAlloc(ray, _rayHits, range, UnitCheckMask);
     _rayHits.SortByDistanceAsc(rayLimit);
     for (int i = 0; i < rayLimit; i++) {
         if (_rayHits[i].transform.CompareTag(StringConst.TagEnvironment)) {
             return null;
         }
         var entity = MonoBehaviourToEntity.GetEntity(_rayHits[i].collider);
         if (entity == null) {
             continue;
         }
         if (entity == source) {
             continue;
         }
         var target = EntityController.GetEntity(entity);
         if (targeting == TargetType.Enemy && !World.Get<FactionSystem>().AreEnemies(source, target)) {
             continue;
         }
         if (targeting == TargetType.Friendly && !World.Get<FactionSystem>().AreFriends(source, target)) {
             continue;
         }
         return target;
     }
     return null;
 }
Пример #6
0
        public static void DebugComponent(int id, string typeName)
        {
            var dict = EntityController.GetEntity(id).GetAllComponents();

            if (dict == null)
            {
                Debug.LogFormat("{0} has no components", id);
                return;
            }
            System.Type   type     = null;
            System.Object instance = null;
            foreach (var cRef in dict)
            {
                if (cRef.Array.ArrayType.Name == typeName)
                {
                    type     = cRef.Array.ArrayType;
                    instance = cRef.Get();
                }
            }
            if (type == null)
            {
                Debug.LogFormat("{0} doesn't have component {1}", id, typeName);
                return;
            }
            var           bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
            var           fieldValues  = type.GetFields(bindingFlags).Select(field => field.GetValue(instance)).ToList();
            StringBuilder sb           = new StringBuilder();

            for (int i = 0; i < fieldValues.Count; i++)
            {
                sb.AppendNewLine(fieldValues[i].ToString());
            }
            Debug.LogFormat("{0} {1}: {2}", id, typeName, sb.ToString());
        }
Пример #7
0
        public static void ListEntityContainer(int id, string typeName)
        {
            var dict = EntityController.GetEntity(id).GetAllComponents();

            if (dict == null)
            {
                Debug.LogFormat("{0} has no components", id);
                return;
            }
            System.Type     type     = null;
            EntityContainer instance = null;

            foreach (var cRef in dict)
            {
                if (cRef.Array.ArrayType.Name == typeName)
                {
                    type     = cRef.Array.ArrayType;
                    instance = cRef.Get <EntityContainer>();
                }
            }
            if (type == null || instance == null)
            {
                Debug.LogFormat("{0} doesn't have component {1}", id, typeName);
                return;
            }
            Debug.LogFormat("Container has {0}", instance.Count);
            for (int i = 0; i < instance.Count; i++)
            {
                Debug.LogFormat("{0}: {1}", i, instance[i].Get <LabelComponent>()?.Text ?? instance[i].Id.ToString());
            }
        }
Пример #8
0
 public Entity this[int index] {
     get {
         if (!_list.HasIndex(index))
         {
             return(null);
         }
         return(EntityController.GetEntity(_list[index]));
     }
 }
Пример #9
0
        public static void HealEntity(int entityId, int amount)
        {
            var entity = EntityController.GetEntity(entityId);

            if (entity == null)
            {
                Console.Log("No Entity " + entityId);
                return;
            }
            entity.Post(new HealingEvent(amount, null, null, "Vitals.Health"));
            Console.Log(entity.Get <StatsContainer>().GetVital("Vitals.Health").ToLabelString());
        }
Пример #10
0
 private void CheckRayList(Entity entity, int limit)
 {
     for (int i = 0; i < limit; i++)
     {
         var hitEntity = EntityController.GetEntity(MonoBehaviourToEntity.GetEntityId(_rayHits[i].collider));
         if (hitEntity == null || hitEntity == entity)
         {
             continue;
         }
         entity.Post(new CollisionEvent(entity, hitEntity, _rayHits[i].point, _rayHits[i].normal));
     }
 }
Пример #11
0
 public Entity GetEntity(T component)
 {
     if (component == null)
     {
         return(null);
     }
     if (_componentToEntity.TryGetValue(component, out var ent))
     {
         return(EntityController.GetEntity(ent));
     }
     return(null);
 }
Пример #12
0
 public void Handle(ActionStateEvent arg)
 {
     if (arg.State == ActionStateEvents.Start)
     {
         Triggered = false;
     }
     else if (arg.State == ActionStateEvents.Impact && !Triggered)
     {
         Triggered = true;
         World.Get <RadiusSystem>().HandleRadius(EntityController.GetEntity(arg.Origin), EntityController.GetEntity(arg.Focus), Radius);
     }
 }
Пример #13
0
 public void HandleGlobal(ManagedArray <ActionStateEvent> list)
 {
     for (int i = 0; i < list.Count; i++)
     {
         var arg  = list[i];
         var data = EntityController.GetEntity(arg.Origin).Find <ActionFxData>().Fx;
         if (data != null)
         {
             data.TriggerEvent(arg);
         }
     }
 }
Пример #14
0
        public static void RecoverEntity(int entityId, int amount)
        {
            var entity = EntityController.GetEntity(entityId);

            if (entity == null)
            {
                Console.Log("No Entity " + entityId);
                return;
            }
            World.Get <RulesSystem>().Post(new HealingEvent(amount, null, null, "Vitals.Energy"));
            Console.Log(entity.Get <StatsContainer>().GetVital("Vitals.Energy").ToLabelString());
        }
Пример #15
0
        public static void SaveEntity(int entityID)
        {
            var entity = EntityController.GetEntity(entityID);

            if (entity != null)
            {
                SerializingUtility.SaveJson(new SerializedEntity(entity), string.Format("{0}/{1}.json", Application
                                                                                        .persistentDataPath, entity.DebugId));
                Console.Log($"Saved {entity.DebugId}");
            }

            Console.Log("Didn't provide a valid Entity ID");
        }
Пример #16
0
        private static CollisionEvent?CheckRayList(Entity originEntity, int limit, bool limitEnemy)
        {
            for (int i = 0; i < limit; i++)
            {
                if (originEntity.IsDestroyed())
                {
                    return(null);
                }
                var hit = _rayHits[i];

                Entity hitEntity     = EntityController.GetEntity(UnityToEntityBridge.GetEntityId(hit.collider));
                bool   isEnvironment = hitEntity == null && hit.transform.IsEnvironment();

#if DEBUG_RAYCAST
                Color pointColor = Color.white;
                if (isEnvironment)
                {
                    pointColor = Color.green;
                }
                else if (hit.transform.CompareTag(StringConst.TagInvalidCollider))
                {
                    pointColor = Color.magenta;
                }
                else if (hitEntity != null)
                {
                    pointColor = Color.red;
                }
                DebugExtension.DebugPoint(_rayHits[i].point + (Vector3.up * (i * 0.1f)), pointColor, 0.25f, 2.5f);
#endif
                if (isEnvironment)
                {
#if DEBUG
                    DebugLog.Add(originEntity.DebugId + " hit environment " + _rayHits[i].transform.name);
#endif
                    originEntity.Post(new EnvironmentCollisionEvent(originEntity, _rayHits[i].point, _rayHits[i].normal));
                    return(null);
                }
                if (hitEntity == null)
                {
                    continue;
                }
                if (IsValidCollision(originEntity, limitEnemy, hitEntity, _rayHits[i].collider, out var sourceNode, out var targetNode))
                {
                    var ce = new CollisionEvent(originEntity, sourceNode, targetNode, _rayHits[i].point, _rayHits[i].normal);
                    hitEntity.Post(ce);
                    originEntity.Post(new PerformedCollisionEvent(sourceNode, targetNode, _rayHits[i].point, _rayHits[i].normal));
                    return(ce);
                }
            }
            return(null);
        }
Пример #17
0
        public static void TestAnimationEvent(int entityId, string clip)
        {
            var entity = EntityController.GetEntity(entityId);

            if (entity == null)
            {
                return;
            }
            var handle = new TestReceiver <AnimatorEvent>();

            entity.AddObserver(handle);
            entity.Post(new PlayAnimation(entity, clip, new AnimatorEvent(entity, clip, true, true)));
            handle.OnDel += receiver => entity.RemoveObserver(handle);
        }
Пример #18
0
        public static void ListComponents(int id)
        {
            var dict = EntityController.GetEntity(id).GetAllComponents();

            if (dict == null)
            {
                Debug.LogFormat("{0} has no components", id);
                return;
            }
            StringBuilder sb = new StringBuilder();

            sb.Append(EntityController.GetEntity(id).Name);
            sb.AppendNewLine(" Components");
            foreach (var cRef in dict)
            {
                sb.AppendNewLine(string.Format("Type {0} Index {1}", cRef.Array.ArrayType.Name, cRef.Index));
            }

            Debug.Log(sb.ToString());
        }
Пример #19
0
 private void TurnUpdate()
 {
     if (TurnCancel())
     {
         return;
     }
     _active.Sort(_nodeSorter);
     //_active.BubbleSort((i, i1) => i.Priority < i1.Priority);
     for (int i = 0; i < _active.Count; i++)
     {
         if (TurnCancel())
         {
             return;
         }
         if (_active[i] == null || _active[i].Value == null)
         {
             continue;
         }
         _active[i].Value.TurnUpdate(TurnRecoverAmount);
         //need to check it is the first activation
         if (_active[i].Value.TryStartTurn())
         {
             EntityController.GetEntity(_active[i].Value.Owner).Post(EntitySignals.TurnReady);
             if (GameOptions.TurnBased)
             {
                 _current = _active[i].Value;
                 break;
             }
         }
     }
     _turnLengthCounter += TurnRecoverAmount;
     if (_turnLengthCounter >= TurnBased.RecoveryNeededToEndTurn)
     {
         NewTurn();
     }
     else
     {
         SystemManager.TurnUpdate(false);
         _turnState = TurnBasedState.Performing;
     }
 }
Пример #20
0
        void OnTriggerEnter(Collider other)
        {
            if (!enabled)
            {
                return;
            }
            var hitEntity = EntityController.GetEntity(MonoBehaviourToEntity.GetEntityId(other));
            var entity    = EntityController.GetEntity(EntityID);

            if (hitEntity == null || hitEntity == entity)
            {
                return;
            }
            if (!entity.Tags.Contain(EntityTags.CanUnityCollide) || !hitEntity.Tags.Contain(EntityTags.CanUnityCollide))
            {
                return;
            }
            var hitPnt = other.ClosestPointOnBounds(transform.position);

            entity.Post(new CollisionEvent(entity, hitEntity, hitPnt, (hitPnt - transform.position).normalized));
        }
Пример #21
0
        void OnCollisionEnter(Collision collision)
        {
            if (!enabled)
            {
                return;
            }
            var hitEntity = EntityController.GetEntity(MonoBehaviourToEntity.GetEntityId(collision.collider));
            var entity    = EntityController.GetEntity(EntityID);

            if (hitEntity == null || hitEntity == entity)
            {
                return;
            }
            if (!entity.Tags.Contain(EntityTags.CanUnityCollide) || !hitEntity.Tags.Contain(EntityTags.CanUnityCollide))
            {
                return;
            }
            var collisionPnt = collision.contacts[0];

            entity.Post(new CollisionEvent(entity, hitEntity, collisionPnt.point, collisionPnt.normal));
        }
Пример #22
0
        public void TryThrow(Entity item)
        {
            var        mouseRay = Player.Cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(mouseRay, out hit, _dropDistance, LayerMasks.DropPanel))
            {
                var use = hit.transform.GetComponent <EntityIdentifier>();
                if (use != null)
                {
                    var interaction = EntityController.GetEntity(use.EntityID).Get <ItemInteraction>();
                    if (interaction != null && interaction.Interaction(item))
                    {
                        UIDragDropHandler.Take();
                        return;
                    }
                }
                var screenPnt = Player.Cam.ScreenToViewportPoint(Input.mousePosition);
                if (screenPnt.y < _maxPlaceScreenY)
                {
                    var inventoryItem = item.Get <InventoryItem>();
                    if (inventoryItem != null && inventoryItem.Inventory.Remove(item))
                    {
                        UIDragDropHandler.Take();
                        World.Get <ItemSceneSystem>().Drop(hit.point + new Vector3(0, 0.5f, 0));
                        return;
                    }
                }
                if (hit.distance < _minDistance)
                {
                    UIDragDropHandler.Return();
                    return;
                }
            }
            UIDragDropHandler.Take();
            var pos = Player.Cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20));

            World.Get <ItemSceneSystem>().Throw(pos);
        }
Пример #23
0
 private static void CheckColliderList(Entity originEntity, Entity ignoreEntity, Vector3 position, int limit, bool limitEnemy)
 {
     for (int i = 0; i < limit; i++)
     {
         if (originEntity.IsDestroyed())
         {
             return;
         }
         var collider  = _colliders[i];
         var hitEntity = EntityController.GetEntity(UnityToEntityBridge.GetEntityId(collider));
         if (hitEntity == ignoreEntity || hitEntity == null)
         {
             continue;
         }
         if (IsValidCollision(originEntity, limitEnemy, hitEntity, collider, out var sourceNode, out var targetNode))
         {
             CollisionExtensions.GenerateHitLocDir(position, hitEntity, collider, out var hitPnt, out var hitNormal);
             var ce = new CollisionEvent(originEntity, sourceNode, targetNode, hitPnt, hitNormal);
             hitEntity.Post(ce);
             originEntity.Post(new PerformedCollisionEvent(sourceNode, targetNode, hitPnt, hitNormal));
         }
     }
 }
Пример #24
0
 public static Entity GetEntity(Collider collider)
 {
     return(_colliderToDictionary.TryGetValue(collider, out var id) ? EntityController.GetEntity(id) : null);
 }
Пример #25
0
        private static CollisionEvent?CheckRayList(Entity originEntity, Ray ray, int limit, bool limitEnemy)
        {
            for (int i = 0; i < limit; i++)
            {
                if (originEntity.IsDestroyed())
                {
                    return(null);
                }
                var hit = _rayHits[i];

                Entity hitEntity     = EntityController.GetEntity(UnityToEntityBridge.GetEntityId(hit.collider));
                bool   isEnvironment = hitEntity == null && hit.transform.IsEnvironment();

#if DEBUG_RAYCAST
                Color pointColor = Color.white;
                if (isEnvironment)
                {
                    pointColor = Color.green;
                }
                else if (hit.transform.CompareTag(StringConst.TagInvalidCollider))
                {
                    pointColor = Color.magenta;
                }
                else if (hitEntity != null)
                {
                    pointColor = Color.red;
                }
                DebugExtension.DrawPoint(_rayHits[i].point + (Vector3.up * (i * 0.1f)), pointColor, 0.25f, 2.5f);
#endif
                if (isEnvironment)
                {
#if DEBUG
                    DebugLog.Add(originEntity.DebugId + " hit environment " + _rayHits[i].transform.name);
#endif
                    originEntity.Post(new EnvironmentCollisionEvent(originEntity, _rayHits[i].point, _rayHits[i].normal));
                    return(null);
                }
                if (hitEntity == null)
                {
                    continue;
                }
                if (IsValidCollision(originEntity, limitEnemy, hitEntity, _rayHits[i].collider, out var sourceNode, out var targetNode))
                {
                    if (targetNode.DetailCollider != null)
                    {
                        var detailTr   = targetNode.DetailCollider.Collider.transform;
                        var localPoint = hit.transform.InverseTransformPoint(ray.origin);
                        var localDir   = hit.transform.InverseTransformDirection(ray.direction);
                        var rayCast    = new Ray(detailTr.TransformPoint(localPoint), detailTr.TransformDirection(localDir));
                        if (!targetNode.DetailCollider.Collider.Raycast(rayCast, out var childHit, 500))
                        {
                            DebugExtension.DrawPoint(childHit.point, Color.yellow, 0.25f, 2.5f);
                            continue;
                        }
                    }
                    var ce = new CollisionEvent(originEntity, sourceNode, targetNode, _rayHits[i].point, _rayHits[i].normal);
                    hitEntity.Post(ce);
                    originEntity.Post(new PerformedCollisionEvent(sourceNode, targetNode, _rayHits[i].point, _rayHits[i].normal));
                    return(ce);
                }
            }
            return(null);
        }
Пример #26
0
 public static void DebugStatus(int entity)
 {
     DebugStatus(EntityController.GetEntity(entity));
 }
Пример #27
0
 public static void PostSignal(int entity, int message)
 {
     EntityController.GetEntity(entity)?.Post(message);
 }