-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_map.py
101 lines (78 loc) · 3.67 KB
/
game_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from typing import Iterable, Iterator, Optional, TYPE_CHECKING
import numpy as np # type: ignore
import tcod
from tcod.console import Console
from PIL import Image
from entity import Actor
import tile_types
from entity import Entity
from globals import *
class GameMap:
def __init__(self, engine, width: int, height: int, entities: Iterable[Entity] = ()):
self.engine = engine
self.width, self.height = width, height
self.entities = set(entities)
self.tiles = np.full((width, height), fill_value=tile_types.wall, order="F")
self.visible = np.full((width, height), fill_value=False, order="F") # Tiles the player can now see
self.explored = np.full((width, height), fill_value=False, order="F") # Tiles the player has seen before
@property
def actors(self) -> Iterator[Actor]:
"""Iterate over this maps living actors."""
yield from (
entity
for entity in self.entities
if isinstance(entity, Actor) and entity.is_alive
)
def get_blocking_entity_at_location(self, location_x: int, location_y: int) -> Optional[Entity]:
for entity in self.entities:
if (
entity.blocks_movement
and entity.x == location_x
and entity.y == location_y
):
return entity
return None
def get_actor_at_location(self, x: int, y: int) -> Optional[Actor]:
for actor in self.actors:
if actor.x == x and actor.y == y:
return actor
return None
def in_bounds(self, x: int, y: int) -> bool:
"""Return True if x and y are inside of the bounds of this map."""
return 0 <= x < self.width and 0 <= y < self.height
def render(self, console: Console) -> None:
"""
Renders the map.
If a tile is in the "visible" array, then draw it with the "light" colors.
If it isn't, but it's in the "explored" array, then draw it with the "dark" colors.
Otherwise, the default is "SHROUD".
console.tiles_rgb[0:self.width, 0:self.height] = self.tiles["dark"]
"""
console.tiles_rgb[0: self.width, 0: self.height] = np.select(
condlist=[self.visible, self.explored],
choicelist=[self.tiles["light"], self.tiles["dark"]],
default=tile_types.SHROUD,
)
entities_sorted_for_rendering = sorted(
self.entities, key=lambda x: x.render_order.value
)
img_temp = Image.open("1bit_imitate/LOTR/tiles/tiles31.png")
img_temp = img_temp.convert("RGB")
# console.draw_semigraphics(pixels=img_temp, x=entity.x, y=entity.y)
for entity in entities_sorted_for_rendering:
# Only print entities that are in the FOV
if self.visible[entity.x, entity.y]:
# test out some features here...
# Used to be: console.print(x=entity.x, y=entity.y, string=entity.char, fg=entity.color)
if entity.char == '@':
console.put_char(x=entity.x, y=entity.y, ch=tcod.tileset.CHARMAP_CP437[18])
elif entity.char == 'S':
console.put_char(x=entity.x, y=entity.y, ch=9829)
elif entity.char == 'D':
console.put_char(x=entity.x, y=entity.y, ch=9830)
elif entity.char == 'W':
console.put_char(x=entity.x, y=entity.y, ch=9827)
else:
console.put_char(x=entity.x, y=entity.y, ch=tcod.tileset.CHARMAP_CP437[15])
##
##