From dd11042d95d0cb996b815818d2766b35ad211e8e Mon Sep 17 00:00:00 2001 From: Ivan Shakhov Date: Tue, 6 Oct 2020 11:28:27 +0200 Subject: [PATCH] fix Dodge the Creeps --- .editorconfig | 11 +++++++ 2d/dodge_the_creeps/Main.gd | 31 +++++++++++------ 2d/dodge_the_creeps/Main.tscn | 2 +- 2d/dodge_the_creeps/Mob.gd | 3 +- 2d/dodge_the_creeps/Mob.tscn | 16 ++++----- 2d/dodge_the_creeps/Player.gd | 23 ++++++++----- .../Dodge the Creeps with C#.csproj | 25 ++------------ mono/dodge_the_creeps/HUD.cs | 5 ++- mono/dodge_the_creeps/Main.cs | 33 +++++++++---------- mono/dodge_the_creeps/Main.tscn | 2 +- mono/dodge_the_creeps/Mob.cs | 14 ++++---- mono/dodge_the_creeps/Mob.tscn | 6 ++-- mono/dodge_the_creeps/Player.cs | 15 +++++---- mono/dodge_the_creeps/Player.tscn | 2 +- .../Properties/AssemblyInfo.cs | 25 -------------- 15 files changed, 97 insertions(+), 116 deletions(-) create mode 100644 .editorconfig delete mode 100644 mono/dodge_the_creeps/Properties/AssemblyInfo.cs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..85f3909e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +# Top-most EditorConfig file. +root = true + +# Unix-style newlines with a newline ending every file. +[*.cs] +insert_final_newline = true +csharp_space_after_cast = false +indent_size = 4 + +[*.csproj] +indent_size = 2 diff --git a/2d/dodge_the_creeps/Main.gd b/2d/dodge_the_creeps/Main.gd index e13c2d33..75d57e61 100644 --- a/2d/dodge_the_creeps/Main.gd +++ b/2d/dodge_the_creeps/Main.gd @@ -1,6 +1,6 @@ extends Node -export(PackedScene) var Mob +export(PackedScene) var _mob_scene var score func _ready(): @@ -16,6 +16,7 @@ func game_over(): func new_game(): + get_tree().call_group("mobs", "queue_free") score = 0 $Player.start($StartPosition.position) $StartTimer.start() @@ -25,16 +26,26 @@ func new_game(): func _on_MobTimer_timeout(): - $MobPath/MobSpawnLocation.offset = randi() - var mob = Mob.instance() - add_child(mob) - var direction = $MobPath/MobSpawnLocation.rotation + TAU / 4 - mob.position = $MobPath/MobSpawnLocation.position + # Choose a random location on Path2D. + var mob_spawn_location = get_node("MobPath/MobSpawnLocation"); + mob_spawn_location.offset = randi() + + # Create a Mob instance and add it to the scene. + var mob_instance = _mob_scene.instance() + add_child(mob_instance) + + # Set the mob's direction perpendicular to the path direction. + var direction = mob_spawn_location.rotation + TAU / 4 + + # Set the mob's position to a random location. + mob_instance.position = mob_spawn_location.position + + # Add some randomness to the direction. direction += rand_range(-TAU / 8, TAU / 8) - mob.rotation = direction - mob.linear_velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0).rotated(direction) - # warning-ignore:return_value_discarded - $HUD.connect("start_game", mob, "_on_start_game") + mob_instance.rotation = direction + + # Choose the velocity. + mob_instance.linear_velocity = Vector2(rand_range(mob_instance.min_speed, mob_instance.max_speed), 0).rotated(direction) func _on_ScoreTimer_timeout(): diff --git a/2d/dodge_the_creeps/Main.tscn b/2d/dodge_the_creeps/Main.tscn index 54d0a238..dbc97033 100644 --- a/2d/dodge_the_creeps/Main.tscn +++ b/2d/dodge_the_creeps/Main.tscn @@ -14,7 +14,7 @@ _data = { [node name="Main" type="Node"] script = ExtResource( 1 ) -Mob = ExtResource( 2 ) +_mob_scene = ExtResource( 2 ) [node name="ColorRect" type="ColorRect" parent="."] anchor_right = 1.0 diff --git a/2d/dodge_the_creeps/Mob.gd b/2d/dodge_the_creeps/Mob.gd index d08f1620..8aa1e84c 100644 --- a/2d/dodge_the_creeps/Mob.gd +++ b/2d/dodge_the_creeps/Mob.gd @@ -3,9 +3,10 @@ extends RigidBody2D #warning-ignore-all:unused_class_variable export var min_speed = 150 export var max_speed = 250 -var mob_types = ["walk", "swim", "fly"] func _ready(): + $AnimatedSprite.playing = true + var mob_types = $AnimatedSprite.frames.get_animation_names() $AnimatedSprite.animation = mob_types[randi() % mob_types.size()] diff --git a/2d/dodge_the_creeps/Mob.tscn b/2d/dodge_the_creeps/Mob.tscn index 7d1dc4f9..d8975ad3 100644 --- a/2d/dodge_the_creeps/Mob.tscn +++ b/2d/dodge_the_creeps/Mob.tscn @@ -10,11 +10,6 @@ [sub_resource type="SpriteFrames" id=1] animations = [ { -"frames": [ ExtResource( 2 ), ExtResource( 3 ) ], -"loop": true, -"name": "fly", -"speed": 3.0 -}, { "frames": [ ExtResource( 4 ), ExtResource( 5 ) ], "loop": true, "name": "walk", @@ -24,13 +19,20 @@ animations = [ { "loop": true, "name": "swim", "speed": 4.0 +}, { +"frames": [ ExtResource( 2 ), ExtResource( 3 ) ], +"loop": true, +"name": "fly", +"speed": 3.0 } ] [sub_resource type="CapsuleShape2D" id=2] radius = 35.2706 height = 23.3281 -[node name="Mob" type="RigidBody2D"] +[node name="Mob" type="RigidBody2D" groups=[ +"mobs", +]] collision_mask = 0 gravity_scale = 0.0 script = ExtResource( 1 ) @@ -42,8 +44,6 @@ __meta__ = { scale = Vector2( 0.75, 0.75 ) frames = SubResource( 1 ) animation = "walk" -frame = 1 -playing = true [node name="CollisionShape2D" type="CollisionShape2D" parent="."] rotation = 1.5708 diff --git a/2d/dodge_the_creeps/Player.gd b/2d/dodge_the_creeps/Player.gd index 4cb5cd24..784d2e05 100644 --- a/2d/dodge_the_creeps/Player.gd +++ b/2d/dodge_the_creeps/Player.gd @@ -2,27 +2,30 @@ extends Area2D signal hit -export var speed = 400 -var screen_size +# These only need to be accessed in this script, so we can make them private. +# Private variables in GDScript have their name starting with an underscore. +export var _speed = 400 # How fast the player will move (pixels/sec). +var _screen_size # Size of the game window. func _ready(): - screen_size = get_viewport_rect().size + _screen_size = get_viewport_rect().size hide() func _process(delta): - var velocity = Vector2() + var velocity = Vector2() # The player's movement vector. velocity.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left") velocity.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up") if velocity.length() > 0: - velocity = velocity.normalized() * speed + velocity = velocity.normalized() * _speed $AnimatedSprite.play() else: $AnimatedSprite.stop() + position += velocity * delta - position.x = clamp(position.x, 0, screen_size.x) - position.y = clamp(position.y, 0, screen_size.y) + position.x = clamp(position.x, 0, _screen_size.x) + position.y = clamp(position.y, 0, _screen_size.y) if velocity.x != 0: $AnimatedSprite.animation = "right" @@ -36,10 +39,12 @@ func _process(delta): func start(pos): position = pos show() - $CollisionShape2D.disabled = false + # Must be deferred as we can't change physics properties on a physics callback. + $CollisionShape2D.set_deferred("disabled", false) func _on_Player_body_entered(_body): - hide() + hide() # Player disappears after being hit. emit_signal("hit") + # Must be deferred as we can't change physics properties on a physics callback. $CollisionShape2D.set_deferred("disabled", true) diff --git a/mono/dodge_the_creeps/Dodge the Creeps with C#.csproj b/mono/dodge_the_creeps/Dodge the Creeps with C#.csproj index 06badaaf..2c7cf43d 100644 --- a/mono/dodge_the_creeps/Dodge the Creeps with C#.csproj +++ b/mono/dodge_the_creeps/Dodge the Creeps with C#.csproj @@ -1,26 +1,5 @@ - + - {75CB0382-CCCC-4A4D-ABF0-C6CD04D9F832} - Library - DodgetheCreepswithC - Dodge the Creeps with C# - 1.0.7374.16792 - net472 - - false - false - false + netstandard2.1 - - - - - - - - - - - \ No newline at end of file diff --git a/mono/dodge_the_creeps/HUD.cs b/mono/dodge_the_creeps/HUD.cs index 606ab9fa..a12e8024 100644 --- a/mono/dodge_the_creeps/HUD.cs +++ b/mono/dodge_the_creeps/HUD.cs @@ -1,5 +1,4 @@ using Godot; -using System; public class HUD : CanvasLayer { @@ -15,7 +14,7 @@ public class HUD : CanvasLayer GetNode("MessageTimer").Start(); } - async public void ShowGameOver() + public async void ShowGameOver() { ShowMessage("Game Over"); @@ -37,7 +36,7 @@ public class HUD : CanvasLayer public void OnStartButtonPressed() { GetNode