/
terrain.py
140 lines (113 loc) · 3.55 KB
/
terrain.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""
Stores game map and classes like Tile, Map, etc.
The levels are set in game.py, generated in mapgen.py.
This file is imported by anything that needs to access the game's levels.
"""
import tcod
from config import DIAGONAL_MOVEMENT, MAP_WIDTH, MAP_HEIGHT
import mapgen
import game
class Tile:
"""
A tile of the map and its properties
"""
blocked = False
blocks_sight = False
char = '.'
fg_color = tcod.white
bg_color = tcod.black
# a tile starts unexplored
explored = False
name = 'floor'
class UpStairs(Tile):
fg_color = tcod.black
bg_color = tcod.white
char = '<'
name = 'upwards staircase'
blocked = True
class DownStairs(Tile):
fg_color = tcod.black
bg_color = tcod.white
char = '>'
name = 'downwards staircase'
blocked = True
class Floor(Tile):
pass # tile defaults are floor
class EndFloor(Tile):
char = '>'
class Wall(Tile):
blocked = True
blocks_sight = True
fg_color = tcod.black
bg_color = tcod.white
char = ' '
name = 'wall'
class Map:
def __init__(self, width, height):
self.width = width
self.height = height
self.objects = []
self.scroll_amount = 0
# make a grid of generic floor tiles
self.tiles = [[Floor()
for y in range(height)]
for x in range(width)]
self.init_fov_and_pathfinding()
self.player_start_pos = (0, height // 2)
def init_fov_and_pathfinding(self):
print("initting fov and pathfinding")
# init fov
self.fov_map = tcod.map_new(self.width, self.height)
self.update_fov_map()
# init pathfinding
self.path = tcod.path_new_using_map(self.fov_map, self.diagonal_cost)
@property
def diagonal_cost(self):
if not DIAGONAL_MOVEMENT:
# with a cost of 0, the pathfinding won't do diagonal movement
return 0
else:
return 1.414
def __getitem__(self, i):
# Map[x][y] returns the tile at (x, y)
return self.tiles[i]
def update_fov_map(self):
for x in range(self.width):
for y in range(self.height):
self.update_fov_tile(x, y)
def update_fov_tile(self, x, y):
tile = self[x][y]
tcod.map_set_properties(self.fov_map, x, y,
not tile.blocks_sight,
not self.is_blocked(x, y))
def update_pathfinding(self):
tcod.path_delete(self.path)
self.path = tcod.path_new_using_map(self.fov_map, self.diagonal_cost)
def is_blocked(self, x, y):
# return true if tile is outside of map
if x < 0 or y < 0 or x >= self.width or y >= self.height:
return True
# test the map tile
if self[x][y].blocked:
return True
# now check for any blocking objects
for object in self.objects:
if object.blocks and (object.x, object.y) == (x, y):
return True
return False
def scroll(self):
# scrolls map by one column
# scrolls all objects, too
# deletes all objects on leftmost column
for object in self.objects[:]:
object.x -= 1
if object.x < 0:
object.dead = True
#game.purge_dead_objects()
self.tiles.pop(0)
# generate new map column
self.scroll_amount += 1
mapgen.generate_new_map_column(self)
self.update_fov_map()
self.update_pathfinding()
map = Map(MAP_WIDTH, MAP_HEIGHT)