The island as a hand-drawn nautical chart: paper land with ink contours every 5 m (heavier every 25 m) and a coastline, the sea in the sea's ink, place names drawn from the seed, the barge's route, and the player's mark. The land the coming tide will flood is hatched, and land the sea has already taken is washed with the sea, so the forecast isn't told by colour alone. - Flood.forecast(level, structures): a copy of the flood spread to a level as update() would, with today's breakwaters. Changes nothing. - ChartMap: FULL (opened and closed with the new "map" binding, M) and MINI (240 m around the player, top left, during the drop and play). The paper is drawn once per world (about 270 ms) and shared; the forecast overlay is redrawn at most once a second, and only when the forecast level, the flood or the pieces changed. - PlaceNames: names from original coastal words and a word for each kind of place, the same for every player of a match. - make chart-captures renders the chart before and after the first tide. Solo matches have no teammates; ChartMap draws `mates` when duos (M7) give it some. Closes #51 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
197 lines
6.5 KiB
GDScript
197 lines
6.5 KiB
GDScript
## Which key or mouse button does what. Each action has one input, held as a code: a
|
|
## physical keycode (positions, so they sit the same on any layout), or a mouse button
|
|
## as its negative index (-1 left, -2 right, ...).
|
|
##
|
|
## The number keys do double duty by design: slot N's key also picks build piece N in
|
|
## build mode, so the builds have no actions of their own.
|
|
##
|
|
## bind() refuses an input another action already uses and names that action, so the
|
|
## settings menu can say what clashes. Escape is kept for menus and can't be bound.
|
|
class_name KeyBindings
|
|
extends RefCounted
|
|
|
|
## Every action, in the order the settings menu lists them, with its default input.
|
|
const ACTIONS: Array[Dictionary] = [
|
|
{"id": "forward", "label": "Forward", "default": KEY_W},
|
|
{"id": "back", "label": "Back", "default": KEY_S},
|
|
{"id": "left", "label": "Left", "default": KEY_A},
|
|
{"id": "right", "label": "Right", "default": KEY_D},
|
|
{"id": "jump", "label": "Jump", "default": KEY_SPACE},
|
|
{"id": "crouch", "label": "Crouch", "default": KEY_CTRL},
|
|
{"id": "sprint", "label": "Sprint", "default": KEY_SHIFT},
|
|
{"id": "fire", "label": "Use, fire or place", "default": -MOUSE_BUTTON_LEFT},
|
|
{"id": "aim", "label": "Aim", "default": -MOUSE_BUTTON_RIGHT},
|
|
{"id": "reload", "label": "Reload", "default": KEY_R},
|
|
{"id": "interact", "label": "Pick up or open", "default": KEY_E},
|
|
{"id": "drop", "label": "Drop held item", "default": KEY_G},
|
|
{"id": "build", "label": "Build mode", "default": KEY_B},
|
|
{"id": "material", "label": "Next material", "default": KEY_TAB},
|
|
{"id": "map", "label": "Chart", "default": KEY_M},
|
|
{"id": "slot_0", "label": "Boathook / wall", "default": KEY_1},
|
|
{"id": "slot_1", "label": "Slot 1 / floor", "default": KEY_2},
|
|
{"id": "slot_2", "label": "Slot 2 / ramp", "default": KEY_3},
|
|
{"id": "slot_3", "label": "Slot 3 / roof", "default": KEY_4},
|
|
{"id": "slot_4", "label": "Slot 4 / breakwater", "default": KEY_5},
|
|
{"id": "slot_5", "label": "Slot 5", "default": KEY_6},
|
|
]
|
|
## Kept for opening and closing menus.
|
|
const RESERVED: Array[int] = [KEY_ESCAPE]
|
|
|
|
## Action id -> code.
|
|
var codes: Dictionary = {}
|
|
|
|
|
|
func _init() -> void:
|
|
reset()
|
|
|
|
|
|
## Every action back to its default.
|
|
func reset() -> void:
|
|
codes.clear()
|
|
for action: Dictionary in ACTIONS:
|
|
codes[action["id"]] = int(action["default"])
|
|
|
|
|
|
static func has_action(action: String) -> bool:
|
|
return _index_of(action) >= 0
|
|
|
|
|
|
static func default_of(action: String) -> int:
|
|
var index: int = _index_of(action)
|
|
return int(ACTIONS[index]["default"]) if index >= 0 else 0
|
|
|
|
|
|
static func label_of(action: String) -> String:
|
|
var index: int = _index_of(action)
|
|
return String(ACTIONS[index]["label"]) if index >= 0 else action
|
|
|
|
|
|
func code_of(action: String) -> int:
|
|
return int(codes.get(action, 0))
|
|
|
|
|
|
## The action using `code`, other than `except`, or "" if none.
|
|
func action_using(code: int, except: String = "") -> String:
|
|
for action: String in codes:
|
|
if action != except and int(codes[action]) == code:
|
|
return action
|
|
return ""
|
|
|
|
|
|
## Binds `action` to `code`. Returns "" when done, or why not: "unknown action",
|
|
## "reserved", "not an input", or the id of the action that already uses it (nothing
|
|
## changes then).
|
|
func bind(action: String, code: int) -> String:
|
|
if not has_action(action):
|
|
return "unknown action"
|
|
if RESERVED.has(code):
|
|
return "reserved"
|
|
if not is_valid_code(code):
|
|
return "not an input"
|
|
var other: String = action_using(code, action)
|
|
if not other.is_empty():
|
|
return other
|
|
codes[action] = code
|
|
return ""
|
|
|
|
|
|
## Every action whose input another action also uses (only possible in a hand-edited
|
|
## settings file; load_from() repairs those).
|
|
func conflicts() -> Array[String]:
|
|
var found: Array[String] = []
|
|
for action: String in codes:
|
|
if not action_using(int(codes[action]), action).is_empty():
|
|
found.append(action)
|
|
found.sort()
|
|
return found
|
|
|
|
|
|
## True if the input for `action` is held now.
|
|
func is_held(action: String) -> bool:
|
|
var code: int = code_of(action)
|
|
if code < 0:
|
|
return Input.is_mouse_button_pressed(-code as MouseButton)
|
|
if code > 0:
|
|
return Input.is_physical_key_pressed(code as Key)
|
|
return false
|
|
|
|
|
|
## The action a press event is for, or "".
|
|
func action_for_event(event: InputEvent) -> String:
|
|
var code: int = code_of_event(event)
|
|
return action_using(code) if code != 0 else ""
|
|
|
|
|
|
## The code of a key or mouse button press, or 0 for anything else.
|
|
static func code_of_event(event: InputEvent) -> int:
|
|
var key: InputEventKey = event as InputEventKey
|
|
if key != null and key.pressed and not key.echo:
|
|
return int(key.physical_keycode) if key.physical_keycode != KEY_NONE else int(key.keycode)
|
|
var button: InputEventMouseButton = event as InputEventMouseButton
|
|
if button != null and button.pressed:
|
|
return -int(button.button_index)
|
|
return 0
|
|
|
|
|
|
static func is_valid_code(code: int) -> bool:
|
|
if code < 0:
|
|
return -code >= MOUSE_BUTTON_LEFT and -code <= MOUSE_BUTTON_XBUTTON2 and -code != MOUSE_BUTTON_WHEEL_UP and -code != MOUSE_BUTTON_WHEEL_DOWN
|
|
return code > 0 and not OS.get_keycode_string(code as Key).is_empty()
|
|
|
|
|
|
## A name for a code, for the settings menu.
|
|
static func describe(code: int) -> String:
|
|
if code < 0:
|
|
match -code:
|
|
MOUSE_BUTTON_LEFT:
|
|
return "Left mouse"
|
|
MOUSE_BUTTON_RIGHT:
|
|
return "Right mouse"
|
|
MOUSE_BUTTON_MIDDLE:
|
|
return "Middle mouse"
|
|
_:
|
|
return "Mouse %d" % -code
|
|
if code == 0:
|
|
return "None"
|
|
return OS.get_keycode_string(code as Key)
|
|
|
|
|
|
## Writes every binding into `config`'s "keys" section.
|
|
func write(config: ConfigFile) -> void:
|
|
for action: String in codes:
|
|
config.set_value("keys", action, int(codes[action]))
|
|
|
|
|
|
## Reads bindings from `config`. Anything missing, malformed or clashing with an
|
|
## earlier action keeps its default, and a default taken by then is left unbound
|
|
## rather than doubled.
|
|
static func read(config: ConfigFile) -> KeyBindings:
|
|
var bindings: KeyBindings = KeyBindings.new()
|
|
for action: Dictionary in ACTIONS:
|
|
var id: String = action["id"]
|
|
if not config.has_section_key("keys", id):
|
|
continue
|
|
var value: Variant = config.get_value("keys", id)
|
|
if value is int and is_valid_code(value) and not RESERVED.has(value):
|
|
bindings.codes[id] = int(value)
|
|
# Repair clashes in list order: the first action keeps its input.
|
|
var taken: Dictionary = {}
|
|
for action: Dictionary in ACTIONS:
|
|
var id: String = action["id"]
|
|
var code: int = int(bindings.codes[id])
|
|
if taken.has(code):
|
|
code = int(action["default"])
|
|
if taken.has(code):
|
|
code = 0
|
|
bindings.codes[id] = code
|
|
if code != 0:
|
|
taken[code] = true
|
|
return bindings
|
|
|
|
|
|
static func _index_of(action: String) -> int:
|
|
for index: int in ACTIONS.size():
|
|
if ACTIONS[index]["id"] == action:
|
|
return index
|
|
return -1
|