GodotComponentTest/scenes/tests/EditorUI.cs

123 lines
3.4 KiB
C#

using Godot;
using System;
using Array = Godot.Collections.Array;
public class EditorUI : Control
{
// exported members
[Export] public NodePath World;
[Export] public NodePath StreamContainer;
// public members
public Vector2 currentTileOffset = Vector2.Zero;
// private members
private Button _resetButton;
private Button _grassButton;
private Button _sandButton;
private Button _waterButton;
private CheckBox _gameGeometryCheckBox;
private CheckBox _physicsGeometryCheckBox;
private TileWorld _tileWorld;
private StreamContainer _streamContainer;
private enum TileType
{
None,
Grass,
Sand,
Water
}
private TileType _currentTileType = TileType.None;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_tileWorld = (TileWorld) GetNode(World);
_streamContainer = (StreamContainer)GetNode(StreamContainer);
// signals
_resetButton = (Button) FindNode("ResetButton");
_resetButton.Connect("pressed", this, nameof(OnResetButton));
_grassButton = (Button) FindNode("GrassButton");
_grassButton.Connect("pressed", this, nameof(OnGrassButton));
_sandButton = (Button) FindNode("SandButton");
_sandButton.Connect("pressed", this, nameof(OnSandButton));
_waterButton = (Button) FindNode("WaterButton");
_waterButton.Connect("pressed", this, nameof(OnWaterButton));
_gameGeometryCheckBox = (CheckBox)FindNode("GameGeometryCheckBox");
_gameGeometryCheckBox.Connect("toggled", this, nameof(OnGameGeometryCheckBoxToggled));
_physicsGeometryCheckBox = (CheckBox)FindNode("PhysicsGeometryCheckBox");
_physicsGeometryCheckBox.Connect("toggled", this, nameof(OnPhysicsGeometryCheckBoxToggled));
}
public void OnResetButton()
{
GD.Print("Resetting Map");
_tileWorld.Seed = _tileWorld.Seed + 1;
_tileWorld.Generate(12);
}
public void OnGrassButton()
{
_currentTileType = TileType.Grass;
}
public void OnSandButton()
{
_currentTileType = TileType.Sand;
}
public void OnWaterButton()
{
_currentTileType = TileType.Water;
}
public void OnGameGeometryCheckBoxToggled(bool pressed)
{
Array gameGeometries = GetTree().GetNodesInGroup("GameGeometry");
foreach (Spatial mesh in gameGeometries)
{
if (mesh != null)
{
mesh.Visible = pressed;
}
}
}
public void OnPhysicsGeometryCheckBoxToggled(bool pressed)
{
Array physicsGeometries = GetTree().GetNodesInGroup("PhysicsGeometry");
foreach (Spatial mesh in physicsGeometries)
{
if (mesh != null)
{
mesh.Visible = pressed;
}
}
}
public void OnTileClicked(Vector2 offsetCoord)
{
switch (_currentTileType)
{
case TileType.Grass:_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Green);
break;
case TileType.Water:_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Blue);
break;
case TileType.Sand:_tileWorld.SetTileColorAtOffset(currentTileOffset, Colors.Yellow);
break;
}
}
}