Exemplo n.º 1
0
    public override void _Ready()
    {
        // local to parent
        var result = Transform.Xform(new Vector2(10, 10));

        GD.Print("(10,10), local to parent: ", result);

        // parent to local
        var result2 = Transform.Inverse().Xform(result);

        GD.Print($"{result}, parent to local: ", result2);

        var result3 = Transform.XformInv(result);

        GD.Print("parent to local, way 2: ", result3);

        // local to global
        var result4 = GlobalTransform.Xform(new Vector2(10, 10));

        GD.Print("(10,10), local to global: ", result4);

        // global to local
        var result5 = GlobalTransform.XformInv(result4);

        GD.Print($"{result4}, global to local");

        // sibling to sibling
        var sibling = GetNode <Godot.Sprite>("../Child2");

        var asGlobal = GlobalTransform.Xform(new Vector2(10, 10));
        var result6  = sibling.GlobalTransform.XformInv(asGlobal);

        GD.Print("(10,10), local to sibling: ", result6);
    }
        public override void _PhysicsProcess(float delta)
        {
            if (_test_shoot)
            {
                Shoot();
                _test_shoot = false;
            }

            if (_dead)
            {
                return;
            }

            if (_player == null)
            {
                _animationTree.Set("arameters/state/current", 0); //Go idle.
                return;
            }

            if (_state == StateEnum.Approach)
            {
                if (_aimPreparing > 0.0)
                {
                    _aimPreparing -= delta;
                    if (_aimPreparing < 0.0)
                    {
                        _aimPreparing = 0;
                    }
                    _animationTree.Set("parameters/aiming/blend_amount", _aimPreparing / AimPrepareTime);
                }

                var toPlayerLocal = GlobalTransform.XformInv(_player.GlobalTransform.origin);
                // The front of the robot is +Z, and atan2 is zero at +X, so we need to use the Z for the X parameter (second one).
                var angleToPlayer = Mathf.Atan2(toPlayerLocal.x, toPlayerLocal.z);
                var tolerance     = Mathf.Deg2Rad(PlayerAimToleranceDegrees);
                if (angleToPlayer > tolerance)
                {
                    _animationTree.Set("parameters/state/current", 1);
                }
                else if (angleToPlayer < -tolerance)
                {
                    _animationTree.Set("parameters/state/current", 2);
                }
                else
                {
                    _animationTree.Set("parameters/state/current", 3);
                    // Facing player, try to shoot.

                    _shootCountdown -= delta;
                    if (_shootCountdown < 0.0)
                    {
                        // See if player can be killed because in they're sight.
                        var rayOrigin = _rayFrom.GlobalTransform.origin;
                        var rayTo     = _player.GlobalTransform.origin + Vector3.Up; // Above middle of player.
                        var col       = GetWorld().DirectSpaceState.IntersectRay(rayOrigin, rayTo, new Godot.Collections.Array()
                        {
                            this
                        });
                        if (col.Count > 0 && col["collider"] is PlayerEntity)
                        {
                            _state        = StateEnum.Aim;
                            _aimCountdown = AimTime;
                            _aimPreparing = 0;
                            _animationTree.Set("parameters/state/current", 0);
                        }
                        else
                        {
                            _shootCountdown = ShootWait;
                        }
                    }
                }
            }
            else if (_state == StateEnum.Aim || _state == StateEnum.Shooting)
            {
                if (_aimPreparing < AimPrepareTime)
                {
                    _aimPreparing += delta;
                    if (_aimPreparing > AimPrepareTime)
                    {
                        _aimPreparing = AimPrepareTime;
                    }
                }

                _animationTree.Set("parameters/aiming/blend_amount", Mathf.Clamp(_aimPreparing / AimPrepareTime, 0, 1));
                _aimCountdown -= delta;
                if (_aimCountdown < 0 && _state == StateEnum.Aim)
                {
                    var rayOrigin = _rayFrom.GlobalTransform.origin;
                    var rayTo     = _player.GlobalTransform.origin + Vector3.Up; // Above middle of player.
                    var col       = GetWorld().DirectSpaceState.IntersectRay(rayOrigin, rayTo, new Godot.Collections.Array()
                    {
                        this
                    });
                    if (col.Count > 0 && col["collider"] is PlayerEntity)
                    {
                        _state = StateEnum.Shooting;
                        _shootAnimation.Play("shoot");
                    }
                    else
                    {
                        ResumeApproach();
                    }
                }

                if (_animationTree.Active)
                {
                    var toCannonLocal = _rayMesh.GlobalTransform.XformInv(_player.GlobalTransform.origin + Vector3.Up);
                    var hAngle        = Mathf.Rad2Deg(Mathf.Atan2(toCannonLocal.x, -toCannonLocal.z));
                    var vAngle        = Mathf.Rad2Deg(Mathf.Atan2(toCannonLocal.y, -toCannonLocal.z));

                    var blendPos = (Vector2)_animationTree.Get("parameters/aim/blend_position");
                    var hMotion  = BlendAimSpeed * delta * -hAngle;
                    blendPos.x += hMotion;
                    blendPos.x  = Mathf.Clamp(blendPos.x, -1, 1);

                    var vMotion = BlendAimSpeed * delta * vAngle;
                    blendPos.y += vMotion;
                    blendPos.y  = Mathf.Clamp(blendPos.y, -1, 1);

                    _animationTree.Set("parameters/aim/blend_position", blendPos);
                }
            }

            _orientation *= _animationTree.GetRootMotionTransform();

            var hVelocity = _orientation.origin / delta;

            _velocity.x = hVelocity.x;
            _velocity.z = hVelocity.z;
            _velocity  += _gravity * delta;
            _velocity   = MoveAndSlide(_velocity, Vector3.Up);

            _orientation.origin = new Vector3();
            _orientation        = _orientation.Orthonormalized();

            var gt = GlobalTransform;

            gt.basis        = _orientation.basis;
            GlobalTransform = gt;
        }