91 lines
3.6 KiB
GDScript
91 lines
3.6 KiB
GDScript
class_name BoardLayout
|
||
## Where things sit on the table, in world units (1 unit is 5 cm, see godot-plan.md §4). Worked out
|
||
## from the current board's rows like app/src/layout.ts: a row is the letter of a location id, the
|
||
## starts sit beyond the first and last rows, and each row is centred. South's side is +z and
|
||
## North's is -z; the camera sits on your side, so the world never turns around.
|
||
|
||
## The playmat (assets/playmat.glb): 36 × 30 in, its top face at y = 0.
|
||
const MAT_SIZE := Vector2(18.288, 15.24)
|
||
const CARD_SIZE := Vector2(1.27, 1.778)
|
||
## Height of a card lying on the mat, and between stacked cards.
|
||
const CARD_Y := 0.006
|
||
const LAYER := 0.0105
|
||
## Gap between card centres side by side.
|
||
const CARD_STEP := 1.36
|
||
|
||
static var at: Dictionary = {}
|
||
static var row_pitch := 0.0
|
||
static var col_pitch := 0.0
|
||
static var tile_size := Vector2.ZERO
|
||
static var _built_for := ""
|
||
|
||
|
||
## Recomputes the positions for GameMap's current board, if it changed.
|
||
static func update() -> void:
|
||
var key := ",".join(GameMap.LOCATIONS)
|
||
if key == _built_for:
|
||
return
|
||
_built_for = key
|
||
var rows := []
|
||
for loc in GameMap.GRID:
|
||
if not rows.has(loc[0]):
|
||
rows.append(loc[0])
|
||
rows.sort()
|
||
var widest := 0
|
||
for r in rows:
|
||
widest = maxi(widest, GameMap.GRID.filter(func(l): return l[0] == r).size())
|
||
row_pitch = MAT_SIZE.y / (rows.size() + 2)
|
||
col_pitch = minf(6.0, (MAT_SIZE.x - 0.5) / widest)
|
||
tile_size = Vector2(col_pitch - 0.4, row_pitch - 0.12)
|
||
at = {}
|
||
var row_z := func(i: int) -> float: return (rows.size() + 1) * row_pitch / 2.0 - i * row_pitch
|
||
for i in rows.size():
|
||
var locs: Array = GameMap.GRID.filter(func(l): return l[0] == rows[i])
|
||
locs.sort_custom(func(a, b): return int(a.substr(1)) < int(b.substr(1)))
|
||
for j in locs.size():
|
||
at[locs[j]] = Vector3((j - (locs.size() - 1) / 2.0) * col_pitch, 0, row_z.call(i + 1))
|
||
at[GameMap.START_LOC[0]] = Vector3(0, 0, row_z.call(0))
|
||
at[GameMap.START_LOC[1]] = Vector3(0, 0, row_z.call(rows.size() + 1))
|
||
|
||
|
||
## Midpoint of a line between two locations.
|
||
static func line_mid(line: Dictionary) -> Vector3:
|
||
return (at[line.a] + at[line.b]) / 2.0
|
||
|
||
|
||
## A location's Server card, at the tile's left end as seen from South.
|
||
static func server_spot(loc: String) -> Vector3:
|
||
return at[loc] + Vector3(-tile_size.x / 2 + CARD_SIZE.x / 2 + 0.08, CARD_Y, 0)
|
||
|
||
|
||
## Programs overlap in a short row at the tile's right end, the newest on top.
|
||
static func program_spot(loc: String, i: int, n: int) -> Vector3:
|
||
var right: float = at[loc].x + tile_size.x / 2 - CARD_SIZE.x / 2 - 0.08
|
||
var step := 0.42
|
||
return Vector3(right - (n - 1 - i) * step, CARD_Y + i * LAYER, at[loc].z)
|
||
|
||
|
||
## A player's Anomaly slots, deck, Server pile and discard, on the mat beside their start: the
|
||
## slots to its left (slot 4 nearest), the piles to its right, as the owner sees them. North's
|
||
## mirror South's through the centre. The bot's hand lies face down past the far edge of the mat.
|
||
static func zone_spot(p: int, zone: String, slot := 0) -> Vector3:
|
||
var row_z: float = at[GameMap.START_LOC[0]].z
|
||
var inner := tile_size.x / 2 + 0.2 + CARD_SIZE.x / 2
|
||
var spot := Vector3.ZERO
|
||
match zone:
|
||
"slot":
|
||
spot = Vector3(-(inner + (4 - slot) * CARD_STEP), CARD_Y, row_z)
|
||
"deck":
|
||
spot = Vector3(inner, 0, row_z)
|
||
"serverPile":
|
||
spot = Vector3(inner + CARD_STEP, 0, row_z)
|
||
"discard":
|
||
spot = Vector3(inner + 2 * CARD_STEP, 0, row_z)
|
||
"hand":
|
||
spot = Vector3(0, CARD_Y, MAT_SIZE.y / 2 + CARD_SIZE.y / 2 + 0.6)
|
||
return spot if p == 0 else Vector3(-spot.x, spot.y, -spot.z)
|
||
|
||
|
||
## Yaw so a card reads the right way up for player p (North's cards face North).
|
||
static func yaw_for(p: int) -> float:
|
||
return 0.0 if p == 0 else PI
|