From 9b0ebcefba889cad777f62a44ee6c81dbdaab1a6 Mon Sep 17 00:00:00 2001 From: Fries Date: Fri, 19 May 2023 00:18:38 -0700 Subject: [PATCH] walls can reset the ball to its starting position WallManager binds to the BodyEntered signal on the Area2D class and detects if the body that entered the area is the ball class, and if it is, it will reset the ball to its starting position. right now, theres no scoring system. this will be added later. --- Scenes/Ball.tscn | 5 ++-- Scenes/Enemy.tscn | 5 ++-- Scenes/Paddle.tscn | 5 ++-- Scenes/Pong.tscn | 12 ++++---- Scenes/Walls.tscn | 8 +++-- Scripts/{ => Managers}/SceneManager.cs | 2 +- Scripts/Managers/WallManager.cs | 29 +++++++++++++++++++ Scripts/{ => Nodes}/Ball.cs | 28 +++++++++++------- Scripts/{ => Nodes}/Enemy.cs | 8 ++--- Scripts/{ => Nodes}/Paddle.cs | 2 +- Scripts/WallManager.cs | 17 ----------- ...libgit_plugin.linux.editor.double.arm64.so | 4 +-- ...ibgit_plugin.linux.editor.double.x86_64.so | 4 +-- 13 files changed, 78 insertions(+), 51 deletions(-) rename Scripts/{ => Managers}/SceneManager.cs (86%) create mode 100644 Scripts/Managers/WallManager.cs rename Scripts/{ => Nodes}/Ball.cs (72%) rename Scripts/{ => Nodes}/Enemy.cs (92%) rename Scripts/{ => Nodes}/Paddle.cs (96%) delete mode 100644 Scripts/WallManager.cs diff --git a/Scenes/Ball.tscn b/Scenes/Ball.tscn index 3d5bc88..766c7cf 100644 --- a/Scenes/Ball.tscn +++ b/Scenes/Ball.tscn @@ -1,6 +1,6 @@ -[gd_scene load_steps=4 format=3 uid="uid://cggi01qnnlnwg"] +[gd_scene load_steps=4 format=3] -[ext_resource type="Script" path="res://Scripts/Ball.cs" id="1_474si"] +[ext_resource type="Script" path="res://Scripts/Nodes/Ball.cs" id="1_474si"] [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_yq7ub"] size = Vector2(50, 50) @@ -9,6 +9,7 @@ size = Vector2(50, 50) size = Vector2(50, 50) [node name="Ball" type="CharacterBody2D"] +collision_mask = 3 script = ExtResource("1_474si") [node name="Sprite2D" type="Sprite2D" parent="."] diff --git a/Scenes/Enemy.tscn b/Scenes/Enemy.tscn index f97d24b..031f189 100644 --- a/Scenes/Enemy.tscn +++ b/Scenes/Enemy.tscn @@ -1,7 +1,7 @@ -[gd_scene load_steps=5 format=3 uid="uid://krt6x241s3x6"] +[gd_scene load_steps=5 format=3] [ext_resource type="PhysicsMaterial" uid="uid://e05n66x8ug77" path="res://BouncyMaterial.tres" id="1_e3kk5"] -[ext_resource type="Script" path="res://Scripts/Enemy.cs" id="1_fbrtv"] +[ext_resource type="Script" path="res://Scripts/Nodes/Enemy.cs" id="1_fbrtv"] [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_iw3nx"] size = Vector2(50, 75) @@ -10,6 +10,7 @@ size = Vector2(50, 75) size = Vector2(12, 150) [node name="Enemy" type="RigidBody2D"] +collision_mask = 3 physics_material_override = ExtResource("1_e3kk5") gravity_scale = 0.0 lock_rotation = true diff --git a/Scenes/Paddle.tscn b/Scenes/Paddle.tscn index 0310304..6ab4142 100644 --- a/Scenes/Paddle.tscn +++ b/Scenes/Paddle.tscn @@ -1,7 +1,7 @@ -[gd_scene load_steps=5 format=3 uid="uid://bklo6torhapa0"] +[gd_scene load_steps=5 format=3] [ext_resource type="PhysicsMaterial" uid="uid://e05n66x8ug77" path="res://BouncyMaterial.tres" id="1_76uik"] -[ext_resource type="Script" path="res://Scripts/Paddle.cs" id="1_uv7s3"] +[ext_resource type="Script" path="res://Scripts/Nodes/Paddle.cs" id="1_uv7s3"] [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_iw3nx"] size = Vector2(50, 75) @@ -10,6 +10,7 @@ size = Vector2(50, 75) size = Vector2(12, 150) [node name="Paddle" type="RigidBody2D"] +collision_mask = 3 physics_material_override = ExtResource("1_76uik") gravity_scale = 0.0 lock_rotation = true diff --git a/Scenes/Pong.tscn b/Scenes/Pong.tscn index 73e9b48..6947216 100644 --- a/Scenes/Pong.tscn +++ b/Scenes/Pong.tscn @@ -1,10 +1,10 @@ -[gd_scene load_steps=6 format=3 uid="uid://kmfgtiugs4m0"] +[gd_scene load_steps=6 format=3 uid="uid://wn48xnd0lstq"] -[ext_resource type="PackedScene" uid="uid://bklo6torhapa0" path="res://Scenes/Paddle.tscn" id="1_5rs0o"] -[ext_resource type="Script" path="res://Scripts/SceneManager.cs" id="1_m8437"] -[ext_resource type="PackedScene" uid="uid://cggi01qnnlnwg" path="res://Scenes/Ball.tscn" id="2_u2ksv"] -[ext_resource type="PackedScene" uid="uid://c5n541vsuvfk8" path="res://Scenes/Walls.tscn" id="3_jfis7"] -[ext_resource type="PackedScene" uid="uid://krt6x241s3x6" path="res://Scenes/Enemy.tscn" id="4_uwvof"] +[ext_resource type="PackedScene" path="res://Scenes/Paddle.tscn" id="1_5rs0o"] +[ext_resource type="Script" path="res://Scripts/Managers/SceneManager.cs" id="1_m8437"] +[ext_resource type="PackedScene" path="res://Scenes/Ball.tscn" id="2_u2ksv"] +[ext_resource type="PackedScene" path="res://Scenes/Walls.tscn" id="3_jfis7"] +[ext_resource type="PackedScene" path="res://Scenes/Enemy.tscn" id="4_uwvof"] [node name="Pong" type="Node2D"] script = ExtResource("1_m8437") diff --git a/Scenes/Walls.tscn b/Scenes/Walls.tscn index 28d72f5..dddf94e 100644 --- a/Scenes/Walls.tscn +++ b/Scenes/Walls.tscn @@ -1,6 +1,6 @@ -[gd_scene load_steps=8 format=3 uid="uid://c5n541vsuvfk8"] +[gd_scene load_steps=8 format=3] -[ext_resource type="Script" path="res://Scripts/WallManager.cs" id="1_mxer2"] +[ext_resource type="Script" path="res://Scripts/Managers/WallManager.cs" id="1_mxer2"] [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_f7jy5"] size = Vector2(30, 600) @@ -48,6 +48,8 @@ shape = SubResource("RectangleShape2D_sip7s") [node name="Top" type="StaticBody2D" parent="."] position = Vector2(2.08165e-12, -287) +collision_layer = 2 +collision_mask = 2 [node name="Top" type="Sprite2D" parent="Top"] position = Vector2(2.08165e-12, 2.08165e-12) @@ -59,6 +61,8 @@ shape = SubResource("RectangleShape2D_rw4mt") [node name="Bottom" type="StaticBody2D" parent="."] position = Vector2(2.08165e-12, 283) +collision_layer = 2 +collision_mask = 2 [node name="Bottom" type="Sprite2D" parent="Bottom"] position = Vector2(2.08165e-12, 2.08165e-12) diff --git a/Scripts/SceneManager.cs b/Scripts/Managers/SceneManager.cs similarity index 86% rename from Scripts/SceneManager.cs rename to Scripts/Managers/SceneManager.cs index b0472c5..4565617 100644 --- a/Scripts/SceneManager.cs +++ b/Scripts/Managers/SceneManager.cs @@ -1,6 +1,6 @@ using Godot; -namespace Pong.Scripts; +namespace Pong.Scripts.Managers; public partial class SceneManager : Node2D { diff --git a/Scripts/Managers/WallManager.cs b/Scripts/Managers/WallManager.cs new file mode 100644 index 0000000..e843964 --- /dev/null +++ b/Scripts/Managers/WallManager.cs @@ -0,0 +1,29 @@ +using Godot; + +namespace Pong.Scripts.Managers; + +public partial class WallManager : Area2D +{ + public override void _Ready() + { + BodyEntered += OnBodyEntered; + } + + private void OnBodyEntered(Node2D body) + { + if (body is not Nodes.Ball ball) return; + ResetBall(ball); + } + + /// + /// reset the ball to the starting position and flick it again. + /// + /// a object that you want to reset. + private async void ResetBall(Nodes.Ball ball) + { + ball.Velocity = Vector2.Zero; + ball.Position = Vector2.Zero; + await ToSignal(GetTree().CreateTimer(0.25), "timeout"); + ball.FlickBall(); + } +} diff --git a/Scripts/Ball.cs b/Scripts/Nodes/Ball.cs similarity index 72% rename from Scripts/Ball.cs rename to Scripts/Nodes/Ball.cs index 78518bb..b90471c 100644 --- a/Scripts/Ball.cs +++ b/Scripts/Nodes/Ball.cs @@ -1,13 +1,11 @@ using Godot; -namespace Pong.Scripts; +namespace Pong.Scripts.Nodes; public partial class Ball : CharacterBody2D { [Export] private double _ballSpeed; [Export] private double _maxRandomAngle; - - private Vector2 _velocity; /// /// this property multiples the ballSpeed by the Meter constant. @@ -15,17 +13,25 @@ public partial class Ball : CharacterBody2D private double BallSpeed => _ballSpeed * Constants.Meter; public override void _Ready() + { + FlickBall(); + } + + /// + /// flick the ball in a direction. + /// + internal void FlickBall() { // this should dispose when this method goes out of scope (this means after the velocity variable is assigned) using var rng = new RandomNumberGenerator(); rng.Randomize(); // set the velocity currently to the left of the screen with a random y angle. - _velocity = GetRandomStartingDirection() * BallSpeed + new Vector2(0, rng.RandfRange(-_maxRandomAngle, _maxRandomAngle) * BallSpeed); + var randomAngle = new Vector2(0, rng.RandfRange(-_maxRandomAngle, _maxRandomAngle) * BallSpeed); + Velocity = GetRandomStartingDirection() * BallSpeed + randomAngle; } public override void _PhysicsProcess(double delta) { - Velocity = _velocity; CollisionCheck(delta); } @@ -35,9 +41,9 @@ public partial class Ball : CharacterBody2D /// delta time from the _PhysicsProcess method. private void CollisionCheck(double delta) { - var collision = MoveAndCollide(_velocity * delta); + var collision = MoveAndCollide(Velocity * delta); if (collision == null) return; - _velocity = _velocity.Bounce(collision.GetNormal()); + Velocity = Velocity.Bounce(collision.GetNormal()); } /// @@ -45,12 +51,14 @@ public partial class Ball : CharacterBody2D /// either returns Vector2.Left or Vector2.Right /// based on that number. /// - private static Vector2 GetRandomStartingDirection() { + private static Vector2 GetRandomStartingDirection() + { using var rng = new RandomNumberGenerator(); rng.Randomize(); - var range = rng.RandiRange(0,1); + var range = rng.RandiRange(0, 1); - if (range == 0) { + if (range == 0) + { return Vector2.Left; } diff --git a/Scripts/Enemy.cs b/Scripts/Nodes/Enemy.cs similarity index 92% rename from Scripts/Enemy.cs rename to Scripts/Nodes/Enemy.cs index 5f5e2fd..db2671b 100644 --- a/Scripts/Enemy.cs +++ b/Scripts/Nodes/Enemy.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using Godot; using Godot.Collections; -namespace Pong.Scripts; +namespace Pong.Scripts.Nodes; public partial class Enemy : RigidBody2D { @@ -36,9 +36,9 @@ public partial class Enemy : RigidBody2D private void Scan(double delta) { - var spaceState = GetWorld2D().DirectSpaceState; + using var spaceState = GetWorld2D().DirectSpaceState; - var query = new PhysicsShapeQueryParameters2D + using var query = new PhysicsShapeQueryParameters2D { Shape = new RectangleShape2D { Size = _scanArea.Size }, Exclude = new Array(new[] { GetRid() }) @@ -65,7 +65,7 @@ public partial class Enemy : RigidBody2D private void TrackBall(double delta, IReadOnlyList result) { // checks if the collider is a ball, if not, return. - if (result[0]["collider"].As() is not { } ball) 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(0, Mathf.Sign(ball.Position.Y - Position.Y)); diff --git a/Scripts/Paddle.cs b/Scripts/Nodes/Paddle.cs similarity index 96% rename from Scripts/Paddle.cs rename to Scripts/Nodes/Paddle.cs index a2ff130..2893d2c 100644 --- a/Scripts/Paddle.cs +++ b/Scripts/Nodes/Paddle.cs @@ -1,6 +1,6 @@ using Godot; -namespace Pong.Scripts; +namespace Pong.Scripts.Nodes; public partial class Paddle : RigidBody2D { diff --git a/Scripts/WallManager.cs b/Scripts/WallManager.cs deleted file mode 100644 index 7736d0a..0000000 --- a/Scripts/WallManager.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Godot; -using System; - -public partial class WallManager : Area2D -{ - // Called when the node enters the scene tree for the first time. - public override void _Ready() - { - - } - - // Called every frame. 'delta' is the elapsed time since the previous frame. - public override void _Process(double delta) - { - } - -} diff --git a/addons/godot-git-plugin/linux/libgit_plugin.linux.editor.double.arm64.so b/addons/godot-git-plugin/linux/libgit_plugin.linux.editor.double.arm64.so index 52b2024..79a3b32 100755 --- a/addons/godot-git-plugin/linux/libgit_plugin.linux.editor.double.arm64.so +++ b/addons/godot-git-plugin/linux/libgit_plugin.linux.editor.double.arm64.so @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:426f8b861429858307593e077b780e4496ba3eeeb183431a41773c0be4f6ed31 -size 8182112 +oid sha256:94a0c3fc43d950240e5c55682f6948f525c5df50fc7c5e020997b75f54c7d800 +size 8650504 diff --git a/addons/godot-git-plugin/linux/libgit_plugin.linux.editor.double.x86_64.so b/addons/godot-git-plugin/linux/libgit_plugin.linux.editor.double.x86_64.so index 0123c58..c4d4087 100755 --- a/addons/godot-git-plugin/linux/libgit_plugin.linux.editor.double.x86_64.so +++ b/addons/godot-git-plugin/linux/libgit_plugin.linux.editor.double.x86_64.so @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:46cd623b7c8baa5779e165e534a82d99a0288bf655c2fc6be92ad80db656fff1 -size 8781400 +oid sha256:a61b2305e1550d1cc7f4101f0c86918c48e53947107f786ee65dbf2decd2f32f +size 15556080