GodotComponentTest/scenes/AdaptiveWorldStream.gd

136 lines
4.3 KiB
GDScript
Raw Normal View History

2022-11-20 20:13:42 +01:00
extends Spatial
onready var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
onready var hex_tile_3d = preload("res://scenes/HexTile3D.tscn")
onready var player = $Player
onready var player_move_component = $Player/Movable
# GUI
onready var tile_label = $Control/HBoxContainer/tile_label
onready var tile_offset_label = $Control/HBoxContainer/tile_offset_label
onready var num_tiles_label = $Control/HBoxContainer/num_tiles_label
onready var num_active_tiles_label = $Control/HBoxContainer/num_active_tiles_label
onready var world_map_texture = $Control/HBoxContainer/WorldMapRect
onready var tile_highlight = $TileHighlight
onready var stream_container = $StreamContainer
onready var stream_active_tiles = $StreamContainer/ActiveTiles
onready var world = $World
# Streaming variables
var last_tile = HexCell.new()
var current_tile = HexCell.new()
var current_tile_offset = Vector2(0,0)
var top_left_cell = HexCell.new()
var bottom_right_cell = HexCell.new()
var stream_world_rect = Rect2()
func _ready():
2022-11-21 20:58:13 +01:00
world.heightmap.lock()
2022-11-20 20:13:42 +01:00
update_streaming_tiles()
2022-11-21 20:58:13 +01:00
2022-11-20 20:13:42 +01:00
func update_streaming_tiles():
var stream_rect_position = hexgrid.get_hex_center3(current_tile)
stream_world_rect = Rect2(
stream_rect_position.x - stream_container.world_rect.size.x / 2, stream_rect_position.z - stream_container.world_rect.size.y / 2,
stream_container.world_rect.size.x, stream_container.world_rect.size.y
)
stream_container.world_rect = stream_world_rect
top_left_cell = hexgrid.get_hex_at(stream_world_rect.position)
bottom_right_cell = hexgrid.get_hex_at(stream_world_rect.end)
# print ("update_streaming_tiles: cells: ", top_left_cell.offset_coords, " to ", bottom_right_cell.offset_coords)
2022-11-21 20:58:13 +01:00
# 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
2022-11-20 20:13:42 +01:00
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
2022-11-21 20:58:13 +01:00
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)
2022-12-04 20:51:02 +01:00
num_tiles_label.text = str(len(stream_container.tiles_by_offit_coord.values()))
2022-11-20 20:13:42 +01:00
num_active_tiles_label.text = str(stream_active_tiles.get_child_count())
func _process(_delta):
last_tile = current_tile
var player_coord = player.transform.origin
current_tile = hexgrid.get_hex_at(Vector2(player_coord.x, player_coord.z))
2022-11-21 20:58:13 +01:00
# world.heightmap.lock()
2022-11-20 20:13:42 +01:00
player.transform.origin.y = world.get_height(current_tile.offset_coords)
player.transform.origin.y = 2.0
2022-11-21 20:58:13 +01:00
# world.heightmap.unlock()
2022-11-20 20:13:42 +01:00
var player_hex_offset_coord = current_tile.offset_coords
tile_label.text = "%d, %d" % [player_hex_offset_coord.x, player_hex_offset_coord.y]
current_tile_offset = Vector2(player_coord.x, player_coord.z) - hexgrid.get_hex_center_from_offset(current_tile.offset_coords)
tile_offset_label.text = "%2.2f, %2.2f" % [current_tile_offset.x, current_tile_offset.y]
tile_highlight.transform.origin = Vector3(hexgrid.get_hex_center3(current_tile))
if current_tile.offset_coords != last_tile.offset_coords:
update_streaming_tiles()
func _input(event):
var velocity = Vector3(0, 0, 0)
if event is InputEventKey:
if Input.is_action_pressed("Forward"):
velocity.z = -5
elif Input.is_action_pressed("Back"):
velocity.z = 5
else:
velocity.z = 0
if Input.is_action_pressed("Left"):
velocity.x = -5
elif Input.is_action_pressed("Right"):
velocity.x = 5
else:
velocity.x = 0
player_move_component.vel = velocity
get_tree().set_input_as_handled()
func _on_World_world_generated():
if world == null || world.heightmap == null:
return
var world_texture = ImageTexture.new()
world_texture.create_from_image(world.heightmap)
world_map_texture.texture = world_texture
update_streaming_tiles()
func _on_GenerateWorldButton_pressed():
world.init_noisemap()