@@ -316,17 +316,21 @@ New InGame state
316316Now there is a new ECS world but the example state does not know how to render it.
317317A new state needs to be made which is aware of the new entities.
318318
319- Create a new script called ``game/states.py ``.
320- ``states `` is for derived classes, ``state `` is for the abstract class.
321- New states will be created in this module and this module will be allowed to import many first party modules without issues.
322-
323319Before adding a new state it is time to add a more complete set of directional keys.
324- These will be added as a dictionary and can be reused anytime we want to know how a key translates to a direction.
320+ Create a new module called ``game/constants.py ``.
321+ Keys will be mapped to direction using a dictionary which can be reused anytime we want to know how a key translates to a direction.
325322Use :python: `from tcod.event import KeySym ` to make ``KeySym `` enums easier to write.
326- Then add the following:
323+
324+ ``game/constants.py `` should look like this:
327325
328326.. code-block :: python
329327
328+ """ Global constants are stored here."""
329+
330+ from typing import Final
331+
332+ from tcod.event import KeySym
333+
330334 DIRECTION_KEYS : Final = {
331335 # Arrow keys
332336 KeySym.LEFT : (- 1 , 0 ),
@@ -358,6 +362,10 @@ Then add the following:
358362 KeySym.n: (1 , 1 ),
359363 }
360364
365+ Create a new module called ``game/states.py ``.
366+ ``states `` is for derived classes, ``state `` is for the abstract class.
367+ New states will be created in this module and this module will be allowed to import many first party modules without issues.
368+
361369Create a new :python: `class InGame: ` decorated with :python: `@attrs.define() `.
362370States will always use ``g.world `` to access the ECS registry.
363371
@@ -472,48 +480,15 @@ It should be at the same level as the ``for`` loop and not inside of it.
472480
473481 from __future__ import annotations
474482
475- from typing import Final
476-
477483 import attrs
478484 import tcod.console
479485 import tcod.event
480- from tcod.event import KeySym
481486
482487 import g
483488 from game.components import Gold, Graphic, Position
489+ from game.constants import DIRECTION_KEYS
484490 from game.tags import IsItem, IsPlayer
485491
486- DIRECTION_KEYS : Final = {
487- # Arrow keys
488- KeySym.LEFT : (- 1 , 0 ),
489- KeySym.RIGHT : (1 , 0 ),
490- KeySym.UP : (0 , - 1 ),
491- KeySym.DOWN : (0 , 1 ),
492- # Arrow key diagonals
493- KeySym.HOME : (- 1 , - 1 ),
494- KeySym.END : (- 1 , 1 ),
495- KeySym.PAGEUP : (1 , - 1 ),
496- KeySym.PAGEDOWN : (1 , 1 ),
497- # Keypad
498- KeySym.KP_4 : (- 1 , 0 ),
499- KeySym.KP_6 : (1 , 0 ),
500- KeySym.KP_8 : (0 , - 1 ),
501- KeySym.KP_2 : (0 , 1 ),
502- KeySym.KP_7 : (- 1 , - 1 ),
503- KeySym.KP_1 : (- 1 , 1 ),
504- KeySym.KP_9 : (1 , - 1 ),
505- KeySym.KP_3 : (1 , 1 ),
506- # VI keys
507- KeySym.h: (- 1 , 0 ),
508- KeySym.l: (1 , 0 ),
509- KeySym.k: (0 , - 1 ),
510- KeySym.j: (0 , 1 ),
511- KeySym.y: (- 1 , - 1 ),
512- KeySym.b: (- 1 , 1 ),
513- KeySym.u: (1 , - 1 ),
514- KeySym.n: (1 , 1 ),
515- }
516-
517492
518493 @attrs.define ()
519494 class InGame :
0 commit comments