files-game/table/card_faces.gd
2026-10-01 17:22:18 -04:00

65 lines
2.4 KiB
GDScript

@tool
class_name CardFaces
## Card materials, made on first use and shared. Each card name gets one face material, so every
## copy of a card uses the same one, and all cards share one back and one edge material.
##
## A card's face is res://assets/cards/<slug>.png. The slug is the name in lowercase with each run
## of other characters turned into "-": "Carbon NanoBots" is carbon-nanobots, and "Execute: Force
## Reboot" is execute-force-reboot. Faces need the import settings in godot-plan.md §4 (VRAM
## Compressed, High Quality, mipmaps); copy the .import file of an existing face.
const CARD_DIR := "res://assets/cards/"
const BACK_TEXTURE := CARD_DIR + "card-back.png"
const SHADER := preload("res://table/card.gdshader")
## Edge color, from card.glb's CardBase material.
const EDGE_COLOR := Color(0.051, 0.049, 0.055)
## card.glb's face and back UVs are a quarter turn out (see card.gdshader). Set this to false
## once they're fixed in Blender.
const FIX_GLB_UVS := true
static var _faces: Dictionary[String, ShaderMaterial] = {}
static var _back: ShaderMaterial
static var _edge: ShaderMaterial
static var _slug_re := RegEx.create_from_string("[^a-z0-9]+")
static func slug(card_name: String) -> String:
return _slug_re.sub(card_name.to_lower(), "-", true).lstrip("-").rstrip("-")
## The face material for a card. A card you can't see ("") and a card with no face image yet
## show the back.
static func face(card_name: String) -> ShaderMaterial:
if card_name.is_empty():
return back()
if not _faces.has(card_name):
var path := CARD_DIR + slug(card_name) + ".png"
if ResourceLoader.exists(path):
_faces[card_name] = _material(load(path), false)
else:
push_warning("No card face for %s at %s" % [card_name, path])
_faces[card_name] = back()
return _faces[card_name]
static func back() -> ShaderMaterial:
if _back == null:
_back = _material(load(BACK_TEXTURE), true)
return _back
static func edge() -> ShaderMaterial:
if _edge == null:
_edge = _material(null, false)
_edge.set_shader_parameter("albedo_color", EDGE_COLOR)
_edge.set_shader_parameter("rim_glow", false)
return _edge
static func _material(texture: Texture2D, is_back: bool) -> ShaderMaterial:
var m := ShaderMaterial.new()
m.shader = SHADER
m.set_shader_parameter("albedo_texture", texture)
m.set_shader_parameter("swap_uv", FIX_GLB_UVS)
m.set_shader_parameter("mirror_u", FIX_GLB_UVS and is_back)
return m