Streaming ported to C#.

WorldChunkRefactoring
Martin Felis 2022-12-03 20:34:01 +01:00
parent 239ba3f614
commit 38b5b7f5fb
4 changed files with 218 additions and 97 deletions

View File

@ -7,11 +7,14 @@ using Array = Godot.Collections.Array;
public class AdaptiveWorldStream : 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;
// scene nodes
private Spatial _tileHighlight;
@ -34,11 +37,14 @@ public class AdaptiveWorldStream : Spatial
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");
// scene nodes
_tileHighlight = GetNode<Spatial>("TileHighlight");
@ -89,40 +95,32 @@ public class AdaptiveWorldStream : Spatial
}
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_x in Enumerable.Range((int)_streamContainer.CurrentOffsetCoordRect.Position.x, (int)_streamContainer.CurrentOffsetCoordRect.Size.x))
{
foreach (int coord_y in Enumerable.Range((int)_streamContainer.OffsetCoordRect.Position.y,
(int)_streamContainer.OffsetCoordRect.Size.y))
foreach (int coord_y in Enumerable.Range((int)_streamContainer.CurrentOffsetCoordRect.Position.y,
(int)_streamContainer.CurrentOffsetCoordRect.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);
Spatial hexTile3d = _streamContainer.GetTile3dAt(new Vector2(coord_x, coord_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);
}
}
}
public override void _Process(float delta)
{
_framesPerSecondLabel.Text = Engine.GetFramesPerSecond().ToString();
_lastTile = _currentTile;
Transform playerTransform = _player.Transform;
@ -132,7 +130,7 @@ public class AdaptiveWorldStream : Spatial
_tileLabel.Text = playerTransform.ToString();
_tileOffsetLabel.Text = _currentTile.OffsetCoords.ToString();
playerTransform.origin += new Vector3(-0.2f, 0, 1) * delta;
playerTransform.origin += new Vector3(-0.1f, 0, 1) * delta;
_player.Transform = playerTransform;
Transform tileHighlightTransform = Transform.Identity;
@ -144,10 +142,11 @@ public class AdaptiveWorldStream : Spatial
if (_currentTile.CubeCoords != _lastTile.CubeCoords)
{
_streamContainer.SetCenterTile(_currentTile);
ClearStreamActiveTiles();
CreateStreamActiveTiles();
// CreateStreamActiveTiles();
_numTilesLabel.Text = _streamContainerActiveTiles.GetChildCount().ToString();
_numCoordsAddedLabel.Text = _streamContainer.AddedCoords.Count.ToString();
_numCoordsRemovedLabel.Text = _streamContainer.RemovedCoords.Count.ToString();
}
}
@ -164,6 +163,10 @@ public class AdaptiveWorldStream : Spatial
_mouseTileLabel.Text = cellAtCursor.OffsetCoords.ToString();
_mouseTileHighlight.Transform = highlightTransform;
_player.Transform = highlightTransform;
if (inputEvent is InputEventMouseButton)
{
_player.Transform = highlightTransform;
}
}
}

View File

