controls.pyΒΆ
Part of Clear Code Zelda.
1"""What the player can do, declared once and carried by the nodes that read it.
2
3The gameplay verbs live on an :class:`InputContext` the player node holds, so
4they are live exactly while the player is in the tree and go away with it. The
5shell and menu actions sit on the scene root's ``input_context``, which is in
6force while that root is mounted, whether by ``set_root`` or ``change_scene``, so a headless run,
7a web export and a test that loads one scene all get them without anybody
8calling a setup function.
9
10The pad mapping is the one a controller player expects from a top-down action
11game: A swings, B casts, the shoulders cycle the weapon and the spell, and
12Start opens the upgrade screen.
13"""
14
15from __future__ import annotations
16
17from simvx.core import Axis1D, Axis2D, Controller, InputContext, Key, Mouse
18
19#: Walking and fighting. Movement is one typed action rather than four opposed
20#: ones, so WASD, the arrows, a controller's left stick and the drawn pad's
21#: stick all feed it and whichever is furthest from rest wins.
22EXPLORING = InputContext(
23 {
24 "move": [
25 Axis2D(up=Key.W, down=Key.S, left=Key.A, right=Key.D),
26 Axis2D(up=Key.UP, down=Key.DOWN, left=Key.LEFT, right=Key.RIGHT),
27 Controller.LEFT_STICK,
28 ],
29 "attack": [Key.SPACE, Controller.A],
30 "magic": [Key.LEFT_CONTROL, Key.RIGHT_CONTROL, Controller.B],
31 "weapon_swap": [Key.Q, Controller.LEFT_BUMPER],
32 "magic_swap": [Key.E, Controller.RIGHT_BUMPER],
33 },
34 name="exploring",
35)
36
37#: What stays available whatever is on screen: leaving the title card, opening
38#: and closing the upgrade screen, driving it, and quitting. A tap counts for
39#: starting because the web runtime delivers touch as a left click, and every
40#: one of them has a pad spelling so a controller never has to be put down.
41SHELL = {
42 "start": [Key.ENTER, Key.SPACE, Mouse.LEFT, Controller.A, Controller.START],
43 "upgrade_menu": [Key.M, Controller.START],
44 "ui_nav": [
45 Axis1D(low=Key.LEFT, high=Key.RIGHT),
46 Axis1D(low=Key.A, high=Key.D),
47 Controller.LEFT_X,
48 ],
49 "ui_select": [Key.SPACE, Key.ENTER, Controller.A],
50 "ui_cancel": [Controller.B],
51 "quit": [Key.ESCAPE],
52}