hadean-godot/Scripts/TileTransform.gd

48 lines
1.1 KiB
GDScript
Raw Normal View History

2024-05-13 18:16:23 -04:00
@tool
extends Node2D
2025-09-24 20:01:45 -04:00
class_name TileTransform
2024-05-13 18:16:23 -04:00
@export var size: Vector2i = Vector2i.ONE:
set(val):
size = val
2025-10-10 03:01:08 -04:00
update_world_from_tile()
queue_redraw()
@export var tile_pos: Vector2i = Vector2i.ZERO:
set(val):
tile_pos = val
update_world_from_tile()
queue_redraw()
2024-05-13 18:16:23 -04:00
2025-09-24 20:01:45 -04:00
var grid: Grid
2024-05-13 18:16:23 -04:00
func _ready():
2025-10-10 03:01:08 -04:00
if Engine.is_editor_hint(): set_notify_transform(true)
2025-09-24 20:01:45 -04:00
grid = Util.find_parent_type(self, Grid)
2025-10-10 03:01:08 -04:00
update_world_from_tile()
2024-05-13 18:16:23 -04:00
func _enter_tree():
2025-10-10 03:01:08 -04:00
if Engine.is_editor_hint(): set_notify_transform(true)
2024-05-13 18:16:23 -04:00
func _exit_tree():
if grid != null:
grid.remove_tile(self)
func _notification(what: int) -> void:
if what == NOTIFICATION_TRANSFORM_CHANGED:
2025-10-10 03:01:08 -04:00
if Engine.is_editor_hint(): _editor_position_changed()
2024-05-13 18:16:23 -04:00
2025-10-10 03:01:08 -04:00
func update_world_from_tile():
if grid == null: return
var worldbox = grid.tile2world_rect(Rect2i(tile_pos, size))
position = worldbox.get_center()
2024-05-13 18:16:23 -04:00
2025-10-10 03:01:08 -04:00
func _editor_position_changed():
2024-05-13 18:16:23 -04:00
var top_left_world_pos = position - grid.tile2world_size(size - Vector2i.ONE) / 2
2025-10-10 03:01:08 -04:00
tile_pos = grid.world2tile(top_left_world_pos)
2024-05-13 18:16:23 -04:00
func get_worldbox():
2025-10-10 03:01:08 -04:00
return grid.tile2world_rect(get_tilebox())
2024-05-13 18:16:23 -04:00
func get_tilebox():
2025-10-10 03:01:08 -04:00
return Rect2i(tile_pos, size)