@ -52,6 +52,10 @@ visible = false
[node name="World" type="Spatial" parent="."]
script = ExtResource( 3 )
[node name="DirectionalLight" type="DirectionalLight" parent="World"]
transform = Transform( 0.328059, -0.878387, 0.347583, 0, 0.367946, 0.929847, -0.944657, -0.305045, 0.120708, 0, 6.59293, 1.20265 )
shadow_enabled = true
[node name="Control" type="Control" parent="."]
margin_right = 40.0
margin_bottom = 40.0
@ -61,97 +65,138 @@ margin_right = 40.0
margin_bottom = 40.0
[node name="GridContainer" type="GridContainer" parent="Control/HBoxContainer"]
margin_right = 109.0
margin_bottom = 104.0
margin_right = 127.0
margin_bottom = 158.0
columns = 2
[node name="Label" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_right = 85.0
[node name="Label9" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_right = 103.0
margin_bottom = 14.0
rect_pivot_offset = Vector2( -335, -33 )
text = "FPS"
[node name="fps_label" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_left = 107.0
margin_right = 127.0
margin_bottom = 14.0
text = "0,0"
[node name="Label" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_top = 18.0
margin_right = 103.0
margin_bottom = 32.0
rect_pivot_offset = Vector2( -335, -33 )
text = "Tile"
[node name="tile_label" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_left = 89.0
margin_right = 109.0
margin_bottom = 14.0
margin_left = 107.0
margin_top = 18.0
margin_right = 127.0
margin_bottom = 32.0
text = "0,0"
[node name="Label2" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_top = 18.0
margin_right = 85.0
margin_bottom = 32.0
margin_top = 36.0
margin_right = 103.0
margin_bottom = 50.0
text = "Tile Offset"
[node name="tile_offset_label" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_left = 89.0
margin_top = 18.0
margin_right = 109.0
margin_bottom = 32.0
margin_left = 107.0
margin_top = 36.0
margin_right = 127.0
margin_bottom = 50.0
text = "0,0"
[node name="Label4" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_top = 36.0
margin_right = 85.0
margin_bottom = 50.0
margin_top = 54.0
margin_right = 103.0
margin_bottom = 68.0
rect_pivot_offset = Vector2( -335, -33 )
text = "Mouse World"
[node name="mouse_world_label" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_left = 89.0
margin_top = 36.0
margin_right = 109.0
margin_bottom = 50.0
margin_left = 107.0
margin_top = 54.0
margin_right = 127.0
margin_bottom = 68.0
text = "0,0"
[node name="Label6" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_top = 54.0
margin_right = 85.0
margin_bottom = 68.0
margin_top = 72.0
margin_right = 103.0
margin_bottom = 86.0
rect_pivot_offset = Vector2( -335, -33 )
text = "Mouse Tile"
[node name="mouse_tile_label" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_left = 89.0
margin_top = 54.0
margin_right = 109.0
margin_bottom = 68.0
margin_left = 107.0
margin_top = 72.0
margin_right = 127.0
margin_bottom = 86.0
text = "0,0"
[node name="Label3" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_top = 72.0
margin_right = 85.0
margin_bottom = 86.0
margin_top = 90.0
margin_right = 103.0
margin_bottom = 104.0
text = "#Tiles"
[node name="num_tiles_label" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_left = 89.0
margin_top = 72.0
margin_right = 109.0
margin_bottom = 86.0
margin_left = 107.0
margin_top = 90.0
margin_right = 127.0
margin_bottom = 104.0
text = "0"
[node name="Label5" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_top = 90.0
margin_right = 85.0
margin_bottom = 104.0
margin_top = 108.0
margin_right = 103.0
margin_bottom = 122.0
text = "#Active"
[node name="num_active_tiles_label" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_left = 89.0
margin_top = 90.0
margin_right = 109.0
margin_bottom = 104.0
margin_left = 107.0
margin_top = 108.0
margin_right = 127.0
margin_bottom = 122.0
text = "0"
[node name="Label7" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_top = 126.0
margin_right = 103.0
margin_bottom = 140.0
text = "#Tiles Added"
[node name="num_coords_added_label" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_left = 107.0
margin_top = 126.0
margin_right = 127.0
margin_bottom = 140.0
text = "0"
[node name="Label8" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_top = 144.0
margin_right = 103.0
margin_bottom = 158.0
text = "#Tiles Removed"
[node name="num_coords_removed_label" type="Label" parent="Control/HBoxContainer/GridContainer"]
margin_left = 107.0
margin_top = 144.0
margin_right = 127.0
margin_bottom = 158.0
text = "0"
[node name="StreamContainer" type="Spatial" parent="."]
script = ExtResource( 4 )
_dimensions = Vector2( 9, 3 )
Dimensions = Vector2( 40, 30 )
[node name="ActiveTiles" type="Spatial" parent="StreamContainer"]
[node name="Bounds" type="MeshInstance" parent="StreamContainer"]
transform = Transform( 4, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0 )
visible = false
mesh = SubResource( 1 )
skeleton = NodePath("../..")
material/0 = SubResource( 2 )
@ -170,7 +215,7 @@ transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5,
shape = SubResource( 7 )
[node name="Camera" type="Camera" parent="Player"]
transform = Transform( 1, 0, 0, 0, 0.511698, 0.859165, 0, -0.859165, 0.511698, -4.76837e-07, 16.8698, 14.3365 )
transform = Transform( 1, 0, 0, 0, 0.511698, 0.859165, 0, -0.859165, 0.511698, -4.76837e-07, 6.61621, 3.80453 )
current = true
fov = 60.0
script = ExtResource( 6 )

View File

@ -15,11 +15,10 @@ height = 1.0
radius = 0.5
[node name="HexTile3D" type="Spatial"]
transform = Transform( -1.62921e-07, 0, 1, 0, 1, 0, -1, 0, -1.62921e-07, 0, 0, 0 )
script = ExtResource( 1 )
[node name="Mesh" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -5, 0 )
transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, -5, 0 )
mesh = SubResource( 6 )
material/0 = ExtResource( 2 )

View File

@ -1,68 +1,142 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
//using Dictionary = Godot.Collections.Dictionary;
public class StreamContainer : Spatial
{
// scene nodes
private MeshInstance _bounds;
private Spatial _activeTiles;
// resources
private PackedScene _hexTileScene = GD.Load<PackedScene>("res://scenes/HexTile3D.tscn");
// exports
[Export] public Vector2 _dimensions = new Vector2(8, 4);
[Export] public Vector2 Dimensions = new Vector2(8, 4);
// other members
private Rect2 _worldRect;
private Rect2 _offsetCoordRect;
private Rect2 _currentOffsetCoordRect;
private Rect2 _oldOffsetCoordRect;
private HexGrid _hexGrid;
public Rect2 OffsetCoordRect
private Dictionary<Vector2, Spatial> _coordToTile = new Dictionary<Vector2, Spatial>();
public List<Vector2> RemovedCoords = new List<Vector2>();
public List<Vector2> AddedCoords = new List<Vector2>();
public Rect2 CurrentOffsetCoordRect
{
get { return _offsetCoordRect; }
get { return _currentOffsetCoordRect; }
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_bounds = GetNode<MeshInstance>("Bounds");
_activeTiles = GetNode<Spatial>("ActiveTiles");
_hexGrid = new HexGrid();
Transform boundsTransform = Transform.Identity;
boundsTransform = boundsTransform.Scaled(new Vector3(_dimensions.x, 1, _dimensions.y));
boundsTransform = boundsTransform.Scaled(new Vector3(Dimensions.x, 1, Dimensions.y));
_bounds.Transform = boundsTransform;
Spatial hexTile3d = (Spatial)_hexTileScene.Instance();
AddChild(hexTile3d);
}
public void UpdateRects(Vector2 centerPlane)
{
Vector2 bottomLeftCoord = centerPlane - new Vector2(_dimensions.x / 2, _dimensions.y / 2);
Vector2 topRightCoord = centerPlane + new Vector2(_dimensions.x / 2, _dimensions.y / 2);
_worldRect = new Rect2(bottomLeftCoord, _dimensions);
GD.Print("World rect now: " + _worldRect.ToString() + " center: " + _worldRect.GetCenter());
_oldOffsetCoordRect = _currentOffsetCoordRect;
Vector2 bottomLeftCoord = centerPlane - new Vector2(Dimensions.x / 2, Dimensions.y / 2);
Vector2 topRightCoord = centerPlane + new Vector2(Dimensions.x / 2, Dimensions.y / 2);
_worldRect = new Rect2(bottomLeftCoord, Dimensions);
// GD.Print("World rect now: " + _worldRect.ToString() + " center: " + _worldRect.GetCenter());
// y axis needs to be inverted as HexGrid's offset coordinates use the opposite axis direction
HexCell bottomLeftCell = _hexGrid.GetHexAt(new Vector2(bottomLeftCoord.x, topRightCoord.y));
HexCell topRightCell = _hexGrid.GetHexAt(new Vector2(topRightCoord.x, bottomLeftCoord.y));
_offsetCoordRect = new Rect2(bottomLeftCell.OffsetCoords,
_currentOffsetCoordRect = new Rect2(bottomLeftCell.OffsetCoords,
topRightCell.OffsetCoords - bottomLeftCell.OffsetCoords + Vector2.One);
GD.Print("Offset rect now: " + _offsetCoordRect.ToString() + " center: " + _offsetCoordRect.GetCenter());
// GD.Print("Offset rect now: " + _currentOffsetCoordRect.ToString() + " center: " +
// _currentOffsetCoordRect.GetCenter());
Transform boundsTransform = _bounds.Transform;
boundsTransform.origin.x = centerPlane.x;
boundsTransform.origin.z = centerPlane.y;
_bounds.Transform = boundsTransform;
GD.Print("Bounds Transform: " + boundsTransform.ToString());
GD.Print("Bounds Global : " + _bounds.GlobalTransform.ToString());
// GD.Print("Bounds Transform: " + boundsTransform.ToString());
// GD.Print("Bounds Global : " + _bounds.GlobalTransform.ToString());
if (!_currentOffsetCoordRect.Equals(_oldOffsetCoordRect))
{
UpdateTileCache();
}
}
public void UpdateTileCache()
{
RemovedCoords.Clear();
AddedCoords.Clear();
Rect2 expandedRect = _currentOffsetCoordRect.Merge(_oldOffsetCoordRect);
foreach (int coord_x in Enumerable.Range(Mathf.FloorToInt(expandedRect.Position.x), Mathf.CeilToInt(expandedRect.Size.x)))
{
foreach (int coord_y in Enumerable.Range(Mathf.FloorToInt(expandedRect.Position.y),
Mathf.CeilToInt(expandedRect.Size.y)))
{
Vector2 coord = new Vector2(coord_x, coord_y);
bool isInCurrent = _currentOffsetCoordRect.HasPoint(coord);
bool isInOld = _oldOffsetCoordRect.HasPoint(coord);
if (isInCurrent && !isInOld)
{
AddedCoords.Add(coord);
GetTile3dAt(coord);
}
else if (isInOld && !isInCurrent)
{
RemovedCoords.Add(coord);
if (_coordToTile.Keys.Contains(coord))
{
Spatial tile3d = _coordToTile[coord];
_activeTiles.RemoveChild(tile3d);
tile3d.QueueFree();
}
}
}
}
}
public Spatial GetTile3dAt(Vector2 offsetCoords)
{
if (!_coordToTile.Keys.Contains(offsetCoords))
{
Spatial tile3d = (Spatial)_hexTileScene.Instance();
HexCell cell = new HexCell();
cell.OffsetCoords = offsetCoords;
Vector2 cellPlaneCoords = _hexGrid.GetHexCenter(cell);
Transform tile3dTransform = Transform.Identity;
tile3dTransform.origin = new Vector3(cellPlaneCoords.x, GD.Randf() * 0.1f, cellPlaneCoords.y);
tile3d.Transform = tile3dTransform;
_activeTiles.AddChild(tile3d);
_coordToTile[offsetCoords] = tile3d;
}
return _coordToTile[offsetCoords];
}
public void SetCenterTile(HexCell cell)
{
UpdateRects(_hexGrid.GetHexCenter(cell));
}
}
}