hanafuda/HanafudaCard.gd
2024-09-02 13:26:07 +02:00

105 lines
2.6 KiB
GDScript

@tool
class_name HanafudaCard
extends Node2D
const CardValue = preload("res://CardValue.gd")
@export var revealed: bool = true: set = reveal
@export var month: int: set = _set_card_month
@export var type: int: set = _set_card_type
var value: CardValue = CardValue.new(Enums.Month.JANUARY, Enums.Type.LIGHT)
@onready var _is_ready := true
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
var texture_path := card_texture()
$Frontside.texture = load(texture_path)
reveal(revealed)
func init_card(value_: CardValue) -> HanafudaCard:
value = value_
return self
func card_texture() -> String:
var m := ""
match value.month:
Enums.Month.JANUARY: m = "January"
Enums.Month.FEBRUARY: m = "February"
Enums.Month.MARCH: m = "March"
Enums.Month.APRIL: m = "April"
Enums.Month.MAY: m = "May"
Enums.Month.JUNE: m = "June"
Enums.Month.JULY: m = "July"
Enums.Month.AUGUST: m = "August"
Enums.Month.SEPTEMBER: m = "September"
Enums.Month.OCTOBER: m = "October"
Enums.Month.NOVEMBER: m = "November"
Enums.Month.DECEMBER: m = "December"
var t := ""
match value.type:
Enums.Type.LIGHT: t = "Hikari"
Enums.Type.ANIMAL: t = "Tane"
Enums.Type.RIBBON: t = "Tanzaku"
Enums.Type.SCRAP_1: t = "Kasu_1"
Enums.Type.SCRAP_2: t = "Kasu_2"
Enums.Type.SCRAP_3: t = "Kasu_3"
var texture_path := "res://assets/png/Hanafuda_{month}_{type}.png".format({"month": m, "type": t})
return texture_path
func reveal(new_reveal: bool) -> void:
revealed = new_reveal
if not _is_ready:
await self.ready
if revealed:
$Frontside.show()
$Backside.hide()
else:
$Frontside.hide()
$Backside.show()
func _set_card_month(new_month: int) -> void:
value.month = new_month
func _set_card_type(new_type: int) -> void:
value.type = new_type
func set_size(new_size: Vector2) -> void:
if not _is_ready:
await self.ready
var texture_size = $Frontside.texture.get_size()
var scale = new_size / texture_size
self.scale = scale
func set_min_dimension(dim: int) -> void:
if not _is_ready:
await self.ready
var texture_size = $Frontside.texture.get_size()
var scale = Vector2(dim, dim) / texture_size
var max_scale = max(scale.x, scale.y)
self.scale = Vector2(max_scale, max_scale)
func set_max_dimension(dim: int) -> void:
if not _is_ready:
await self.ready
var texture_size = $Frontside.texture.get_size()
var scale = Vector2(dim, dim) / texture_size
var min_scale = min(scale.x, scale.y)
self.scale = Vector2(min_scale, min_scale)
func get_size() -> Vector2:
if not _is_ready:
await self.ready
var texture_size = $Frontside.texture.get_size()
return texture_size * self.scale