extends GridMap class_name Chunk const Pawn = preload("res://scenes/Pawn.tscn") var location: Vector2i var size = 0 var should_remove = false var chunks_array: Array func _init(x: int, z: int, size: int, chunks_array: Array): self.location.x = x self.location.y = z self.size = size self.chunks_array = chunks_array mesh_library = load("res://assets/blocs/blocs.meshlib") cell_size = Vector3(1, 1, 1) cell_center_x = false cell_center_y = false cell_center_z = false set_collision_layer_value(7, true) set_collision_mask_value(7, true) set_collision_layer_value(8, true) set_collision_mask_value(8, true) create_chunk() create_water() Global.world.new_entity.connect(_on_new_entity) func create_chunk(): for mx in size: for mz in size: var position = Vector2i(mx + (size) * location.x, mz + (size) * location.y) # position.x = min(position.x, Global.world.width - 1) # position.y = min(position.y, Global.world.height - 1) var bloc = Global.world.get_bloc(position) 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(position) 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 create_water(): var plane_mesh = PlaneMesh.new() plane_mesh.size = Vector2(size, size) plane_mesh.material = preload("res://assets/water.material") var mesh_instance = MeshInstance3D.new() mesh_instance.mesh = plane_mesh var collision_shape = CollisionShape3D.new() var box = BoxShape3D.new() box.size = Vector3(512, 0, 512) collision_shape.shape = box var static_body = StaticBody3D.new() static_body.set_name("Ocean") static_body.set_collision_layer_value(8, true) static_body.add_child(mesh_instance) static_body.add_child(collision_shape) static_body.translate(Vector3(size/2, 0.5, size/2)) add_child(static_body) func is_inside_chunk(position: Vector2i) : var min_x = location.x * size var max_x = (location.x + 1) * size - 1 var min_y = location.y * size var max_y = (location.y + 1) * size - 1 if position.x >= min_x and position.x <= max_x and position.y >= min_y and position.y <= max_y: return true return false func get_chunk_position(position: Vector2i): var min_x = location.x * size var max_x = (location.x + 1) * size - 1 var min_y = location.y * size var max_y = (location.y + 1) * size - 1 var chunk_position = Vector2i(position.x - min_x, position.y - min_y) if chunk_position.x > max_x or chunk_position.y > max_y: return null return chunk_position func _on_new_entity(entity): if is_inside_chunk(entity.position): var chunk_position = get_chunk_position(entity.position) var entity_position = Vector3(chunk_position.x, entity.get_height(), chunk_position.y) var pawn = Pawn.instantiate() pawn.id = entity.id pawn.position = entity_position pawn.connect_to_world(pawn.id, chunks_array) add_child(pawn)