diff --git a/Scenes/Objects/Walls.tscn b/Scenes/Objects/Walls.tscn index 0e2a530..f85dfc6 100644 --- a/Scenes/Objects/Walls.tscn +++ b/Scenes/Objects/Walls.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=8 format=3 uid="uid://b1787fyse1y6m"] +[gd_scene load_steps=9 format=3 uid="uid://cmk6ierwi7sdt"] -[ext_resource type="Script" path="res://Scripts/Managers/Walls.cs" id="1_mxer2"] +[ext_resource type="Script" path="res://Scripts/Managers/WallScalingManager.cs" id="1_mrl25"] +[ext_resource type="Script" path="res://Scripts/Managers/WallCollisionManager.cs" id="1_we5my"] [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_f7jy5"] size = Vector2(30, 1) @@ -21,11 +22,12 @@ size = Vector2(1, 30) size = Vector2(1, 30) [node name="Walls" type="Node2D"] +script = ExtResource("1_mrl25") [node name="Left" type="Area2D" parent="."] position = Vector2(-393, -2) scale = Vector2(1, 600) -script = ExtResource("1_mxer2") +script = ExtResource("1_we5my") PlayerNumber = 1 [node name="Left" type="Sprite2D" parent="Left"] @@ -39,7 +41,7 @@ shape = SubResource("RectangleShape2D_1mqst") [node name="Right" type="Area2D" parent="."] position = Vector2(393, -2) scale = Vector2(1, 600) -script = ExtResource("1_mxer2") +script = ExtResource("1_we5my") [node name="Right" type="Sprite2D" parent="Right"] position = Vector2(2.08165e-12, 2.08165e-12) diff --git a/Scenes/Pong.tscn b/Scenes/Pong.tscn index c534e22..ff77722 100644 --- a/Scenes/Pong.tscn +++ b/Scenes/Pong.tscn @@ -4,7 +4,7 @@ [ext_resource type="Script" path="res://Scripts/Managers/PongSceneManager.cs" id="1_ee533"] [ext_resource type="PackedScene" uid="uid://clsmrwvyrt7av" path="res://Scenes/UI/Score.tscn" id="2_f3jwj"] [ext_resource type="PackedScene" path="res://Scenes/Objects/Ball.tscn" id="2_u2ksv"] -[ext_resource type="PackedScene" uid="uid://b1787fyse1y6m" path="res://Scenes/Objects/Walls.tscn" id="3_jfis7"] +[ext_resource type="PackedScene" uid="uid://cmk6ierwi7sdt" path="res://Scenes/Objects/Walls.tscn" id="3_jfis7"] [ext_resource type="PackedScene" path="res://Scenes/Objects/Enemy.tscn" id="4_uwvof"] [node name="Pong" type="Node2D"] @@ -17,7 +17,6 @@ position = Vector2(-350, 0) _moveSpeed = 50.0 [node name="Camera2D" type="Camera2D" parent="."] -position = Vector2(0, -2) [node name="Ball" parent="." instance=ExtResource("2_u2ksv")] _ballSpeed = 50.0 diff --git a/Scripts/Constants.cs b/Scripts/Constants.cs index 73529d9..53f3325 100644 --- a/Scripts/Constants.cs +++ b/Scripts/Constants.cs @@ -6,4 +6,10 @@ public static class Constants /// a constant representing 1 Meter in pixels. /// internal const double Meter = 10; + + /// + /// the extents (half of the size) of the pixel size of either the width for the vertical walls, + /// or the height for the horizontal walls. + /// + internal const long WallSizeExtents = 15; } diff --git a/Scripts/Managers/Walls.cs b/Scripts/Managers/WallCollisionManager.cs similarity index 95% rename from Scripts/Managers/Walls.cs rename to Scripts/Managers/WallCollisionManager.cs index be4179c..63738e4 100644 --- a/Scripts/Managers/Walls.cs +++ b/Scripts/Managers/WallCollisionManager.cs @@ -4,7 +4,7 @@ using Pong.Scripts.Objects; namespace Pong.Scripts.Managers; -public partial class Walls : Area2D +public partial class WallCollisionManager : Area2D { [Export] internal PlayerNumber PlayerNumber; diff --git a/Scripts/Managers/WallScalingManager.cs b/Scripts/Managers/WallScalingManager.cs new file mode 100644 index 0000000..e42d7ec --- /dev/null +++ b/Scripts/Managers/WallScalingManager.cs @@ -0,0 +1,60 @@ +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("Left"); + _rightWall = GetNode("Right"); + _topWall = GetNode("Top"); + _bottomWall = GetNode("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 }; + } +}