TableTower/core/roll_table_factory.gd

73 lines
3.3 KiB
GDScript3
Raw Normal View History

class_name RollTableFactory
extends RefCounted
## Makes new, ready-to-use cards of every kind (RollTable.create hands its
## work to here). Each kind starts with sensible entries and settings, so a
## fresh card already rolls or asks something.
## A well-formed card of any kind. `sub` picks the variant: the choice
## ("pick", "number", "text"), the loop mode ("times", "while", "until") or
## the variable type ("number", "text", "list", "collection", "scale").
## roll: four "Result N" entries · choice pick: three options ·
## choice number/text: one "Answer" entry · loop: "Each time" and
## "Afterwards" · variable: no entries · sheet: one template entry ·
## router: an "if …" case (when ROUTER_DEFAULT_WHEN) and an "Otherwise" case ·
## check: "Success" and "Failure" (2d6 vs 8) · variable "collection": the
## stem is the name's slug without a final "s" ("Skills" -> "skill").
static func create(kind: String, id: String, card_name: String, sub: String = "") -> RollTable:
var table := RollTable.new(id, card_name)
table.kind = kind if RollTable.KINDS.has(kind) else RollTable.KIND_ROLL
match table.kind:
RollTable.KIND_ROLL:
for n in range(1, 5):
table.entries.append(TableEntry.new("e%d" % n, "Result %d" % n))
RollTable.KIND_CHOICE:
_fill_choice(table, sub)
RollTable.KIND_LOOP:
table.loop_mode = sub if RollTable.LOOPS.has(sub) else "times"
table.loop_expr = "3" if table.loop_mode == "times" else "i < 3"
table.entries.append(TableEntry.new("e1", "Each time"))
table.entries.append(TableEntry.new("e2", "Afterwards"))
RollTable.KIND_VARIABLE:
_fill_variable(table, card_name, sub)
RollTable.KIND_SHEET:
table.entries.append(TableEntry.new("e1", "[b]%s[/b]" % card_name))
RollTable.KIND_ROUTER:
var first := TableEntry.new("e1", "if …")
first.when = RollTable.ROUTER_DEFAULT_WHEN
table.entries.append(first)
table.entries.append(TableEntry.new("e2", "Otherwise"))
RollTable.KIND_CHECK:
table.entries.append(TableEntry.new("e1", "Success"))
table.entries.append(TableEntry.new("e2", "Failure"))
return table
## A choice card: its question and its starting options or answer.
static func _fill_choice(table: RollTable, sub: String) -> void:
table.choice = sub if RollTable.CHOICES.has(sub) else "pick"
match table.choice:
"pick":
table.prompt = "Which one?"
for n in range(1, 4):
table.entries.append(TableEntry.new("e%d" % n, "Option %d" % n))
"number":
table.prompt = "How many?"
table.entries.append(TableEntry.new("e1", "Answer"))
"text":
table.prompt = "What is it?"
table.entries.append(TableEntry.new("e1", "Answer"))
## A variable card: its type, its name (from the card name) and its start.
static func _fill_variable(table: RollTable, card_name: String, sub: String) -> void:
table.var_type = sub if RollTable.VAR_TYPES.has(sub) else "number"
table.var_name = JsonIO.slug(Formatter.strip_tags(card_name), "var") if card_name != "" else "var"
var stem := table.var_name
if table.var_type == RollTable.VAR_COLLECTION and stem.length() > 3 and stem.ends_with("s") and not stem.ends_with("ss"):
table.var_name = stem.substr(0, stem.length() - 1)
table.initial = "0" if table.var_type == "number" else ""
if table.var_type == RollTable.VAR_SCALE:
table.steps.assign(["Low", "Medium", "High"])
table.initial = "Low"