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/Chunks.gd

122 lines
5.0 KiB

extends GridMap
@onready var camera = $Camera3D
@onready var entities = []
const Entity3D = preload("res://scenes/Instance.tscn")
func _ready():
setFloor()
setEntities()
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(Vector2(mx, mz))
var my: float = bloc.y
var meshID
var mesh_rotation
if bloc.type != 0:
var neighbours = Global.world.get_neighbours_4_at_same_height(Vector2(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 = entities.size()
entity_instance.position = map_to_local(Vector3(entity.position.x, entity.position.y, entity.position.z))
entity_instance.connect_to_world(entity_instance.id)
entities.append(entity_instance)
add_child(entity_instance)
# TEST À ENLEVER
var inputs = {"ui_right": Vector2.RIGHT,
"ui_left": Vector2.LEFT,
"ui_up": Vector2.UP,
"ui_down": Vector2.DOWN}
func _unhandled_input(event):
for dir in inputs.keys():
if event.is_action_pressed(dir):
var new_position = Global.world.entities[23].position2D + inputs[dir]
# var new_position3d = map_to_local(Vector3(new_position.x, 0, new_position.y))
# var new_position2d = Vector2(new_position3d.x, new_position3d.z)
Global.world.entities[23].move(new_position)
if event is InputEventMouseMotion:
var position = get_mouse_position()
# print(position)
print(Global.world.get_bloc(Vector2i(position.x, position.z)))
var sprite_position = Vector3(round(position.x), ceil(position.y), round(position.z)) + Vector3(0, 0.01, 0)
# print(sprite_position)
$Sprite3D.position = sprite_position
# if event.is_action_pressed("alt_command"):
# FIN TEST À ENLEVER
func get_mouse_position():
var mouse_pos = get_viewport().get_mouse_position()
var ray_length = 1000
var from = camera.project_ray_origin(mouse_pos)
var to = from + camera.project_ray_normal(mouse_pos) * ray_length
var space = get_world_3d().direct_space_state
var ray_query = PhysicsRayQueryParameters3D.new()
ray_query.from = from
ray_query.to = to
ray_query.collide_with_areas = true
var raycast_result = space.intersect_ray(ray_query)
var position = Vector3()
if raycast_result:
position = get_used_cells()[raycast_result.shape]
return position