Esempio n. 1
0
        /// <summary>
        /// Apply effects to the target changing its parameters.
        /// </summary>
        /// <param name="targetAgent"></param>
        private void INTERNAL_ApplyEffect(Agent targetAgent)
        {
            if (!targetAgent || !_agent)
            {
                return;
            }

            OnApplyEffect(targetAgent.gameObject);
            var hitPower = UnityEngine.Random.Range(_minPower, _maxPower + 1);

            if (_interruptTarget)
            {
                targetAgent.Interrupt();
            }
            if (_affectHealth)
            {
                switch (_healthAffectionWay)
                {
                case AffectionWay.Reduce:
                    targetAgent.Resources.Damage(hitPower);
                    break;

                case AffectionWay.Add:
                    targetAgent.Resources.Heal(hitPower);
                    break;
                }
            }

            if (_affectEnergy)
            {
                switch (_energyAffectionWay)
                {
                case AffectionWay.Reduce:
                    targetAgent.Resources.UseEnergy(hitPower);
                    break;

                case AffectionWay.Add:
                    targetAgent.Resources.AddEnergy(hitPower);
                    break;
                }
            }

            if (_pushPower > 0)
            {
                targetAgent.Motion.Push(_agent.transform.position, hitPower * _pushPower);
            }

            if (_setStatus)
            {
                targetAgent.SetStatus(_status, _statusDuration);
            }

            if (_setPositionAsTarget)
            {
                var tmp = targetAgent.Perception.Memory.GetDefaultEntity(_agent.transform.position);
                targetAgent.Target = tmp.transform;
            }

            if (_additionalEffects.Count <= 0)
            {
                return;
            }
            foreach (var effect in _additionalEffects)
            {
                if (effect)
                {
                    targetAgent.AddEffect(effect, _agent);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Implementation of finding the target.
        /// </summary>
        /// <returns></returns>
        private IEnumerator INTERNAL_ApplyEffectEnum()
        {
            // Create the loading special effects.
            OnApplyEffectOnInit(_sourceObject);

            // Cast the ray to find the target
            if (_initSkillBy == InitSkillBy.Ray)
            {
                //Try find target agent
                var targetDir = _agent.Target
                                        ? _agent.Target.position - _agent.transform.position
                                        : Perception.InitRay(_range, 0, 90, _agent.Perception.Origin);
                var fov = _fieldOfView > 0 ? _fieldOfView : _agent.GeneralSettings.AimFieldOfView;
                if (Vector3.Angle(targetDir, _agent.Perception.Origin.forward) >= fov)
                {
                    yield break;
                }
                RaycastHit hit;
                if (!Physics.Raycast(_agent.GeneralSettings.ShootOrigin.position, targetDir, out hit, _range))
                {
                    yield break;
                }
                var agent = hit.transform.gameObject.GetComponent <Agent>();

                var unit = hit.transform.gameObject.GetComponent <Unit>();
                if (unit)
                {
                    var targetIsFriend = _agent.Unit.IsFriend(unit);
                    if (targetIsFriend && !_canAffectFriends)
                    {
                        yield break;
                    }
                    if (!targetIsFriend && !_canAffectEnemies)
                    {
                        yield break;
                    }
                }

                //Apply effects to him
                if (agent)
                {
                    agent.AddEffect(this, _agent);
                }
                else
                {
                    INTERNAL_ApplyEffectNonAgent(hit.transform.gameObject);
                }
            }

            // Create a Projectile and pass the skill to it.
            else if (_initSkillBy == InitSkillBy.Projectile)
            {
                var origin = _agent.GeneralSettings.ShootOrigin ?? _agent.Perception.Origin;
                var pjtl   = Instantiate(_projectilePrefab, origin.position, origin.rotation) as Projectile;
                //pjtl.GetComponent<Projectile>().Init(_agent, _agent.Target, this, _minPower, _maxPower, _canAffectEnemies, _canAffectFriends);
                pjtl.Init(_agent, _agent.Target, this, _minPower, _maxPower, _canAffectEnemies, _canAffectFriends);
            }

            // Apply the skill to oneself.
            else if (_initSkillBy == InitSkillBy.Self)
            {
                _agent.AddEffect(this, _agent);
            }

            // Apply effects to a target directly by a reference to the GameObject.
            else if (_initSkillBy == InitSkillBy.Direct)
            {
                if (!_agent.Target)
                {
                    yield return(null);
                }
                var agent = _agent.Target.GetComponent <Agent>();
                if (agent)
                {
                    var unit = _agent.Target.GetComponent <Unit>();
                    if (unit)
                    {
                        var targetIsFriend = _agent.Unit.IsFriend(unit);
                        if (targetIsFriend && !_canAffectFriends)
                        {
                            yield break;
                        }
                        if (!targetIsFriend && !_canAffectEnemies)
                        {
                            yield break;
                        }
                    }

                    agent.AddEffect(this, _agent);
                }
                else
                {
                    INTERNAL_ApplyEffectNonAgent(_agent.Target.gameObject);
                }
            }

            // Find all appropriate targets in cpecified radius and apply effects to them.
            else if (_initSkillBy == InitSkillBy.Radius)
            {
                var agents = FindObjectsOfType <Agent>();
                foreach (var agent in agents)
                {
                    var targetIsFriend = _agent.Unit.IsFriend(agent.Unit);
                    if (targetIsFriend && !_canAffectFriends)
                    {
                        continue;
                    }
                    if (!targetIsFriend && !_canAffectEnemies)
                    {
                        continue;
                    }

                    if (Vector3.Distance(agent.transform.position, _agent.transform.position) <= _range &&
                        agent != _agent)
                    {
                        agent.AddEffect(this, _agent);
                    }
                }
            }

            // Create special effects on target after applying the effects.
            if (_agent.Target)
            {
                var agent = _agent.Target.GetComponent <Agent>();
                if (agent)
                {
                    var targetIsFriend = _agent.Unit.IsFriend(agent.Unit);
                    if (targetIsFriend && !_canAffectFriends)
                    {
                        yield break;
                    }
                    if (!targetIsFriend && !_canAffectEnemies)
                    {
                        yield break;
                    }
                }

                OnApplyEffect(_agent.Target.gameObject);
            }
        }