Ejemplo n.º 1
0
    void FixedUpdate()
    {
        if (_sacrificing)
        {
            if (_sacrificeJumping)
            {
                _body.velocity += new Vector2(0, -9) * Time.fixedDeltaTime;
            }
            else
            {
                var dir = _jumpPoint - transform.position;
                if (dir.magnitude < 0.1)
                {
                    _sacrificeJumping = true;
                    _body.velocity    = new Vector2(2, 5);
                    _animator.State   = EntityState.Idle;
                    return;
                }

                _animator.State = EntityState.Running;
                _body.velocity  = Vector3.ClampMagnitude(dir.normalized * FleeSpeed, dir.magnitude / Time.deltaTime);
                return;
            }
            return;
        }
        if (_exiting)
        {
            var dir = _exitPoint - transform.position;
            if (dir.magnitude < 0.1)
            {
                Destroy(gameObject);
                return;
            }
            transform.position = transform.position + Vector3.ClampMagnitude(dir.normalized * FleeSpeed * Time.deltaTime, dir.magnitude);
        }
        else
        {
            var player = Level.Instance.CurrentPlayer;
            if (Random.Range(1, 30) == 1)
            {
                _fleeBias = Random.Range(-10, 10);
            }

            if (player && player.gameObject.activeInHierarchy)
            {
                var dif = transform.position - player.transform.position;
                if (dif.magnitude < FearDistance)
                {
                    _fleeing  = true;
                    _walkTime = 0;
                    var speed      = Mathf.Max(FleeSpeed / 4, FleeSpeed * (1 - dif.magnitude / FearDistance));
                    var fleeDegree = MathHelper.Vector2ToDegrees(dif) + _fleeBias;
                    var fleeDir    = MathHelper.DegreeToVector2(fleeDegree);
                    var hit        = Physics2D.Raycast(transform.position, fleeDir,
                                                       speed * 0.2f + Mathf.Max(_collider.bounds.extents.x, _collider.bounds.extents.y), LayerHelper.MoveMask);
                    if (hit.collider != null)
                    {
                        float hitAngle        = MathHelper.Vector2ToDegrees(hit.normal);
                        float leftDifference  = MathHelper.AngleDifference(fleeDegree, hitAngle - 90);
                        float rightDifference = MathHelper.AngleDifference(fleeDegree, hitAngle + 90);
                        if (leftDifference < rightDifference)
                        {
                            fleeDegree = hitAngle - 90;
                        }
                        else
                        {
                            fleeDegree = hitAngle + 90;
                        }
                        fleeDir = MathHelper.DegreeToVector2(fleeDegree);
                    }

                    _animator.State = EntityState.Running;
                    _body.velocity  = fleeDir * speed;
                }
                else
                {
                    _fleeing   = false;
                    _walkTime -= Time.deltaTime;
                    if (_walkTime < 0)
                    {
                        _body.velocity *= 0.95f;
                        if (_body.velocity.magnitude < 0.05)
                        {
                            _animator.State = EntityState.Idle;
                        }
                    }

                    if (WalkChance > Random.Range(0.0f, 1.0f))
                    {
                        _walkTime       = WalkTime;
                        _body.velocity  = Random.onUnitSphere * WalkSpeed;
                        _animator.State = EntityState.Walking;
                    }
                }
            }

            if (_body.velocity.x > 0.05)
            {
                _animator.FlipX = false;
            }
            else if (_body.velocity.x < -0.05)
            {
                _animator.FlipX = true;
            }
        }
    }