我如何在3 × 3网格中生成块,以便在场景中始终有9个网格实例?目前,每当玩家移动到一个新区域/块时,我只生成一个块。
为了让你们更容易,我把它放在github仓库里,这样你们就可以下载和尝试了!https://github.com/Dragon20C/GODOT--Flat-Terrain-Generation
相关代码:
var chunk_x = floor(player.translation.x / chunk.size)
var chunk_z = floor(player.translation.z / chunk.size)
var new_chunk_pos = Vector2(chunk_x, chunk_z)
if new_chunk_pos != chunk_pos:
if !new_chunk_pos in previous_chunks:
chunk_pos = new_chunk_pos
var instance = chunk_scene.instance()
add_child(instance)
instance.chunk_position_set(Vector3(chunk_pos.x * chunk.size,0,chunk_pos.y * chunk.size))
previous_chunks.append(chunk_pos)
将此代码提取为一个新函数:
if !new_chunk_pos in previous_chunks:
chunk_pos = new_chunk_pos
var instance = chunk_scene.instance()
add_child(instance)
instance.chunk_position_set(Vector3(chunk_pos.x * chunk.size,0,chunk_pos.y * chunk.size))
previous_chunks.append(chunk_pos)
则称其为传递new_chunk_pos
及其周围块的位置。即x在new_chunk_pos.x - 1
到new_chunk_pos.x + 1
范围内,y在new_chunk_pos.y - 1
到new_chunk_pos.y + 1
范围内的位置。