From 02c5b9041531dbe577e100c0a3ca9597e8555dec Mon Sep 17 00:00:00 2001 From: Fries Date: Tue, 16 May 2023 23:15:31 -0700 Subject: [PATCH] make a basic prototype for a scene. right now theres just a moving ball, inside a box, with a working paddle. i had to make the ball kinematic and tell the ball to bounce when it hits the other direction. --- GodotCharacter.cs | 16 ---------------- GodotCharacter.tscn | 8 -------- Scenes/Ball.tscn | 20 ++++++++++++++++++++ Scenes/Paddle.tscn | 22 ++++++++++++++++++++++ Scenes/Pong.tscn | 19 +++++++++++++++++++ Scenes/Walls.tscn | 35 +++++++++++++++++++++++++++++++++++ Scripts/Ball.cs | 37 +++++++++++++++++++++++++++++++++++++ Scripts/Constants.cs | 9 +++++++++ Scripts/Paddle.cs | 44 ++++++++++++++++++++++++++++++++++++++++++++ pong.tscn | 9 --------- project.godot | 22 +++++++++++++++++++++- 11 files changed, 207 insertions(+), 34 deletions(-) delete mode 100644 GodotCharacter.cs delete mode 100644 GodotCharacter.tscn create mode 100644 Scenes/Ball.tscn create mode 100644 Scenes/Paddle.tscn create mode 100644 Scenes/Pong.tscn create mode 100644 Scenes/Walls.tscn create mode 100644 Scripts/Ball.cs create mode 100644 Scripts/Constants.cs create mode 100644 Scripts/Paddle.cs delete mode 100644 pong.tscn diff --git a/GodotCharacter.cs b/GodotCharacter.cs deleted file mode 100644 index 57818f5..0000000 --- a/GodotCharacter.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Godot; - -namespace Pong; - -public partial class GodotCharacter : Sprite2D -{ - private const double Meter = 10; - - [Export] - private double _moveSpeed; - - public override void _Process(double delta) - { - Position += Vector2.Right * Meter * _moveSpeed * delta; - } -} diff --git a/GodotCharacter.tscn b/GodotCharacter.tscn deleted file mode 100644 index e26fecb..0000000 --- a/GodotCharacter.tscn +++ /dev/null @@ -1,8 +0,0 @@ -[gd_scene load_steps=3 format=3 uid="uid://cnwfyao83u6y5"] - -[ext_resource type="Texture2D" uid="uid://b186qihsjblv7" path="res://icon.svg" id="1_hmqt3"] -[ext_resource type="Script" path="res://GodotCharacter.cs" id="2_006i4"] - -[node name="GodotCharacter" type="Sprite2D"] -texture = ExtResource("1_hmqt3") -script = ExtResource("2_006i4") diff --git a/Scenes/Ball.tscn b/Scenes/Ball.tscn new file mode 100644 index 0000000..3d5bc88 --- /dev/null +++ b/Scenes/Ball.tscn @@ -0,0 +1,20 @@ +[gd_scene load_steps=4 format=3 uid="uid://cggi01qnnlnwg"] + +[ext_resource type="Script" path="res://Scripts/Ball.cs" id="1_474si"] + +[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_yq7ub"] +size = Vector2(50, 50) + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_i1hik"] +size = Vector2(50, 50) + +[node name="Ball" type="CharacterBody2D"] +script = ExtResource("1_474si") + +[node name="Sprite2D" type="Sprite2D" parent="."] +position = Vector2(-1, -1) +texture = SubResource("PlaceholderTexture2D_yq7ub") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(-1, -1) +shape = SubResource("RectangleShape2D_i1hik") diff --git a/Scenes/Paddle.tscn b/Scenes/Paddle.tscn new file mode 100644 index 0000000..8b2f2ea --- /dev/null +++ b/Scenes/Paddle.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=4 format=3 uid="uid://bklo6torhapa0"] + +[ext_resource type="Script" path="res://Scripts/Paddle.cs" id="1_uv7s3"] +[ext_resource type="Texture2D" uid="uid://b186qihsjblv7" path="res://icon.svg" id="2_62eyv"] + +[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_r5a55"] +friction = 0.0 +bounce = 1.0 + +[node name="Paddle" type="RigidBody2D"] +physics_material_override = SubResource("PhysicsMaterial_r5a55") +gravity_scale = 0.0 +lock_rotation = true +script = ExtResource("1_uv7s3") + +[node name="Sprite" type="Sprite2D" parent="."] +scale = Vector2(0.25, 2) +texture = ExtResource("2_62eyv") + +[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."] +scale = Vector2(0.25, 2) +polygon = PackedVector2Array(64, -60.8, 64, 59, 59.6, 64, -60.1, 64, -64, 60.7, -64, -59, -59, -63.8, 59.1, -64) diff --git a/Scenes/Pong.tscn b/Scenes/Pong.tscn new file mode 100644 index 0000000..d40a0f4 --- /dev/null +++ b/Scenes/Pong.tscn @@ -0,0 +1,19 @@ +[gd_scene load_steps=4 format=3 uid="uid://kmfgtiugs4m0"] + +[ext_resource type="PackedScene" uid="uid://bklo6torhapa0" path="res://Scenes/Paddle.tscn" id="1_5rs0o"] +[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"] + +[node name="Pong" type="Node2D"] + +[node name="Paddle" parent="." instance=ExtResource("1_5rs0o")] +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 + +[node name="Walls" parent="." instance=ExtResource("3_jfis7")] diff --git a/Scenes/Walls.tscn b/Scenes/Walls.tscn new file mode 100644 index 0000000..bf851fe --- /dev/null +++ b/Scenes/Walls.tscn @@ -0,0 +1,35 @@ +[gd_scene load_steps=4 format=3 uid="uid://c5n541vsuvfk8"] + +[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_f7jy5"] +size = Vector2(30, 600) + +[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_868ov"] +size = Vector2(756, 30) + +[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_a4dbc"] +friction = 0.0 +bounce = 1.0 + +[node name="Walls" type="Node2D"] + +[node name="Left" type="Sprite2D" parent="."] +position = Vector2(-393, -2) +texture = SubResource("PlaceholderTexture2D_f7jy5") + +[node name="Right" type="Sprite2D" parent="."] +position = Vector2(393, -2) +texture = SubResource("PlaceholderTexture2D_f7jy5") + +[node name="Top" type="Sprite2D" parent="."] +position = Vector2(0, -287) +texture = SubResource("PlaceholderTexture2D_868ov") + +[node name="Bottom" type="Sprite2D" parent="."] +position = Vector2(0, 283) +texture = SubResource("PlaceholderTexture2D_868ov") + +[node name="StaticBody2D" type="StaticBody2D" parent="."] +physics_material_override = SubResource("PhysicsMaterial_a4dbc") + +[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="StaticBody2D"] +polygon = PackedVector2Array(-408, -302, 408, -302, 408, 298, -408, 298, -408, -301, -378, -272, -378, 268, 378, 268, 378, -272, -378, -272) diff --git a/Scripts/Ball.cs b/Scripts/Ball.cs new file mode 100644 index 0000000..66c541d --- /dev/null +++ b/Scripts/Ball.cs @@ -0,0 +1,37 @@ +using Godot; + +namespace Pong.Scripts; + +public partial class Ball : CharacterBody2D +{ + [Export] private double _ballSpeed; + + private Vector2 _velocity; + + /// + /// this property multiples the ballSpeed by the Meter constant. + /// + private double BallSpeed => _ballSpeed * Constants.Meter; + + public override void _Ready() + { + _velocity = Vector2.Left * BallSpeed; + } + + public override void _PhysicsProcess(double delta) + { + Velocity = _velocity; + CollisionCheck(delta); + } + + /// + /// this method moves the ball and bounces if it collides with something. + /// + /// delta time from the _PhysicsProcess method. + private void CollisionCheck(double delta) + { + var collision = MoveAndCollide(_velocity * delta); + if (collision == null) return; + _velocity = _velocity.Bounce(collision.GetNormal()); + } +} diff --git a/Scripts/Constants.cs b/Scripts/Constants.cs new file mode 100644 index 0000000..73529d9 --- /dev/null +++ b/Scripts/Constants.cs @@ -0,0 +1,9 @@ +namespace Pong.Scripts; + +public static class Constants +{ + /// + /// a constant representing 1 Meter in pixels. + /// + internal const double Meter = 10; +} diff --git a/Scripts/Paddle.cs b/Scripts/Paddle.cs new file mode 100644 index 0000000..9949560 --- /dev/null +++ b/Scripts/Paddle.cs @@ -0,0 +1,44 @@ +using Godot; + +namespace Pong.Scripts; + +public partial class Paddle : RigidBody2D +{ + [Export] private double _moveSpeed; + + private double _verticalInput; + + /// + /// property that multiples the moveSpeed by the Meter constant. + /// + private double MoveSpeed => _moveSpeed * Constants.Meter; + + public override void _Process(double delta) + { + _verticalInput = GetVerticalInput(); + } + + public override void _PhysicsProcess(double delta) + { + LinearVelocity = Vector2.Up * MoveSpeed * _verticalInput; + } + + /// + /// a method that returns a number that represents input on the vertical axis. + /// + /// 1 for up, -1 for down, 0 for nothing. + private static double GetVerticalInput() + { + if (Input.IsActionPressed("paddle_up")) + { + return 1; + } + + if (Input.IsActionPressed("paddle_down")) + { + return -1; + } + + return 0; + } +} diff --git a/pong.tscn b/pong.tscn deleted file mode 100644 index d4ea3c0..0000000 --- a/pong.tscn +++ /dev/null @@ -1,9 +0,0 @@ -[gd_scene load_steps=2 format=3 uid="uid://btnh2d0qsywpt"] - -[ext_resource type="PackedScene" uid="uid://cnwfyao83u6y5" path="res://GodotCharacter.tscn" id="1_0pstb"] - -[node name="Pong" type="Node2D"] - -[node name="GodotCharacter" parent="." instance=ExtResource("1_0pstb")] -position = Vector2(-2, 0) -_moveSpeed = 5.0 diff --git a/project.godot b/project.godot index 0decf43..05e6c71 100644 --- a/project.godot +++ b/project.godot @@ -11,10 +11,15 @@ config_version=5 [application] config/name="Pong" -run/main_scene="res://pong.tscn" +run/main_scene="res://Scenes/Pong.tscn" config/features=PackedStringArray("4.0", "C#", "Double Precision", "Mobile") config/icon="res://icon.svg" +[display] + +window/size/viewport_width=800 +window/size/viewport_height=600 + [dotnet] project/assembly_name="Pong" @@ -24,6 +29,21 @@ project/assembly_name="Pong" version_control/plugin_name="GitPlugin" version_control/autoload_on_startup=true +[input] + +paddle_up={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"echo":false,"script":null) +] +} +paddle_down={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null) +] +} + [rendering] renderer/rendering_method="mobile"