Made it so the player cant go past the edges of the screen

The player's y position is also fixed
This commit is contained in:
Puppet committed 2025-09-05 17:31:00 -07:00
1 parent 87bbaafb75
commit 5fc6bc36f1
1 file changed
+8 -4
+8 -4
View File
@@ -3,6 +3,7 @@ using System;
public partial class Player : Node2D { public partial class Player : Node2D {
private float MovementSpeed = 800f; private float MovementSpeed = 800f;
private float ScreenBorders = 120f;
public override void _Ready() { public override void _Ready() {
Position = new Vector2(x: DisplayServer.WindowGetSize().X / 2, y: 0); Position = new Vector2(x: DisplayServer.WindowGetSize().X / 2, y: 0);
@@ -11,16 +12,19 @@ public partial class Player : Node2D {
public override void _Process(double delta) { public override void _Process(double delta) {
var ScreenSize = GetViewport().GetVisibleRect().Size; var ScreenSize = GetViewport().GetVisibleRect().Size;
var WindowSize = DisplayServer.WindowGetSize(); var WindowSize = DisplayServer.WindowGetSize();
var Scale = WindowSize / ScreenSize; var WindowScale = WindowSize / ScreenSize;
Vector2 Pos = Position; Vector2 Pos = Position;
Pos.Y = (DisplayServer.WindowGetSize().Y - 90) / Scale.Y; Pos.Y = (WindowSize.Y / WindowScale.Y) - 100;
if (Input.IsKeyPressed((Key)Key.Left) || Input.IsKeyPressed((Key)Key.A)) { if ((Input.IsKeyPressed((Key)Key.Left) || Input.IsKeyPressed((Key)Key.A)) && Pos.X > ScreenBorders) {
GetNode<Sprite2D>("Texture").FlipH = false;
Pos.X -= MovementSpeed * (float)delta; Pos.X -= MovementSpeed * (float)delta;
} }
if (Input.IsKeyPressed((Key)Key.Right) || Input.IsKeyPressed((Key)Key.D)) { float ScreenWidth = (WindowSize.X / WindowScale.X) - ScreenBorders;
if ((Input.IsKeyPressed((Key)Key.Right) || Input.IsKeyPressed((Key)Key.D)) && Pos.X < ScreenWidth) {
GetNode<Sprite2D>("Texture").FlipH = true;
Pos.X += MovementSpeed * (float)delta; Pos.X += MovementSpeed * (float)delta;
} }