diff --git a/Scenes/Objects/Walls.tscn b/Scenes/Objects/Walls.tscn index f85dfc6..9fca8a6 100644 --- a/Scenes/Objects/Walls.tscn +++ b/Scenes/Objects/Walls.tscn @@ -1,6 +1,5 @@ -[gd_scene load_steps=9 format=3 uid="uid://cmk6ierwi7sdt"] +[gd_scene load_steps=8 format=3 uid="uid://cmk6ierwi7sdt"] -[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"] @@ -22,7 +21,6 @@ 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) diff --git a/Scenes/Pong.tscn b/Scenes/Pong.tscn index ff77722..be69553 100644 --- a/Scenes/Pong.tscn +++ b/Scenes/Pong.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=7 format=3 uid="uid://bh3p1hnvsviu6"] +[gd_scene load_steps=8 format=3 uid="uid://bh3p1hnvsviu6"] [ext_resource type="PackedScene" path="res://Scenes/Objects/Paddle.tscn" id="1_5rs0o"] [ext_resource type="Script" path="res://Scripts/Managers/PongSceneManager.cs" id="1_ee533"] +[ext_resource type="Script" path="res://Scripts/Managers/ScalingManager.cs" id="2_dc5sv"] [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://cmk6ierwi7sdt" path="res://Scenes/Objects/Walls.tscn" id="3_jfis7"] @@ -10,6 +11,9 @@ [node name="Pong" type="Node2D"] script = ExtResource("1_ee533") +[node name="ScalingManager" type="Node" parent="."] +script = ExtResource("2_dc5sv") + [node name="Score" parent="." instance=ExtResource("2_f3jwj")] [node name="Paddle" parent="." instance=ExtResource("1_5rs0o")] diff --git a/Scenes/UI/Score.tscn b/Scenes/UI/Score.tscn index ac98af8..85ec1b1 100644 --- a/Scenes/UI/Score.tscn +++ b/Scenes/UI/Score.tscn @@ -5,10 +5,8 @@ [node name="Score" type="HFlowContainer"] z_index = 1 -offset_left = -378.0 -offset_top = -272.0 -offset_right = 378.0 -offset_bottom = 268.0 +offset_right = 756.0 +offset_bottom = 540.0 script = ExtResource("1_ytifn") [node name="Player1" type="Label" parent="."] diff --git a/Scripts/GlobalUsings.cs b/Scripts/GlobalUsings.cs new file mode 100644 index 0000000..21b1703 --- /dev/null +++ b/Scripts/GlobalUsings.cs @@ -0,0 +1 @@ +global using Godot; diff --git a/Scripts/Managers/ScalingManager.cs b/Scripts/Managers/ScalingManager.cs new file mode 100644 index 0000000..18b0902 --- /dev/null +++ b/Scripts/Managers/ScalingManager.cs @@ -0,0 +1,74 @@ +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(); + GetTree().Root.SizeChanged += AdaptToGameResolution; + AdaptToGameResolution(); + } + + public override void _ExitTree() + { + GetTree().Root.SizeChanged -= AdaptToGameResolution; + } + + private void GetNodes() + { + _score = GetNode("../Score"); + _paddle = GetNode("../Paddle"); + _ball = GetNode("../Ball"); + + _leftWall = GetNode("../Walls/Left"); + _rightWall = GetNode("../Walls/Right"); + _topWall = GetNode("../Walls/Top"); + _bottomWall = GetNode("../Walls/Bottom"); + + _enemy = GetNode("../Enemy"); + } + + private void AdaptToGameResolution() + { + _gameResolution = DisplayServer.WindowGetSize(); + _edgePosition = CalculateEdgePosition(_gameResolution); + + SetWallPosition(_edgePosition); + _score.Position = new Vector2(-_edgePosition.X + 30, -_edgePosition.Y + 30); + } + + 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 }; + } +} diff --git a/Scripts/Managers/UI/Score.gd b/Scripts/Managers/UI/Score.gd index ec5730c..e9a6683 100644 --- a/Scripts/Managers/UI/Score.gd +++ b/Scripts/Managers/UI/Score.gd @@ -13,10 +13,6 @@ func _ready(): PlayerOneLabel = get_node("Player1") PlayerTwoLabel = get_node("Player2") -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(_delta): - pass - ## when the ball collides with the wall, this method is called by the WallManager func score(player_number: int): if player_number == PlayerOne: diff --git a/Scripts/Managers/WallScalingManager.cs b/Scripts/Managers/WallScalingManager.cs deleted file mode 100644 index e42d7ec..0000000 --- a/Scripts/Managers/WallScalingManager.cs +++ /dev/null @@ -1,60 +0,0 @@ -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 }; - } -} diff --git a/Scripts/Objects/Ball.cs b/Scripts/Objects/Ball.cs index 841e7bf..177c56c 100644 --- a/Scripts/Objects/Ball.cs +++ b/Scripts/Objects/Ball.cs @@ -1,5 +1,3 @@ -using Godot; - namespace Pong.Scripts.Objects; public partial class Ball : CharacterBody2D diff --git a/Scripts/Objects/Enemy.cs b/Scripts/Objects/Enemy.cs index c0dc227..c42c837 100644 --- a/Scripts/Objects/Enemy.cs +++ b/Scripts/Objects/Enemy.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using Godot; using Godot.Collections; namespace Pong.Scripts.Objects; diff --git a/Scripts/Objects/Paddle.cs b/Scripts/Objects/Paddle.cs index ba5bf16..cebc4ed 100644 --- a/Scripts/Objects/Paddle.cs +++ b/Scripts/Objects/Paddle.cs @@ -1,5 +1,3 @@ -using Godot; - namespace Pong.Scripts.Objects; public partial class Paddle : RigidBody2D