Пример #1
0
        /// <summary>
        /// Told by the brains to stop fleeing.
        /// </summary>
        public void ToStopFleeing()
        {
            if (_isFleeing && isActiveAndEnabled)
            {
                Message("ToStopMoving");
            }

            _isFleeing   = false;
            _targetBlock = null;
        }
Пример #2
0
        private void OnTriggerExit(Collider other)
        {
            var zone = other.GetComponent <FleeZone>();

            if (zone != null && zone == _zone)
            {
                _zone.Unregister(gameObject);
                _wasRegistered = false;
                _zone          = null;
            }
        }
Пример #3
0
        static void Gizmo(FleeZone block, GizmoType type)
        {
            var isSelected = (type & GizmoType.Selected) != 0;
            var alpha      = isSelected ? 0.8f : 0.4f;

            var collider = block.GetComponent <BoxCollider>();

            if (collider != null)
            {
                var bounds = collider.bounds;

                Gizmos.color = new Color(0, 0.5f, 1, alpha);
                Gizmos.DrawCube(bounds.center, bounds.extents * 2);
            }
        }
Пример #4
0
        private void OnTriggerEnter(Collider other)
        {
            var zone = other.GetComponent <FleeZone>();

            if (zone != null)
            {
                if (_zone != null)
                {
                    _zone.Unregister(gameObject);
                }
                _zone = zone;

                if (_isFleeing)
                {
                    _zone.Register(gameObject);
                    _wasRegistered = true;
                }
            }
        }
Пример #5
0
        private void findNewFleePosition(bool isAlreadyTooClose)
        {
            var targetPosition = transform.position;
            var targetDot      = -10f;

            var fromThreat = transform.position - _threatPosition;

            fromThreat.y = 0;
            fromThreat.Normalize();

            FleeZone targetBlock = null;

            foreach (var block in FleeZone.All)
            {
                var position = block.transform.position;
                position.y = block.Bottom;

                if (Vector3.Distance(position, _threatPosition) < AvoidDistance)
                {
                    continue;
                }

                if (block == _targetBlock)
                {
                    continue;
                }

                var dot = -1f;

                if (NavMesh.CalculatePath(transform.position, position, 1, _path))
                {
                    var count = _path.GetCornersNonAlloc(_corners);

                    if (count < 1)
                    {
                        continue;
                    }

                    if (!isAlreadyTooClose)
                    {
                        var isOk = true;

                        for (int i = 0; i < count; i++)
                        {
                            var a = i == 0 ? transform.position : _corners[i - 1];
                            var b = _corners[i];

                            var closest = Util.FindClosestToPath(a, b, _threatPosition);

                            if (Vector3.Distance(closest, _threatPosition) < AvoidDistance)
                            {
                                isOk = false;
                                break;
                            }
                        }

                        if (!isOk)
                        {
                            continue;
                        }
                    }

                    var first = _corners[0];
                    if (count > 1)
                    {
                        first = _corners[1];
                    }

                    var vector = first - transform.position;
                    vector.y = 0;
                    vector.Normalize();

                    dot = Vector3.Dot(vector, fromThreat) * Vector3.Distance(position, transform.position);
                }
                else
                {
                    continue;
                }

                if (dot > targetDot)
                {
                    targetDot      = dot;
                    targetPosition = position + (-block.Width * 0.5f + Random.Range(0f, block.Width)) * block.transform.right +
                                     (-block.Depth * 0.5f + Random.Range(0f, block.Depth)) * block.transform.forward;

                    targetBlock = block;
                }
            }

            _targetBlock    = targetBlock;
            _targetPosition = targetPosition;

            Message("ToSprintTo", targetPosition);
            Message("ToFaceWalkDirection", targetPosition);
        }