浏览代码

Update Pong with C# to Godot 4.2.1 (#966)

behrooz bozorgchamy 1 年之前
父节点
当前提交
523c7d34c0

+ 3 - 3
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()

+ 4 - 4
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;
     }
 

+ 3 - 2
mono/pong/Pong with C#.csproj

@@ -1,6 +1,7 @@
-<Project Sdk="Godot.NET.Sdk/4.0.0-dev5">
+<Project Sdk="Godot.NET.Sdk/4.2.1">
   <PropertyGroup>
-    <TargetFramework>net472</TargetFramework>
+    <TargetFramework>net6.0</TargetFramework>
+    <EnableDynamicLoading>true</EnableDynamicLoading>
     <RootNamespace>Pong</RootNamespace>
   </PropertyGroup>
 </Project>

+ 1 - 1
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

+ 1 - 1
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

+ 34 - 34
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"]

+ 25 - 15
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)

+ 1 - 1
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