Stream container starts to work.

WorldChunkRefactoring
Martin Felis 2022-12-03 00:43:19 +01:00
parent af9e71e698
commit 239ba3f614
5 changed files with 112 additions and 19 deletions

View File

@ -9,17 +9,17 @@
config_version=4
_global_script_classes=[ {
"base": "Node",
"base": "Reference",
"class": "ClickableComponent",
"language": "GDScript",
"path": "res://components/ClickableComponent.gd"
}, {
"base": "KinematicBody2D",
"base": "Reference",
"class": "CollisionLine",
"language": "GDScript",
"path": "res://utils/CollisionLine.gd"
}, {
"base": "Node",
"base": "Reference",
"class": "ColorComponent",
"language": "GDScript",
"path": "res://components/ColorComponent.gd"
@ -54,7 +54,7 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://utils/SpringDamper.gd"
}, {
"base": "Sprite",
"base": "Reference",
"class": "TintedSpriteComponent",
"language": "GDScript",
"path": "res://components/TintedSpriteComponent.gd"

View File

@ -16,7 +16,9 @@ public class AdaptiveWorldStream : Spatial
// scene nodes
private Spatial _tileHighlight;
private Spatial _mouseTileHighlight;
private StreamContainer _streamContainer;
private Area _streamContainerArea;
private Spatial _streamContainerActiveTiles;
private Player _player;
// Resources
@ -41,7 +43,9 @@ public class AdaptiveWorldStream : Spatial
// 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");
// resources
@ -56,7 +60,12 @@ public class AdaptiveWorldStream : Spatial
// connect signals
var result = _streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent));
CreateTileGrid();
// CreateTileGrid();
//playerTransform.origin += new Vector3(0, 0, -1) * delta;
Transform playerTransform = _player.Transform;
playerTransform.origin.x = 3;
_player.Transform = playerTransform;
}
public void CreateTileGrid()
@ -79,6 +88,39 @@ 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_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);
}
}
}
public override void _Process(float delta)
{
_lastTile = _currentTile;
@ -87,10 +129,10 @@ public class AdaptiveWorldStream : Spatial
Vector3 playerCoord = playerTransform.origin;
_currentTile = _hexGrid.GetHexAt(new Vector2(playerCoord.x, playerCoord.z));
_tileLabel.Text = delta.ToString();
_tileLabel.Text = playerTransform.ToString();
_tileOffsetLabel.Text = _currentTile.OffsetCoords.ToString();
playerTransform.origin += new Vector3(0, 0, -1) * delta;
playerTransform.origin += new Vector3(-0.2f, 0, 1) * delta;
_player.Transform = playerTransform;
Transform tileHighlightTransform = Transform.Identity;
@ -98,6 +140,15 @@ public class AdaptiveWorldStream : Spatial
tileHighlightTransform.origin.x = currentTileCenter.x;
tileHighlightTransform.origin.z = currentTileCenter.y;
_tileHighlight.Transform = tileHighlightTransform;
if (_currentTile.CubeCoords != _lastTile.CubeCoords)
{
_streamContainer.SetCenterTile(_currentTile);
ClearStreamActiveTiles();
CreateStreamActiveTiles();
_numTilesLabel.Text = _streamContainerActiveTiles.GetChildCount().ToString();
}
}
public void OnAreaInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal, int shapeIndex)
@ -113,5 +164,6 @@ public class AdaptiveWorldStream : Spatial
_mouseTileLabel.Text = cellAtCursor.OffsetCoords.ToString();
_mouseTileHighlight.Transform = highlightTransform;
_player.Transform = highlightTransform;
}
}

View File

@ -9,13 +9,14 @@
[ext_resource path="res://utils/TileHighlight.tscn" type="PackedScene" id=7]
[sub_resource type="CubeMesh" id=1]
size = Vector3( 1, 1, 1 )
[sub_resource type="SpatialMaterial" id=2]
params_blend_mode = 3
albedo_color = Color( 1, 1, 1, 0.156863 )
[sub_resource type="BoxShape" id=9]
extents = Vector3( 10, 1, 10 )
extents = Vector3( 20, 1, 20 )
[sub_resource type="CapsuleShape" id=7]
radius = 0.2
@ -145,12 +146,12 @@ text = "0"
[node name="StreamContainer" type="Spatial" parent="."]
script = ExtResource( 4 )
_dimensions = Vector2( 4, 8 )
_dimensions = Vector2( 9, 3 )
[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, -1, 0 )
transform = Transform( 4, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0 )
mesh = SubResource( 1 )
skeleton = NodePath("../..")
material/0 = SubResource( 2 )
@ -161,12 +162,6 @@ material/0 = SubResource( 2 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0 )
shape = SubResource( 9 )
[node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, 0.511698, 0.859165, 0, -0.859165, 0.511698, -4.76837e-07, 7.29903, 4.46831 )
current = true
fov = 60.0
script = ExtResource( 6 )
[node name="Player" type="KinematicBody" parent="."]
script = ExtResource( 5 )
@ -174,6 +169,12 @@ script = ExtResource( 5 )
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 )
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 )
current = true
fov = 60.0
script = ExtResource( 6 )
[node name="MeshInstance" type="MeshInstance" parent="Player"]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 )
mesh = SubResource( 8 )

View File

@ -10,19 +10,59 @@ public class StreamContainer : Spatial
// resources
private PackedScene _hexTileScene = GD.Load<PackedScene>("res://scenes/HexTile3D.tscn");
// exports
[Export] public Vector2 _dimensions = new Vector2(8, 4);
// other members
private Rect2 _worldRect;
private Rect2 _offsetCoordRect;
private HexGrid _hexGrid;
public Rect2 OffsetCoordRect
{
get { return _offsetCoordRect; }
}
// 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 / 2, 1, _dimensions.y / 2));
boundsTransform = boundsTransform.Scaled(new Vector3(_dimensions.x, 1, _dimensions.y));
_bounds.Transform = boundsTransform;
}
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());
// 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,
topRightCell.OffsetCoords - bottomLeftCell.OffsetCoords + Vector2.One);
GD.Print("Offset rect now: " + _offsetCoordRect.ToString() + " center: " + _offsetCoordRect.GetCenter());
Transform boundsTransform = _bounds.Transform;
boundsTransform.origin.x = centerPlane.x;
boundsTransform.origin.z = centerPlane.y;
_bounds.Transform = boundsTransform;
Update
GD.Print("Bounds Transform: " + boundsTransform.ToString());
GD.Print("Bounds Global : " + _bounds.GlobalTransform.ToString());
}
public void SetCenterTile(HexCell cell)
{
UpdateRects(_hexGrid.GetHexCenter(cell));
}
}

View File

@ -20,7 +20,7 @@ roughness = 0.0
[node name="TileHighlight" type="Spatial"]
[node name="TileMesh" type="MeshInstance" parent="."]
transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, 0, 0 )
transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, -0.05, 0 )
cast_shadow = 0
mesh = SubResource( 3 )
skeleton = NodePath("../..")