예제 #1
0
        public void Update()
        {
            if (Parent.Scene.ServiceProvider.GetService <ITimeService>().TimeScale == 0)
            {
                return;
            }

            bool heal        = true;
            var  timeService = Parent.Scene.ServiceProvider.GetService <ITimeService>();

            if (lastHeal + HealSpeed > timeService.AbsoluteTime)
            {
                return;
            }
            lastHeal = timeService.AbsoluteTime;

            var target = FindNearestEntity();

            if (target == null)
            {
                return;
            }

            OwnerComponent ownerComponent = Parent.GetComponent <OwnerComponent>();

            foreach (var other in Parent.Scene.ServiceProvider.GetService <IGameObjectService>().GetEntities())
            {
                if (!other.HasComponent <OwnerComponent>())
                {
                    continue;
                }

                OwnerComponent otherOwnerComponent = other.GetComponent <OwnerComponent>();

                if (ownerComponent.Owner == otherOwnerComponent.Owner)
                {
                    continue;
                }
                if (!HasInRange(other))
                {
                    continue;
                }

                heal = false;
            }

            if (heal)
            {
                target.GetComponent <HealthComponent>().Heal(Heal);
            }
        }
예제 #2
0
        private IEntity FindNearestEntity()
        {
            var entitiesInRange = new Dictionary <float, IEntity>();

            OwnerComponent ownerComponent = Parent.GetComponent <OwnerComponent>();

            foreach (var other in Parent.Scene.ServiceProvider.GetService <IGameObjectService>().GetEntities())
            {
                if (!other.HasComponent <OwnerComponent>())
                {
                    continue;
                }
                if (other == Parent)
                {
                    continue;
                }
                OwnerComponent otherOwnerComponent = other.GetComponent <OwnerComponent>();
                if (ownerComponent.Owner != otherOwnerComponent.Owner)
                {
                    continue;
                }
                if (!other.HasComponent <HealthComponent>() || !other.HasComponent <HealComponent>())
                {
                    continue;
                }
                HealthComponent otherHealth = other.GetComponent <HealthComponent>();
                if (otherHealth.Dead)
                {
                    continue;
                }
                if (otherHealth.Health >= otherHealth.MaxHealth)
                {
                    continue;
                }
                if (!HasInRange(other))
                {
                    continue;
                }
                var range = GetRange(other);
                if (!entitiesInRange.ContainsKey(range))
                {
                    entitiesInRange.Add(range, other);
                }
            }

            var sortedEntities = entitiesInRange.OrderBy(obj => obj.Key).ToDictionary(obj => obj.Key, obj => obj.Value);

            return(sortedEntities.Count == 0 ? null : sortedEntities.First().Value);
        }
        private bool FindNewTarget()
        {
            OwnerComponent ownerComponent = Parent.GetComponent <OwnerComponent>();
            var            targets        = new List <IEntity>();

            foreach (var other in Parent.Scene.ServiceProvider.GetService <IGameObjectService>().GetEntities())
            {
                if (!other.HasComponent <OwnerComponent>())
                {
                    continue;
                }
                OwnerComponent otherOwnerComponent = other.GetComponent <OwnerComponent>();
                if (ownerComponent.Owner == otherOwnerComponent.Owner)
                {
                    continue;
                }
                if (!other.HasComponent <HealthComponent>())
                {
                    continue;
                }
                if (!HasInRange(other))
                {
                    continue;
                }

                targets.Add(other);
            }

            if (targets.Count == 0)
            {
                return(false);
            }

            targets.Sort((obj1, obj2) => (int)((Parent.Position - obj1.Position).Length - (Parent.Position - obj2.Position).Length));
            Target     = targets[0];
            NextTarget = null;
            return(true);
        }