diff --git a/Scenes/Pong.tscn b/Scenes/Pong.tscn index be69553..6af3bf5 100644 --- a/Scenes/Pong.tscn +++ b/Scenes/Pong.tscn @@ -15,6 +15,10 @@ script = ExtResource("1_ee533") script = ExtResource("2_dc5sv") [node name="Score" parent="." instance=ExtResource("2_f3jwj")] +offset_left = -370.0 +offset_top = -270.0 +offset_right = 370.0 +offset_bottom = 270.0 [node name="Paddle" parent="." instance=ExtResource("1_5rs0o")] position = Vector2(-350, 0) diff --git a/Scripts/Managers/ScalingManager.cs b/Scripts/Managers/ScalingManager.cs index 18b0902..c2d13d6 100644 --- a/Scripts/Managers/ScalingManager.cs +++ b/Scripts/Managers/ScalingManager.cs @@ -19,15 +19,18 @@ public partial class ScalingManager : Node public override void _EnterTree() { GetNodes(); - GetTree().Root.SizeChanged += AdaptToGameResolution; - AdaptToGameResolution(); + GetTree().Root.SizeChanged += AdaptToGameResolution; + AdaptToGameResolution(); } public override void _ExitTree() { - GetTree().Root.SizeChanged -= AdaptToGameResolution; + GetTree().Root.SizeChanged -= AdaptToGameResolution; } + /// + /// a method that gets all the nodes that the scaling manager will manage the position and scale of. + /// private void GetNodes() { _score = GetNode("../Score"); @@ -42,24 +45,33 @@ public partial class ScalingManager : Node _enemy = GetNode("../Enemy"); } + /// + /// adapt to the current resolution of the game. + /// private void AdaptToGameResolution() { _gameResolution = DisplayServer.WindowGetSize(); _edgePosition = CalculateEdgePosition(_gameResolution); - SetWallPosition(_edgePosition); - _score.Position = new Vector2(-_edgePosition.X + 30, -_edgePosition.Y + 30); + SetWallPosition(); + SetScorePosition(); } + /// + /// calculate the edges of the screen according to 0, 0 being in the center of the screen. + /// private static Vector2 CalculateEdgePosition(Vector2 resolution) { return new Vector2(resolution.X / 2, resolution.Y / 2); } - private void SetWallPosition(Vector2 edgePosition) + /// + /// set the position and scale of the walls according to the edge position. + /// + private void SetWallPosition() { - var xSize = edgePosition.X - Constants.WallSizeExtents; - var ySize = edgePosition.Y - Constants.WallSizeExtents; + var xSize = _edgePosition.X - Constants.WallSizeExtents; + var ySize = _edgePosition.Y - Constants.WallSizeExtents; _leftWall.Position = new Vector2(-xSize, 0); _rightWall.Position = new Vector2(xSize, 0); @@ -71,4 +83,14 @@ public partial class ScalingManager : Node _topWall.Scale = _topWall.Scale with { X = _gameResolution.X - 60 }; _bottomWall.Scale = _bottomWall.Scale with { X = _gameResolution.X - 60 }; } + + /// + /// set the position of the score ui. this has to be calculated with 0, 0 being the top left of the screen as + /// the UI's point starts from there instead of the point being in the middle of the screen. + /// + private void SetScorePosition() + { + _score.Position = new Vector2(-_edgePosition.X + 30, -_edgePosition.Y + 30); + _score.Size = new Vector2(_gameResolution.X - 60, _gameResolution.Y - 60); + } }