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.
107 lines
3.8 KiB
107 lines
3.8 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
|
|
|
|
func _ready():
|
|
setGridChunk(0, ChunkWidth, 0, ChunkHeight)
|
|
|
|
func generateGridChunk(cameraPos: Vector3) -> void:
|
|
clear()
|
|
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)
|
|
|
|
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 my: float = Global.world.find_elevation(Vector2(mx, mz))
|
|
var meshID
|
|
var mesh_rotation
|
|
if my > 0:
|
|
var neighbours = Global.world.get_neighbours_4_at_same_height(Vector2(mx, mz))
|
|
match neighbours:
|
|
Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_3
|
|
mesh_rotation = Global.GRID_ROTATION[3]
|
|
|
|
Global.directions_4.LEFT:
|
|
meshID = Global.bloc_sides_id.SIDE_3
|
|
mesh_rotation = Global.GRID_ROTATION[1]
|
|
|
|
Global.directions_4.LEFT + Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_2_OPPOSITE
|
|
mesh_rotation = Global.GRID_ROTATION[0]
|
|
|
|
Global.directions_4.BOTTOM:
|
|
meshID = Global.bloc_sides_id.SIDE_3
|
|
mesh_rotation = Global.GRID_ROTATION[0]
|
|
|
|
Global.directions_4.BOTTOM + Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_2_ANGLE
|
|
mesh_rotation = Global.GRID_ROTATION[3]
|
|
|
|
Global.directions_4.BOTTOM + Global.directions_4.LEFT:
|
|
meshID = Global.bloc_sides_id.SIDE_2_ANGLE
|
|
mesh_rotation = Global.GRID_ROTATION[0]
|
|
|
|
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]
|
|
|
|
Global.directions_4.TOP:
|
|
meshID = Global.bloc_sides_id.SIDE_3
|
|
mesh_rotation = Global.GRID_ROTATION[2]
|
|
|
|
Global.directions_4.TOP + Global.directions_4.RIGHT:
|
|
meshID = Global.bloc_sides_id.SIDE_2_ANGLE
|
|
mesh_rotation = Global.GRID_ROTATION[2]
|
|
|
|
Global.directions_4.TOP + Global.directions_4.LEFT:
|
|
meshID = Global.bloc_sides_id.SIDE_2_ANGLE
|
|
mesh_rotation = Global.GRID_ROTATION[1]
|
|
|
|
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]
|
|
|
|
Global.directions_4.TOP + Global.directions_4.BOTTOM:
|
|
meshID = Global.bloc_sides_id.SIDE_2_OPPOSITE
|
|
mesh_rotation = Global.GRID_ROTATION[1]
|
|
|
|
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]
|
|
|
|
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]
|
|
|
|
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]
|
|
|
|
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 _on_map_update_timer_timeout():
|
|
generateGridChunk(camera.global_transform.origin)
|
|
|