48 lines
1.1 KiB
GDScript
48 lines
1.1 KiB
GDScript
@tool
|
|
extends Node2D
|
|
class_name TileTransform
|
|
|
|
@export var size: Vector2i = Vector2i.ONE:
|
|
set(val):
|
|
size = val
|
|
update_world_from_tile()
|
|
queue_redraw()
|
|
@export var tile_pos: Vector2i = Vector2i.ZERO:
|
|
set(val):
|
|
tile_pos = val
|
|
update_world_from_tile()
|
|
queue_redraw()
|
|
|
|
var grid: Grid
|
|
|
|
func _ready():
|
|
if Engine.is_editor_hint(): set_notify_transform(true)
|
|
grid = Util.find_parent_type(self, Grid)
|
|
update_world_from_tile()
|
|
|
|
func _enter_tree():
|
|
if Engine.is_editor_hint(): set_notify_transform(true)
|
|
|
|
func _exit_tree():
|
|
if grid != null:
|
|
grid.remove_tile(self)
|
|
|
|
func _notification(what: int) -> void:
|
|
if what == NOTIFICATION_TRANSFORM_CHANGED:
|
|
if Engine.is_editor_hint(): _editor_position_changed()
|
|
|
|
func update_world_from_tile():
|
|
if grid == null: return
|
|
var worldbox = grid.tile2world_rect(Rect2i(tile_pos, size))
|
|
position = worldbox.get_center()
|
|
|
|
func _editor_position_changed():
|
|
var top_left_world_pos = position - grid.tile2world_size(size - Vector2i.ONE) / 2
|
|
tile_pos = grid.world2tile(top_left_world_pos)
|
|
|
|
func get_worldbox():
|
|
return grid.tile2world_rect(get_tilebox())
|
|
|
|
func get_tilebox():
|
|
return Rect2i(tile_pos, size)
|