the walls can now scale to the resolution

but the paddles and score text stay where they are. i plan on adding the paddles and score text moving later.
This commit is contained in:
Fries 2023-05-21 19:41:48 -07:00
parent 8ab2f09e62
commit 41f7e7b1a0
5 changed files with 74 additions and 7 deletions

View file

@ -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)

View file

@ -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

View file

@ -6,4 +6,10 @@ public static class Constants
/// a constant representing 1 Meter in pixels.
/// </summary>
internal const double Meter = 10;
/// <summary>
/// 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.
/// </summary>
internal const long WallSizeExtents = 15;
}

View file

@ -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;

View file

@ -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<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 };
}
}