Files
seeding-planets/gui/pointer/scripts/pointer.gd

316 lines
12 KiB
GDScript

extends Node
const DEFAULT_ACTION_COLOR = Color.WHITE
const ENERGY_ACTION_COLOR = Color("ffff2b")
const NO_ENERGY_ACTION_COLOR = Color.RED
const ZONE_OPACITY = 0.4
const ZONE_ACTIVATED_COLOR = Color("#96B3DB")
const ZONE_DEACTIVATED_COLOR = Color("#FF006E")
const CARD_VISUALISATION_TIME = 0.3
const CARD_UP_PADDING = 50
const PRESS_TIME_DRAG := 0.15
@export var default_cursor: Texture2D
@export var action_cursor: Texture2D
var all_inspected: Array[Node]
var inspected: Node = null
var inspected_card_info: CardInfo = null
var inspected_inventory_slot: InventoryGuiItemMouseDetector = null
var player: Player # renseigné par Player
var can_interact: bool = false
var current_selected_item: Item = null
var have_energy_to_use_item: bool = false
var could_use_item: bool = false
var can_use_item: bool = false
var press_time := 0.
var press_action_done := false
var dragging_inspected := false
var action_disabled := false
func get_current_inspected() -> Node:
if all_inspected.size() > 0:
var closest_ind := -1
if player:
var mouse_pos := player.get_global_mouse_position()
var closest_dist := INF
all_inspected = all_inspected.filter(func(node): return is_instance_valid(node))
for i in all_inspected.size():
var node := all_inspected[i]
if node is Node2D:
var dist = node.global_position.distance_squared_to(mouse_pos)
if dist < closest_dist:
closest_dist = dist
closest_ind = i
elif closest_ind < 0:
closest_ind = i
if all_inspected.size() > 0:
return all_inspected[closest_ind]
return null
func _ready():
Input.set_custom_mouse_cursor(default_cursor)
%Action.visible = false
func _process(delta):
if player and not action_disabled and not get_tree().paused:
process_player_actions(delta)
else:
%ActionProgressBar.value = 0.
dragging_inspected = false
%Inspector.position = get_viewport().get_mouse_position()
# if not action_disabled and not dragging_inspected and current_selected_item and SceneManager.actual_scene.scene_id == "REGION":
# %ActionZone.radius = current_selected_item.usage_zone_radius * GameInfo.settings_data.zoom
# %ActionZone.color = ZONE_ACTIVATED_COLOR if can_use_item else ZONE_DEACTIVATED_COLOR
# else:
# %ActionZone.radius = 0
# %ActionZone.queue_redraw()
update_card()
update_inspector(get_current_inspected())
update_cursor()
if player:
if dragging_inspected:
inspected.global_position = player.get_global_mouse_position()
if inspected is ItemObject:
var global_dir: Vector2 = (inspected.global_position - player.global_position).normalized()
inspected.object_sprite.tractor_beam.position = 30 * global_dir
inspected.object_sprite.tractor_beam.rotation = Vector2.RIGHT.angle_to(global_dir)
if not inspected.object_sprite.tractor_beam.emitting:
inspected.object_sprite.tractor_beam.emitting = true
func process_player_actions(delta: float):
can_interact = (
inspected
and inspected is Interactable
and player.can_interact(inspected)
)
current_selected_item = player.data.inventory.get_item()
could_use_item = (
current_selected_item
and player.preview_could_use_item(current_selected_item)
)
have_energy_to_use_item = (
current_selected_item
and player.has_energy_to_use_item(current_selected_item)
)
can_use_item = could_use_item and have_energy_to_use_item
if Input.is_action_pressed("move_pointer"):
player.try_move(
player.get_global_mouse_position()
)
if Input.is_action_just_pressed("drop"):
player.drop_item()
if Input.is_action_just_pressed("action"):
press_time = 0
press_time += delta
if (
Input.is_action_pressed("action")
):
if (
can_use_item
and current_selected_item.is_action_need_press_time()
and not press_action_done
and not can_interact
and not inspected is InventoryGuiItemMouseDetector
):
%ActionProgressBar.value = press_time / current_selected_item.get_action_press_time() * 100
if not %ActionProgressPlayer.playing:
%ActionProgressPlayer.play()
%ActionProgressPlayer.pitch_scale = 1. / (current_selected_item.get_action_press_time() / %ActionProgressPlayer.stream.get_length())
if press_time > current_selected_item.get_action_press_time():
%ActionPlayer.play()
player.try_use_item(
current_selected_item,
player.get_global_mouse_position()
)
press_action_done = true
else:
press_action_done = false
%ActionProgressPlayer.playing = false
%ActionProgressBar.value = 0.
if Input.is_action_just_pressed("action"):
if inspected is InventoryGuiItemMouseDetector:
GameInfo.game_data.player_data.inventory.set_current_item(inspected.index)
elif can_use_item and not current_selected_item.is_action_need_press_time() and current_selected_item.deactivate_interactable():
player.try_use_item(current_selected_item, player.get_global_mouse_position())
elif can_interact:
var interactable = inspected as Interactable
player.try_interact(interactable)
elif can_use_item and not current_selected_item.is_action_need_press_time():
player.try_use_item(
current_selected_item,
player.get_global_mouse_position()
)
if (
Input.is_action_just_released("action")
and can_use_item
and current_selected_item.deactivate_interactable()
):
if dragging_inspected:
player.try_use_item(current_selected_item, player.get_global_mouse_position())
else:
player.instruction = null
func inspect(node: Node):
if (
not node in all_inspected
and not dragging_inspected
):
all_inspected.append(node)
if node is InventoryGuiItemMouseDetector:
inspected_inventory_slot = node
func update_card():
if (
not inspected or inspected_card_info == null
or get_tree().paused
or action_disabled
or dragging_inspected
):
%CardVisualiser.hide()
elif inspected != null:
if inspected_card_info != %CardVisualiser.card_info:
%CardVisualiser.card_info = inspected_card_info
var camera = get_viewport().get_camera_2d()
var screen_size = get_viewport().get_visible_rect().size # * get_viewport().get_camera_2d().zoom
if inspected is InspectableEntity:
%CardPosition.position = (
(inspected.global_position - camera.global_position) * get_viewport().get_camera_2d().zoom
+ ((screen_size) / 2)
+ inspected.get_card_up_padding() * Vector2.UP
)
elif inspected is Control:
%CardPosition.position = inspected.global_position + inspected.size / 2 + CARD_UP_PADDING * Vector2.UP
elif inspected is Node3D:
%CardPosition.position = (
get_viewport().get_camera_3d().unproject_position(inspected.global_position)
+ CARD_UP_PADDING * Vector2.UP
)
%CardVisualiser.show()
func update_inspector(current_inspected):
if current_inspected:
if inspected != current_inspected:
if inspected and inspected.has_method("inspect"):
inspected.inspect(false)
inspected = current_inspected
if inspected.has_method("card_info"):
inspected_card_info = inspected.card_info()
else:
inspected_card_info = null
if inspected.has_method("inspect"):
inspected.inspect(true)
else:
if inspected and inspected.has_method("inspect"):
inspected.inspect(false)
inspected = null
if player and not action_disabled and (get_tree() and not get_tree().paused):
if dragging_inspected:
%Action.visible = true
if inspected is ItemObject:
%ActionText.text = inspected.item.name
elif inspected is InventoryGuiItemMouseDetector:
var item = GameInfo.game_data.player_data.inventory.get_item(inspected.index)
if item:
%ActionText.text = item.name
else:
%Action.visible = false
elif can_interact and inspected and inspected is Interactable and (not current_selected_item or not current_selected_item.deactivate_interactable()):
%Action.visible = true
%ActionText.text = inspected.interact_text()
%Action.modulate = DEFAULT_ACTION_COLOR if inspected.interaction_cost(player) == 0 else ENERGY_ACTION_COLOR
%ActionEnergyImage.visible = inspected.interaction_cost(player) != 0
elif current_selected_item and current_selected_item.use_text() != "":
%Action.visible = true
%ActionText.text = current_selected_item.use_text() + (tr("NO_ENERGY_LEFT") if not have_energy_to_use_item else "")
if can_use_item:
%Action.modulate = DEFAULT_ACTION_COLOR if current_selected_item.energy_usage == 0 else ENERGY_ACTION_COLOR
else:
%Action.modulate = NO_ENERGY_ACTION_COLOR
%ActionEnergyImage.visible = current_selected_item.energy_usage != 0
else:
%Action.visible = false
else:
%Action.visible = false
func update_cursor():
var cursor := default_cursor
var hotspot : = Vector2(0.,0.)
if not Pause.pause and player and not action_disabled:
if current_selected_item and current_selected_item.get_pointer():
cursor = current_selected_item.get_pointer()
if Input.is_action_pressed("action") and current_selected_item.get_clicked_pointer():
cursor = current_selected_item.get_clicked_pointer()
if current_selected_item.is_pointer_center():
hotspot = Vector2(cursor.get_width()/2., cursor.get_height()/2.)
elif can_interact:
cursor = action_cursor
Input.set_custom_mouse_cursor(cursor,0,hotspot)
func stop_inspect(node: Node):
if not dragging_inspected:
all_inspected.erase(node)
if node is InventoryGuiItemMouseDetector:
inspected_inventory_slot = null
func start_dragging():
if not dragging_inspected:
dragging_inspected = true
if inspected is ItemObject:
inspected.mouse_over = false
elif inspected is InventoryGuiItemMouseDetector:
var item_to_drop: Item = GameInfo.game_data.player_data.inventory.get_item(inspected.index)
if item_to_drop and item_to_drop.type != Item.ItemType.TOOL_ITEM:
GameInfo.game_data.player_data.inventory.remove_item_at(inspected.index)
var dropped_item_object := player.terrain.drop_item(item_to_drop, player.get_global_mouse_position())
inspected_inventory_slot = inspected
inspected = dropped_item_object
player.region.save()
else:
dragging_inspected = false
if dragging_inspected:
all_inspected.clear()
all_inspected.append(inspected)
can_interact = false
func stop_dragging():
if dragging_inspected:
dragging_inspected = false
if inspected is ItemObject:
inspected.object_sprite.tractor_beam.emitting = false
if (
inspected_inventory_slot is InventoryGuiItemMouseDetector
and GameInfo.game_data.player_data.inventory.add_item_at(inspected.item, inspected_inventory_slot.index)
):
all_inspected.clear()
inspected.queue_free()
inspected = null