files-game/table/card_3d.gd
2026-10-01 23:01:56 -04:00

274 lines
8.7 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 (at normal speed; see Motion).
const GLIDE_TIME := 0.26
## Speed trails: ghosts left per unit moved, how fast a card must move to leave them, how long one
## lasts.
const TRAIL_DENSITY := 2.5
const TRAIL_MIN_SPEED := 10.0
const TRAIL_LIFE := 0.15
## Most ghosts a card keeps at once.
const TRAIL_MAX := 14
const TRAIL_SHADER := preload("res://present/trail.gdshader")
## 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
var _flash_tween: Tween
var _flash := Color(0, 0, 0, 0)
## Speed trail: whether one is on, its color, where the card was last frame, and the live ghosts.
var _trail := false
var _trail_color := Color("#22d3ee")
var _trail_from := Vector3.ZERO
var _ghosts: Array = []
static var _ghost_mesh: PlaneMesh
static var _ghost_material: ShaderMaterial
## Where the last glide was headed, so an unchanged target doesn't restart it.
var _goal := Transform3D()
var _goal_set := false
## On its way out of sight (into a hidden pile): freed when it gets there.
var _leaving := false
func _ready() -> void:
_mesh = $Model.find_children("*", "MeshInstance3D")[0]
_apply_materials()
_apply_uniforms()
# Only while a trail is on or fading (see _process).
set_process(false)
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 Motion.instant() or not is_inside_tree():
transform = xf
return
_tween = create_tween()
Motion.rush_in(_tween, self, "transform", xf, GLIDE_TIME)
## Sends the card along `path` (transforms in its parent's space, the goal last) on the motion
## curve (gameplay-plan.md §B6): it starts at path[0], rushes to path[1], drifts to path[2] and
## rushes out to the goal. Shorter paths skip the drift. `times` are each leg's seconds, `delay`
## waits first (at path[0]), and `trail` leaves a speed trail in the rushes.
func travel(path: Array, times: Array, delay := 0.0, trail := true, color := Color("#22d3ee"), leave := false) -> void:
var goal: Transform3D = path.back()
_goal = goal
_goal_set = true
_leaving = leave
if _tween != null:
_tween.kill()
if Motion.instant() or not is_inside_tree():
transform = goal
if leave:
queue_free()
return
transform = path[0]
_tween = create_tween()
if delay > 0.0:
_tween.tween_interval(Motion.secs(delay))
_tween.tween_callback(func(): _start_trail(trail, color))
var legs := path.size() - 1
for i in legs:
var t: float = times[mini(i, times.size() - 1)]
if legs >= 3 and i == 1:
Motion.drift(_tween, self, "transform", path[i + 1], t)
elif i == legs - 1 and legs > 1:
Motion.rush_out(_tween, self, "transform", path[i + 1], t)
else:
Motion.rush_in(_tween, self, "transform", path[i + 1], t)
_tween.tween_callback(func(): _trail = false)
if leave:
_tween.tween_callback(queue_free)
## Whether the card is still on its way somewhere.
func moving() -> bool:
return _tween != null and _tween.is_valid() and _tween.is_running()
## Jumps to where the card is headed (skipping its animation).
func finish() -> void:
if moving():
_tween.kill()
transform = _goal
_trail = false
if _leaving:
queue_free()
if _flash_tween != null:
_flash_tween.kill()
_set_flash(Color(0, 0, 0, 0))
## A flash of `color` that fades out over `t` seconds, after `delay`.
func flash(color: Color, t := 0.5, delay := 0.0) -> void:
if Motion.instant() or not is_inside_tree():
return
if _flash_tween != null:
_flash_tween.kill()
_flash_tween = create_tween()
if delay > 0.0:
_flash_tween.tween_interval(Motion.secs(delay))
_flash_tween.tween_method(_set_flash, Color(color, 2.2), Color(color, 0.0), Motion.secs(t)).set_trans(Tween.TRANS_EXPO).set_ease(Tween.EASE_OUT)
func _set_flash(c: Color) -> void:
_flash = c
if _mesh != null:
_mesh.set_instance_shader_parameter("flash", c)
func _start_trail(on: bool, color: Color) -> void:
_trail = on
_trail_color = color
_trail_from = position
if on:
set_process(true)
## Leaves ghosts along the way while the card rushes, and fades the old ones.
func _process(delta: float) -> void:
if Engine.is_editor_hint():
return
if _trail and get_parent() != null:
var step := position - _trail_from
var dist := step.length()
if delta > 0.0 and dist / delta >= TRAIL_MIN_SPEED:
var n := mini(3, ceili(dist * TRAIL_DENSITY))
for i in n:
_spawn_ghost(_trail_from.lerp(position, float(i + 1) / n), float(i + 1) / n)
_trail_from = position
var now := Time.get_ticks_msec() / 1000.0
while not _ghosts.is_empty() and now - _ghosts[0].born > TRAIL_LIFE:
_ghosts.pop_front().node.queue_free()
for g in _ghosts:
var k: float = 1.0 - (now - g.born) / TRAIL_LIFE
(g.node as GeometryInstance3D).set_instance_shader_parameter("fade", k * k * g.strength)
if not _trail and _ghosts.is_empty():
set_process(false)
func _spawn_ghost(at: Vector3, strength: float) -> void:
if _ghost_mesh == null:
_ghost_mesh = PlaneMesh.new()
_ghost_mesh.size = Vector2(1.27, 1.778)
_ghost_material = ShaderMaterial.new()
_ghost_material.shader = TRAIL_SHADER
var g := MeshInstance3D.new()
g.mesh = _ghost_mesh
g.material_override = _ghost_material
g.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
get_parent().add_child(g)
g.transform = Transform3D(transform.basis, at)
g.set_instance_shader_parameter("tint", _trail_color)
g.set_instance_shader_parameter("fade", strength)
# Cards held near the camera (your hand) leave fainter ghosts: they're large on screen.
var near := 0.35 if get_parent() is Node3D and get_parent().get_parent() is Camera3D else 1.0
_ghosts.append({"node": g, "born": Time.get_ticks_msec() / 1000.0, "strength": (0.15 + 0.35 * strength) * near})
if _ghosts.size() > TRAIL_MAX:
_ghosts.pop_front().node.queue_free()
func _exit_tree() -> void:
for g in _ghosts:
if is_instance_valid(g.node):
g.node.queue_free()
_ghosts = []
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)
_mesh.set_instance_shader_parameter("flash", _flash)