From 2cfeda9c125fb3a8eee016b5330165be205d7788 Mon Sep 17 00:00:00 2001 From: Sascha Date: Fri, 1 Sep 2023 17:37:40 +0200 Subject: [PATCH] Tutorial4 --- Abdullah.tscn | 4 +++- Scripts/Abdullah.cs | 26 +++++++++++++++++--------- Scripts/World.cs | 22 +++++++++++----------- World.tscn | 8 +------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/Abdullah.tscn b/Abdullah.tscn index e11e451..b58a282 100644 --- a/Abdullah.tscn +++ b/Abdullah.tscn @@ -9,7 +9,9 @@ position = Vector2(460, 266) texture = ExtResource("1_h0rd0") script = ExtResource("2_4tdcf") -[node name="Child" type="Sprite2D" parent="."] +[node name="Tomato" type="Sprite2D" parent="."] position = Vector2(100, 100) scale = Vector2(0.5, 0.5) texture = ExtResource("3_ffpuj") + +[node name="Clock" type="Timer" parent="."] diff --git a/Scripts/Abdullah.cs b/Scripts/Abdullah.cs index 7a113fc..ffe70e2 100644 --- a/Scripts/Abdullah.cs +++ b/Scripts/Abdullah.cs @@ -3,16 +3,24 @@ using Godot; namespace Tutorial1.Scripts; public partial class Abdullah : Sprite2D { - private const int AMOUNT = 5; - private Sprite2D child; + private Timer clock; - public override void _Ready() => child = GetNode("Child"); + [Signal] public delegate void MovedEventHandler(float x, float y); - public override void _Process(double delta) { - uint randomNumber = GD.Randi() % 4; - if (randomNumber == 0) Position += new Vector2(0, -AMOUNT); - if (randomNumber == 1) Position += new Vector2(0, AMOUNT); - if (randomNumber == 2) Position += new Vector2(-AMOUNT, 0); - if (randomNumber == 3) Position += new Vector2(AMOUNT, 0); + public override void _Ready() { + clock = GetNode("Clock"); + clock.WaitTime = 1; + clock.Timeout += ClockOnTimeout; + clock.Start(); + Moved += OnMoved; } + + private static void OnMoved(float x, float y) => GD.Print($"Moved x={x},y={y}"); + + private void ClockOnTimeout() { + Vector2 newPosition = new(GD.RandRange(0, 500), GD.RandRange(0, 500)); + Position = newPosition; + EmitSignal("Moved", newPosition.X, newPosition.Y); + } + } \ No newline at end of file diff --git a/Scripts/World.cs b/Scripts/World.cs index 8d11234..df17633 100644 --- a/Scripts/World.cs +++ b/Scripts/World.cs @@ -3,15 +3,15 @@ using Godot; namespace Tutorial1.Scripts; public partial class World : Node2D { - private PackedScene packedScene; - public override void _Ready() => packedScene = GD.Load("res://Abdullah.tscn"); - - public override void _UnhandledInput(InputEvent @event) { - if (@event is InputEventMouseButton mouseEvent) - //Create instance of Sprite scene - if (packedScene.Instantiate() is Sprite2D abdullah) { - abdullah.Position = mouseEvent.Position; - AddChild(abdullah); - } - } + // private PackedScene packedScene; + // public override void _Ready() => packedScene = GD.Load("res://Abdullah.tscn"); + // + // public override void _UnhandledInput(InputEvent @event) { + // if (@event is InputEventMouseButton mouseEvent) + // //Create instance of Sprite scene + // if (packedScene.Instantiate() is Sprite2D abdullah) { + // abdullah.Position = mouseEvent.Position; + // AddChild(abdullah); + // } + // } } \ No newline at end of file diff --git a/World.tscn b/World.tscn index 4a15d68..75f401b 100644 --- a/World.tscn +++ b/World.tscn @@ -7,10 +7,4 @@ script = ExtResource("1_4gg20") [node name="Abdullah" parent="." instance=ExtResource("1_0rt2v")] -position = Vector2(540, 144) - -[node name="Abdullah2" parent="." instance=ExtResource("1_0rt2v")] -position = Vector2(206, 268) - -[node name="Abdullah3" parent="." instance=ExtResource("1_0rt2v")] -position = Vector2(855, 424) +position = Vector2(504, 310)