extends Node3D @onready var ground = $Ground @onready var camera =$Camera @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 == "pawn": selection = highlight else: selection = reset_highlight() var entity = Global.world.add_entity("BaseUnit", Vector2i(highlight.grid_position.x, highlight.grid_position.y)) if Input.is_action_just_pressed("main_command"): if selection.type == "pawn": if selection.entity is Unit: selection.entity.walk_to(Global.world.get_real_coordinates(Vector2i(round(highlight.data.position.x), round(highlight.data.position.z)))) func _unhandled_input(event): if event is InputEventMouseMotion: if highlight != get_highlight(): if highlight.type == "pawn": highlight.pawn.unhighlight() highlight = get_highlight() if highlight.type == "case": highlighter.transparency = 0.0 var sprite_position = Vector3((highlight.board_position.x), (highlight.board_position.y), (highlight.board_position.z)) + Vector3(0, 0.05, 0) highlighter.position = sprite_position else: highlighter.transparency = 1.0 if highlight.type == "pawn": highlight.pawn.highlight() 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 = round(raycast_result.position) position.y = Global.world.get_height(Vector2i(position.x, position.z)) if "Ocean" in raycast_result.collider.name: position.y += ground.WATER_HEIGHT result["type"] = "case" 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"] = "pawn" result["pawn"] = raycast_result.collider result["entity"] = Global.world.entities[raycast_result.collider.id] return result func reset_highlight(): return { "type": null, "data": null }