diff --git a/Scenes/Objects/Ball.tscn b/Scenes/Objects/Ball.tscn index 05e0125..9eb4329 100644 --- a/Scenes/Objects/Ball.tscn +++ b/Scenes/Objects/Ball.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=4 format=3] +[gd_scene load_steps=4 format=3 uid="uid://dts6gwgqbre22"] [ext_resource type="Script" path="res://Scripts/Objects/Ball.cs" id="1_474si"] diff --git a/Scenes/Objects/Enemy.tscn b/Scenes/Objects/Enemy.tscn index 73c317b..4bda572 100644 --- a/Scenes/Objects/Enemy.tscn +++ b/Scenes/Objects/Enemy.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=5 format=3] +[gd_scene load_steps=5 format=3 uid="uid://ch0fhyeawosrh"] [ext_resource type="PhysicsMaterial" uid="uid://e05n66x8ug77" path="res://Resources/BouncyMaterial.tres" id="1_e3kk5"] [ext_resource type="Script" path="res://Scripts/Objects/Enemy.cs" id="1_fbrtv"] diff --git a/Scenes/Pong.tscn b/Scenes/Pong.tscn index 9379d99..cbd297c 100644 --- a/Scenes/Pong.tscn +++ b/Scenes/Pong.tscn @@ -1,12 +1,12 @@ [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="PackedScene" uid="uid://ijf4pw1wmb4t" 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://dts6gwgqbre22" 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"] -[ext_resource type="PackedScene" path="res://Scenes/Objects/Enemy.tscn" id="4_uwvof"] +[ext_resource type="PackedScene" uid="uid://ch0fhyeawosrh" path="res://Scenes/Objects/Enemy.tscn" id="4_uwvof"] [node name="Pong" type="Node2D"] script = ExtResource("1_ee533") @@ -19,7 +19,7 @@ offset_bottom = 270.0 [node name="Paddle" parent="." instance=ExtResource("1_5rs0o")] position = Vector2(-350, 0) -_moveSpeed = 50.0 +MoveSpeed = 50.0 [node name="Camera2D" type="Camera2D" parent="."] @@ -31,7 +31,7 @@ _maxRandomAngle = 0.42 [node name="Enemy" parent="." instance=ExtResource("4_uwvof")] position = Vector2(350, 0) -_moveSpeed = 50.0 +MoveSpeed = 50.0 [node name="ScalingManager" type="Node" parent="."] script = ExtResource("2_dc5sv") diff --git a/Scripts/Data/DoubleExtensions.cs b/Scripts/Data/DoubleExtensions.cs new file mode 100644 index 0000000..2049fba --- /dev/null +++ b/Scripts/Data/DoubleExtensions.cs @@ -0,0 +1,9 @@ +namespace Pong.Scripts.Data; + +public static class DoubleExtensions +{ + public static double ByMeter(this double num) + { + return num * Constants.Meter; + } +} diff --git a/Scripts/Managers/BasePaddle.cs b/Scripts/Managers/BasePaddle.cs new file mode 100644 index 0000000..f1ce280 --- /dev/null +++ b/Scripts/Managers/BasePaddle.cs @@ -0,0 +1,19 @@ +namespace Pong.Scripts.Managers; + +public partial class BasePaddle: RigidBody2D +{ + [Export] protected double MoveSpeed; + + protected ScalingManager ScalingManager; + protected Vector2? NewPosition; + + public override void _EnterTree() + { + ScalingManager = GetNode("../ScalingManager"); + } + + protected void SetNewPosition(Vector2 newPosition) + { + NewPosition = newPosition; + } +} diff --git a/Scripts/Managers/ScalingManager.cs b/Scripts/Managers/ScalingManager.cs index 3fa80a9..a9c497d 100644 --- a/Scripts/Managers/ScalingManager.cs +++ b/Scripts/Managers/ScalingManager.cs @@ -19,9 +19,9 @@ public partial class ScalingManager : Node private Vector2I _gameResolution; private GameArea _gameArea; - public event EventHandler NewPaddlePosition; - public event EventHandler NewEnemyPosition; - public event EventHandler NewBallPosition; + public event Action NewPaddlePosition; + public event Action NewEnemyPosition; + public event Action NewBallPosition; public override void _EnterTree() { @@ -68,6 +68,7 @@ public partial class ScalingManager : Node SetWallPosition(); SetScorePosition(); SetPaddlePosition(normalizedPaddlePosition); + SetEnemyPosition(normalizedEnemyPosition); } /// @@ -105,18 +106,22 @@ public partial class ScalingManager : Node private void SetPaddlePosition(Vector2 normalizedPosition) { var newPaddlePos = _gameArea.GlobalizePosition(normalizedPosition with { X = 0 }) + new Vector2(48, 0); - NewPaddlePosition?.Invoke(this, newPaddlePos); + NewPaddlePosition?.Invoke(newPaddlePos); } private void SetEnemyPosition(Vector2 normalizedPosition) { - var newEnemyPos = _gameArea.GlobalizePosition(normalizedPosition with {X = 0}) + new Vector2(48,0); - NewEnemyPosition?.Invoke(this, newEnemyPos); + var newEnemyPos = _gameArea.GlobalizePosition(normalizedPosition with { X = 1 }) + new Vector2(-48, 0); + NewEnemyPosition?.Invoke(newEnemyPos); } private void SetBallPosition(Vector2 normalizedPosition) { var newBallPos = _gameArea.GlobalizePosition(normalizedPosition); - NewBallPosition?.Invoke(this, newBallPos); + NewBallPosition?.Invoke(newBallPos); + } + + public static void SetNewPosition(ref Vector2 newPositionField) + { } } diff --git a/Scripts/Objects/Enemy.cs b/Scripts/Objects/Enemy.cs index c42c837..4f063e7 100644 --- a/Scripts/Objects/Enemy.cs +++ b/Scripts/Objects/Enemy.cs @@ -1,29 +1,32 @@ using System; using System.Collections.Generic; using Godot.Collections; +using Pong.Scripts.Data; +using Pong.Scripts.Managers; namespace Pong.Scripts.Objects; -public partial class Enemy : RigidBody2D +public partial class Enemy : BasePaddle { /// /// draw shapes to the screen (like ). /// [Export] private bool _drawDebugShapes; - [Export] private double _moveSpeed; private CollisionShape2D _collisionShape; private Rect2 _scanArea; - + private PhysicsDirectSpaceState2D _spaceState; private readonly Variant _collider = "collider"; private PhysicsShapeQueryParameters2D _query; - public override void _Ready() + public override void _EnterTree() { + base._EnterTree(); + ScalingManager.NewEnemyPosition += SetNewPosition; _collisionShape = GetNode("CollisionShape2D"); GenerateCastArea(); - + _spaceState = GetWorld2D().DirectSpaceState; _query = new PhysicsShapeQueryParameters2D { @@ -34,7 +37,17 @@ public partial class Enemy : RigidBody2D public override void _PhysicsProcess(double delta) { - Scan(delta); + } + + public override void _IntegrateForces(PhysicsDirectBodyState2D state) + { + if (NewPosition.HasValue) + { + state.Transform = new Transform2D(0, NewPosition.Value); + NewPosition = null; + } + + Scan(GetPhysicsProcessDeltaTime(), state); } public override void _Draw() @@ -43,7 +56,7 @@ public partial class Enemy : RigidBody2D DrawRect(_scanArea, Colors.Aqua); } - private void Scan(double delta) + private void Scan(double delta, PhysicsDirectBodyState2D state) { var result = _spaceState.IntersectShape(_query); @@ -53,27 +66,28 @@ public partial class Enemy : RigidBody2D return; } - TrackBall(delta, result); + TrackBall(delta, result, state); } /// /// track the distance between the ball and the enemy paddle on the y axis and move velocity accordingly. /// /// how long it took to complete the last frame in seconds. this should be constant as - /// should be executed in the physics process method which should be separate from the main frame rate. + /// should be executed in the physics process method which should be separate from the main frame rate. /// a dictionary of objects that collided with the cast. this method only works if - /// theres an object that has the Ball class. - private void TrackBall(double delta, IReadOnlyList result) + /// theres an object that has the Ball class. + /// + private void TrackBall(double delta, IReadOnlyList result, PhysicsDirectBodyState2D state) { // checks if the collider is a ball, if not, return. if (result[0][_collider].As() is not { } ball) return; // gets the sign of the distance between the ball and the paddle on the y axis var normalisedDistance = new Vector2 { Y = Mathf.Sign(ball.Position.Y - Position.Y) }; - var linearVelocity = normalisedDistance * _moveSpeed * Constants.Meter; + var linearVelocity = normalisedDistance * MoveSpeed.ByMeter(); // lerp the velocity to smooth out jerky movement. - LinearVelocity = LinearVelocity.Lerp(linearVelocity, delta); + state.LinearVelocity = LinearVelocity.Lerp(linearVelocity, delta); } /// diff --git a/Scripts/Objects/Paddle.cs b/Scripts/Objects/Paddle.cs index 9369911..7182aaf 100644 --- a/Scripts/Objects/Paddle.cs +++ b/Scripts/Objects/Paddle.cs @@ -1,30 +1,21 @@ +using Pong.Scripts.Data; using Pong.Scripts.Managers; namespace Pong.Scripts.Objects; -public partial class Paddle : RigidBody2D +public partial class Paddle : BasePaddle { - [Export] private double _moveSpeed; - private double _verticalInput; private readonly StringName _paddleUp = "paddle_up"; private readonly StringName _paddleDown = "paddle_down"; - private ScalingManager _scalingManager; private bool _canMove = true; private Vector2 _originalPosition; - private Vector2? _newPosition; - - /// - /// property that multiples the moveSpeed by the Meter constant. - /// - private double MoveSpeed => _moveSpeed * Constants.Meter; - public override void _EnterTree() { - _scalingManager = GetNode("../ScalingManager"); - _scalingManager.NewPaddlePosition += (_, vector2) => _newPosition = vector2; + base._EnterTree(); + ScalingManager.NewPaddlePosition += SetNewPosition; } public override void _Process(double delta) @@ -34,13 +25,13 @@ public partial class Paddle : RigidBody2D public override void _IntegrateForces(PhysicsDirectBodyState2D state) { - if (_newPosition.HasValue) + if (NewPosition.HasValue) { - state.Transform = new Transform2D(0, _newPosition.Value); - _newPosition = null; + state.Transform = new Transform2D(0, NewPosition.Value); + NewPosition = null; } - state.LinearVelocity = Vector2.Up * MoveSpeed * _verticalInput; + state.LinearVelocity = Vector2.Up * MoveSpeed.ByMeter() * _verticalInput; } /// diff --git a/addons/BaseMenu/BaseMenu.cs b/addons/BaseMenu/BaseMenu.cs deleted file mode 100644 index 7755723..0000000 --- a/addons/BaseMenu/BaseMenu.cs +++ /dev/null @@ -1,20 +0,0 @@ -#if TOOLS -using Godot; - -namespace Pong.addons.BaseMenu; - -[Tool] -public partial class BaseMenu : EditorPlugin -{ - public override void _EnterTree() - { - var baseMenuScript = GD.Load