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.
144 lines
5.3 KiB
144 lines
5.3 KiB
extends GridMap
|
|
class_name Chunk
|
|
|
|
const Entity3D = preload("res://scenes/Instance.tscn")
|
|
|
|
var x = 0
|
|
var z = 0
|
|
var size = 0
|
|
var should_remove = false
|
|
|
|
func _init(x: int, z: int, size: int):
|
|
self.x = x
|
|
self.z = z
|
|
self.size = size
|
|
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) * x, mz + (size) * z)
|
|
# 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 = x * size
|
|
var max_x = (x + 1) * size - 1
|
|
var min_y = z * size
|
|
var max_y = (z + 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 = x * size
|
|
var max_x = (x + 1) * size - 1
|
|
var min_y = z * size
|
|
var max_y = (z + 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 entity_instance = Entity3D.instantiate()
|
|
entity_instance.id = entity.id
|
|
entity_instance.position = entity_position
|
|
entity_instance.connect_to_world(entity_instance.id)
|
|
add_child(entity_instance)
|
|
|