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);
            }
        }
        public bool TargetReady()
        {
            if (CameraTarget != null && CameraTarget.gameObject.activeInHierarchy)
            {
                return(true);
            }

            ICEWorldEntity[] _entities = GameObject.FindObjectsOfType <ICEWorldEntity>();
            if (_entities != null && _entities.Length > 0)
            {
                ICEWorldEntity _entity = _entities[UnityEngine.Random.Range(0, _entities.Length)];
                if (_entity != null)
                {
                    CameraTarget = _entity.ObjectTransform;
                }
            }

            if (CameraTarget != null && CameraTarget.gameObject.activeInHierarchy)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the root entity or null in cases that this entity is the root entity
        /// </summary>
        /// <returns>The parent entity.</returns>
        public ICEWorldEntity GetRootEntity()
        {
            ICEWorldEntity _root = null;

            // if transform.root and transform are identic the entity is its own root
            if (transform.root == transform)
            {
                _root = this;
            }

            // if transform.root and transform.parent are identic and the transform.parent
            // is an entity, the parent is the root, otherwise this entity is its own root
            else if (transform.root == transform.parent)
            {
                _root = transform.parent.GetComponent <ICEWorldEntity>();
            }

            // if both prior checks fails and _root stills empty we try to find the root entity within the hierarchy
            if (_root == null)
            {
                ICEWorldEntity[] _entities = GetParentEntities();
                if (_entities != null && _entities.Length > 1)
                {
                    foreach (ICEWorldEntity _entity in _entities)
                    {
                        if (_entity == this)
                        {
                            continue;
                        }

                        if (_entity.transform.root == _entity.transform || !_entity.HasParentEntities())
                        {
                            return(_entity);
                        }
                    }
                }
            }

            // if m_RootEntity stills empty it seems that there are no higher entities within the hierarchy, so we assume that this
            // entity is the root entity so we return this.
            if (_root == null)
            {
                _root = this;
            }

            return(_root);
        }
Esempio n. 4
0
        public bool ContainsChild(ICEWorldEntity _object)
        {
            if (_object == null)
            {
                return(false);
            }

            ICEWorldEntity[] _entities = transform.GetComponentsInChildren <ICEWorldEntity>();
            foreach (ICEWorldEntity _entity in _entities)
            {
                if (_entity == _object)
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 5
0
        /// <summary>
        /// Instantiate a new clone of the specified reference object by using the defined _position and _rotation.
        /// </summary>
        /// <param name="_object">Object.</param>
        /// <param name="_position">Position.</param>
        /// <param name="_rotation">Rotation.</param>
        public static GameObject Instantiate(GameObject _reference, Vector3 _position, Quaternion _rotation)
        {
            if (_reference == null)
            {
                return(null);
            }

            ICEWorldEntity _entity = _reference.GetComponent <ICEWorldEntity>();

            if (_entity != null)
            {
                _position.y += _entity.BaseOffset;
            }

            if (ICEWorldRegister.Instance != null)
            {
                return(ICEWorldRegister.Instance.Instantiate(_reference, _position, _rotation));
            }
            else
            {
                return((GameObject)GameObject.Instantiate(_reference, _position, _rotation));
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Recyles or instantiate a clone of the specified reference object by using the defined _position and _rotation.
        /// </summary>
        /// <param name="_object">Object.</param>
        /// <param name="_position">Position.</param>
        /// <param name="_rotation">Rotation.</param>
        public static GameObject Spawn(GameObject _object, Vector3 _position, Quaternion _rotation)
        {
            if (_object == null)
            {
                return(null);
            }

            ICEWorldEntity _entity = _object.GetComponent <ICEWorldEntity>();

            if (_entity != null)
            {
                _position.y += _entity.BaseOffset;
            }

            if (ICEWorldRegister.Instance != null)
            {
                return(ICEWorldRegister.Instance.Spawn(_object, _position, _rotation));
            }
            else
            {
                return(WorldManager.Instantiate(_object, _position, _rotation));
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Gets the ICEWorldEntity of the specified object.
        /// </summary>
        /// <returns>The ICEWorldEntity.</returns>
        /// <param name="_object">Object.</param>
        public static ICEWorldEntity GetWorldEntity(GameObject _object)
        {
            if (_object == null)
            {
                return(null);
            }

            // First we try to get an entity from the specified object ...
            ICEWorldEntity _entity = _object.GetComponent <ICEWorldEntity>();

            // ... if this failed we try to get one from its parents ...
            if (_entity == null)
            {
                _entity = _object.GetComponentInParent <ICEWorldEntity>();
            }

            // ... and finaly we try to get one from its children ...
            if (_entity == null)
            {
                _entity = _object.GetComponentInChildren <ICEWorldEntity>();
            }

            return(_entity);
        }
Esempio n. 8
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);
            }
        }