From 7b2bdc6d9fb1603b3fae9017564a51cd7335a91c Mon Sep 17 00:00:00 2001 From: Sascha Date: Mon, 11 Sep 2023 10:28:55 +0200 Subject: [PATCH] spawn Character to random GlobalPosition --- Character.tscn | 34 +++++++++++++++++++++++++++------- Scripts/Button.cs | 18 ++++++++++++++++-- Scripts/Character.cs | 25 ++++--------------------- Scripts/World.cs | 11 ----------- World.tscn | 43 +++++++++++++------------------------------ 5 files changed, 60 insertions(+), 71 deletions(-) diff --git a/Character.tscn b/Character.tscn index cc7fcf0..1a167f0 100644 --- a/Character.tscn +++ b/Character.tscn @@ -1,16 +1,36 @@ -[gd_scene load_steps=4 format=3 uid="uid://bkvgqv8jki0us"] +[gd_scene load_steps=3 format=3 uid="uid://bkvgqv8jki0us"] -[ext_resource type="Texture2D" uid="uid://dc1t7wtq620d" path="res://Icons/barrels_E.png" id="1_dsvt3"] [ext_resource type="Script" path="res://Scripts/Character.cs" id="2_7gv41"] [ext_resource type="Texture2D" uid="uid://driormxb3in5o" path="res://Icons/Male_2_Idle0.png" id="3_w4es2"] -[node name="Barrels" type="Sprite2D"] -position = Vector2(460, 266) -texture = ExtResource("1_dsvt3") +[node name="Character" type="Node"] script = ExtResource("2_7gv41") [node name="Man" type="Sprite2D" parent="."] -position = Vector2(130, 60) +position = Vector2(146, 273) texture = ExtResource("3_w4es2") -[node name="Clock" type="Timer" parent="."] +[node name="PanelContainer2" type="PanelContainer" parent="Man"] +offset_left = -100.0 +offset_top = 30.0 +offset_right = 100.0 +offset_bottom = 57.0 + +[node name="VBoxContainer" type="VBoxContainer" parent="Man/PanelContainer2"] +layout_mode = 2 + +[node name="HBoxContainer" type="HBoxContainer" parent="Man/PanelContainer2/VBoxContainer"] +layout_mode = 2 +size_flags_vertical = 3 + +[node name="Label" type="Label" parent="Man/PanelContainer2/VBoxContainer/HBoxContainer"] +layout_mode = 2 +text = "Health" + +[node name="HealthBar" type="ProgressBar" parent="Man/PanelContainer2/VBoxContainer/HBoxContainer"] +layout_mode = 2 +size_flags_horizontal = 3 +max_value = 354.0 +step = 1.0 +value = 67.0 +rounded = true diff --git a/Scripts/Button.cs b/Scripts/Button.cs index 920dc14..5a916b6 100644 --- a/Scripts/Button.cs +++ b/Scripts/Button.cs @@ -3,9 +3,23 @@ using Godot; namespace Tutorial1.Scripts; public partial class Button : Godot.Button { - public override void _Ready() => Pressed += OnPressed; + private PackedScene packedScene; + public override void _Ready() { + Pressed += OnPressed; + packedScene = GD.Load("res://Character.tscn"); + } - private static void OnPressed() => GD.Print("button pressed!"); + private void OnPressed() { + Node character = packedScene.Instantiate(); + AddChild(character); + + Sprite2D sprite2D = character.GetChild(0); + RandomNumberGenerator random = new(); + random.Randomize(); + sprite2D.GlobalPosition = new(random.RandiRange(100,1000), random.RandiRange(100,1000)); + + GD.Print($"Add new character to position {sprite2D.GlobalPosition}."); + } public override void _Process(double delta) { } } \ No newline at end of file diff --git a/Scripts/Character.cs b/Scripts/Character.cs index 0889d52..329b8c2 100644 --- a/Scripts/Character.cs +++ b/Scripts/Character.cs @@ -2,28 +2,11 @@ using Godot; namespace Tutorial1.Scripts; -public partial class Character : Sprite2D { - // private Timer clock; - - // [Signal] - // public delegate void MovedEventHandler(float x, float y); - - // public override void _Ready() { - // // clock = GetNode("Clock"); - // // clock.WaitTime = 1; - // // clock.Timeout += ClockOnTimeout; - // // clock.Start(); - // // Moved += (x, y) => GD.Print($"Moved by 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); - // } +public partial class Character : Node { + private Sprite2D sprite2D; + public override void _Ready() => sprite2D = GetChild(0); public override void _UnhandledInput(InputEvent @event) { - if (@event is InputEventMouseButton { Pressed: true } buttonEvent) - Position = buttonEvent.GlobalPosition; + if (@event is InputEventMouseButton { Pressed: true } buttonEvent) sprite2D.Position = buttonEvent.GlobalPosition; } } \ No newline at end of file diff --git a/Scripts/World.cs b/Scripts/World.cs index da086be..58d3a8b 100644 --- a/Scripts/World.cs +++ b/Scripts/World.cs @@ -3,15 +3,4 @@ using Godot; namespace Tutorial1.Scripts; public partial class World : Node2D { - // private PackedScene packedScene; - // public override void _Ready() => packedScene = GD.Load("res://Character.tscn"); - // - // public override void _UnhandledInput(InputEvent @event) { - // if (@event is InputEventMouseButton mouseEvent) - // //Create instance of Sprite scene - // if (packedScene.Instantiate() is Sprite2D character) { - // character.Position = mouseEvent.Position; - // AddChild(character); - // } - // } } \ No newline at end of file diff --git a/World.tscn b/World.tscn index 3642f92..d046dcf 100644 --- a/World.tscn +++ b/World.tscn @@ -1,14 +1,16 @@ -[gd_scene load_steps=4 format=3 uid="uid://cefeqjem0f3wo"] +[gd_scene load_steps=5 format=3 uid="uid://cefeqjem0f3wo"] [ext_resource type="Script" path="res://Scripts/World.cs" id="1_4gg20"] -[ext_resource type="PackedScene" uid="uid://bkvgqv8jki0us" path="res://Character.tscn" id="2_5yetv"] +[ext_resource type="Texture2D" uid="uid://dc1t7wtq620d" path="res://Icons/barrels_E.png" id="2_i6vvh"] [ext_resource type="Texture2D" uid="uid://d0ft51rdr6k02" path="res://Icons/Godot.svg" id="3_6nyvq"] +[ext_resource type="Script" path="res://Scripts/Button.cs" id="5_tbcjx"] [node name="World" type="Node2D"] script = ExtResource("1_4gg20") -[node name="Barrels" parent="." instance=ExtResource("2_5yetv")] -position = Vector2(300, 287) +[node name="Barrels" type="Sprite2D" parent="."] +position = Vector2(993, 353) +texture = ExtResource("2_i6vvh") [node name="Control" type="Control" parent="."] layout_mode = 3 @@ -30,11 +32,18 @@ alignment = 1 [node name="HBoxContainer3" type="HBoxContainer" parent="Control/PanelContainer1/VBoxContainer"] layout_mode = 2 size_flags_vertical = 3 +alignment = 1 [node name="TextureRect" type="TextureRect" parent="Control/PanelContainer1/VBoxContainer/HBoxContainer3"] layout_mode = 2 texture = ExtResource("3_6nyvq") +[node name="Button" type="Button" parent="Control/PanelContainer1/VBoxContainer/HBoxContainer3"] +layout_mode = 2 +size_flags_horizontal = 3 +text = "New Character" +script = ExtResource("5_tbcjx") + [node name="GridContainer" type="GridContainer" parent="Control/PanelContainer1/VBoxContainer"] layout_mode = 2 columns = 2 @@ -81,29 +90,3 @@ size_flags_horizontal = 3 size_flags_vertical = 1 size_flags_stretch_ratio = 2.0 value = 33.0 - -[node name="PanelContainer2" type="PanelContainer" parent="Control"] -layout_mode = 0 -offset_left = 10.0 -offset_top = 603.0 -offset_right = 534.0 -offset_bottom = 634.0 - -[node name="VBoxContainer" type="VBoxContainer" parent="Control/PanelContainer2"] -layout_mode = 2 - -[node name="HBoxContainer" type="HBoxContainer" parent="Control/PanelContainer2/VBoxContainer"] -layout_mode = 2 -size_flags_vertical = 3 - -[node name="Label" type="Label" parent="Control/PanelContainer2/VBoxContainer/HBoxContainer"] -layout_mode = 2 -text = "Health" - -[node name="HealthBar" type="ProgressBar" parent="Control/PanelContainer2/VBoxContainer/HBoxContainer"] -layout_mode = 2 -size_flags_horizontal = 3 -max_value = 354.0 -step = 1.0 -value = 67.0 -rounded = true