GodotComponentTest/scenes/Game.cs

229 lines
8.6 KiB
C#
Raw Normal View History

2022-12-02 21:09:40 +01:00
using Godot;
using System.Diagnostics;
2022-12-02 21:09:40 +01:00
using System.Linq;
using Array = Godot.Collections.Array;
public class Game : Spatial
2022-12-02 21:09:40 +01:00
{
// ui elements
2022-12-03 20:34:01 +01:00
private Label _framesPerSecondLabel;
2022-12-02 21:09:40 +01:00
private Label _tileLabel;
private Label _tileOffsetLabel;
private Label _numTilesLabel;
private Label _mouseWorldLabel;
private Label _mouseTileLabel;
2022-12-03 20:34:01 +01:00
private Label _numCoordsAddedLabel;
private Label _numCoordsRemovedLabel;
private TextureRect _worldTextureRect;
2022-12-02 21:09:40 +01:00
// scene nodes
private Spatial _tileHighlight;
private Spatial _mouseTileHighlight;
2022-12-03 00:43:19 +01:00
private StreamContainer _streamContainer;
2022-12-02 21:09:40 +01:00
private Area _streamContainerArea;
2022-12-03 00:43:19 +01:00
private Spatial _streamContainerActiveTiles;
2022-12-02 21:09:40 +01:00
private Player _player;
2023-01-04 22:49:00 +01:00
private Chest _chest;
private TileWorld _tileWorld;
private Camera _camera;
2022-12-04 20:51:02 +01:00
2022-12-02 21:09:40 +01:00
// Resources
private PackedScene _tileHighlightScene;
2022-12-04 20:51:02 +01:00
2022-12-02 21:09:40 +01:00
// other members
private HexGrid _hexGrid;
private HexCell _lastTile;
private HexCell _currentTile;
private Vector2 _currentTileOffset;
private Vector3 _cameraOffset;
2022-12-02 21:09:40 +01:00
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
// UI elements
2022-12-03 20:34:01 +01:00
_framesPerSecondLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/fps_label");
2022-12-02 21:09:40 +01:00
_tileLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/tile_label");
_tileOffsetLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/tile_offset_label");
_numTilesLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/num_tiles_label");
_mouseWorldLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/mouse_world_label");
_mouseTileLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/mouse_tile_label");
2022-12-03 20:34:01 +01:00
_numCoordsAddedLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/num_coords_added_label");
_numCoordsRemovedLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/num_coords_removed_label");
_worldTextureRect = GetNode<TextureRect>("Control/WorldTextureRect");
2022-12-02 21:09:40 +01:00
// scene nodes
_tileHighlight = GetNode<Spatial>("TileHighlight");
_mouseTileHighlight = GetNode<Spatial>("MouseTileHighlight");
2022-12-03 00:43:19 +01:00
_streamContainer = GetNode<StreamContainer>("StreamContainer");
2022-12-02 21:09:40 +01:00
_streamContainerArea = GetNode<Area>("StreamContainer/Area");
2022-12-03 00:43:19 +01:00
_streamContainerActiveTiles = GetNode<Spatial>("StreamContainer/ActiveTiles");
2022-12-02 21:09:40 +01:00
_player = GetNode<Player>("Player");
_chest = GetNode<Chest>("Entities/Chest");
_tileWorld = GetNode<TileWorld>("TileWorld");
_camera = GetNode<Camera>("Camera");
_cameraOffset = _camera.GlobalTranslation - _player.GlobalTranslation;
2023-02-12 21:10:28 +01:00
Debug.Assert(_tileWorld != null);
2022-12-02 21:09:40 +01:00
// resources
_tileHighlightScene = GD.Load<PackedScene>("utils/TileHighlight.tscn");
2022-12-04 20:51:02 +01:00
2022-12-02 21:09:40 +01:00
// other members
_lastTile = new HexCell();
_currentTile = new HexCell();
_currentTileOffset = new Vector2();
_hexGrid = new HexGrid();
// update data
_worldTextureRect.RectSize = _tileWorld.Size;
2023-02-12 21:10:28 +01:00
2022-12-02 21:09:40 +01:00
// connect signals
2022-12-04 20:51:02 +01:00
_streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent));
2023-01-04 22:49:00 +01:00
_streamContainer.Connect("TileClicked", this, nameof(OnTileClicked));
_tileWorld.Connect("WorldGenerated", this, nameof(OnWorldGenerated));
2023-02-12 21:10:28 +01:00
2023-02-12 16:44:48 +01:00
// register entity events
Array entityNodes = FindNode("Entities").GetChildren();
foreach (Node node in entityNodes)
{
if (node is Chest)
{
node.Connect("EntityClicked", this, nameof(OnEntityClicked));
}
}
2023-02-12 21:10:28 +01:00
// perform dependency injection
//_streamContainer.SetWorld(_tileWorld);
WorldInfoComponent worldInfoComponent = _player.GetNode<WorldInfoComponent>("WorldInfo");
if (worldInfoComponent != null)
{
worldInfoComponent.SetWorld(_tileWorld);
}
2023-02-12 21:10:28 +01:00
_tileWorld.Generate();
2022-12-04 21:54:44 +01:00
UpdateCurrentTile();
_streamContainer.SetCenterTile(_currentTile);
2022-12-02 21:09:40 +01:00
}
public void CreateTileGrid()
{
foreach (int x_index in Enumerable.Range(-3, 6))
{
foreach (int y_index in Enumerable.Range(0, 1))
{
var tile = (Spatial)_tileHighlightScene.Instance();
HexCell cell = new HexCell();
Vector2 planeCoords = _hexGrid.GetHexCenterFromOffset(new Vector2(x_index, y_index));
Transform tileTransform = Transform.Identity;
tileTransform.origin.x = planeCoords.x;
tileTransform.origin.z = planeCoords.y;
tile.Transform = tileTransform;
AddChild(tile);
GD.Print("Added tile at offset coords " + new Vector2(x_index, y_index).ToString() +
" and world coords " + tileTransform.origin.ToString());
}
}
}
2022-12-03 00:43:19 +01:00
2022-12-04 21:54:44 +01:00
public void UpdateCurrentTile()
2022-12-02 21:09:40 +01:00
{
Transform playerTransform = _player.Transform;
Vector3 playerCoord = playerTransform.origin;
_currentTile = _hexGrid.GetHexAt(new Vector2(playerCoord.x, playerCoord.z));
2022-12-03 00:43:19 +01:00
_tileLabel.Text = playerTransform.ToString();
2022-12-02 21:09:40 +01:00
_tileOffsetLabel.Text = _currentTile.OffsetCoords.ToString();
_player.Transform = playerTransform;
2022-12-04 21:54:44 +01:00
}
2022-12-04 21:54:44 +01:00
public override void _Process(float delta)
{
_framesPerSecondLabel.Text = Engine.GetFramesPerSecond().ToString();
_lastTile = _currentTile;
2022-12-02 21:09:40 +01:00
2022-12-04 21:54:44 +01:00
UpdateCurrentTile();
2022-12-02 21:09:40 +01:00
Transform tileHighlightTransform = Transform.Identity;
Vector2 currentTileCenter = _hexGrid.GetHexCenter(_currentTile);
tileHighlightTransform.origin.x = currentTileCenter.x;
tileHighlightTransform.origin.z = currentTileCenter.y;
_tileHighlight.Transform = tileHighlightTransform;
2022-12-03 00:43:19 +01:00
if (_currentTile.CubeCoords != _lastTile.CubeCoords)
{
_streamContainer.SetCenterTile(_currentTile);
_numTilesLabel.Text = _streamContainerActiveTiles.GetChildCount().ToString();
2022-12-03 20:34:01 +01:00
_numCoordsAddedLabel.Text = _streamContainer.AddedCoords.Count.ToString();
_numCoordsRemovedLabel.Text = _streamContainer.RemovedCoords.Count.ToString();
2022-12-03 00:43:19 +01:00
}
2022-12-02 21:09:40 +01:00
Transform cameraTransform = _camera.Transform;
cameraTransform.origin = _player.GlobalTranslation + _cameraOffset;
_camera.Transform = cameraTransform;
}
public void OnAreaInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal,
int shapeIndex)
2022-12-02 21:09:40 +01:00
{
HexCell cellAtCursor = _hexGrid.GetHexAt(new Vector2(position.x, position.z));
Transform highlightTransform = Transform.Identity;
Vector2 cellPlaneCoords = _hexGrid.GetHexCenter(cellAtCursor);
cellPlaneCoords = _hexGrid.GetHexCenterFromOffset(cellAtCursor.OffsetCoords);
highlightTransform.origin.x = cellPlaneCoords.x;
highlightTransform.origin.z = cellPlaneCoords.y;
_mouseWorldLabel.Text = position.ToString();
_mouseTileLabel.Text = cellAtCursor.OffsetCoords.ToString();
_mouseTileHighlight.Transform = highlightTransform;
2022-12-03 20:34:01 +01:00
2023-02-12 21:10:28 +01:00
if (inputEvent is InputEventMouseButton && ((InputEventMouseButton)inputEvent).Pressed)
2022-12-03 20:34:01 +01:00
{
2023-01-04 22:49:00 +01:00
_streamContainer.EmitSignal("TileClicked", _streamContainer.GetTile3dAt(cellAtCursor.OffsetCoords));
2022-12-04 20:51:02 +01:00
}
}
2023-01-04 22:49:00 +01:00
public void OnTileClicked(HexTile3D tile)
2022-12-04 20:51:02 +01:00
{
if (_player == null)
{
return;
2022-12-03 20:34:01 +01:00
}
2022-12-04 20:51:02 +01:00
2023-02-12 21:10:28 +01:00
_player.TaskQueueComponent.Reset();
_player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.NavigationTask(
new NavigationComponent.NavigationPoint(tile.GlobalTranslation)));
2023-01-04 22:49:00 +01:00
}
public void OnEntityClicked(Entity entity)
{
GD.Print("Clicked on entity at " + entity.GlobalTranslation);
Spatial mountPoint = (Spatial)entity.FindNode("MountPoint");
if (mountPoint != null)
{
2023-02-12 21:10:28 +01:00
_player.TaskQueueComponent.Reset();
_player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.NavigationTask(
new NavigationComponent.NavigationPoint(mountPoint.GlobalTransform)));
_player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.InteractionTask(entity));
}
2022-12-02 21:09:40 +01:00
}
public void OnWorldGenerated()
{
GD.Print("Using new map");
ImageTexture new_world_texture = new ImageTexture();
2023-02-12 21:10:28 +01:00
_tileWorld.Heightmap.Unlock();
new_world_texture.CreateFromImage(_tileWorld.Heightmap);
_tileWorld.Heightmap.Lock();
_worldTextureRect.Texture = new_world_texture;
}
2022-12-02 21:09:40 +01:00
}