add a scene wide scaling manager.
this is so i can scale all the objects to the proper positions and scales when the resolution changes.
This commit is contained in:
parent
41f7e7b1a0
commit
7bae1587d2
10 changed files with 83 additions and 77 deletions
|
@ -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)
|
||||
|
|
|
@ -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")]
|
||||
|
|
|
@ -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="."]
|
||||
|
|
1
Scripts/GlobalUsings.cs
Normal file
1
Scripts/GlobalUsings.cs
Normal file
|
@ -0,0 +1 @@
|
|||
global using Godot;
|
74
Scripts/Managers/ScalingManager.cs
Normal file
74
Scripts/Managers/ScalingManager.cs
Normal file
|
@ -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<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");
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
}
|
|
@ -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:
|
||||
|
|
|
@ -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<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 };
|
||||
}
|
||||
}
|
|
@ -1,5 +1,3 @@
|
|||
using Godot;
|
||||
|
||||
namespace Pong.Scripts.Objects;
|
||||
|
||||
public partial class Ball : CharacterBody2D
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Pong.Scripts.Objects;
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
using Godot;
|
||||
|
||||
namespace Pong.Scripts.Objects;
|
||||
|
||||
public partial class Paddle : RigidBody2D
|
||||
|
|
Loading…
Reference in a new issue