add support for scaling the score ui.

the score ui seems to use the top left corner to start drawing the ui so i have to calculate things differently but this works!
This commit is contained in:
Fries 2023-05-22 11:23:44 -07:00
parent 7bae1587d2
commit e1b0fa338f
2 changed files with 34 additions and 8 deletions

View file

@ -15,6 +15,10 @@ script = ExtResource("1_ee533")
script = ExtResource("2_dc5sv") script = ExtResource("2_dc5sv")
[node name="Score" parent="." instance=ExtResource("2_f3jwj")] [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")] [node name="Paddle" parent="." instance=ExtResource("1_5rs0o")]
position = Vector2(-350, 0) position = Vector2(-350, 0)

View file

@ -19,15 +19,18 @@ public partial class ScalingManager : Node
public override void _EnterTree() public override void _EnterTree()
{ {
GetNodes(); GetNodes();
GetTree().Root.SizeChanged += AdaptToGameResolution; GetTree().Root.SizeChanged += AdaptToGameResolution;
AdaptToGameResolution(); AdaptToGameResolution();
} }
public override void _ExitTree() public override void _ExitTree()
{ {
GetTree().Root.SizeChanged -= AdaptToGameResolution; GetTree().Root.SizeChanged -= AdaptToGameResolution;
} }
/// <summary>
/// a method that gets all the nodes that the scaling manager will manage the position and scale of.
/// </summary>
private void GetNodes() private void GetNodes()
{ {
_score = GetNode<HFlowContainer>("../Score"); _score = GetNode<HFlowContainer>("../Score");
@ -42,24 +45,33 @@ public partial class ScalingManager : Node
_enemy = GetNode<RigidBody2D>("../Enemy"); _enemy = GetNode<RigidBody2D>("../Enemy");
} }
/// <summary>
/// adapt to the current resolution of the game.
/// </summary>
private void AdaptToGameResolution() private void AdaptToGameResolution()
{ {
_gameResolution = DisplayServer.WindowGetSize(); _gameResolution = DisplayServer.WindowGetSize();
_edgePosition = CalculateEdgePosition(_gameResolution); _edgePosition = CalculateEdgePosition(_gameResolution);
SetWallPosition(_edgePosition); SetWallPosition();
_score.Position = new Vector2(-_edgePosition.X + 30, -_edgePosition.Y + 30); SetScorePosition();
} }
/// <summary>
/// calculate the edges of the screen according to 0, 0 being in the center of the screen.
/// </summary>
private static Vector2 CalculateEdgePosition(Vector2 resolution) private static Vector2 CalculateEdgePosition(Vector2 resolution)
{ {
return new Vector2(resolution.X / 2, resolution.Y / 2); return new Vector2(resolution.X / 2, resolution.Y / 2);
} }
private void SetWallPosition(Vector2 edgePosition) /// <summary>
/// set the position and scale of the walls according to the edge position.
/// </summary>
private void SetWallPosition()
{ {
var xSize = edgePosition.X - Constants.WallSizeExtents; var xSize = _edgePosition.X - Constants.WallSizeExtents;
var ySize = edgePosition.Y - Constants.WallSizeExtents; var ySize = _edgePosition.Y - Constants.WallSizeExtents;
_leftWall.Position = new Vector2(-xSize, 0); _leftWall.Position = new Vector2(-xSize, 0);
_rightWall.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 }; _topWall.Scale = _topWall.Scale with { X = _gameResolution.X - 60 };
_bottomWall.Scale = _bottomWall.Scale with { X = _gameResolution.X - 60 }; _bottomWall.Scale = _bottomWall.Scale with { X = _gameResolution.X - 60 };
} }
/// <summary>
/// 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.
/// </summary>
private void SetScorePosition()
{
_score.Position = new Vector2(-_edgePosition.X + 30, -_edgePosition.Y + 30);
_score.Size = new Vector2(_gameResolution.X - 60, _gameResolution.Y - 60);
}
} }