Untitled

 avatar
unknown
plain_text
a month ago
1.4 kB
3
Indexable
using Godot;
using System;

public partial class Player : Area2D
{
	// Called when the node enters the scene tree for the first time.

	[Export]
	public int Speed {get; set; } = 400;

	public Vector2 screenSize;
	public override void _Ready()
    {
        screenSize = GetViewportRect().Size;
    }

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
    {
        
		var velocity = Vector2.Zero;
		var animatedSprite2D = GetNode<AnimatedSprite2D>("AnimatedSprite2D");


		if (Input.IsActionPressed("move_right"))
        {
            velocity.X += 1;
        }
		if (Input.IsActionPressed("move_left"))
        {
            velocity.X -= 1;
        }
		if (Input.IsActionPressed("move_up"))
        {
            velocity.Y -= 1;
        }
		if (Input.IsActionPressed("move_down"))
        {
            velocity.Y += 1;
        }



		if (velocity.Length() > 1)
        {
            
			velocity = velocity.Normalized() * Speed;

			Position += velocity * (float) delta;

        }

		if (velocity.X != 0)
        {
            animatedSprite2D.Animation = "walk";
			animatedSprite2D.FlipV = false;
			animatedSprite2D.FlipH = velocity.X < 0;
        }
		if (velocity.Y != 0)
        {
            animatedSprite2D.Animation = "up";
			animatedSprite2D.FlipV = velocity.Y > 0;
        }
    }
}
Editor is loading...
Leave a Comment