public bool TryAddCommand(Command cmd)
        {
            if (!cmd.CanStart())
            {
                return(false);
            }
            var otherCmd = GetCommand(cmd.Owner.Entity);

            if (otherCmd != null)
            {
                if (!otherCmd.CanBeReplacedBy(cmd))
                {
                    cmd.Owner.Post(new StatusUpdate(cmd.Owner, "Can't replace current command"));
                    return(false);
                }
                otherCmd.Cancel();
                _commands.Remove(otherCmd);
            }
            cmd.StartCommand();
#if DEBUG
            DebugLog.Add(cmd.Owner.Entity.DebugId + " started command " + cmd.GetType());
#endif
            _commands.Add(cmd);
            return(true);
        }
示例#2
0
        //private void UpdateSenses(SensorSimpleComponent simple) {
        //    simple.TempList.Clear();
        //    World.Get<FactionSystem>().FillFactionEnemiesList(simple.TempList, simple.Faction);
        //    var owner = simple.GetEntity();
        //    var gridPos = owner.Get<GridPosition>().Position;
        //    for (int i = 0; i < simple.TempList.Count; i++) {
        //        var target = simple.TempList[i];
        //        if (gridPos.Distance(target.Get<GridPosition>().Position) > simple.MaxHearDistance) {
        //            continue;
        //        }
        //        if (World.Get<LineOfSightSystem>().CanSeeOrHear(owner, target, out var hearingBlocked)) {
        //            simple.AddWatch(target, true);
        //        }
        //        else if (!hearingBlocked) {
        //            simple.AddWatch(target, false);
        //        }
        //    }
        //    simple.UpdateWatchTargets();
        //}

        public void Handle(TakeDamageEvent arg)
        {
            var sensorTargets = arg.Target.Entity.Find <SensorTargetsComponent>();

#if DEBUG
            DebugLog.Add(
                sensorTargets.GetEntity().DebugId + " was attacked by " + arg.Origin?.Entity.DebugId + " parent " +
                arg.Origin?.Entity.ParentId + " is pooled " + arg.Origin?.Entity.Pooled);
#endif
            sensorTargets.AddWatch(arg.Origin, true);
        }
示例#3
0
        public void Destroy()
        {
            if (IsDestroyed() || _toDeleteList.Contains(this))
            {
                return;
            }
#if DEBUG
            DebugLog.Add("Added " + DebugId + " to delete list");
#endif
            _toDeleteList.Add(this);
        }
示例#4
0
        public void Post <T>(int entity, T msg) where T : struct, IEntityMessage
        {
#if DEBUGMSGS
            DebugLog.Add(DebugId + " posted " + msg.GetType().Name);
#endif
            World.Enqueue(msg);
            if (!_entityEvents.TryGetValue(entity, out var hub))
            {
                return;
            }
            hub.Post(msg);
        }
示例#5
0
        public static bool IsValidCollision(Entity entity, bool limitEnemy, Entity hitEntity, Collider collider, out CollidableTemplate
                                            sourceTemplate, out CollidableTemplate targetTemplate)
        {
            sourceTemplate = targetTemplate = null;
            if (hitEntity == entity)
            {
                return(false);
            }
            var tr = collider.transform;

            if (tr.CompareTag(StringConst.TagInvalidCollider) ||
                tr.CompareTag(StringConst.TagSensor))
            {
                return(false);
            }
            if (!hitEntity.Tags.Contain(EntityTags.CanUnityCollide))
            {
                return(false);
            }
            if (AreEntitiesConnected(hitEntity, entity))
            {
                return(false);
            }
#if DEBUG
            DebugLog.Add(entity.DebugId + " hit actor " + tr.name);
#endif
            sourceTemplate = entity.FindTemplate <CollidableTemplate>();
            targetTemplate = hitEntity.FindTemplate <CollidableTemplate>();
            if (sourceTemplate == null || targetTemplate == null || sourceTemplate == targetTemplate)
            {
                return(false);
            }
            if (limitEnemy)
            {
                if (World.Get <FactionSystem>().AreEnemies(sourceTemplate.Entity, targetTemplate.Entity))
                {
                    if (entity.Tags.IsConfused)
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!entity.Tags.IsConfused)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
        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);
        }
