Advertisement
xFazz

No field of view

Feb 14th, 2022
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. ***tile_types.py***
  2. from typing import Tuple
  3.  
  4. import numpy as np # type: ignore
  5.  
  6. # Tile graphics are structured type compatible with Consoles.tiles_rgb
  7. graphic_dt = np.dtype(
  8. [
  9. ("ch", np.int32), # Unicode codepoint
  10. ("fg", "3B"), # 3 unsigned bytes, for RGB colors
  11. ("bg", "3B")
  12. ]
  13. )
  14.  
  15. # Tile struct used for statically defined tile data.
  16. tile_dt = np.dtype(
  17. [
  18. ("walkable", np.bool), # True if this tile can be walked over
  19. ("transparent", np.bool), # True if this tile doesn't block FOV.
  20. ("dark", graphic_dt), # Graphics for when this tile is not in FOV.
  21. ]
  22. )
  23.  
  24. def new_tile(
  25. *, # Enforce the use of keywords, so that parameter order doesn't matter
  26. walkable: int,
  27. transparent: int,
  28. dark: Tuple[int, Tuple[int, int, int], Tuple[int, int, int]],
  29. ) -> np.ndarray:
  30. """Helper function for defining individual tile types """
  31. return np.array((walkable, transparent, dark), dtype=tile_dt)
  32.  
  33. floor = new_tile(
  34. walkable=True, transparent=True, dark=(ord(" "), (255, 255, 255), (50, 50, 150))
  35. )
  36. wall = new_tile(
  37. walkable=False, transparent=False, dark=(ord(" "), (255, 255, 255), (0, 0, 100)),
  38. )
  39.  
  40. ***engine.py***
  41. from typing import Set, Iterable, Any
  42.  
  43. from tcod.context import Context
  44. from tcod.console import Console
  45.  
  46. from actions import EscapeAction, MovementAction
  47. from entity import Entity
  48. from game_map import GameMap
  49. from input_handlers import EventHandler
  50.  
  51. class Engine:
  52. def __init__(self, entities: Set[Entity], event_handler: EventHandler, game_map: GameMap, player: Entity):
  53. self.entities = entities
  54. self.event_handler = event_handler
  55. self.game_map = game_map
  56. self.player = player
  57.  
  58. def handle_events(self, events: Iterable[Any]) -> None:
  59. for event in events:
  60. action = self.event_handler.dispatch(event) # Essentially (event_handler.ev_quit or event_handler.ev_keydown)
  61.  
  62. if action is None: # Remember that action is either None or an action subclass.
  63. continue
  64.  
  65. action.perform(self, self.player)
  66.  
  67. def render(self, console: Console, context: Context) -> None:
  68. self.game_map.render(console)
  69.  
  70. for entity in self.entities:
  71. console.print(entity.x, entity.y, entity.char, fg=entity.color)
  72.  
  73. context.present(console)
  74. console.clear()
  75.  
  76. ***game_map.py***
  77. import numpy as np
  78. from tcod.console import Console
  79.  
  80. import tile_types
  81.  
  82. class GameMap:
  83. def __init__(self, width: int, height: int):
  84. self.width, self.height = width, height
  85. self.tiles = np.full((width, height), fill_value=tile_types.wall, order = "F")
  86.  
  87. def in_bounds(self, x: int, y: int) -> bool:
  88. """Return True if x and y are inside of the bounds of this map."""
  89. return 0 <= x < self.width and 0 <= y < self.height
  90.  
  91. def render(self, console: Console) -> None:
  92. console.tiles_rgb[0:self.width, 0:self.height] = self.tiles["dark"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement