Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Godot;
- using System;
- public partial class Player : Area2D
- {
- [Export]
- public int Speed { get; set; }
- public Vector2 ScreenSize;
- // Called when the node enters the scene tree for the first time.
- 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)
- {
- Vector2 velocity = Vector2.Zero;
- if (Input.IsActionPressed("move_right"))
- {
- velocity.X += 1;
- GD.Print("right");
- }
- if (Input.IsActionPressed("move_left"))
- {
- velocity.X -= 1;
- GD.Print("left");
- }
- if (Input.IsActionPressed("move_down"))
- {
- velocity.Y += 1;
- GD.Print("down");
- }
- if (Input.IsActionPressed("move_up"))
- {
- velocity.Y -= 1;
- GD.Print("up");
- }
- GD.Print($"X: {Position.X} Y: {Position.Y}");
- var sprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
- if (velocity.Length() > 0)
- {
- velocity = velocity.Normalized() * Speed;
- sprite.Play();
- }
- else
- {
- sprite.Stop();
- }
- Position += velocity * (float)delta;
- Position = new Vector2(
- x: Mathf.Clamp(Position.X, 0, ScreenSize.X),
- y: Mathf.Clamp(Position.Y, 0, ScreenSize.Y)
- );
- if (velocity.X != 0)
- {
- sprite.Animation = "walk";
- sprite.FlipV = false;
- sprite.FlipH = velocity.X < 0;
- }
- else if (velocity.Y != 0)
- {
- sprite.Animation = "up";
- sprite.FlipV = velocity.Y > 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment