From 3e611870658edd17a6d370d8af60d293545a66f4 Mon Sep 17 00:00:00 2001 From: Fries Date: Tue, 23 May 2023 10:38:40 -0700 Subject: [PATCH] add an .editorconfig. and i also added some events for when i want the ball and enemy paddle to move. --- .editorconfig | 4 ++++ Scenes/Pong.tscn | 8 ++++---- Scripts/Constants.cs | 2 +- Scripts/Data/GameArea.cs | 13 ++++++++++++- Scripts/Managers/ScalingManager.cs | 22 +++++++++++++++++++++- Scripts/Objects/Paddle.cs | 2 -- 6 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..512ecc5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*] +end_of_line = lf +insert_final_newline = true +indent_style = tab diff --git a/Scenes/Pong.tscn b/Scenes/Pong.tscn index 715cf7c..9379d99 100644 --- a/Scenes/Pong.tscn +++ b/Scenes/Pong.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=8 format=3 uid="uid://bh3p1hnvsviu6"] -[ext_resource type="PackedScene" uid="uid://ijf4pw1wmb4t" path="res://Scenes/Objects/Paddle.tscn" id="1_5rs0o"] +[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"] @@ -11,9 +11,6 @@ [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")] offset_left = -370.0 offset_top = -270.0 @@ -35,3 +32,6 @@ _maxRandomAngle = 0.42 [node name="Enemy" parent="." instance=ExtResource("4_uwvof")] position = Vector2(350, 0) _moveSpeed = 50.0 + +[node name="ScalingManager" type="Node" parent="."] +script = ExtResource("2_dc5sv") diff --git a/Scripts/Constants.cs b/Scripts/Constants.cs index e550a08..43ea013 100644 --- a/Scripts/Constants.cs +++ b/Scripts/Constants.cs @@ -11,7 +11,7 @@ public static class Constants /// the pixel size of either the vertical or the horizontal size of the walls. /// public const long WallSize = 30; - + /// /// 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. diff --git a/Scripts/Data/GameArea.cs b/Scripts/Data/GameArea.cs index 58ec1bb..3713b0d 100644 --- a/Scripts/Data/GameArea.cs +++ b/Scripts/Data/GameArea.cs @@ -1,6 +1,9 @@ namespace Pong.Scripts.Data; -public record GameArea +/// +/// a record class that represents the game area. +/// +public record class GameArea { /// /// the X coordinate of the GameArea wrapped in a struct that makes operating on it easier. this is divided @@ -14,6 +17,10 @@ public record GameArea /// public GameCoordinate Y { get; } + /// + /// take a global position inside the game area and normalize it to a value between 0 and 1 which represents the game area. + /// + /// a Vector2 containing a global position inside the game area. public Vector2 NormalizePosition(Vector2 globalPosition) { var normalizedX = Mathf.InverseLerp(-X.ByWallSizeExtents, X.ByWallSizeExtents, globalPosition.X); @@ -22,6 +29,10 @@ public record GameArea } + /// + /// take a normalized position and convert it back into a globalized position. + /// + /// a Vector2 containing a normalized position. public Vector2 GlobalizePosition(Vector2 normalizedPosition) { var globalizedX = Mathf.Lerp(-X.ByWallSizeExtents, X.ByWallSizeExtents, normalizedPosition.X); diff --git a/Scripts/Managers/ScalingManager.cs b/Scripts/Managers/ScalingManager.cs index 70718de..3fa80a9 100644 --- a/Scripts/Managers/ScalingManager.cs +++ b/Scripts/Managers/ScalingManager.cs @@ -20,6 +20,8 @@ public partial class ScalingManager : Node private GameArea _gameArea; public event EventHandler NewPaddlePosition; + public event EventHandler NewEnemyPosition; + public event EventHandler NewBallPosition; public override void _EnterTree() { @@ -57,6 +59,9 @@ public partial class ScalingManager : Node { var normalizedPaddlePosition = _gameArea?.NormalizePosition(_paddle.Position) ?? new Vector2(0, 0.5); + var normalizedEnemyPosition = _gameArea?.NormalizePosition(_enemy.Position) ?? new Vector2(1, 0.5); + var normalizedBallPosition = _gameArea?.NormalizePosition(_ball.Position) ?? new Vector2(0, 0); + _gameResolution = DisplayServer.WindowGetSize(); _gameArea = new GameArea(_gameResolution); @@ -94,9 +99,24 @@ public partial class ScalingManager : Node _score.Size = new Vector2(_gameResolution.X - 60, _gameResolution.Y - 60); } + /// + /// set the paddle position to a normalized position that gets globalized. the paddle gets the new position with an event. + /// private void SetPaddlePosition(Vector2 normalizedPosition) { - var newPaddlePos = _gameArea.GlobalizePosition(normalizedPosition with { X = 0 }) + new Vector2(24, 0); + var newPaddlePos = _gameArea.GlobalizePosition(normalizedPosition with { X = 0 }) + new Vector2(48, 0); NewPaddlePosition?.Invoke(this, newPaddlePos); } + + private void SetEnemyPosition(Vector2 normalizedPosition) + { + var newEnemyPos = _gameArea.GlobalizePosition(normalizedPosition with {X = 0}) + new Vector2(48,0); + NewEnemyPosition?.Invoke(this, newEnemyPos); + } + + private void SetBallPosition(Vector2 normalizedPosition) + { + var newBallPos = _gameArea.GlobalizePosition(normalizedPosition); + NewBallPosition?.Invoke(this, newBallPos); + } } diff --git a/Scripts/Objects/Paddle.cs b/Scripts/Objects/Paddle.cs index b050d72..9369911 100644 --- a/Scripts/Objects/Paddle.cs +++ b/Scripts/Objects/Paddle.cs @@ -23,8 +23,6 @@ public partial class Paddle : RigidBody2D public override void _EnterTree() { - SetNotifyTransform(true); - SetNotifyLocalTransform(true); _scalingManager = GetNode("../ScalingManager"); _scalingManager.NewPaddlePosition += (_, vector2) => _newPosition = vector2; }