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

77 lines
2.7 KiB

extends Node3D
const Entity3D = preload("res://scenes/Instance.tscn")
@onready var ground = $Ground
@onready var camera =$Camera
@onready var highlighter = $Highlighter
@onready var gridmap = $GridMap
var highlight = {}
var selection = {}
func _ready():
highlight = reset_highlight()
selection = reset_highlight()
func _process(delta):
if Input.is_action_just_pressed("alt_command"):
if highlight.type == 1:
selection = highlight
else:
selection = reset_highlight()
var entity = Global.world.add_entity(Vector2i(highlight.grid_position.x, highlight.grid_position.y))
var entity_instance = Entity3D.instantiate()
entity_instance.id = entity.id
entity_instance.position = highlight.data.position
entity_instance.connect_to_world(entity_instance.id)
add_child(entity_instance)
if Input.is_action_just_pressed("main_command"):
if selection.type == 1:
var path = Global.world.entities[selection.data.id].path_create(Global.world.get_real_coordinates(Vector2i(highlight.data.position.x, highlight.data.position.z)))
Global.world.entities[selection.data.id].path_start()
func _unhandled_input(event):
if event is InputEventMouseMotion:
highlight = get_highlight()
if highlight.type == 0:
highlighter.transparency = 0.0
var sprite_position = Vector3(round(highlight.board_position.x), ceil(highlight.board_position.y), round(highlight.board_position.z)) + Vector3(0, 0.01, 0)
highlighter.position = sprite_position
else:
highlighter.transparency = 1.0
func get_highlight():
var result = reset_highlight()
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)
if raycast_result:
var collider = raycast_result.collider
if "Chunk" in raycast_result.collider.name or "Ocean" in raycast_result.collider.name:
var position = gridmap.local_to_map(raycast_result.position)
# var position = collider.get_used_cells()[raycast_result.shape] * Vector3i(collider.x, 1, collider.z)
result["type"] = 0
result["board_position"] = position
result["grid_position"] = Global.world.get_real_coordinates(Vector2i(position.x, position.z))
result["data"] = Global.world.get_bloc(result["grid_position"])
elif "id" in raycast_result.collider:
result["type"] = 1
result["data"] = Global.world.entities[raycast_result.collider.id].get_data()
return result
func reset_highlight():
return {
"type": -1,
"data": null
}