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.
This commit is contained in:
parent
794a3646ed
commit
02c5b90415
11 changed files with 207 additions and 34 deletions
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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")
|
|
20
Scenes/Ball.tscn
Normal file
20
Scenes/Ball.tscn
Normal file
|
@ -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")
|
22
Scenes/Paddle.tscn
Normal file
22
Scenes/Paddle.tscn
Normal file
|
@ -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)
|
19
Scenes/Pong.tscn
Normal file
19
Scenes/Pong.tscn
Normal file
|
@ -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")]
|
35
Scenes/Walls.tscn
Normal file
35
Scenes/Walls.tscn
Normal file
|
@ -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)
|
37
Scripts/Ball.cs
Normal file
37
Scripts/Ball.cs
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
using Godot;
|
||||||
|
|
||||||
|
namespace Pong.Scripts;
|
||||||
|
|
||||||
|
public partial class Ball : CharacterBody2D
|
||||||
|
{
|
||||||
|
[Export] private double _ballSpeed;
|
||||||
|
|
||||||
|
private Vector2 _velocity;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// this property multiples the ballSpeed by the Meter constant.
|
||||||
|
/// </summary>
|
||||||
|
private double BallSpeed => _ballSpeed * Constants.Meter;
|
||||||
|
|
||||||
|
public override void _Ready()
|
||||||
|
{
|
||||||
|
_velocity = Vector2.Left * BallSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void _PhysicsProcess(double delta)
|
||||||
|
{
|
||||||
|
Velocity = _velocity;
|
||||||
|
CollisionCheck(delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// this method moves the ball and bounces if it collides with something.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="delta">delta time from the _PhysicsProcess method.</param>
|
||||||
|
private void CollisionCheck(double delta)
|
||||||
|
{
|
||||||
|
var collision = MoveAndCollide(_velocity * delta);
|
||||||
|
if (collision == null) return;
|
||||||
|
_velocity = _velocity.Bounce(collision.GetNormal());
|
||||||
|
}
|
||||||
|
}
|
9
Scripts/Constants.cs
Normal file
9
Scripts/Constants.cs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
namespace Pong.Scripts;
|
||||||
|
|
||||||
|
public static class Constants
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// a constant representing 1 Meter in pixels.
|
||||||
|
/// </summary>
|
||||||
|
internal const double Meter = 10;
|
||||||
|
}
|
44
Scripts/Paddle.cs
Normal file
44
Scripts/Paddle.cs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
using Godot;
|
||||||
|
|
||||||
|
namespace Pong.Scripts;
|
||||||
|
|
||||||
|
public partial class Paddle : RigidBody2D
|
||||||
|
{
|
||||||
|
[Export] private double _moveSpeed;
|
||||||
|
|
||||||
|
private double _verticalInput;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// property that multiples the moveSpeed by the Meter constant.
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// a method that returns a number that represents input on the vertical axis.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>1 for up, -1 for down, 0 for nothing.</returns>
|
||||||
|
private static double GetVerticalInput()
|
||||||
|
{
|
||||||
|
if (Input.IsActionPressed("paddle_up"))
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Input.IsActionPressed("paddle_down"))
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
|
|
@ -11,10 +11,15 @@ config_version=5
|
||||||
[application]
|
[application]
|
||||||
|
|
||||||
config/name="Pong"
|
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/features=PackedStringArray("4.0", "C#", "Double Precision", "Mobile")
|
||||||
config/icon="res://icon.svg"
|
config/icon="res://icon.svg"
|
||||||
|
|
||||||
|
[display]
|
||||||
|
|
||||||
|
window/size/viewport_width=800
|
||||||
|
window/size/viewport_height=600
|
||||||
|
|
||||||
[dotnet]
|
[dotnet]
|
||||||
|
|
||||||
project/assembly_name="Pong"
|
project/assembly_name="Pong"
|
||||||
|
@ -24,6 +29,21 @@ project/assembly_name="Pong"
|
||||||
version_control/plugin_name="GitPlugin"
|
version_control/plugin_name="GitPlugin"
|
||||||
version_control/autoload_on_startup=true
|
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]
|
[rendering]
|
||||||
|
|
||||||
renderer/rendering_method="mobile"
|
renderer/rendering_method="mobile"
|
||||||
|
|
Loading…
Reference in a new issue