extends Node2D

@export var tile_type: int = 0
var grid_position: Vector2i

@onready var sprite: Sprite2D = $Sprite2D

# Target size for each tile to fit in the 64x64 grid cells
const TILE_SIZE = 48  # Slightly smaller than 64 to leave some padding

var tile_textures = [
	preload("res://assets/sprites/gems/bg_19.png"),
	preload("res://assets/sprites/gems/dg_19.png"),
	preload("res://assets/sprites/gems/gg_19.png"),
	preload("res://assets/sprites/gems/mg_19.png"),
]

func _set_tile_type(value: int) -> void:
	tile_type = value
	if value >= 0 and value < tile_textures.size():
		sprite.texture = tile_textures[value]
		_scale_sprite_to_fit()

func _scale_sprite_to_fit() -> void:
	if sprite.texture:
		var texture_size = sprite.texture.get_size()
		var max_dimension = max(texture_size.x, texture_size.y)
		var scale_factor = TILE_SIZE / max_dimension
		sprite.scale = Vector2(scale_factor, scale_factor)

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	_set_tile_type(tile_type)
