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:
1 parent
87bbaafb75
commit
5fc6bc36f1
1 file changed
+8
-4
+8
-4
@@ -3,6 +3,7 @@ using System;
|
||||
|
||||
public partial class Player : Node2D {
|
||||
private float MovementSpeed = 800f;
|
||||
private float ScreenBorders = 120f;
|
||||
|
||||
public override void _Ready() {
|
||||
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) {
|
||||
var ScreenSize = GetViewport().GetVisibleRect().Size;
|
||||
var WindowSize = DisplayServer.WindowGetSize();
|
||||
var Scale = WindowSize / ScreenSize;
|
||||
var WindowScale = WindowSize / ScreenSize;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user