From 68dd73fb956fd3f3a498edb5623806d83568014c Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Thu, 8 Dec 2022 20:53:49 +0000 Subject: [PATCH] enter and exit build mode ui #5 --- half-earth/nodes/game.tscn | 23 +++++++--- half-earth/nodes/ui/build_mode_ui.tscn | 45 +++++++++++++++++++ half-earth/nodes/{ => ui}/debug_ui.tscn | 31 ++++++------- half-earth/scripts/InteractionMode.cs | 5 +++ half-earth/scripts/WorldGrid.cs | 3 ++ half-earth/scripts/ui/BuildModeUI.cs | 59 +++++++++++++++++++++++++ half-earth/scripts/{ => ui}/DebugUI.cs | 0 7 files changed, 141 insertions(+), 25 deletions(-) create mode 100644 half-earth/nodes/ui/build_mode_ui.tscn rename half-earth/nodes/{ => ui}/debug_ui.tscn (65%) create mode 100644 half-earth/scripts/ui/BuildModeUI.cs rename half-earth/scripts/{ => ui}/DebugUI.cs (100%) diff --git a/half-earth/nodes/game.tscn b/half-earth/nodes/game.tscn index 47ea5a6..5515fad 100644 --- a/half-earth/nodes/game.tscn +++ b/half-earth/nodes/game.tscn @@ -1,10 +1,11 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://nodes/grid.tscn" type="PackedScene" id=1] [ext_resource path="res://nodes/grid_cursor.tscn" type="PackedScene" id=2] -[ext_resource path="res://nodes/debug_ui.tscn" type="PackedScene" id=3] +[ext_resource path="res://nodes/ui/debug_ui.tscn" type="PackedScene" id=3] [ext_resource path="res://nodes/clock.tscn" type="PackedScene" id=4] [ext_resource path="res://nodes/interaction_mode.tscn" type="PackedScene" id=5] +[ext_resource path="res://nodes/ui/build_mode_ui.tscn" type="PackedScene" id=6] [node name="Root" type="Node"] @@ -14,14 +15,22 @@ DiffusionCoefficient = 0.1 [node name="Cursor" parent="." instance=ExtResource( 2 )] Grid = NodePath("../Grid") -[node name="Debug UI" parent="." instance=ExtResource( 3 )] - [node name="Clock" parent="." instance=ExtResource( 4 )] _autoTick = true [node name="Interaction Mode" parent="." instance=ExtResource( 5 )] -[connection signal="OnPauseChanged" from="Clock" to="Debug UI" method="_on_Clock_OnPauseChanged"] +[node name="UI" type="Control" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 + +[node name="Debug" parent="UI" instance=ExtResource( 3 )] + +[node name="Build Mode" parent="UI" instance=ExtResource( 6 )] + +[connection signal="OnPauseChanged" from="Clock" to="UI/Debug" method="_on_Clock_OnPauseChanged"] [connection signal="OnTick" from="Clock" to="Grid" method="_on_Clock_OnTick"] -[connection signal="OnTick" from="Clock" to="Debug UI" method="_on_Clock_OnTick"] -[connection signal="OnInteractionModeChanged" from="Interaction Mode" to="Debug UI" method="_on_Interaction_Mode_OnInteractionModeChanged"] +[connection signal="OnTick" from="Clock" to="UI/Debug" method="_on_Clock_OnTick"] +[connection signal="OnInteractionModeChanged" from="Interaction Mode" to="UI/Debug" method="_on_Interaction_Mode_OnInteractionModeChanged"] +[connection signal="OnInteractionModeChanged" from="Interaction Mode" to="UI/Build Mode" method="_on_Interaction_Mode_OnInteractionModeChanged"] +[connection signal="OnExit" from="UI/Build Mode" to="Interaction Mode" method="Reset"] diff --git a/half-earth/nodes/ui/build_mode_ui.tscn b/half-earth/nodes/ui/build_mode_ui.tscn new file mode 100644 index 0000000..9e0be0e --- /dev/null +++ b/half-earth/nodes/ui/build_mode_ui.tscn @@ -0,0 +1,45 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://scripts/ui/BuildModeUI.cs" type="Script" id=1] + +[node name="Build Mode" type="Control"] +anchor_right = 1.0 +anchor_bottom = 1.0 +script = ExtResource( 1 ) + +[node name="ColorRect" type="ColorRect" parent="."] +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_top = -40.0 +grow_horizontal = 0 +grow_vertical = 0 + +[node name="Exit" type="Button" parent="ColorRect"] +margin_left = 2.0 +margin_top = 2.0 +margin_right = 38.0 +margin_bottom = 38.0 + +[node name="Label" type="Label" parent="ColorRect/Exit"] +anchor_right = 1.0 +anchor_bottom = 1.0 +text = "exit" +align = 1 +valign = 1 + +[node name="Exit2" type="Button" parent="ColorRect"] +margin_left = 54.0 +margin_top = 2.0 +margin_right = 90.0 +margin_bottom = 38.0 + +[node name="Label" type="Label" parent="ColorRect/Exit2"] +anchor_right = 1.0 +anchor_bottom = 1.0 +text = "developed" +align = 1 +valign = 1 +autowrap = true + +[connection signal="pressed" from="ColorRect/Exit" to="." method="_on_Exit_pressed"] diff --git a/half-earth/nodes/debug_ui.tscn b/half-earth/nodes/ui/debug_ui.tscn similarity index 65% rename from half-earth/nodes/debug_ui.tscn rename to half-earth/nodes/ui/debug_ui.tscn index 95d6ea2..a880e13 100644 --- a/half-earth/nodes/debug_ui.tscn +++ b/half-earth/nodes/ui/debug_ui.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=2] -[ext_resource path="res://scripts/DebugUI.cs" type="Script" id=1] +[ext_resource path="res://scripts/ui/DebugUI.cs" type="Script" id=1] [node name="Debug UI" type="Control"] anchor_right = 1.0 @@ -12,41 +12,36 @@ InteractionModeLabelPath = NodePath("Interaction Mode") [node name="Interaction Mode" type="Label" parent="."] anchor_left = 1.0 -anchor_top = 1.0 anchor_right = 1.0 -anchor_bottom = 1.0 -margin_left = -94.0 -margin_top = -64.0 -margin_right = -10.0 -margin_bottom = -50.0 +margin_left = -102.0 +margin_top = 15.0 +margin_right = -14.0 +margin_bottom = 29.0 grow_horizontal = 0 grow_vertical = 0 text = "mode: SELECT" align = 2 -valign = 2 [node name="Ticks" type="Label" parent="."] anchor_left = 1.0 -anchor_top = 1.0 anchor_right = 1.0 -anchor_bottom = 1.0 -margin_right = -10.0 -margin_bottom = -30.0 +margin_left = -60.0 +margin_top = 35.0 +margin_right = -14.0 +margin_bottom = 49.0 grow_horizontal = 0 grow_vertical = 0 text = "ticks: 0" align = 2 -valign = 2 [node name="Paused" type="Label" parent="."] anchor_left = 1.0 -anchor_top = 1.0 anchor_right = 1.0 -anchor_bottom = 1.0 -margin_right = -10.0 -margin_bottom = -10.0 +margin_left = -90.0 +margin_top = 55.0 +margin_right = -14.0 +margin_bottom = 69.0 grow_horizontal = 0 grow_vertical = 0 text = "pause: false" align = 2 -valign = 2 diff --git a/half-earth/scripts/InteractionMode.cs b/half-earth/scripts/InteractionMode.cs index 5c6da41..b3a2c4d 100644 --- a/half-earth/scripts/InteractionMode.cs +++ b/half-earth/scripts/InteractionMode.cs @@ -15,6 +15,11 @@ public class InteractionMode : Node BUILD = 1 } + public void Reset() + { + Current = Mode.SELECT; + } + public Mode Current { get => _current; diff --git a/half-earth/scripts/WorldGrid.cs b/half-earth/scripts/WorldGrid.cs index 45e7e2b..68640da 100644 --- a/half-earth/scripts/WorldGrid.cs +++ b/half-earth/scripts/WorldGrid.cs @@ -54,6 +54,9 @@ public class WorldGrid : Node2D public void SetTileValue(int x, int y, float value) { + if (!IsInBounds(x, y)) + return; + _tiles[x, y].value = value; } diff --git a/half-earth/scripts/ui/BuildModeUI.cs b/half-earth/scripts/ui/BuildModeUI.cs new file mode 100644 index 0000000..43d8e1c --- /dev/null +++ b/half-earth/scripts/ui/BuildModeUI.cs @@ -0,0 +1,59 @@ +using Godot; +using System; + +public class BuildModeUI : Control +{ + [Signal] + delegate void OnExit(); + + // Declare member variables here. Examples: + // private int a = 2; + // private string b = "text"; + + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + Visible = false; + } + + public void _on_Exit_pressed() + { + Exit(); + } + + public void _on_Interaction_Mode_OnInteractionModeChanged(InteractionMode.Mode oldMode, InteractionMode.Mode newMode) + { + if (oldMode == InteractionMode.Mode.BUILD) + { + Visible = false; + return; + } + if (newMode == InteractionMode.Mode.BUILD) + { + Visible = true; + return; + } + } + + private void Exit() + { + EmitSignal(nameof(OnExit)); + } + + public override void _Input(InputEvent @event) + { + if (!Visible) + return; + + if (!(@event is InputEventKey keyEvent)) + return; + + if (keyEvent.Pressed) + return; + + if ((KeyList)keyEvent.Scancode == KeyList.Escape) + { + Exit(); + } + } +} diff --git a/half-earth/scripts/DebugUI.cs b/half-earth/scripts/ui/DebugUI.cs similarity index 100% rename from half-earth/scripts/DebugUI.cs rename to half-earth/scripts/ui/DebugUI.cs