/
painter.py
244 lines (203 loc) · 8.24 KB
/
painter.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# coding=UTF-8
import __builtin__
from unicurses import *
from enum import Terrain, Vegetation
import globals
def replaceChar(str,idx,char):
_str = str[:idx] + char + str[idx+1:]
return _str
class Colors:
DEFAULT = 1
WATER_BORDER = 2
WATER_PLAYER = 3
GRASS_BORDER = 4
GRASS_PLAYER = 5
GRASS_FOREST = 6
GRASS_CITY = 7
def __init__(self):
init_pair(Colors.DEFAULT, COLOR_WHITE, COLOR_BLACK)
init_pair(Colors.WATER_BORDER, COLOR_WHITE,COLOR_BLUE)
init_pair(Colors.WATER_PLAYER, COLOR_RED, COLOR_BLUE)
init_pair(Colors.GRASS_BORDER, COLOR_WHITE,COLOR_GREEN)
init_pair(Colors.GRASS_PLAYER, COLOR_RED, COLOR_GREEN)
init_pair(Colors.GRASS_FOREST, COLOR_GREEN, COLOR_GREEN)
init_pair(Colors.GRASS_CITY, COLOR_CYAN, COLOR_GREEN)
self.DEFAULT = color_pair(Colors.DEFAULT)
self.WATER_BORDER = color_pair(Colors.WATER_BORDER)
self.WATER_PLAYER = color_pair(Colors.WATER_PLAYER) + A_BOLD
self.GRASS_BORDER = color_pair(Colors.GRASS_BORDER)
self.GRASS_PLAYER = color_pair(Colors.GRASS_PLAYER) + A_BOLD
self.GRASS_FOREST = color_pair(Colors.GRASS_FOREST) + A_BOLD
self.GRASS_CITY = color_pair(Colors.GRASS_CITY) + A_BOLD
class Symbols:
BLANK = ' '
V_BORDER = '|'
R_BORDER = '/'
L_BORDER = '\\'
PLAYER = '@'
TREE = '8'
CITY = 'M'
WORKER = ';'
class Cell():
def __init__(self, symbol, color):
self.symbol = symbol
self.color = color
def set(self, symbol, color):
self.symbol = symbol
self.color = color
class Painter():
def __init__(self,rows,cols,window):
self.draw_borders = False
self.colors = Colors()
self.window = window
box(self.window)
self.cam_size = [globals.CAM_WIDTH, globals.CAM_HEIGHT]
self.cam_pos = [0,0]
#map_height: 3 + 2*(rows-1) + 2 for border
#map_width: 5 + 4*cols + 2 for border
self.world_size = [5+4*cols, 3+2*(rows-1)]
x = self.findCenterCoords(cols, 0)[0]+1
y = self.findCenterCoords(0, rows)[1]
self.cell_matrix = list()
for k in range(y):
_ls = list()
for j in range(x):
cell = Cell(Symbols.BLANK, Colors.DEFAULT)
_ls.append(cell)
self.cell_matrix.append(_ls)
def updateTile(self,tile):
self.updateTileCenter(tile)
self.updateTileEdges(tile)
self.updateTileBorders(tile)
def updateTileCenter(self,tile):
x,y = tile.pos
pos_x, pos_y = self.findCenterCoords(x,y)
if tile.has_player:
char = "@"
color = self.colors.GRASS_PLAYER
elif tile.has_worker:
char = ";"
color = self.colors.GRASS_CITY
elif tile.has_city:
char = "M"
color = self.colors.GRASS_CITY
else:
if tile.vegetation == Vegetation.FOREST:
char = "8"
color = self.colors.GRASS_FOREST
else:
char = " "
if tile.terrain == Terrain.WATER:
color = self.colors.WATER_BORDER
else:
color = self.colors.GRASS_BORDER
#update
self.cell_matrix[pos_y][pos_x].symbol = char
self.cell_matrix[pos_y][pos_x].color = color
#draw
#wmove(self.window, pos_y+1, pos_x+1)
#waddstr(self.window, char,color)
def updateTileEdges(self,tile):
if tile.vegetation == Vegetation.NONE:
char = " "
else: #if forest
char = "8"
if tile.terrain == Terrain.WATER:
color = self.colors.WATER_BORDER
else:
color = self.colors.GRASS_FOREST
pos_x, pos_y = self.findCenterCoords(tile.pos[0], tile.pos[1])
#update
self.cell_matrix[pos_y-1][pos_x].symbol = char
self.cell_matrix[pos_y-1][pos_x].color = color
self.cell_matrix[pos_y+1][pos_x].symbol = char
self.cell_matrix[pos_y+1][pos_x].color = color
self.cell_matrix[pos_y][pos_x-1].symbol = char
self.cell_matrix[pos_y][pos_x-1].color = color
self.cell_matrix[pos_y][pos_x+1].symbol = char
self.cell_matrix[pos_y][pos_x+1].color = color
def updateTileBorders(self, tile):
if tile.terrain == Terrain.WATER:
color = self.colors.WATER_BORDER
else:
color = self.colors.GRASS_BORDER
pos_x, pos_y = self.findCenterCoords(tile.pos[0], tile.pos[1])
#char cells
if self.draw_borders:
self.cell_matrix[pos_y-1][pos_x-1].set(Symbols.R_BORDER, color)
self.cell_matrix[pos_y-1][pos_x+1].set(Symbols.L_BORDER, color)
self.cell_matrix[pos_y][pos_x-2].set(Symbols.V_BORDER, color)
self.cell_matrix[pos_y][pos_x+2].set(Symbols.V_BORDER, color)
self.cell_matrix[pos_y+1][pos_x-1].set(Symbols.L_BORDER, color)
self.cell_matrix[pos_y+1][pos_x+1].set(Symbols.R_BORDER, color)
else:
self.cell_matrix[pos_y-1][pos_x-1].set(Symbols.BLANK, color)
self.cell_matrix[pos_y-1][pos_x+1].set(Symbols.BLANK, color)
self.cell_matrix[pos_y][pos_x-2].set(Symbols.BLANK, color)
self.cell_matrix[pos_y][pos_x+2].set(Symbols.BLANK, color)
self.cell_matrix[pos_y+1][pos_x-1].set(Symbols.BLANK, color)
self.cell_matrix[pos_y+1][pos_x+1].set(Symbols.BLANK, color)
def updateAllTiles(self, world):
water = __builtin__.filter(world.isWater, world.tiles)
grass = __builtin__.filter(world.isFlat, world.tiles)
for tile in grass:
self.updateTile(tile)
for tile in water:
self.updateTile(tile)
def updateAllTileBorders(self, world):
water = __builtin__.filter(world.isWater, world.tiles)
grass = __builtin__.filter(world.isFlat, world.tiles)
for tile in grass:
self.updateTileBorders(tile)
for tile in water:
self.updateTileBorders(tile)
#returns char cell coordinate
def findCenterCoords(self,x,y):
row = (1 + 2*y)
column = 4*x
if y % 2 == 0:
column += 4
else:
column += 2
return [column, row]
def moveCamera(self,x,y):
if x > 0:
#can full shift be made?
if self.cam_pos[0] + globals.CAM_WIDTH -2 + x <= self.world_size[0]:
self.cam_pos[0] += x
self.drawWindow()
#is the camera already at the border?
elif not self.cam_pos[0] == self.world_size[0] - self.cam_size[0] + 2:
self.cam_pos[0] = self.world_size[0] - self.cam_size[0] + 2
self.drawWindow()
elif x < 0:
if self.cam_pos[0]+x >= 0:
self.cam_pos[0] += x
self.drawWindow()
elif not self.cam_pos[0] == 0:
self.cam_pos[0] = 0
self.drawWindow()
if y > 0:
if self.cam_pos[1] + globals.CAM_HEIGHT -2 + y <= self.world_size[1]:
self.cam_pos[1] += y
self.drawWindow()
elif not self.cam_pos[1] == self.world_size[1] - self.cam_size[1] + 2:
self.cam_pos[1] = self.world_size[1] - self.cam_size[1] + 2
self.drawWindow()
elif y < 0:
if self.cam_pos[1] + y >= 0:
self.cam_pos[1] += y
self.drawWindow()
elif not self.cam_pos[1] == 0:
self.cam_pos[1] = 0
self.drawWindow()
#redraws all tiles within camera
def drawWindow(self):
for x in range(self.cam_pos[0], self.cam_pos[0]+self.cam_size[0] -2):
for y in range(self.cam_pos[1], self.cam_pos[1] + self.cam_size[1]-2):
wmove(self.window,y-self.cam_pos[1]+1, x-self.cam_pos[0]+1)
waddstr(self.window, self.cell_matrix[y][x].symbol, self.cell_matrix[y][x].color)
self.showChanges()
def showChanges(self):
update_panels()
doupdate()