2022-12-02 21:09:40 +01:00
|
|
|
using Godot;
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using Dictionary = Godot.Collections.Dictionary;
|
|
|
|
using Array = Godot.Collections.Array;
|
|
|
|
|
|
|
|
public class AdaptiveWorldStream : Spatial
|
|
|
|
{
|
|
|
|
// ui elements
|
|
|
|
private Label _tileLabel;
|
|
|
|
private Label _tileOffsetLabel;
|
|
|
|
private Label _numTilesLabel;
|
|
|
|
private Label _mouseWorldLabel;
|
|
|
|
private Label _mouseTileLabel;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Resources
|
|
|
|
private PackedScene _tileHighlightScene;
|
|
|
|
|
|
|
|
// other members
|
|
|
|
private HexGrid _hexGrid;
|
|
|
|
private HexCell _lastTile;
|
|
|
|
private HexCell _currentTile;
|
|
|
|
private Vector2 _currentTileOffset;
|
|
|
|
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
|
|
public override void _Ready()
|
|
|
|
{
|
|
|
|
// UI elements
|
|
|
|
_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");
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
|
|
|
|
// resources
|
|
|
|
_tileHighlightScene = GD.Load<PackedScene>("utils/TileHighlight.tscn");
|
|
|
|
|
|
|
|
// other members
|
|
|
|
_lastTile = new HexCell();
|
|
|
|
_currentTile = new HexCell();
|
|
|
|
_currentTileOffset = new Vector2();
|
|
|
|
_hexGrid = new HexGrid();
|
|
|
|
|
|
|
|
// connect signals
|
|
|
|
var result = _streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent));
|
|
|
|
|
2022-12-03 00:43:19 +01:00
|
|
|
// CreateTileGrid();
|
|
|
|
|
|
|
|
//playerTransform.origin += new Vector3(0, 0, -1) * delta;
|
|
|
|
Transform playerTransform = _player.Transform;
|
|
|
|
playerTransform.origin.x = 3;
|
|
|
|
_player.Transform = playerTransform;
|
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
|
|
|
|
|
|
|
public void ClearStreamActiveTiles()
|
|
|
|
{
|
|
|
|
foreach (Node child in _streamContainerActiveTiles.GetChildren())
|
|
|
|
{
|
|
|
|
child.QueueFree();
|
|
|
|
_streamContainerActiveTiles.RemoveChild(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void CreateStreamActiveTiles()
|
|
|
|
{
|
|
|
|
foreach (int coord_x in Enumerable.Range((int)_streamContainer.OffsetCoordRect.Position.x, (int)_streamContainer.OffsetCoordRect.Size.x))
|
|
|
|
{
|
|
|
|
foreach (int coord_y in Enumerable.Range((int)_streamContainer.OffsetCoordRect.Position.y,
|
|
|
|
(int)_streamContainer.OffsetCoordRect.Size.y))
|
|
|
|
{
|
|
|
|
HexCell cell = new HexCell();
|
|
|
|
cell.OffsetCoords = new Vector2(coord_x, coord_y);
|
|
|
|
Vector2 cellWorldCenter = _hexGrid.GetHexCenter(cell);
|
|
|
|
|
|
|
|
Spatial highlightTile = (Spatial)_tileHighlightScene.Instance();
|
|
|
|
Transform highlightTileTransform = Transform.Identity;
|
|
|
|
highlightTileTransform.origin.x = cellWorldCenter.x;
|
|
|
|
highlightTileTransform.origin.z = cellWorldCenter.y;
|
|
|
|
highlightTile.Transform = highlightTileTransform;
|
|
|
|
|
|
|
|
_streamContainerActiveTiles.AddChild(highlightTile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 21:09:40 +01:00
|
|
|
public override void _Process(float delta)
|
|
|
|
{
|
|
|
|
_lastTile = _currentTile;
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2022-12-03 00:43:19 +01:00
|
|
|
playerTransform.origin += new Vector3(-0.2f, 0, 1) * delta;
|
2022-12-02 21:09:40 +01:00
|
|
|
_player.Transform = playerTransform;
|
|
|
|
|
|
|
|
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);
|
|
|
|
ClearStreamActiveTiles();
|
|
|
|
CreateStreamActiveTiles();
|
|
|
|
|
|
|
|
_numTilesLabel.Text = _streamContainerActiveTiles.GetChildCount().ToString();
|
|
|
|
}
|
2022-12-02 21:09:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
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 00:43:19 +01:00
|
|
|
_player.Transform = highlightTransform;
|
2022-12-02 21:09:40 +01:00
|
|
|
}
|
|
|
|
}
|