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

64 lines
2.1 KiB

extends Node3D
@onready var grid = $Grid
@onready var camera =$Camera3D
@onready var highlighter = $Highlighter
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()
if Input.is_action_just_pressed("main_command"):
if selection.type == 1:
Global.world.entities[selection.data.id].move(Vector2(highlight.data.position.x, highlight.data.position.z))
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.data.position.x), ceil(highlight.data.position.y), round(highlight.data.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:
if raycast_result.collider.name == "Grid":
var position = grid.get_used_cells()[raycast_result.shape]
result["type"] = 0
result["data"] = Global.world.get_bloc(Vector2i(position.x, position.z))
if raycast_result.collider.name == "Ocean":
var position = grid.get_used_cells()[raycast_result.shape]
result["type"] = 0
result["data"] = Global.world.get_bloc(Vector2i(grid.local_to_map(raycast_result.position).x, grid.local_to_map(raycast_result.position).z))
elif raycast_result.collider.get("id"):
result["type"] = 1
result["data"] = Global.world.entities[raycast_result.collider.id].get_data()
return result
func reset_highlight():
return {
"type": -1,
"data": null
}