Files
Cozypaw-Hospital/scripts/systems/room_navigator.gd
T

126 lines
3.7 KiB
GDScript

## RoomNavigator — autoload that moves the Camera2D smoothly between hospital floors, rooms, and the home/garden area.
extends Node
signal room_changed(floor_index: int, room_index: int)
signal home_entered()
signal hospital_entered()
const FLOOR_HEIGHT: float = 720.0
const ROOM_WIDTH: float = 1280.0
const HOME_CAMERA_X: float = 640.0
const HOME_CAMERA_Y: float = 1080.0
const CAMERA_TWEEN_DURATION: float = 0.6
const HOME_ROOM_NAME: String = "garden_party"
const _ROOM_NAMES: Dictionary = {
Vector2i(0, 0): "reception",
Vector2i(0, 1): "giftshop",
Vector2i(0, 2): "restaurant",
Vector2i(0, 3): "emergency",
Vector2i(1, 0): "xray",
Vector2i(1, 1): "pharmacy",
Vector2i(1, 2): "lab",
Vector2i(1, 3): "patient_rooms",
Vector2i(2, 0): "ultrasound",
Vector2i(2, 1): "delivery_room",
Vector2i(2, 2): "nursery",
}
var _current_floor: int = 0
var _current_room: int = 0
var _is_at_home: bool = false
var _camera: Camera2D
var _active_tween: Tween
func initialize(camera: Camera2D) -> void:
_camera = camera
func go_to_floor(floor_index: int) -> void:
go_to_room(floor_index, 0)
func go_to_room(floor_index: int, room_index: int) -> void:
if not _is_at_home and floor_index == _current_floor and room_index == _current_room:
return
_is_at_home = false
_current_floor = floor_index
_current_room = room_index
var room_name: String = _ROOM_NAMES.get(Vector2i(floor_index, room_index), "")
if not room_name.is_empty():
GameState.set_current_room(room_name)
if _camera == null:
return
var target_x: float = room_index * ROOM_WIDTH + ROOM_WIDTH * 0.5
var target_y: float = floor_index * -FLOOR_HEIGHT + FLOOR_HEIGHT * 0.5
if _active_tween != null:
_active_tween.kill()
_active_tween = create_tween()
_active_tween.set_ease(Tween.EASE_IN_OUT)
_active_tween.set_trans(Tween.TRANS_SINE)
_active_tween.tween_property(_camera, "position", Vector2(target_x, target_y), CAMERA_TWEEN_DURATION)
_active_tween.finished.connect(func() -> void: room_changed.emit(floor_index, room_index))
func go_to_home() -> void:
if _is_at_home:
return
_is_at_home = true
GameState.set_current_room(HOME_ROOM_NAME)
if _camera == null:
return
if _active_tween != null:
_active_tween.kill()
_active_tween = create_tween()
_active_tween.set_ease(Tween.EASE_IN_OUT)
_active_tween.set_trans(Tween.TRANS_SINE)
_active_tween.tween_property(_camera, "position", Vector2(HOME_CAMERA_X, HOME_CAMERA_Y), CAMERA_TWEEN_DURATION)
_active_tween.finished.connect(func() -> void: home_entered.emit())
func go_to_hospital() -> void:
if not _is_at_home:
return
_is_at_home = false
var room_name: String = _ROOM_NAMES.get(Vector2i(_current_floor, _current_room), "")
if not room_name.is_empty():
GameState.set_current_room(room_name)
if _camera == null:
return
var target_x: float = _current_room * ROOM_WIDTH + ROOM_WIDTH * 0.5
var target_y: float = _current_floor * -FLOOR_HEIGHT + FLOOR_HEIGHT * 0.5
if _active_tween != null:
_active_tween.kill()
_active_tween = create_tween()
_active_tween.set_ease(Tween.EASE_IN_OUT)
_active_tween.set_trans(Tween.TRANS_SINE)
_active_tween.tween_property(_camera, "position", Vector2(target_x, target_y), CAMERA_TWEEN_DURATION)
_active_tween.finished.connect(func() -> void: hospital_entered.emit())
func go_to_room_by_name(room_name: String) -> void:
if room_name == HOME_ROOM_NAME:
go_to_home()
return
for key: Vector2i in _ROOM_NAMES:
if _ROOM_NAMES[key] == room_name:
go_to_room(key.x, key.y)
return
func get_room_name(floor_index: int, room_index: int) -> String:
return _ROOM_NAMES.get(Vector2i(floor_index, room_index), "")
func get_current_floor() -> int:
return _current_floor
func get_current_room() -> int:
return _current_room
func is_at_home() -> bool:
return _is_at_home