示例#7
0
        public void PostSignal(int entity, int messageType)
        {
#if DEBUGMSGS
            if (msg > 0)
            {
                DebugLog.Add(DebugId + " posted " + msg + " " + EntitySignals.GetNameAt(msg));
            }
#endif
            if (!_entityEvents.TryGetValue(entity, out var hub))
            {
                return;
            }
            hub.PostSignal(messageType);
        }
示例#8
0
        public void Handle(StatusUpdate arg)
        {
#if DEBUG
            DebugLog.Add(arg.Target + " received status " + arg.Update);
#endif
            var statusComponent = arg.Target.Get <StatusUpdateComponent>();
            if (statusComponent != null)
            {
                statusComponent.Status = arg.Update;
            }
            var floatingText = arg.Target.Find <FloatingTextStatusComponent>();
            if (floatingText != null)
            {
                FloatingText.Message(arg.Update, floatingText.Tr.position + floatingText.Offset, arg.Color);
            }
        }
示例#9
0
        public void PostAll <T>(Entity entity, T msg) where T : struct, IEntityMessage
        {
#if DEBUGMSGS
            DebugLog.Add(DebugId + " posted " + msg.GetType().Name);
#endif
            World.Enqueue(msg);
            var targetEntity = entity;
            while (targetEntity != null)
            {
                if (_entityEvents.TryGetValue(targetEntity, out var hub))
                {
                    hub.Post(msg);
                }
                targetEntity = targetEntity.GetParent();
            }
        }
示例#10
0
        public static void SpawnProjectile(Entity owner, string id, Vector3 target, Vector3 spawnPos, Quaternion spawnRot, ActionFx
                                           actionFx = null)
        {
            if (_configs.Count == 0)
            {
                Init();
            }
            if (!_configs.TryGetValue(id, out var config))
            {
#if DEBUG
                DebugLog.Add("Couldn't find project config " + id, false);
#endif
                return;
            }
            SpawnProjectile(owner, config, target, spawnPos, spawnRot, actionFx);
        }
示例#11
0
        public bool Chase(CharacterTemplate target)
        {
            //var gridPosition = target.Get<GridPosition>().Position;
            //if (gridPosition.IsNeighbor(Position.c.Position)) {
            //    return false;
            //}
            if (!World.Get <FactionSystem>().AreEnemies(Entity, target))
            {
                return(false);
            }
            Chasing = true;
            Entity.Post(new SetMoveTarget(Entity, target.Tr, null));
            Entity.Post(new SetLookTarget(Entity, target, false));
#if DEBUG
            DebugLog.Add(Entity.DebugId + " chasing " + target.Entity.DebugId);
#endif
            return(true);
        }
示例#12
0
        public static void ProcessPendingDeletes()
        {
            for (int i = 0; i < _toDeleteList.Count; i++)
            {
                if (_toDeleteList[i].Factory != null && _toDeleteList[i].Factory.TryStore(_toDeleteList[i]))
                {
#if DEBUG
                    DebugLog.Add("Pooled " + _toDeleteList[i].Name + " to " + _toDeleteList[i].Factory.GetType().Name);
#endif
                    continue;
                }
                _toDeleteList[i].Post(new EntityDestroyed(_toDeleteList[i]));
#if DEBUG
                DebugLog.Add("Deleted " + _toDeleteList[i].Name);
#endif
                _pool.Store(_toDeleteList[i]);
            }
            _toDeleteList.Clear();
        }
