Minor tweaks

WorldChunkRefactoring
Martin Felis 2022-11-21 20:58:13 +01:00
parent 6136346faa
commit dd7fa53c33
5 changed files with 40 additions and 20 deletions

View File

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

View File

@ -29,7 +29,9 @@ var stream_world_rect = Rect2()
func _ready():
world.heightmap.lock()
update_streaming_tiles()
func update_streaming_tiles():
@ -46,20 +48,28 @@ func update_streaming_tiles():
# print ("update_streaming_tiles: cells: ", top_left_cell.offset_coords, " to ", bottom_right_cell.offset_coords)
world.heightmap.lock()
# for tile_3d in stream_active_tiles.get_children():
# var height = world.get_height(tile_3d.game_tile.offset_coords)
# tile_3d.transform.origin.y = height
for cell_x in range (top_left_cell.offset_coords.x, bottom_right_cell.offset_coords.x + 1):
for cell_y in range (bottom_right_cell.offset_coords.y, top_left_cell.offset_coords.y + 1):
if cell_x < 0 || cell_x >= world.size || cell_y < 0 || cell_y >= world.size:
continue
var tile_3d = stream_container.create_hextile3d_at(Vector2(cell_x, cell_y))
var hex_center = hexgrid.get_hex_center_from_offset(Vector2(cell_x, cell_y))
var height = world.get_height(Vector2(cell_x, cell_y))
tile_3d.transform.origin = Vector3(hex_center.x, height, hex_center.y)
world.heightmap.unlock()
var tile_3d = stream_container.create_hextile3d_at(Vector2(cell_x, cell_y))
if tile_3d.transform.origin.y == -9999:
var hex_center = hexgrid.get_hex_center(tile_3d.game_tile)
var height = world.get_height(Vector2(cell_x, cell_y))
if height < 0:
tile_3d.set_tiletype(GameTile.TileType.Sand)
else:
tile_3d.set_tiletype(GameTile.TileType.Grass)
tile_3d.transform.origin = Vector3(hex_center.x, height, hex_center.y)
num_tiles_label.text = str(len(stream_container.tiles_by_offset_coord.values()))
num_active_tiles_label.text = str(stream_active_tiles.get_child_count())
@ -70,10 +80,10 @@ func _process(_delta):
var player_coord = player.transform.origin
current_tile = hexgrid.get_hex_at(Vector2(player_coord.x, player_coord.z))
world.heightmap.lock()
# world.heightmap.lock()
player.transform.origin.y = world.get_height(current_tile.offset_coords)
player.transform.origin.y = 2.0
world.heightmap.unlock()
# world.heightmap.unlock()
var player_hex_offset_coord = current_tile.offset_coords
tile_label.text = "%d, %d" % [player_hex_offset_coord.x, player_hex_offset_coord.y]

View File

@ -127,12 +127,13 @@ text = "0"
[node name="StreamContainer" type="Spatial" parent="."]
script = ExtResource( 4 )
world_rect = Rect2( 0, 0, 25, 20 )
world_rect = Rect2( 0, 0, 20, 20 )
[node name="ActiveTiles" type="Spatial" parent="StreamContainer"]
[node name="Bounds" type="MeshInstance" parent="StreamContainer"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0 )
visible = false
mesh = SubResource( 1 )
skeleton = NodePath("../..")
material/0 = SubResource( 2 )

View File

@ -26,11 +26,20 @@ func is_hex_coord_in_rect (coord: Vector2):
return coord.x >= offset_coord_rect.position.x and coord.x < offset_coord_rect.end.x and coord.y >= offset_coord_rect.position.y and coord.y < offset_coord_rect.end.y
func instantiate_hextile3d():
return hextile3d.instance()
func add_hextile_to_tree(hextile3d):
active_tiles.add_child(hextile3d)
func create_hextile3d_at (coord: Vector2) -> HexTile3D:
if not coord in tiles_by_offset_coord.keys():
var new_hextile3d = hextile3d.instance()
active_tiles.add_child(new_hextile3d)
var new_hextile3d = instantiate_hextile3d()
add_hextile_to_tree(new_hextile3d)
new_hextile3d.game_tile.offset_coords = coord
new_hextile3d.transform.origin.y = -9999
tiles_by_offset_coord[coord] = new_hextile3d
return tiles_by_offset_coord[coord]
@ -46,7 +55,7 @@ func cleanup_tiles():
child.queue_free()
active_tiles.remove_child(child)
num_deleted = num_deleted + 1
# print ("deleted ", num_deleted, " tiles")

View File

@ -27,7 +27,7 @@ func init_noisemap():
var noise_texture = NoiseTexture.new()
var noise_generator = OpenSimplexNoise.new()
noise_generator.seed = randi()
noise_generator.seed = -1626828106#randi()
print ("seed: ", noise_generator.seed)
noise_generator.octaves = 3
noise_generator.period = 5
@ -41,5 +41,5 @@ func init_noisemap():
func get_height(coord: Vector2) -> float:
return heightmap.get_pixel(coord.x, coord.y).r * 3
return heightmap.get_pixel(coord.x, coord.y).r * 3 - 1.5