You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.7 KiB
GDScript

2 years ago
@tool
## A node representing MetSys room.
##
## RoomInstance is a node that allows for integration between scenes and MetSys database. In editor it will provide drawing for room bounds and in game it provides some helper methods for interacting with the map data. [code]RoomInstance.tscn[/code] scene is provided for convenience. You should add it to every scene used as MetSys room. It's recommended to put the node at [code](0, 0)[/code] global coordinates.
extends Node3D
2 years ago
var GRID_COLOR: Color
var GRID_PASSAGE_COLOR: Color
var cells: Array[Vector3i]
var initialized: bool
var room_name: String
var min_cell := Vector2i(999999, 999999)
var max_cell := Vector2i(-999999, -999999)
var layer: int
func _enter_tree() -> void:
1 year ago
if not Engine.is_editor_hint():
MetSys.current_room = self
if initialized:
return
initialized = true
if Engine.is_editor_hint():
MetSys.room_assign_updated.connect(_update_assigned_scene)
var theme: Theme = load("res://addons/MetroidvaniaSystem/Database/DatabaseTheme.tres")
GRID_COLOR = theme.get_color(&"scene_cell_border", &"MetSys")
GRID_PASSAGE_COLOR = theme.get_color(&"scene_room_exit", &"MetSys")
_update_assigned_scene()
2 years ago
func _exit_tree() -> void:
1 year ago
if MetSys.current_room == self:
MetSys.current_room = null
2 years ago
func _update_assigned_scene():
1 year ago
var owner_node := owner if owner != null else self
room_name = owner_node.scene_file_path.trim_prefix(MetSys.settings.map_root_folder)
cells = MetSys.map_data.get_cells_assigned_to(room_name)
if cells.is_empty():
return
layer = cells[0].z
for p in cells:
min_cell.x = mini(min_cell.x, p.x)
min_cell.y = mini(min_cell.y, p.y)
max_cell.x = maxi(max_cell.x, p.x)
max_cell.y = maxi(max_cell.y, p.y)
2 years ago
## Returns the full size of this room, based on the cells and [code]in_game_cell_size[/code] defined in MetSys Settings.
func get_size() -> Vector2:
1 year ago
return Vector2(max_cell - min_cell + Vector2i.ONE) * MetSys.settings.in_game_cell_size
2 years ago
## Returns this rooms cells in local coordinates, i.e. with [code](0, 0)[/code] being the top-left cell.
func get_local_cells() -> Array[Vector2i]:
1 year ago
var ret: Array[Vector2i]
ret.assign(cells.map(func(coords: Vector3i) -> Vector2i:
return Vector2i(coords.x - min_cell.x, coords.y - min_cell.y)))
return ret
2 years ago
## Returns the top-left cell's flat coordinates within the room's rectangle.
func get_base_coords() -> Vector2i:
1 year ago
return min_cell
2 years ago
## Returns the bottom-right cell's flat coordinates within the room's rectangle.
func get_end_coords() -> Vector2i:
1 year ago
return max_cell
2 years ago
## Returns the room's layer.
func get_layer() -> int:
1 year ago
return layer