示例#13
0
        private void UpdateNode(ref DirectionalSpriteNode node)
        {
            node.Animator.Billboard.Apply(node.Renderer.SpriteTr, node.Animator.Backwards, ref node.Animator.LastAngleHeight);
            if (node.Animator.Requests.Count > 0)
            {
                var request = node.Animator.Requests[0];
                if (node.Animator.Requests.Count > 1)
                {
                    for (int r = 1; r < node.Animator.Requests.Count; r++)
                    {
                        if (!request.OverrideClip && node.Animator.Requests[r].OverrideClip)
                        {
                            request = node.Animator.Requests[r];
                        }
                    }
                }
#if DEBUG
                DebugLog.Add("Playing " + request.Clip + " for " + node.Entity.DebugId);
#endif
                node.PlayAnimation(request.Clip, request.OverrideClip);
                node.Animator.Requests.Clear();
            }

            if (node.Animator.CurrentClipHolder == null || node.Animator.IsSimpleClip)
            {
                node.CheckMoving();
            }

            var orientation = SpriteFacingControl.GetCameraSide(node.Animator.Facing, node.Renderer.SpriteTr,
                                                                node.Renderer.BaseTr, 5, out var inMargin);
            if (node.Animator.Orientation == orientation || (inMargin && (orientation.IsAdjacent(node.Animator.Orientation))))
            {
                if (node.CheckFrameUpdate())
                {
                    node.UpdateSpriteFrame();
                }
                return;
            }
            node.Animator.Orientation = orientation;
            node.CheckFrameUpdate();
            node.UpdateSpriteFrame();
        }
示例#14
0
        public Entity SpawnProjectile(Entity owner, string id, Vector3 target, Vector3 spawnPos, Quaternion spawnRot)
        {
            if (_templates.Count == 0)
            {
                Init();
            }
            if (!_templates.TryGetValue(id, out var template))
            {
#if DEBUG
                DebugLog.Add("Couldn't find project template " + id, false);
#endif
                return(null);
            }
            var entity = GetProjectile(template);
            var node   = entity.GetNode <ProjectileNode>();
            node.MoveTarget.SetMoveTarget(target);
            if (template.ActionFx != null)
            {
                node.ActionFx.ChangeFx(template.ActionFx);
            }
            var prefab = ItemPool.Spawn(UnityDirs.ActionSpawn, template.Type, spawnPos, spawnRot);
            if (prefab == null)
            {
                return(entity);
            }
            var spawn = prefab.GetComponent <IProjectile>();
            entity.Add(new TransformComponent(prefab.Transform));
            switch (template.Type)
            {
            default:
            case "Simple":
                break;

            case "SpriteAnimation":
                spawn.SetColor(template.MainColor, Color.white * template.GlowPower);
                if (template.Animation != null)
                {
                    var spriteRenderer = prefab.Renderers[0] as SpriteRenderer;
                    entity.Add(new SpriteAnimationComponent(spriteRenderer, template.Animation, false, template.Billboard));
                }
                break;

            case "VolumeLaser":
                spawn.SetColor(template.MainColor, template.OffsetColor);
                break;
            }
            spawn.SetSize(template.Size, template.Length);
            switch (template.Movement)
            {
            case "Forward":
            case "Arc":
                prefab.Transform.LookAt(target, prefab.Transform.up);
                break;

            case "Force":
                //var force = transform.forward * ForceRange.Lerp(Mathf.Clamp01(charging.ElapsedTime / MaxChargeTime));
                break;
            }
            if (spawn.Rigidbody != null)
            {
                node.Rb.SetRb(spawn.Rigidbody);
            }
            entity.Tags.Add(EntityTags.Moving);
            node.Rendering.Set(spawn);
            UnityToEntityBridge.RegisterToEntity(prefab.Transform.gameObject, entity);
            entity.ParentId = owner.Id;
            entity.Post(new ProjectileSpawned(template, entity));
            return(entity);
        }
示例#15
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);
        }