From 523c7d34c059037ef5e39b219f475429e228af38 Mon Sep 17 00:00:00 2001 From: behrooz bozorgchamy Date: Mon, 25 Mar 2024 16:25:51 +0100 Subject: [PATCH] Update Pong with C# to Godot 4.2.1 (#966) --- mono/pong/Logic/Ball.cs | 6 +-- mono/pong/Logic/Paddle.cs | 8 ++-- mono/pong/Pong with C#.csproj | 5 ++- mono/pong/ball.png.import | 2 +- mono/pong/paddle.png.import | 2 +- mono/pong/pong.tscn | 68 +++++++++++++++++----------------- mono/pong/project.godot | 40 ++++++++++++-------- mono/pong/separator.png.import | 2 +- 8 files changed, 72 insertions(+), 61 deletions(-) diff --git a/mono/pong/Logic/Ball.cs b/mono/pong/Logic/Ball.cs index 4c2d6fefe6..9cdea90a77 100644 --- a/mono/pong/Logic/Ball.cs +++ b/mono/pong/Logic/Ball.cs @@ -7,17 +7,17 @@ public partial class Ball : Area2D public Vector2 direction = Vector2.Left; private Vector2 _initialPos; - private float _speed = DefaultSpeed; + private double _speed = DefaultSpeed; public override void _Ready() { _initialPos = Position; } - public override void _Process(float delta) + public override void _Process(double delta) { _speed += delta * 2; - Position += _speed * delta * direction; + Position += (float)(_speed * delta) * direction; } public void Reset() diff --git a/mono/pong/Logic/Paddle.cs b/mono/pong/Logic/Paddle.cs index f53bcf1ab8..1e16e2c421 100644 --- a/mono/pong/Logic/Paddle.cs +++ b/mono/pong/Logic/Paddle.cs @@ -12,19 +12,19 @@ public partial class Paddle : Area2D public override void _Ready() { - string name = Name.ToLower(); + string name = Name.ToString().ToLower(); _up = name + "_move_up"; _down = name + "_move_down"; _ballDir = name == "left" ? 1 : -1; } - public override void _Process(float delta) + public override void _Process(double delta) { // Move up and down based on input. float input = Input.GetActionStrength(_down) - Input.GetActionStrength(_up); Vector2 position = Position; // Required so that we can modify position.y. - position += new Vector2(0, input * MoveSpeed * delta); - position.y = Mathf.Clamp(position.y, 16, GetViewportRect().Size.y - 16); + position += new Vector2(0, input * MoveSpeed * (float)delta); + position = new(position.X, Mathf.Clamp(position.Y, 16, GetViewportRect().Size.X - 16)); Position = position; } diff --git a/mono/pong/Pong with C#.csproj b/mono/pong/Pong with C#.csproj index ea3be585e3..c313c5876d 100644 --- a/mono/pong/Pong with C#.csproj +++ b/mono/pong/Pong with C#.csproj @@ -1,6 +1,7 @@ - + - net472 + net6.0 + true Pong \ No newline at end of file diff --git a/mono/pong/ball.png.import b/mono/pong/ball.png.import index 3e35503ae9..b1c17690eb 100644 --- a/mono/pong/ball.png.import +++ b/mono/pong/ball.png.import @@ -2,7 +2,7 @@ importer="texture" type="CompressedTexture2D" -uid="uid://dy8ya5bkuar1e" +uid="uid://13ht46tyr4ae" path="res://.godot/imported/ball.png-9a4ca347acb7532f6ae347744a6b04f7.ctex" metadata={ "vram_texture": false diff --git a/mono/pong/paddle.png.import b/mono/pong/paddle.png.import index e85391e2d5..821bb1f13e 100644 --- a/mono/pong/paddle.png.import +++ b/mono/pong/paddle.png.import @@ -2,7 +2,7 @@ importer="texture" type="CompressedTexture2D" -uid="uid://bssbicievt24u" +uid="uid://cgbjbhglgxf0k" path="res://.godot/imported/paddle.png-0e798fb0912613386507c9904d5cc01a.ctex" metadata={ "vram_texture": false diff --git a/mono/pong/pong.tscn b/mono/pong/pong.tscn index cb114fd0fd..4dea211681 100644 --- a/mono/pong/pong.tscn +++ b/mono/pong/pong.tscn @@ -1,24 +1,24 @@ -[gd_scene load_steps=12 format=2] +[gd_scene load_steps=12 format=3 uid="uid://bufr8nynnqrj"] -[ext_resource path="res://Logic/Paddle.cs" type="Script" id=1] -[ext_resource path="res://paddle.png" type="Texture2D" id=2] -[ext_resource path="res://Logic/Ball.cs" type="Script" id=4] -[ext_resource path="res://ball.png" type="Texture2D" id=5] -[ext_resource path="res://separator.png" type="Texture2D" id=6] -[ext_resource path="res://Logic/Wall.cs" type="Script" id=7] -[ext_resource path="res://Logic/CeilingFloor.cs" type="Script" id=8] +[ext_resource type="Script" path="res://Logic/Paddle.cs" id="1"] +[ext_resource type="Texture2D" uid="uid://cgbjbhglgxf0k" path="res://paddle.png" id="2"] +[ext_resource type="Script" path="res://Logic/Ball.cs" id="4"] +[ext_resource type="Texture2D" uid="uid://13ht46tyr4ae" path="res://ball.png" id="5"] +[ext_resource type="Texture2D" uid="uid://cim6es185kfto" path="res://separator.png" id="6"] +[ext_resource type="Script" path="res://Logic/Wall.cs" id="7"] +[ext_resource type="Script" path="res://Logic/CeilingFloor.cs" id="8"] -[sub_resource type="RectangleShape2D" id=1] -extents = Vector2(4, 16) +[sub_resource type="RectangleShape2D" id="1"] +size = Vector2(4, 16) -[sub_resource type="RectangleShape2D" id=2] -extents = Vector2(4, 4) +[sub_resource type="RectangleShape2D" id="2"] +size = Vector2(4, 4) -[sub_resource type="RectangleShape2D" id=3] -extents = Vector2(10, 200) +[sub_resource type="RectangleShape2D" id="3"] +size = Vector2(10, 200) -[sub_resource type="RectangleShape2D" id=4] -extents = Vector2(320, 10) +[sub_resource type="RectangleShape2D" id="4"] +size = Vector2(320, 10) [node name="Pong" type="Node2D"] @@ -30,69 +30,69 @@ color = Color(0.141176, 0.152941, 0.164706, 1) [node name="Left" type="Area2D" parent="."] modulate = Color(0, 1, 1, 1) position = Vector2(67.6285, 192.594) -script = ExtResource( 1 ) +script = ExtResource("1") [node name="Sprite2D" type="Sprite2D" parent="Left"] -texture = ExtResource( 2 ) +texture = ExtResource("2") [node name="Collision" type="CollisionShape2D" parent="Left"] -shape = SubResource( 1 ) +shape = SubResource("1") [node name="Right" type="Area2D" parent="."] modulate = Color(1, 0, 1, 1) position = Vector2(563.815, 188.919) -script = ExtResource( 1 ) +script = ExtResource("1") [node name="Sprite2D" type="Sprite2D" parent="Right"] -texture = ExtResource( 2 ) +texture = ExtResource("2") [node name="Collision" type="CollisionShape2D" parent="Right"] -shape = SubResource( 1 ) +shape = SubResource("1") [node name="Ball" type="Area2D" parent="."] position = Vector2(320.5, 191.124) -script = ExtResource( 4 ) +script = ExtResource("4") [node name="Sprite2D" type="Sprite2D" parent="Ball"] -texture = ExtResource( 5 ) +texture = ExtResource("5") [node name="Collision" type="CollisionShape2D" parent="Ball"] -shape = SubResource( 2 ) +shape = SubResource("2") [node name="Separator" type="Sprite2D" parent="."] position = Vector2(320, 200) -texture = ExtResource( 6 ) +texture = ExtResource("6") [node name="Node2D" type="Node2D" parent="."] [node name="LeftWall" type="Area2D" parent="."] position = Vector2(-10, 200) -script = ExtResource( 7 ) +script = ExtResource("7") [node name="Collision" type="CollisionShape2D" parent="LeftWall"] -shape = SubResource( 3 ) +shape = SubResource("3") [node name="RightWall" type="Area2D" parent="."] position = Vector2(650, 200) -script = ExtResource( 7 ) +script = ExtResource("7") [node name="Collision" type="CollisionShape2D" parent="RightWall"] -shape = SubResource( 3 ) +shape = SubResource("3") [node name="Ceiling" type="Area2D" parent="."] position = Vector2(320, -10) -script = ExtResource( 8 ) +script = ExtResource("8") [node name="Collision" type="CollisionShape2D" parent="Ceiling"] -shape = SubResource( 4 ) +shape = SubResource("4") [node name="Floor" type="Area2D" parent="."] position = Vector2(320, 410) -script = ExtResource( 8 ) +script = ExtResource("8") _bounceDirection = -1 [node name="Collision" type="CollisionShape2D" parent="Floor"] -shape = SubResource( 4 ) +shape = SubResource("4") [connection signal="area_entered" from="Left" to="Left" method="OnAreaEntered"] [connection signal="area_entered" from="Right" to="Right" method="OnAreaEntered"] diff --git a/mono/pong/project.godot b/mono/pong/project.godot index 8ec0588e5b..4726ce9243 100644 --- a/mono/pong/project.godot +++ b/mono/pong/project.godot @@ -15,7 +15,7 @@ config/description="A simple Pong game. This demo shows best practices for game development in Godot, including signals." config/tags=PackedStringArray("2d", "demo", "official") run/main_scene="pong.tscn" -config/features=PackedStringArray("4.2") +config/features=PackedStringArray("4.2", "C#") config/icon="res://icon.webp" [display] @@ -23,40 +23,50 @@ config/icon="res://icon.webp" window/size/viewport_width=640 window/size/viewport_height=400 window/stretch/mode="canvas_items" +window/stretch/scale_mode="integer" + +[dotnet] + +project/assembly_name="Pong with C#" [input] left_move_down={ "deadzone": 0.5, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":90,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) -, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null) -, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null) -, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":83,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) +"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":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) +, 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":68,"key_label":0,"unicode":100,"echo":false,"script":null) ] } left_move_up={ "deadzone": 0.5, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":65,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) -, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null) -, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null) -, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":87,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) +"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":11,"pressure":0.0,"pressed":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":122,"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":65,"key_label":0,"unicode":113,"echo":false,"script":null) ] } right_move_down={ "deadzone": 0.5, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":16777234,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) -, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":1.0,"script":null) -, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":13,"pressure":0.0,"pressed":false,"script":null) +"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":1.0,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":12,"pressure":0.0,"pressed":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":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":4194321,"key_label":0,"unicode":0,"echo":false,"script":null) ] } right_move_up={ "deadzone": 0.5, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":16777232,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) -, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":-1.0,"script":null) -, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":12,"pressure":0.0,"pressed":false,"script":null) +"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":1,"axis":1,"axis_value":-1.0,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":1,"button_index":11,"pressure":0.0,"pressed":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":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":4194319,"key_label":0,"unicode":0,"echo":false,"script":null) ] } [rendering] +textures/canvas_textures/default_texture_filter=0 +renderer/rendering_method="gl_compatibility" +renderer/rendering_method.mobile="gl_compatibility" viewport/default_clear_color=Color(0, 0, 0, 1) diff --git a/mono/pong/separator.png.import b/mono/pong/separator.png.import index fb59f65345..f28c63234e 100644 --- a/mono/pong/separator.png.import +++ b/mono/pong/separator.png.import @@ -2,7 +2,7 @@ importer="texture" type="CompressedTexture2D" -uid="uid://bxqd5psne5h38" +uid="uid://cim6es185kfto" path="res://.godot/imported/separator.png-f981c8489b9148e2e1dc63398273da74.ctex" metadata={ "vram_texture": false