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.
133 lines
5.1 KiB
133 lines
5.1 KiB
extends GridMap
|
|
class_name Chunk
|
|
|
|
const Pawn = preload("res://scenes/Pawn.tscn")
|
|
const WATER_HEIGHT = 0.5
|
|
|
|
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 OceanTile = preload("res://scenes/Water.tscn");
|
|
var water = OceanTile.instantiate()
|
|
water.mesh.size.x = size
|
|
water.mesh.size.y = size
|
|
water.translate(Vector3(size/2, WATER_HEIGHT, size/2))
|
|
add_child(water)
|
|
|
|
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)
|
|
|