add scenes
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
extends Node3D
|
||||
|
||||
const CHUNK_TYPE_BIOME: int = 0
|
||||
const CHUNK_TYPE_STRAIGHT_TRACK: int = 1
|
||||
const CHUNK_TYPE_CURVED_TRACK: int = 2
|
||||
|
||||
const CHUNK_GENERATION_STEPS_PER_FRAME: int = 2
|
||||
const CHUNK_CLEANUP_STEPS_PER_FRAME: int = 4
|
||||
const LAMPPOST_WIRE_STEPS_PER_FRAME: int = 1
|
||||
const RAILWAY_SCENE_DIRECTORY: String = "res://tgcc/chunk/railway/scene"
|
||||
|
||||
@export_group("Rails")
|
||||
@export var rail_path: Path3D
|
||||
@@ -31,11 +36,15 @@ var pending_cleanup_cells: Array[Vector2i] = []
|
||||
var pending_wire_cells: Array[Vector2i] = []
|
||||
var pending_wire_lookup: Dictionary = {}
|
||||
var pending_radar_update: bool = false
|
||||
var rail_chunk_catalogue: Dictionary = {}
|
||||
|
||||
var manual_biome: Biome = null
|
||||
|
||||
func _ready() -> void:
|
||||
if biome_list.is_empty() or rail_path == null: return
|
||||
|
||||
#connect events
|
||||
UIEvents.update_rail_chunks.connect(_update_rail_chunks)
|
||||
|
||||
noise_generator = FastNoiseLite.new()
|
||||
noise_generator.noise_type = FastNoiseLite.TYPE_PERLIN
|
||||
@@ -48,6 +57,7 @@ func _ready() -> void:
|
||||
altitude_generator.frequency = district_scale * 0.5
|
||||
|
||||
_warm_chunk_candidate_cache()
|
||||
_warm_rail_chunk_catalogue()
|
||||
_update_set_pieces()
|
||||
|
||||
func _update_set_pieces() -> void:
|
||||
@@ -154,6 +164,28 @@ func _warm_chunk_candidate_cache() -> void:
|
||||
for scene in unique_scenes.values():
|
||||
_get_chunk_scene_metadata(scene)
|
||||
|
||||
func _warm_rail_chunk_catalogue() -> void:
|
||||
rail_chunk_catalogue.clear()
|
||||
|
||||
var directory := DirAccess.open(RAILWAY_SCENE_DIRECTORY)
|
||||
if directory == null:
|
||||
return
|
||||
|
||||
directory.list_dir_begin()
|
||||
var file_name := directory.get_next()
|
||||
while file_name != "":
|
||||
if not directory.current_is_dir() and file_name.ends_with(".tscn"):
|
||||
var scene_path := "%s/%s" % [RAILWAY_SCENE_DIRECTORY, file_name]
|
||||
var scene := load(scene_path) as PackedScene
|
||||
if scene != null:
|
||||
var chunk_type = _get_chunk_type_from_scene(scene)
|
||||
if chunk_type == CHUNK_TYPE_STRAIGHT_TRACK or chunk_type == CHUNK_TYPE_CURVED_TRACK:
|
||||
if not rail_chunk_catalogue.has(chunk_type):
|
||||
rail_chunk_catalogue[chunk_type] = []
|
||||
rail_chunk_catalogue[chunk_type].append(scene)
|
||||
file_name = directory.get_next()
|
||||
directory.list_dir_end()
|
||||
|
||||
func _get_chunk_scene_cache_key(scene: PackedScene) -> String:
|
||||
if scene == null:
|
||||
return ""
|
||||
@@ -209,6 +241,20 @@ func _get_cached_chunk_info(instance: Node, scene: PackedScene) -> Node:
|
||||
return instance
|
||||
return instance.get_node_or_null(info_path)
|
||||
|
||||
func _get_chunk_type_from_scene(scene: PackedScene) -> int:
|
||||
if scene == null:
|
||||
return CHUNK_TYPE_BIOME
|
||||
|
||||
var preview_chunk = scene.instantiate()
|
||||
var info_list: Array[Node] = []
|
||||
collect_all_chunkinfo(preview_chunk, info_list)
|
||||
var info_node = info_list[0] if info_list.size() > 0 else null
|
||||
var chunk_type = CHUNK_TYPE_BIOME
|
||||
if info_node != null and "chunk_type" in info_node:
|
||||
chunk_type = info_node.chunk_type
|
||||
preview_chunk.queue_free()
|
||||
return chunk_type
|
||||
|
||||
func _clear_pending_world_work() -> void:
|
||||
pending_generation_cells.clear()
|
||||
pending_cleanup_cells.clear()
|
||||
@@ -216,6 +262,94 @@ func _clear_pending_world_work() -> void:
|
||||
pending_wire_lookup.clear()
|
||||
pending_radar_update = false
|
||||
|
||||
func _collect_replaceable_rail_chunks(root: Node, result: Array[Node3D]) -> void:
|
||||
if root == null:
|
||||
return
|
||||
|
||||
if root is Node3D:
|
||||
var node_3d := root as Node3D
|
||||
if node_3d.scene_file_path.begins_with(RAILWAY_SCENE_DIRECTORY):
|
||||
var info_list: Array[Node] = []
|
||||
collect_all_chunkinfo(node_3d, info_list)
|
||||
var info_node = info_list[0] if info_list.size() > 0 else null
|
||||
if info_node != null and "chunk_type" in info_node:
|
||||
var chunk_type = info_node.chunk_type
|
||||
if chunk_type == CHUNK_TYPE_STRAIGHT_TRACK or chunk_type == CHUNK_TYPE_CURVED_TRACK:
|
||||
result.append(node_3d)
|
||||
return
|
||||
|
||||
for child in root.get_children():
|
||||
_collect_replaceable_rail_chunks(child, result)
|
||||
|
||||
func _pick_replacement_rail_scene(chunk_type: int, current_scene_path: String) -> PackedScene:
|
||||
var candidates: Array = rail_chunk_catalogue.get(chunk_type, [])
|
||||
if candidates.is_empty():
|
||||
return null
|
||||
|
||||
var alternatives: Array[PackedScene] = []
|
||||
for candidate in candidates:
|
||||
var scene := candidate as PackedScene
|
||||
if scene != null and scene.resource_path != current_scene_path:
|
||||
alternatives.append(scene)
|
||||
|
||||
if not alternatives.is_empty():
|
||||
return alternatives.pick_random()
|
||||
return candidates.pick_random() as PackedScene
|
||||
|
||||
func _replace_rail_chunk_instance(chunk_root: Node3D) -> bool:
|
||||
if chunk_root == null or not is_instance_valid(chunk_root):
|
||||
return false
|
||||
|
||||
var info_list: Array[Node] = []
|
||||
collect_all_chunkinfo(chunk_root, info_list)
|
||||
var info_node = info_list[0] if info_list.size() > 0 else null
|
||||
if info_node == null or not "chunk_type" in info_node:
|
||||
return false
|
||||
|
||||
var replacement_scene = _pick_replacement_rail_scene(info_node.chunk_type, chunk_root.scene_file_path)
|
||||
if replacement_scene == null:
|
||||
return false
|
||||
|
||||
var chunk_parent = chunk_root.get_parent()
|
||||
var replacement = replacement_scene.instantiate() as Node3D
|
||||
if chunk_parent == null or replacement == null:
|
||||
return false
|
||||
|
||||
var chunk_index = chunk_root.get_index()
|
||||
var chunk_name = chunk_root.name
|
||||
chunk_root.name = "%s_old" % chunk_name
|
||||
|
||||
replacement.transform = chunk_root.transform
|
||||
chunk_parent.add_child(replacement)
|
||||
chunk_parent.move_child(replacement, chunk_index)
|
||||
replacement.name = chunk_name
|
||||
replacement.owner = chunk_root.owner
|
||||
chunk_root.queue_free()
|
||||
return true
|
||||
|
||||
func _refresh_rail_chunks() -> void:
|
||||
print("update rail chunks")
|
||||
_warm_rail_chunk_catalogue()
|
||||
|
||||
var current_scene: Node = get_tree().current_scene
|
||||
if current_scene == null:
|
||||
return
|
||||
|
||||
var replaceable_chunks: Array[Node3D] = []
|
||||
_collect_replaceable_rail_chunks(current_scene, replaceable_chunks)
|
||||
|
||||
var replaced_count = 0
|
||||
for chunk_root in replaceable_chunks:
|
||||
if _replace_rail_chunk_instance(chunk_root):
|
||||
replaced_count += 1
|
||||
|
||||
if replaced_count > 0:
|
||||
await get_tree().process_frame
|
||||
_destroy_and_regenrate_world()
|
||||
|
||||
func _update_rail_chunks() -> void:
|
||||
call_deferred("_refresh_rail_chunks")
|
||||
|
||||
func _refresh_world(center: Vector2i) -> void:
|
||||
_rebuild_generation_queue(center)
|
||||
_rebuild_cleanup_queue(center)
|
||||
|
||||
@@ -30,6 +30,10 @@ signal toggle_shadows(value: bool)
|
||||
@warning_ignore("unused_signal")
|
||||
signal toggle_fog(value: bool)
|
||||
|
||||
#railway signals
|
||||
@warning_ignore("unused_signal")
|
||||
signal update_rail_chunks()
|
||||
|
||||
#day cycle signals
|
||||
@warning_ignore("unused_signal")
|
||||
signal toggle_pause_daytime(value: bool)
|
||||
|
||||
Reference in New Issue
Block a user