2023-05-22 17:41:56 +00:00
|
|
|
namespace Pong.Scripts.Managers;
|
|
|
|
|
|
|
|
public partial class ScalingManager : Node
|
|
|
|
{
|
|
|
|
private HFlowContainer _score;
|
|
|
|
private RigidBody2D _paddle;
|
|
|
|
private CharacterBody2D _ball;
|
|
|
|
|
|
|
|
private Area2D _leftWall;
|
|
|
|
private Area2D _rightWall;
|
|
|
|
private StaticBody2D _topWall;
|
|
|
|
private StaticBody2D _bottomWall;
|
|
|
|
|
|
|
|
private RigidBody2D _enemy;
|
|
|
|
|
|
|
|
private Vector2I _gameResolution;
|
|
|
|
private Vector2 _edgePosition;
|
|
|
|
|
|
|
|
public override void _EnterTree()
|
|
|
|
{
|
|
|
|
GetNodes();
|
2023-05-22 18:23:44 +00:00
|
|
|
GetTree().Root.SizeChanged += AdaptToGameResolution;
|
|
|
|
AdaptToGameResolution();
|
2023-05-22 17:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void _ExitTree()
|
|
|
|
{
|
2023-05-22 18:23:44 +00:00
|
|
|
GetTree().Root.SizeChanged -= AdaptToGameResolution;
|
2023-05-22 17:41:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-22 18:23:44 +00:00
|
|
|
/// <summary>
|
|
|
|
/// a method that gets all the nodes that the scaling manager will manage the position and scale of.
|
|
|
|
/// </summary>
|
2023-05-22 17:41:56 +00:00
|
|
|
private void GetNodes()
|
|
|
|
{
|
|
|
|
_score = GetNode<HFlowContainer>("../Score");
|
|
|
|
_paddle = GetNode<RigidBody2D>("../Paddle");
|
|
|
|
_ball = GetNode<CharacterBody2D>("../Ball");
|
|
|
|
|
|
|
|
_leftWall = GetNode<Area2D>("../Walls/Left");
|
|
|
|
_rightWall = GetNode<Area2D>("../Walls/Right");
|
|
|
|
_topWall = GetNode<StaticBody2D>("../Walls/Top");
|
|
|
|
_bottomWall = GetNode<StaticBody2D>("../Walls/Bottom");
|
|
|
|
|
|
|
|
_enemy = GetNode<RigidBody2D>("../Enemy");
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:23:44 +00:00
|
|
|
/// <summary>
|
|
|
|
/// adapt to the current resolution of the game.
|
|
|
|
/// </summary>
|
2023-05-22 17:41:56 +00:00
|
|
|
private void AdaptToGameResolution()
|
|
|
|
{
|
|
|
|
_gameResolution = DisplayServer.WindowGetSize();
|
|
|
|
_edgePosition = CalculateEdgePosition(_gameResolution);
|
|
|
|
|
2023-05-22 18:23:44 +00:00
|
|
|
SetWallPosition();
|
|
|
|
SetScorePosition();
|
2023-05-22 17:41:56 +00:00
|
|
|
}
|
|
|
|
|
2023-05-22 18:23:44 +00:00
|
|
|
/// <summary>
|
|
|
|
/// calculate the edges of the screen according to 0, 0 being in the center of the screen.
|
|
|
|
/// </summary>
|
2023-05-22 17:41:56 +00:00
|
|
|
private static Vector2 CalculateEdgePosition(Vector2 resolution)
|
|
|
|
{
|
|
|
|
return new Vector2(resolution.X / 2, resolution.Y / 2);
|
|
|
|
}
|
|
|
|
|
2023-05-22 18:23:44 +00:00
|
|
|
/// <summary>
|
|
|
|
/// set the position and scale of the walls according to the edge position.
|
|
|
|
/// </summary>
|
|
|
|
private void SetWallPosition()
|
2023-05-22 17:41:56 +00:00
|
|
|
{
|
2023-05-22 18:23:44 +00:00
|
|
|
var xSize = _edgePosition.X - Constants.WallSizeExtents;
|
|
|
|
var ySize = _edgePosition.Y - Constants.WallSizeExtents;
|
2023-05-22 17:41:56 +00:00
|
|
|
|
|
|
|
_leftWall.Position = new Vector2(-xSize, 0);
|
|
|
|
_rightWall.Position = new Vector2(xSize, 0);
|
|
|
|
_topWall.Position = new Vector2(0, -ySize);
|
|
|
|
_bottomWall.Position = new Vector2(0, ySize);
|
|
|
|
|
|
|
|
_leftWall.Scale = _leftWall.Scale with { Y = _gameResolution.Y };
|
|
|
|
_rightWall.Scale = _rightWall.Scale with { Y = _gameResolution.Y };
|
|
|
|
_topWall.Scale = _topWall.Scale with { X = _gameResolution.X - 60 };
|
|
|
|
_bottomWall.Scale = _bottomWall.Scale with { X = _gameResolution.X - 60 };
|
|
|
|
}
|
2023-05-22 18:23:44 +00:00
|
|
|
|
|
|
|
/// <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);
|
|
|
|
}
|
2023-05-22 17:41:56 +00:00
|
|
|
}
|