예제 #1
0
    public override void _Process(float delta)
    {
        // Called every frame. Delta is time since last frame.
        // Update game logic here.
        velocity = new Vector2();

        // change velocity according to pressed keys
        if (Input.IsActionPressed("ui_right"))
        {
            velocity.x += 1;
        }
        if (Input.IsActionPressed("ui_left"))
        {
            velocity.x -= 1;
        }
        if (Input.IsActionPressed("ui_down"))
        {
            velocity.y += 1;
        }
        if (Input.IsActionPressed("ui_up"))
        {
            velocity.y -= 1;
        }

        var animatedSprite = ((AnimatedSprite)GetNode("AnimatedSprite"));

        if (velocity.Length() > 0)
        {
            velocity = velocity.Normalized() * SPEED;
            animatedSprite.Play();
        }
        else
        {
            animatedSprite.Stop();
        }

        Position += velocity * delta;
        float clampedX = GameMath.Clamped(Position.x, 0f, screensize.x);
        float clampedY = GameMath.Clamped(Position.y, 0f, screensize.y);

        Position = new Vector2(clampedX, clampedY);
        //Position.y = GameMath.Clamped(Position.y, 0f, screensize.y);

        if (velocity.x != 0)
        {
            animatedSprite.Animation = "right";
            animatedSprite.FlipV     = false;
            animatedSprite.FlipH     = velocity.x < 0;
        }
        else if (velocity.y != 0)
        {
            animatedSprite.Animation = "up";
            animatedSprite.FlipV     = velocity.y > 0;
        }
    }