extract build mode to class #5

This commit is contained in:
Cat Flynn 2022-12-08 21:38:05 +00:00
parent 503caa4430
commit 3b96a58dab
8 changed files with 144 additions and 99 deletions

View File

@ -1,11 +1,12 @@
[gd_scene load_steps=8 format=2] [gd_scene load_steps=9 format=2]
[ext_resource path="res://nodes/grid.tscn" type="PackedScene" id=1] [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/grid_cursor.tscn" type="PackedScene" id=2]
[ext_resource path="res://nodes/ui/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/clock.tscn" type="PackedScene" id=4]
[ext_resource path="res://nodes/interaction_mode.tscn" type="PackedScene" id=5] [ext_resource path="res://nodes/interaction_modes/interaction_mode.tscn" type="PackedScene" id=5]
[ext_resource path="res://nodes/ui/build_mode_ui.tscn" type="PackedScene" id=6] [ext_resource path="res://nodes/ui/build_mode_ui.tscn" type="PackedScene" id=6]
[ext_resource path="res://nodes/interaction_modes/build_mode.tscn" type="PackedScene" id=7]
[sub_resource type="Curve" id=1] [sub_resource type="Curve" id=1]
_data = [ Vector2( 0, 0 ), 0.0, 5.0, 0, 0, Vector2( 0.5, 1 ), 0.0, 0.0, 0, 0, Vector2( 1, 0 ), -4.0, 0.0, 0, 0 ] _data = [ Vector2( 0, 0 ), 0.0, 5.0, 0, 0, Vector2( 0.5, 1 ), 0.0, 0.0, 0, 0, Vector2( 1, 0 ), -4.0, 0.0, 0, 0 ]
@ -23,6 +24,10 @@ Grid = NodePath("../Grid")
_pulseShape = SubResource( 1 ) _pulseShape = SubResource( 1 )
[node name="Interaction Mode" parent="." instance=ExtResource( 5 )] [node name="Interaction Mode" parent="." instance=ExtResource( 5 )]
_buildModePath = NodePath("../Build Mode")
[node name="Build Mode" parent="." instance=ExtResource( 7 )]
_key = 66
[node name="UI" type="Control" parent="."] [node name="UI" type="Control" parent="."]
anchor_right = 1.0 anchor_right = 1.0
@ -31,11 +36,13 @@ anchor_bottom = 1.0
[node name="Debug" parent="UI" instance=ExtResource( 3 )] [node name="Debug" parent="UI" instance=ExtResource( 3 )]
[node name="Build Mode" parent="UI" instance=ExtResource( 6 )] [node name="Build Mode" parent="UI" instance=ExtResource( 6 )]
_buildModePath = NodePath("../../Build Mode")
[connection signal="OnPauseChanged" from="Clock" to="UI/Debug" method="_on_Clock_OnPauseChanged"] [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="Grid" method="_on_Clock_OnTick"]
[connection signal="OnTick" from="Clock" to="UI/Debug" method="_on_Clock_OnTick"] [connection signal="OnTick" from="Clock" to="UI/Debug" method="_on_Clock_OnTick"]
[connection signal="OnInteractionModeChanged" from="Interaction Mode" to="Cursor" method="_on_Interaction_Mode_OnInteractionModeChanged"] [connection signal="OnInteractionModeChanged" from="Interaction Mode" to="Cursor" method="_on_Interaction_Mode_OnInteractionModeChanged"]
[connection signal="OnInteractionModeChanged" from="Interaction Mode" to="UI/Debug" method="_on_Interaction_Mode_OnInteractionModeChanged"] [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="OnModeEntered" from="Build Mode" to="Interaction Mode" method="_on_Build_Mode_OnModeEntered"]
[connection signal="OnModeExited" from="Build Mode" to="Interaction Mode" method="_on_Build_Mode_OnModeExited"]
[connection signal="OnExit" from="UI/Build Mode" to="Interaction Mode" method="Reset"] [connection signal="OnExit" from="UI/Build Mode" to="Interaction Mode" method="Reset"]

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://scripts/interaction_modes/BuildMode.cs" type="Script" id=1]
[node name="Build Mode" type="Node"]
script = ExtResource( 1 )

View File

@ -1,7 +1,6 @@
[gd_scene load_steps=2 format=2] [gd_scene load_steps=2 format=2]
[ext_resource path="res://scripts/InteractionMode.cs" type="Script" id=1] [ext_resource path="res://scripts/interaction_modes/InteractionMode.cs" type="Script" id=1]
[node name="Interaction Mode" type="Node"] [node name="Interaction Mode" type="Node"]
script = ExtResource( 1 ) script = ExtResource( 1 )
_buildMode = 66

View File

@ -74,7 +74,6 @@ public class GridCursor : Sprite
_material.SetShaderParam(T, a); _material.SetShaderParam(T, a);
} }
public void _on_Interaction_Mode_OnInteractionModeChanged(InteractionMode.Mode oldMode, InteractionMode.Mode newMode) public void _on_Interaction_Mode_OnInteractionModeChanged(InteractionMode.Mode oldMode, InteractionMode.Mode newMode)
{ {
switch (newMode) switch (newMode)

View File

@ -1,63 +0,0 @@
using Godot;
using System;
public class InteractionMode : Node
{
[Export]
private KeyList _buildMode;
[Signal]
delegate void OnInteractionModeChanged(Mode oldMode, Mode newMode);
public enum Mode
{
SELECT = 0,
BUILD = 1
}
public void Reset()
{
Current = Mode.SELECT;
}
public Mode Current
{
get => _current;
set
{
if (_current == value)
return;
var old = _current;
_current = value;
EmitSignal(nameof(OnInteractionModeChanged), old, _current);
}
}
private Mode _current = Mode.SELECT;
public override void _Input(InputEvent @event)
{
base._Input(@event);
if (!(@event is InputEventKey keyEvent))
return;
if (!keyEvent.Pressed)
return;
var key = (KeyList)keyEvent.Scancode;
// TODO: this should be developed into a UI stack
if (key == KeyList.Escape)
{
Current = Mode.SELECT;
return;
}
if (key == _buildMode)
{
Current = Mode.BUILD;
}
}
}

View File

@ -0,0 +1,66 @@
using Godot;
using System;
public class BuildMode : Node
{
[Signal]
delegate void OnModeEntered();
[Signal]
delegate void OnModeExited();
[Export]
private KeyList _key;
public bool Active
{
get => _active;
private set
{
if (value == _active)
return;
_active = value;
var signal = _active
? nameof(OnModeEntered)
: nameof(OnModeExited);
EmitSignal(signal);
}
}
private bool _active;
private const int NO_TILE_TYPE_SELECTED = -1;
public int SelectedTileType { get; set; } = NO_TILE_TYPE_SELECTED;
public override void _Ready()
{
}
public override void _Input(InputEvent @event)
{
base._Input(@event);
if (!(@event is InputEventKey keyEvent))
return;
if (!keyEvent.Pressed)
return;
var key = (KeyList)keyEvent.Scancode;
// TODO: this should be developed into a UI stack
if (key == KeyList.Escape)
{
Active = false;
return;
}
if (key == _key)
{
Active = true;
return;
}
}
}

View File

@ -0,0 +1,54 @@
using Godot;
using System;
public class InteractionMode : Node
{
[Export]
private NodePath _buildModePath;
private BuildMode _buildMode;
[Signal]
delegate void OnInteractionModeChanged(Mode oldMode, Mode newMode);
public enum Mode
{
SELECT = 0,
BUILD = 1
}
public Mode Current { get; private set; }
public void _on_Build_Mode_OnModeEntered()
{
ChangeMode(Mode.BUILD);
}
public void _on_Build_Mode_OnModeExited()
{
ChangeMode(Mode.SELECT);
}
private void ChangeMode(Mode mode)
{
if (Current == mode)
return;
var old = Current;
Current = mode;
EmitSignal(nameof(OnInteractionModeChanged), old, mode);
}
public override void _Ready()
{
base._Ready();
_buildMode = GetNode<BuildMode>(_buildModePath);
Reset();
}
public void Reset()
{
Current = Mode.SELECT;
}
}

View File

@ -6,13 +6,14 @@ public class BuildModeUI : Control
[Signal] [Signal]
delegate void OnExit(); delegate void OnExit();
// Declare member variables here. Examples: [Export]
// private int a = 2; private NodePath _buildModePath;
// private string b = "text"; private BuildMode _buildMode;
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
{ {
_buildMode = GetNode<BuildMode>(_buildModePath);
Visible = false; Visible = false;
} }
@ -21,39 +22,15 @@ public class BuildModeUI : Control
Exit(); 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() private void Exit()
{ {
EmitSignal(nameof(OnExit)); EmitSignal(nameof(OnExit));
} }
public override void _Input(InputEvent @event) public override void _Process(float delta)
{ {
if (!Visible) base._Process(delta);
return;
if (!(@event is InputEventKey keyEvent)) Visible = _buildMode.Active;
return;
if (keyEvent.Pressed)
return;
if ((KeyList)keyEvent.Scancode == KeyList.Escape)
{
Exit();
}
} }
} }