61 lines
1.6 KiB
C#
61 lines
1.6 KiB
C#
|
using Godot;
|
||
|
|
||
|
namespace Pong.Scripts.Managers;
|
||
|
|
||
|
public partial class WallScalingManager : Node
|
||
|
{
|
||
|
private Area2D _leftWall;
|
||
|
private Area2D _rightWall;
|
||
|
private StaticBody2D _topWall;
|
||
|
private StaticBody2D _bottomWall;
|
||
|
|
||
|
private Vector2I _gameResolution;
|
||
|
|
||
|
private void GetNodes()
|
||
|
{
|
||
|
_leftWall = GetNode<Area2D>("Left");
|
||
|
_rightWall = GetNode<Area2D>("Right");
|
||
|
_topWall = GetNode<StaticBody2D>("Top");
|
||
|
_bottomWall = GetNode<StaticBody2D>("Bottom");
|
||
|
}
|
||
|
|
||
|
public override void _EnterTree()
|
||
|
{
|
||
|
GetNodes();
|
||
|
GetTree().Root.SizeChanged += AdaptToGameResolution;
|
||
|
AdaptToGameResolution();
|
||
|
}
|
||
|
|
||
|
public override void _ExitTree()
|
||
|
{
|
||
|
GetTree().Root.SizeChanged -= AdaptToGameResolution;
|
||
|
}
|
||
|
|
||
|
private void AdaptToGameResolution()
|
||
|
{
|
||
|
_gameResolution = DisplayServer.WindowGetSize();
|
||
|
SetWallPosition(CalculateEdgePosition(_gameResolution));
|
||
|
}
|
||
|
|
||
|
private static Vector2 CalculateEdgePosition(Vector2 resolution)
|
||
|
{
|
||
|
return new Vector2(resolution.X / 2, resolution.Y / 2);
|
||
|
}
|
||
|
|
||
|
private void SetWallPosition(Vector2 edgePosition)
|
||
|
{
|
||
|
var xSize = edgePosition.X - Constants.WallSizeExtents;
|
||
|
var ySize = edgePosition.Y - Constants.WallSizeExtents;
|
||
|
|
||
|
_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 };
|
||
|
}
|
||
|
}
|