Files
ai-game-2/MainGame.gd
Bryce 9b5a7040c2 Add get_current_room_name() helper to MainGame
Provides a convenient way to identify the current room at runtime by
extracting the room name from the scene script's resource path, rather
than relying on node names (which are always 'background').
2026-04-29 15:54:24 -07:00

124 lines
4.8 KiB
GDScript

extends Node2D
var cursors = [load("res://boot_icon.png"), load("res://eye_icon.png"), load("res://hand_icon.png"), load("res://speech_icon.png")]
var hourglass_cursor = load("res://hourglass_icon.png")
var item_cursor: Texture2D = null
var previous_cursor_index: int = 0
var is_script_running: bool = false
var is_cursor_locked: bool = false # When true, hourglass is shown and cursor can't be changed
func get_scene() -> Scene:
return $SceneViewport/background
func get_current_room_name() -> String:
var scene = get_scene()
if scene and scene.get_script():
var script_path = scene.get_script().resource_path
if script_path.begins_with("res://scenes/"):
return script_path.trim_suffix(".gd").get_file()
return ""
# Called when the node enters the scene tree for the first time.
func _ready():
# get_scene().connect("transitioned", Callable($SceneViewport/label, "_on_transitioned"))
get_scene().connect("transitioned", Callable(self, "_on_transitioned"))
reset_camera(get_scene())
ActionState.set_current_scene(get_scene())
pass # Replace with function body.
func _on_transitioned(scene):
reset_camera(scene)
$SceneViewport/label._on_transitioned(scene)
ActionState.set_current_scene(scene)
camera.position = Vector2(0,0 )
#scene.connect("transitioned", $SceneViwport/label, "_on_transitioned")
scene.connect("transitioned", Callable(self, "_on_transitioned"))
@onready var camera:Camera2D = $SceneViewport/PlayerTracker/Camera2D
func reset_camera(scene):
#$Node2D.scale = Vector2(scene.background_scale, scene.background_scale)
#var far_point = self.to_global(Vector2(scene.find_child("bg").texture.get_width(), scene.find_child("bg").texture.get_height() ) )
var x:Sprite2D = scene.find_child("bg");
var height = x.texture.get_height()
var far_point = self.to_global(Vector2(x.texture.get_width(), x.texture.get_height() ) * x.get_global_transform())
camera.make_current()
camera.limit_right = far_point.x
camera.limit_bottom=far_point.y
get_scene().get_node("Graham/RemoteTransform2D").remote_path=$SceneViewport/PlayerTracker.get_path()
camera.reset_smoothing()
camera.limit_right = far_point.x
camera.limit_bottom = far_point.y
#$Node2D/background/Graham/Camera2D.reset_smoothing()
#$Node2D/background/Graham/Camera2D.reset_smoothing()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var player = get_scene().get_node("Graham")
$SceneViewport/PlayerTracker/Camera2D.set_target(player.facing)
var s = get_scene().ego_scale(player)
player.scale = Vector2(s, s)
# Update cursor if script state changed
var script = $GameScript.current_script
if script and not is_script_running:
set_script_cursor()
elif not script and is_script_running:
restore_cursor()
func set_script_cursor() -> void:
previous_cursor_index = ActionState.current_action
is_script_running = true
is_cursor_locked = true # Lock cursor to hourglass
Input.set_custom_mouse_cursor(hourglass_cursor)
func restore_cursor() -> void:
is_script_running = false
is_cursor_locked = false # Unlock cursor
if previous_cursor_index == ActionState.Action.ITEM:
if item_cursor:
Input.set_custom_mouse_cursor(item_cursor)
else:
Input.set_custom_mouse_cursor(cursors[ActionState.Action.WALK])
else:
Input.set_custom_mouse_cursor(cursors[previous_cursor_index])
func _unhandled_input(event: InputEvent) -> void:
$SceneViewport.push_input(event)
func _on_backpack_show_overlay() -> void:
$InventoryOverlayLayer/InventoryOverlay.show_overlay()
func _on_backpack_hide_overlay() -> void:
$InventoryOverlayLayer/InventoryOverlay.hide_overlay()
func _on_overlay_item_confirmed(item_id: String) -> void:
InventoryManager.select_item(item_id)
var def = InventoryManager.get_item_definition(item_id)
if def and def.icon:
item_cursor = def.icon
Input.set_custom_mouse_cursor(item_cursor)
ActionState.current_action = ActionState.Action.ITEM
else:
Input.set_custom_mouse_cursor(cursors[ActionState.Action.WALK])
func _input(event):
if event.is_action_released("quit"):
get_tree().quit()
if event.is_action_released("right_click") and not is_cursor_locked:
var prev_action = ActionState.current_action
ActionState.current_action = (ActionState.current_action + 1) % 5
if ActionState.current_action == ActionState.Action.ITEM:
if InventoryManager.selected_item and item_cursor:
Input.set_custom_mouse_cursor(item_cursor, Input.CursorShape.CURSOR_ARROW, Vector2(0,0))
else:
ActionState.current_action = (ActionState.current_action + 1) % 5
Input.set_custom_mouse_cursor(cursors[ActionState.current_action], Input.CursorShape.CURSOR_ARROW, Vector2(0,0))
else:
if prev_action == ActionState.Action.ITEM:
InventoryManager.clear_selection()
Input.set_custom_mouse_cursor(cursors[ActionState.current_action], Input.CursorShape.CURSOR_ARROW, Vector2(0,0))