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

123 lines
3.6 KiB
GDScript

@tool
class_name Card3D
extends Node3D
## A card on the table. Every card uses this one scene: showing a different card only swaps the
## face material, which CardFaces shares between all copies of that card. The origin is the
## card's center, so it flips and turns in place.
## Surfaces of the card mesh in assets/card.glb.
const FACE := 0
const BACK := 1
const EDGE := 2
## Half the card's thickness: a card at this height lies on the table.
const REST_HEIGHT := 0.005
## Seconds a card takes to slide to a new spot.
const GLIDE_TIME := 0.28
## Every card jumps instead of sliding (for tests).
static var instant := false
## The card's name, as in cards.csv. Leave it empty for a card you can't see, such as one in the
## bot's hand: both sides then show the back.
@export var card_name := "":
set(value):
if value == card_name and _mesh != null:
return
card_name = value
_apply_materials()
## Face up or face down: a half turn around the card's long axis.
@export var face_up := true:
set(value):
face_up = value
rotation.z = 0.0 if value else PI
## Glow for legal targets and selections. Alpha is the strength; 0 is off.
@export var highlight := Color(0, 0, 0, 0):
set(value):
if value == highlight:
return
highlight = value
_apply_uniforms()
## Grays the card out, for disabled Hardware and Programs. 0 is normal, 1 dark gray.
@export_range(0.0, 1.0) var dim := 0.0:
set(value):
if value == dim:
return
dim = value
_apply_uniforms()
## What clicking or hovering this card means to the table, e.g. "loc:C2" or "hand:Hotfix" (see
## Moves). Empty: not clickable.
var pick_key := "":
set(value):
pick_key = value
if _body != null:
_body.set_meta("key", value)
_body.input_ray_pickable = value != ""
_body.collision_layer = 1 if value != "" else 0
var _mesh: MeshInstance3D
var _body: StaticBody3D
var _tween: Tween
## Where the last glide was headed, so an unchanged target doesn't restart it.
var _goal := Transform3D()
var _goal_set := false
func _ready() -> void:
_mesh = $Model.find_children("*", "MeshInstance3D")[0]
_apply_materials()
_apply_uniforms()
if not Engine.is_editor_hint():
_body = StaticBody3D.new()
var shape := CollisionShape3D.new()
var box := BoxShape3D.new()
box.size = Vector3(1.27, 0.02, 1.778)
shape.shape = box
_body.add_child(shape)
_body.set_meta("card", self)
add_child(_body)
pick_key = pick_key
## Whether the card casts shadows. Off for cards held up to the camera.
func set_shadows(on: bool) -> void:
if _mesh != null:
_mesh.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_ON if on else GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
## Whether the card has been sent anywhere yet (a new card has no spot until its first glide).
func has_goal() -> bool:
return _goal_set
## Slides the card to `xf` (in its parent's space), or jumps there when `instant`.
func glide(xf: Transform3D, instant := false) -> void:
if _goal_set and _goal.is_equal_approx(xf):
return
_goal = xf
_goal_set = true
if _tween != null:
_tween.kill()
if instant or Card3D.instant or not is_inside_tree():
transform = xf
return
_tween = create_tween().set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT)
_tween.tween_property(self, "transform", xf, GLIDE_TIME)
func _apply_materials() -> void:
if _mesh == null:
return
_mesh.set_surface_override_material(FACE, CardFaces.face(card_name))
_mesh.set_surface_override_material(BACK, CardFaces.back())
_mesh.set_surface_override_material(EDGE, CardFaces.edge())
func _apply_uniforms() -> void:
if _mesh == null:
return
_mesh.set_instance_shader_parameter("highlight", highlight)
_mesh.set_instance_shader_parameter("dim", dim)