Esempio n. 1
0
        /// <summary>
        /// Sends the specified damage to an external target object.
        /// </summary>
        /// <param name="_target">Target.</param>
        /// <param name="_attacker">Attacker.</param>
        /// <param name="_impact_type">Impact type.</param>
        /// <param name="_damage">Damage.</param>
        /// <param name="_damage_method">Damage method.</param>
        /// <param name="_force">Force.</param>
        private static void SendTargetDamage(GameObject _sender, GameObject _target, DamageTransferType _impact_type, float _damage, string _damage_method, Vector3 _damage_point, DamageForceType _force_type, float _force)
        {
            if (_target == null || _sender == null || _target == _sender)
            {
                return;
            }

            bool _handled = false;

            if (_impact_type == DamageTransferType.Direct || _impact_type == DamageTransferType.DirectOrMessage || _impact_type == DamageTransferType.DirectAndMessage)
            {
                _handled = EntityDamageConverter.HandleDamage(_sender, _target, _impact_type, _damage, _damage_method, _damage_point, _force_type, _force);

                if (_handled == false)
                {
                    ICEWorldEntity _entity = ICEWorldEntity.GetWorldEntity(_target);
                    if (_entity != null)
                    {
                        Vector3 _position  = (_damage_point == Vector3.zero ? _entity.transform.position : _damage_point);
                        Vector3 _direction = _sender.transform.position - _target.transform.position;

                        _entity.AddDamage(_damage, _direction, _position, _sender.transform, _force);
                        _handled = true;
                    }
                }
            }

            if (_impact_type == DamageTransferType.Message || _impact_type == DamageTransferType.DirectAndMessage || (_impact_type == DamageTransferType.DirectOrMessage && _handled == false))
            {
                _target.SendMessageUpwards(_damage_method, _damage, SendMessageOptions.DontRequireReceiver);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// SendDamage handles damage and impact forces for the specified target object. You can use this static method to
        /// affect each entity object within your scene. Please note that _target can be adjusted to null, in
        /// such a case the _force_type will be automatically changed to DamageForceType.Explosion and the origin
        /// of the detonation will be the _sender.transform.position.
        /// </summary>
        /// <param name="_sender">Sender.</param>
        /// <param name="_target">Target.</param>
        /// <param name="_impact_type">Impact type.</param>
        /// <param name="_damage">Damage.</param>
        /// <param name="_damage_method">Damage method.</param>
        /// <param name="_damage_point">Damage point.</param>
        /// <param name="_force_type">Force type.</param>
        /// <param name="_force">Force.</param>
        /// <param name="_radius">Radius.</param>
        public static void SendDamage(GameObject _sender, GameObject _target, DamageTransferType _impact_type, float _damage, string _damage_method, Vector3 _damage_point, DamageForceType _force_type, float _force, float _radius)
        {
            if (_sender == null)
            {
                return;
            }

            if (_target == null)
            {
                _force_type = DamageForceType.Explosion;
            }

            // If the force type is an explosion will will handle first the explosion impact to all objects around the specified target
            // in cases the target will be NULL (e.g. remote or timed detonation of an explosive etc.) the sender will be the origin of
            // the explosion.
            if (_force_type == DamageForceType.Explosion)
            {
                _damage_point = (_damage_point == Vector3.zero ? (_target != null ? _target.transform.position : _sender.transform.position) : _damage_point);

                Collider[] _colliders = Physics.OverlapSphere(_damage_point, _radius);
                if (_colliders != null)
                {
                    foreach (Collider _collider in _colliders)
                    {
                        if (_collider == null || _collider.gameObject == _target || _collider.gameObject == _sender)
                        {
                            continue;
                        }

                        float _distance   = PositionTools.Distance(_damage_point, _collider.gameObject.transform.position);
                        float _multiplier = Mathf.Clamp01(1 - MathTools.Normalize(_distance, 0, _radius));

                        // If a explosion radius is given we will try to apply a suitable force to the colliders gamesobject
                        if (_radius > 0)
                        {
                            if (_collider.attachedRigidbody != null && !_collider.attachedRigidbody.isKinematic)
                            {
                                _collider.attachedRigidbody.AddExplosionForce(_force * _multiplier, _damage_point, _radius);
                            }
                            else
                            {
                                ICEWorldEntity _entity = ICEWorldEntity.GetWorldEntity(_collider.gameObject);
                                if (_entity != null)
                                {
                                    Vector3 _direction = _collider.transform.position - _damage_point;
                                    _entity.ApplyImpact(_direction, _force * _multiplier);
                                }
                            }
                        }

                        // SendTargetDamage will try now to damage the colliders gameobject according to the given distance and multiplier
                        ICEWorldEntity.SendTargetDamage(_sender, _collider.gameObject, _impact_type, _damage * _multiplier, _damage_method, _damage_point, _force_type, _force * _multiplier);
                    }
                }
            }


            if (_target != null)
            {
                // whenever a target is specified and the defined force type isn't NONE we try to apply also a force to the target
                if (_force_type != DamageForceType.None)
                {
                    Vector3 _direction = _target.transform.position - _sender.transform.position;
                    _direction.Normalize();
                    // Handle Target Rigidbody and forces
                    Rigidbody _target_rigidbody = _target.GetComponent <Rigidbody>();
                    if (_target_rigidbody != null && !_target_rigidbody.isKinematic)
                    {
                        _target_rigidbody.AddForce(_direction.normalized * _force, ForceMode.Force);
                    }
                    else
                    {
                        ICEWorldEntity _entity = ICEWorldEntity.GetWorldEntity(_target);
                        if (_entity != null)
                        {
                            _entity.ApplyImpact(_direction, _force);
                        }
                    }
                }

                // Finally we try to damage the specified target
                ICEWorldEntity.SendTargetDamage(_sender, _target, _impact_type, _damage, _damage_method, _damage_point, _force_type, _force);
            }
        }