GodotComponentTest/scenes/Game.cs

259 lines
9.9 KiB
C#

using Godot;
using System.Diagnostics;
using System.Linq;
using Array = Godot.Collections.Array;
public class Game : Spatial
{
// ui elements
private Label _framesPerSecondLabel;
private Label _tileLabel;
private Label _tileOffsetLabel;
private Label _numTilesLabel;
private Label _mouseWorldLabel;
private Label _mouseTileLabel;
private Label _numCoordsAddedLabel;
private Label _numCoordsRemovedLabel;
private TextureRect _worldTextureRect;
private Button _generateWorldButton;
// scene nodes
private Spatial _tileHighlight;
private Spatial _mouseTileHighlight;
private StreamContainer _streamContainer;
private Area _streamContainerArea;
private Spatial _streamContainerActiveTiles;
private Player _player;
private Chest _chest;
private TileWorld _tileWorld;
private Camera _camera;
// Resources
private PackedScene _tileHighlightScene;
private ShaderMaterial _tileMaterial;
// other members
private HexGrid _hexGrid;
private HexCell _lastTile;
private HexCell _currentTile;
private Vector2 _currentTileOffset;
private Vector3 _cameraOffset;
private ImageTexture _blackWhitePatternTexture;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
// UI elements
_framesPerSecondLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/fps_label");
_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");
_numCoordsAddedLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/num_coords_added_label");
_numCoordsRemovedLabel = GetNode<Label>("Control/HBoxContainer/GridContainer/num_coords_removed_label");
_worldTextureRect = (TextureRect)FindNode("WorldTextureRect");
_generateWorldButton = (Button)FindNode("WorldGenerateButton");
// scene nodes
_tileHighlight = GetNode<Spatial>("TileHighlight");
_mouseTileHighlight = GetNode<Spatial>("MouseTileHighlight");
_streamContainer = GetNode<StreamContainer>("StreamContainer");
_streamContainerArea = GetNode<Area>("StreamContainer/Area");
_streamContainerActiveTiles = GetNode<Spatial>("StreamContainer/ActiveTiles");
_player = GetNode<Player>("Player");
_chest = GetNode<Chest>("Entities/Chest");
_tileWorld = GetNode<TileWorld>("TileWorld");
_camera = GetNode<Camera>("Camera");
_cameraOffset = _camera.GlobalTranslation - _player.GlobalTranslation;
Debug.Assert(_tileWorld != null);
// resources
_tileHighlightScene = GD.Load<PackedScene>("utils/TileHighlight.tscn");
_tileMaterial = GD.Load<ShaderMaterial>("materials/HexTileTextureLookup.tres");
Debug.Assert(_tileMaterial != null);
_blackWhitePatternTexture = new ImageTexture();
Image image = new Image();
image.Load("assets/4x4checker.png");
_blackWhitePatternTexture.CreateFromImage(image, (uint) (Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
// other members
_lastTile = new HexCell();
_currentTile = new HexCell();
_currentTileOffset = new Vector2();
_hexGrid = new HexGrid();
// update data
_worldTextureRect.RectSize = _tileWorld.Size;
// connect signals
_streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent));
_streamContainer.Connect("TileClicked", this, nameof(OnTileClicked));
_streamContainer.Connect("TileHovered", this, nameof(OnTileHovered));
_tileWorld.Connect("WorldGenerated", this, nameof(OnWorldGenerated));
_generateWorldButton.Connect("pressed", this, nameof(OnGenerateButton));
// register entity events
Array entityNodes = FindNode("Entities").GetChildren();
foreach (Node node in entityNodes)
{
if (node is Chest)
{
node.Connect("EntityClicked", this, nameof(OnEntityClicked));
}
}
// perform dependency injection
//_streamContainer.SetWorld(_tileWorld);Clicked
WorldInfoComponent worldInfoComponent = _player.GetNode<WorldInfoComponent>("WorldInfo");
if (worldInfoComponent != null)
{
worldInfoComponent.SetWorld(_tileWorld);
}
_tileWorld.Generate();
UpdateCurrentTile();
_streamContainer.SetCenterTile(_currentTile);
}
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());
}
}
}
public void UpdateCurrentTile()
{
Transform playerTransform = _player.Transform;
Vector3 playerCoord = playerTransform.origin;
_currentTile = _hexGrid.GetHexAt(new Vector2(playerCoord.x, playerCoord.z));
_tileLabel.Text = playerTransform.ToString();
_tileOffsetLabel.Text = _currentTile.OffsetCoords.ToString();
_player.Transform = playerTransform;
}
public override void _Process(float delta)
{
_framesPerSecondLabel.Text = Engine.GetFramesPerSecond().ToString();
_lastTile = _currentTile;
UpdateCurrentTile();
Transform tileHighlightTransform = Transform.Identity;
Vector2 currentTileCenter = _hexGrid.GetHexCenter(_currentTile);
tileHighlightTransform.origin.x = currentTileCenter.x;
tileHighlightTransform.origin.z = currentTileCenter.y;
_tileHighlight.Transform = tileHighlightTransform;
if (_currentTile.CubeCoords != _lastTile.CubeCoords)
{
_streamContainer.SetCenterTile(_currentTile);
_numTilesLabel.Text = _streamContainerActiveTiles.GetChildCount().ToString();
_numCoordsAddedLabel.Text = _streamContainer.AddedCoords.Count.ToString();
_numCoordsRemovedLabel.Text = _streamContainer.RemovedCoords.Count.ToString();
}
Transform cameraTransform = _camera.Transform;
cameraTransform.origin = _player.GlobalTranslation + _cameraOffset;
_camera.Transform = cameraTransform;
}
public void OnGenerateButton()
{
GD.Print("Generating");
_tileWorld.Seed = _tileWorld.Seed + 1;
_tileWorld.Generate();
}
public void OnAreaInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal,
int shapeIndex)
{
HexCell cellAtCursor = _hexGrid.GetHexAt(new Vector2(position.x, position.z));
Transform highlightTransform = Transform.Identity;
_mouseWorldLabel.Text = position.ToString();
_mouseTileLabel.Text = cellAtCursor.OffsetCoords.ToString();
_mouseTileHighlight.Transform = highlightTransform;
if (inputEvent is InputEventMouseButton && ((InputEventMouseButton)inputEvent).Pressed)
{
_streamContainer.EmitSignal("TileClicked", _streamContainer.GetTile3dAt(cellAtCursor.OffsetCoords));
}
}
public void OnTileClicked(HexTile3D tile)
{
if (_player == null)
{
return;
}
_player.TaskQueueComponent.Reset();
_player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.NavigationTask(
new NavigationComponent.NavigationPoint(tile.GlobalTranslation)));
}
public void OnTileHovered(HexTile3D tile)
{
Transform highlightTransform = _tileHighlight.GlobalTransform;
highlightTransform.origin.x = tile.GlobalTranslation.x;
highlightTransform.origin.z = tile.GlobalTranslation.z;
highlightTransform.origin.y = 0.1f;
_mouseTileHighlight.Transform = highlightTransform;
_mouseWorldLabel.Text = tile.GlobalTranslation.ToString();
_mouseTileLabel.Text = tile.OffsetCoords.ToString();
}
public void OnEntityClicked(Entity entity)
{
GD.Print("Clicked on entity at " + entity.GlobalTranslation);
Spatial mountPoint = (Spatial)entity.FindNode("MountPoint");
if (mountPoint != null)
{
_player.TaskQueueComponent.Reset();
_player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.NavigationTask(
new NavigationComponent.NavigationPoint(mountPoint.GlobalTransform)));
_player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.InteractionTask(entity));
}
}
public void OnWorldGenerated()
{
GD.Print("Using new map");
ImageTexture new_world_texture = new ImageTexture();
new_world_texture.CreateFromImage(_tileWorld.Colormap, (uint) (Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_worldTextureRect.Texture = new_world_texture;
_tileMaterial.SetShaderParam("MapAlbedoTexture", new_world_texture);
_tileMaterial.SetShaderParam("TextureSize", (int)_tileWorld.Colormap.GetSize().x);
_streamContainer.OnWorldGenerated();
//_streamContainer.SetTileMaterial(_tileMaterial);
}
}