44 lines
1.1 KiB
GDScript3
44 lines
1.1 KiB
GDScript3
|
|
class_name TableEntry
|
|||
|
|
extends RefCounted
|
|||
|
|
## One possible result on a table. An entry may pipe into other tables, and on a
|
|||
|
|
## ranged (dice) table it covers a span of die values such as 01–30.
|
|||
|
|
|
|||
|
|
var id: String = ""
|
|||
|
|
var text: String = ""
|
|||
|
|
## IDs of the tables this entry rolls on next.
|
|||
|
|
var pipes: Array[String] = []
|
|||
|
|
var has_range: bool = false
|
|||
|
|
var range_lo: int = 1
|
|||
|
|
var range_hi: int = 1
|
|||
|
|
|
|||
|
|
|
|||
|
|
func _init(p_id: String = "", p_text: String = "") -> void:
|
|||
|
|
id = p_id
|
|||
|
|
text = p_text
|
|||
|
|
|
|||
|
|
|
|||
|
|
## How many die faces this entry covers. Plain entries cover exactly one.
|
|||
|
|
func span() -> int:
|
|||
|
|
return range_hi - range_lo + 1 if has_range else 1
|
|||
|
|
|
|||
|
|
|
|||
|
|
func to_dict() -> Dictionary:
|
|||
|
|
var d := {"id": id, "text": text}
|
|||
|
|
if not pipes.is_empty():
|
|||
|
|
d["pipes"] = pipes.duplicate()
|
|||
|
|
if has_range:
|
|||
|
|
d["range"] = [range_lo, range_hi]
|
|||
|
|
return d
|
|||
|
|
|
|||
|
|
|
|||
|
|
static func from_dict(d: Dictionary) -> TableEntry:
|
|||
|
|
var e := TableEntry.new(str(d.get("id", "")), str(d.get("text", "")))
|
|||
|
|
for p in d.get("pipes", []):
|
|||
|
|
e.pipes.append(str(p))
|
|||
|
|
var r: Variant = d.get("range")
|
|||
|
|
if r is Array and r.size() == 2:
|
|||
|
|
e.has_range = true
|
|||
|
|
e.range_lo = int(r[0])
|
|||
|
|
e.range_hi = int(r[1])
|
|||
|
|
return e
|