public bool SetTarget(ShipController other, bool forceSet = false)
    {
        if (other == null)
        {
            target = null;
            OnTargetChanged?.Invoke(target);
            return(false);
        }

        bool targetInRange = IsShipInRange(other);

        if (targetInRange || forceSet)
        {
            target = other;
            OnTargetChanged?.Invoke(target);

            if (forceSet)
            {
                lockTarget = true;
            }

            Debug.Log($"Targeter: New target acquired {other.gameObject.name}");
        }

        return(targetInRange);
    }
 private void Update()
 {
     if (target != null && (IsShipInRange(target) == false && lockTarget == false))
     {
         target = null;
         OnTargetChanged?.Invoke(target);
     }
 }
Example #3
0
        public void SetAttackTarget(uint targetId)
        {
            if (targetId == CreatureId || AutoAttackTargetId == targetId)
            {
                // if we want to attack ourselves or if the current target is already the one we want... no change needed.
                return;
            }

            // save the previus target to report
            var oldTargetId = AutoAttackTargetId;

            if (targetId == 0)
            {
                // clearing our target.
                if (AutoAttackTargetId != 0)
                {
                    var attackTarget = Game.Instance.GetCreatureWithId(AutoAttackTargetId);

                    if (attackTarget != null)
                    {
                        attackTarget.OnThingChanged -= CheckAutoAttack;
                    }

                    AutoAttackTargetId = 0;
                }
            }
            else
            {
                // TODO: verify against Hostiles.Union(Friendly).
                // if (creature != null)
                // {
                AutoAttackTargetId = targetId;

                var attackTarget = Game.Instance.GetCreatureWithId(AutoAttackTargetId);

                if (attackTarget != null)
                {
                    attackTarget.OnThingChanged += CheckAutoAttack;
                }

                // }
                // else
                // {
                //    Console.WriteLine("Taget creature not found in attacker\'s view.");
                // }
            }

            // report the change to our subscribers.
            OnTargetChanged?.Invoke(oldTargetId, targetId);
            CheckAutoAttack(this, new ThingStateChangedEventArgs()
            {
                PropertyChanged = nameof(location)
            });
        }
Example #4
0
        public override void OnUpdate()
        {
            // Begin search for a target if none is currently assigned
            if (currentTarget == null)
            {
                // Filter targets down to those that within range
                var targetsInRange = potentialTargets
                                     .Where(x => x.Exists)
                                     .Where(x => GameObject.Position.GetDistance(x.Position) <= range)
                                     .Where(x => Vec3Ex.VisibleOnScreen(x.Position))
                                     .ToList();

                if (targetsInRange.Count != 0)
                {
                    // Find the target is closest
                    currentTarget = targetsInRange.OrderBy(x => GameObject.Position.GetDistance(x.Position)).First();

                    OnTargetChanged?.Invoke(currentTarget);
                }
                else
                {
                    if (!wereNoTargetsFound)
                    {
                        OnNoTargetsFound?.Invoke();
                        wereNoTargetsFound = true;
                    }
                }
            }
            else
            {
                // Make sure the currentTarget entity exists before doing anything.
                // Otherwise, start searching for a new target.
                if (currentTarget.Exists)
                {
                    var distance = GameObject.Position.GetDistance(currentTarget.Position);
                    if (distance > range)
                    {
                        Global.gEnv.pLog.LogToConsole("Target fell out of range");
                        currentTarget = null;
                    }
                }
                else
                {
                    currentTarget = null;
                }

                wereNoTargetsFound = false;
            }
        }
Example #5
0
    public void UpdateTarget(ShipEntity target)
    {
        //cant target teammates
        if (target.teamID == _teamID)
        {
            return;
        }

        _target = target;

        if (_teamID == 0)
        {
            LogManager.getInstance.AddEntry("[" + this.name + "]: " + "targeting " + target.name + ".");
        }

        OnTargetChanged?.Invoke(target);
    }
Example #6
0
 public void TargetChange(string targetName, int index)
 {
     Target      = targetName;
     TargetIndex = index;
     OnTargetChanged?.Invoke();
 }