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.
 
Gridmap/scripts/Ground.gd

254 lines
9.5 KiB

extends Node3D
const Entity3D = preload("res://scenes/Instance.tscn")
var thread
var displayed_chunks = {} # displayed_chunks
var unready_chunks = {}
#@onready var camera = get_node("Camera3D")
#@export @onready var camera = get_node(camera)
@export var camera: Camera3D
var chunks: Array
func _ready():
thread = Thread.new()
# pass
init_chunks()
# setFloor()
# setEntities()
func init_chunks():
for x in Global.world.width / Global.world.chunk_size:
chunks.append([])
for y in Global.world.height / Global.world.chunk_size:
chunks[x].append([])
chunks[x][y] = Chunk.new(x, y, Global.world.chunk_size)
chunks[x][y].set_name("Chunk")
chunks[x][y]
# print(chunks)
#
#func create_chunk(coord: Vector2i):
# var chunk = GridMap.new()
# for mx in Global.world.chunk_size:
# for mz in Global.world.chunk_size:
# var bloc = Global.world.get_bloc(Vector2i(mx * Global.world.chunk_size, mz * Global.world.chunk_size))
# var my: float = bloc.position.y
# var meshID
# var mesh_rotation
# if bloc.type != 0:
# var neighbours = Global.world.get_neighbours_4_at_same_height(Vector2i(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]
#
# chunk.set_cell_item( Vector3(mx, my, mz) , meshID, mesh_rotation)
# return chunk
#func setFloor():
# clear()
# for mz in range(0, Global.world.width - 1):
# for mx in range(0, Global.world.height - 1):
# var bloc = Global.world.get_bloc(Vector2i(mx, mz))
# var my: float = bloc.position.y
# var meshID
# var mesh_rotation
# if bloc.type != 0:
# var neighbours = Global.world.get_neighbours_4_at_same_height(Vector2i(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():
# for entity in Global.world.entities:
# var entity_instance = Entity3D.instantiate()
# entity_instance.id = entity.id
# entity_instance.position = map_to_local(Vector3(entity.position.x, entity.position.y, entity.position.z))
# entity_instance.connect_to_world(entity_instance.id)
# add_child(entity_instance)
func add_chunk(x, z):
var key = str(x) + "," + str(z)
if displayed_chunks.has(key) or unready_chunks.has(key):
return
if not thread.is_started():
thread.start(Callable(self, "load_chunk").bind([thread, x, z]))
# thread.start(self, "load_chunk", [thread, x, z])
unready_chunks[key] = 1
#
#func gen_chunk(array):
# var thread = array[0]
# var x = array[1]
# var z = array[2]
# var chunk = {
# "x": x,
# "y": z,
# "should_remove": true
# }
#
# call_deferred("load_done", chunk, thread)
func load_chunk(array):
var thread = array[0]
var posX = array[1]
var posZ = array[2]
var x = fmod(posX, Global.world.width / Global.world.chunk_size)
var z = fmod(posZ, Global.world.height / Global.world.chunk_size)
#
# # Chargement du chunk
var chunk = chunks[x][z]
## var chunk = Chunk.new()
# chunk.translate(Vector3(posX * Global.world.chunk_size, 0, posZ * Global.world.chunk_size))
var chunk_position = Vector2(posX, posZ)
chunk.position = Vector3(posX * Global.world.chunk_size, 0, posZ * Global.world.chunk_size)
call_deferred("load_done", chunk, thread, chunk_position)
func load_done(chunk, thread, position):
add_child(chunk)
var key = str(position.x) + "," + str(position.y)
displayed_chunks[key] = chunk
unready_chunks.erase(key)
thread.wait_to_finish()
func get_chunk(x, z):
var key = str(x) + "," + str(z)
if displayed_chunks.has(key):
return displayed_chunks.get(key)
return null
func _process(delta):
update_chunks()
clean_up_chunks()
reset_chunks()
func update_chunks():
var camera_translation = camera.position
var c_x = int(camera_translation.x) / Global.world.chunk_size
var c_z = int(camera_translation.z) / Global.world.chunk_size
for x in range(c_x - Global.world.chunk_number * 0.5, c_x + Global.world.chunk_number * 0.53):
for z in range(c_z - Global.world.chunk_number * 0.5, c_z + Global.world.chunk_number * 0.53):
add_chunk(x, z)
var chunk = get_chunk(x, z)
if chunk != null:
chunk.should_remove = false
func clean_up_chunks():
for key in displayed_chunks:
var chunk = displayed_chunks[key]
if chunk.should_remove:
remove_child(chunk)
displayed_chunks.erase(key)
func reset_chunks():
for key in displayed_chunks:
var chunk = displayed_chunks[key]
chunk.should_remove = true