You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
5.3 KiB
132 lines
5.3 KiB
extends GridMap
|
|
|
|
@export var ChunkWidth: int = 128
|
|
@export var ChunkHeight: int = 128
|
|
@export var WorldWidth: int = 1024
|
|
@export var WorldHeight: int = 1024
|
|
|
|
@onready var camera = $Camera3D
|
|
|
|
@onready var multimeshInstances = []
|
|
|
|
func _ready():
|
|
setGridChunk(0, ChunkWidth, 0, ChunkHeight)
|
|
|
|
func generateGridChunk(cameraPos: Vector3) -> void:
|
|
clear()
|
|
for instance in multimeshInstances:
|
|
remove_child(instance)
|
|
multimeshInstances = []
|
|
|
|
var chunkCenter = local_to_map(cameraPos)
|
|
var rowStart: float = chunkCenter.z - (ChunkHeight / 2)
|
|
var rowEnd: float = chunkCenter.z + (ChunkHeight / 2)
|
|
var columnStart: float = chunkCenter.x - (ChunkWidth / 2)
|
|
var columnEnd: float = chunkCenter.x + (ChunkWidth / 2)
|
|
setGridChunk(columnStart, columnEnd, rowStart, rowEnd)
|
|
setEntities(columnStart, columnEnd, rowStart, rowEnd)
|
|
|
|
func setGridChunk(columnStart: float, columnEnd: float, rowStart: float, rowEnd: float) -> void:
|
|
if columnStart < 0 :
|
|
columnStart = 0
|
|
if rowStart < 0 :
|
|
rowStart = 0
|
|
if columnEnd > Global.world.width - 1 :
|
|
columnEnd =Global.world.width - 2
|
|
if rowEnd > Global.world.height - 1 :
|
|
rowEnd = Global.world.height - 2
|
|
|
|
for mz in range(rowStart, rowEnd):
|
|
for mx in range(columnStart, columnEnd):
|
|
var bloc = Global.world.get_bloc(Vector2(mx, mz))
|
|
var my: float = bloc.y
|
|
var meshID
|
|
var mesh_rotation
|
|
if bloc.type != 0:
|
|
var neighbours = Global.world.get_neighbours_4_at_same_height(Vector2(mx, mz))
|
|
if neighbours == Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_3
|
|
mesh_rotation = Global.GRID_ROTATION[3]
|
|
elif neighbours == Global.directions_4.LEFT:
|
|
meshID = Global.bloc_sides_id.SIDE_3
|
|
mesh_rotation = Global.GRID_ROTATION[1]
|
|
elif neighbours == Global.directions_4.LEFT + Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_2_OPPOSITE
|
|
mesh_rotation = Global.GRID_ROTATION[0]
|
|
elif neighbours == Global.directions_4.BOTTOM:
|
|
meshID = Global.bloc_sides_id.SIDE_3
|
|
mesh_rotation = Global.GRID_ROTATION[0]
|
|
elif neighbours == Global.directions_4.BOTTOM + Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_2_ANGLE
|
|
mesh_rotation = Global.GRID_ROTATION[3]
|
|
elif neighbours == Global.directions_4.BOTTOM + Global.directions_4.LEFT:
|
|
meshID = Global.bloc_sides_id.SIDE_2_ANGLE
|
|
mesh_rotation = Global.GRID_ROTATION[0]
|
|
elif neighbours == Global.directions_4.BOTTOM + Global.directions_4.LEFT + Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_1
|
|
mesh_rotation = Global.GRID_ROTATION[0]
|
|
elif neighbours == Global.directions_4.TOP:
|
|
meshID = Global.bloc_sides_id.SIDE_3
|
|
mesh_rotation = Global.GRID_ROTATION[2]
|
|
elif neighbours == Global.directions_4.TOP + Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_2_ANGLE
|
|
mesh_rotation = Global.GRID_ROTATION[2]
|
|
elif neighbours == Global.directions_4.TOP + Global.directions_4.LEFT:
|
|
meshID = Global.bloc_sides_id.SIDE_2_ANGLE
|
|
mesh_rotation = Global.GRID_ROTATION[1]
|
|
elif neighbours == Global.directions_4.TOP + Global.directions_4.LEFT + Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_1
|
|
mesh_rotation = Global.GRID_ROTATION[2]
|
|
elif neighbours == Global.directions_4.TOP + Global.directions_4.BOTTOM:
|
|
meshID = Global.bloc_sides_id.SIDE_2_OPPOSITE
|
|
mesh_rotation = Global.GRID_ROTATION[1]
|
|
elif neighbours == Global.directions_4.TOP + Global.directions_4.BOTTOM + Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_1
|
|
mesh_rotation = Global.GRID_ROTATION[3]
|
|
elif neighbours == Global.directions_4.TOP + Global.directions_4.BOTTOM + Global.directions_4.LEFT:
|
|
meshID = Global.bloc_sides_id.SIDE_1
|
|
mesh_rotation = Global.GRID_ROTATION[1]
|
|
elif neighbours == Global.directions_4.TOP + Global.directions_4.BOTTOM + Global.directions_4.LEFT + Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_0
|
|
mesh_rotation = Global.GRID_ROTATION[0]
|
|
elif neighbours == 0:
|
|
meshID = Global.bloc_sides_id.SIDE_4
|
|
mesh_rotation = Global.GRID_ROTATION[0]
|
|
|
|
set_cell_item( Vector3(mx, my, mz) , meshID, mesh_rotation)
|
|
|
|
func setEntities(columnStart: float, columnEnd: float, rowStart: float, rowEnd: float) -> void:
|
|
if columnStart < 0 :
|
|
columnStart = 0
|
|
if rowStart < 0 :
|
|
rowStart = 0
|
|
if columnEnd > Global.world.width - 1 :
|
|
columnEnd =Global.world.width - 2
|
|
if rowEnd > Global.world.height - 1 :
|
|
rowEnd = Global.world.height - 2
|
|
var multimesh = MultiMesh.new()
|
|
multimesh.mesh = CapsuleMesh.new()
|
|
multimesh.transform_format = MultiMesh.TRANSFORM_3D
|
|
var instances = []
|
|
for mz in range(rowStart, rowEnd):
|
|
for mx in range(columnStart, columnEnd):
|
|
var bloc = Global.world.get_bloc(Vector2(mx, mz))
|
|
if bloc.entity != -1:
|
|
instances.append(map_to_local(Vector3(bloc.x, bloc.y, bloc.z)))
|
|
|
|
|
|
multimesh.instance_count = instances.size()
|
|
for instance_index in multimesh.instance_count:
|
|
var transform := Transform3D()
|
|
transform.origin = instances[instance_index]
|
|
|
|
multimesh.set_instance_transform(instance_index, transform)
|
|
|
|
if instances.size():
|
|
var multimesh_instance = MultiMeshInstance3D.new()
|
|
multimesh_instance.multimesh = multimesh
|
|
multimeshInstances.append(multimesh_instance)
|
|
add_child(multimeshInstances[multimeshInstances.size()-1])
|
|
|
|
func _on_map_update_timer_timeout():
|
|
generateGridChunk(camera.global_transform.origin)
|
|
|