-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamemap.py
307 lines (254 loc) · 9.33 KB
/
gamemap.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import cPickle
import curses
import textout
import color
import configs
import textfield
import init
LEVEL_WIDTH = 256
LEVEL_HEIGHT = 256
def nothing():
pass
def loadFromFile(filename, theWorld):
try:
res = cPickle.load(file(filename))
except:
res = FakeGameMap(0, 0, 0, 0)
res.pos = theWorld.softPos
mappos = theWorld.screenposMap()
res.x = mappos[0]
res.y = mappos[1]
res.w = mappos[2]
res.h = mappos[3]
if 'persons' not in res.__dict__:
res.persons = dict()
for persons in res.persons.values():
persons.tf = theWorld.textField
persons.theWorld = theWorld
persons.gMap = res
if not hasattr(persons, 'profile'):
persons.profile = {"hp": 100}
for position in res.persons:
res.persons[position].pos = list(position)
if not hasattr(res, 'foreground'):
res.foreground = dict()
return res
class GameMap(object):
def __init__(self, x, y, w, h):
# Screen position
self.x = x
self.y = y
self.w = w
self.h = h
self.pos = [LEVEL_WIDTH/2, LEVEL_HEIGHT/2]
self.size = [LEVEL_WIDTH, LEVEL_HEIGHT]
self.drawPos = []
self.drawAllFlag = True
self.namedField = dict()
self.persons = dict()
self.foreground = dict()
# Preparing level
self.clear()
def clear(self):
self.map = [{'ascii': ' ',
'fg': 3,
'bg': 7,
'walkable': True,} for x in \
xrange(LEVEL_WIDTH * LEVEL_HEIGHT)]
def __getitem__(self, pos):
x, y = pos
return self.map[y * LEVEL_WIDTH + x]
def __setitem__(self, pos, v):
x, y = pos
self.map[y * LEVEL_WIDTH + x] = v
def getAscii(self, x, y):
return self[x, y]['ascii']
def getFG(self, x, y):
return self[x, y]['fg']
def getBG(self, x, y):
return self[x, y]['bg']
def isWalkable(self, x, y):
return self[x, y]['walkable']
def getElem(self, x, y):
return self[x, y]
def setAscii(self, x, y, ascii):
if ascii != "\n":
self[x, y]['ascii'] = ascii
return True
return False
def setFG(self, x, y, color):
self[x, y]['fg'] = color
def setBG(self, x, y, color):
self[x, y]['bg'] = color
def setWalkable(self, x, y, walkable):
self[x, y]['walkable'] = walkable
def saveToFile(self, filename):
cPickle.dump(self, file(filename, 'w'))
def loadFromFile(self, filename):
pass
def setNamedField(self, k, pos):
self.namedField[k] = pos
def getNamedField(self, k):
if k not in self.namedField:
return None
return self.namedField[k]
def resize(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
def to_screenpos(self, x, y):
ret = (x + self.x + self.w/2 - self.pos[0] + self.w%2, \
y + self.y + self.h/2 - self.pos[1] + self.h%2)
if (ret[0] <= self.x) or (ret[0] >= (self.x + self.w)) or \
(ret[1] <= self.y) or (ret[1] >= (self.y + self.h)):
return 0, 0
return ret
def draw_colored(self, pos, elem):
dst.addstr(self.y + elem[1] + self.h/2 - self.pos[1] + self.h%2,
self.x + elem[0] + self.w/2 - self.pos[0] + self.w%2,
self[pos]['ascii'],
color.color(self[pos]['fg'], self[pos]['bg']))
def draw_bw(self, pos, elem):
dst.addstr(self.y + elem[1] + self.h/2 - self.pos[1] + self.h%2,
self.x + elem[0] + self.w/2 - self.pos[0] + self.w%2,
self[pos]['ascii'])
def draw(self, dst):
newList = list()
if self.drawAllFlag == True:
self.drawAll(dst)
else:
for elem in self.drawPos:
pos = (elem[0], elem[1])
if (pos[0] < 0) or (pos[1] < 0) or \
(pos[0] >= LEVEL_WIDTH) or \
(pos[1] >= LEVEL_HEIGHT):
continue
screenpos = self.to_screenpos(pos[0], pos[1])
if configs.misc.COLORED == True:
if tuple(pos) in self.foreground:
mapDraw = self.foreground[tuple(pos)]
dst.addstr(screenpos[1], screenpos[0],
mapDraw[0],
color.color(mapDraw[1], mapDraw[2]))
self.foreground.pop(pos)
newList.append(pos)
elif tuple(pos) in self.persons:
mapDraw = self.persons[tuple(pos)].mapDraw
dst.addstr(screenpos[1], screenpos[0],
mapDraw[0],
color.color(mapDraw[1], mapDraw[2]))
else:
dst.addstr(screenpos[1], screenpos[0],
self[pos]['ascii'],
color.color(self[pos]['fg'],
self[pos]['bg']))
else:
dst.addstr(screenpos[1], screenpos[0], self[pos]['ascii'])
self.drawPos = newList
def drawAll(self, dst):
self.drawAllFlag = False
for h in range(self.h):
for w in range(self.w):
pos = (self.pos[0] + (self.w/2 - w),
self.pos[1] + (self.h/2 - h))
if (pos[0] < 0) or (pos[1] < 0) or \
(pos[0] >= LEVEL_WIDTH) or \
(pos[1] >= LEVEL_HEIGHT):
continue
if configs.misc.COLORED == True:
if tuple(pos) in self.foreground:
mapDraw = self.foreground[tuple(pos)]
dst.addstr(self.y + self.h - h,
self.x + self.w - w,
mapDraw[0],
color.color(mapDraw[1], mapDraw[2]))
self.foreground.pop(pos)
elif tuple(pos) in self.persons:
mapDraw = self.persons[tuple(pos)].mapDraw
dst.addstr(self.y + self.h - h,
self.x + self.w - w,
mapDraw[0],
color.color(mapDraw[1], mapDraw[2]))
else:
dst.addstr(self.y + self.h - h,
self.x + self.w - w,
self[pos]['ascii'],
color.color(self[pos]['fg'],
self[pos]['bg']))
else:
dst.addstr(self.y + self.h - h,
self.x + self.w - w,
self[pos]['ascii'])
class FakeGameMap(GameMap):
def clear(self):
pass
def __getitem__(self, pos):
return {'ascii': '+',
'fg': 7,
'bg': 3,
'walkable': False,}
def __setitem__(self, pos, v):
pass
def getAscii(self, x, y):
return ' '
def getFG(self, x, y):
return 3
def getBG(self, x, y):
return 7
def isWalkable(self, x, y):
return True
def getElem(self, x, y):
return {'ascii': ' ',
'fg': 3,
'bg': 7,
'walkable': True,}
def setAscii(self, x, y, ascii):
if ascii != "\n":
return True
return False
def setFG(self, x, y, color):
pass
def setBG(self, x, y, color):
pass
def setWalkable(self, x, y, walkable):
pass
def saveToFile(self, filename):
pass
def loadFromFile(self, filename):
pass
def draw(self, dst):
if self.drawAllFlag == True:
self.drawAll(dst)
else:
for elem in self.drawPos:
pos = (elem[0], elem[1])
if (pos[0] < 0) or (pos[1] < 0) or \
(pos[0] >= LEVEL_WIDTH) or \
(pos[1] >= LEVEL_HEIGHT):
continue
screenpos = self.to_screenpos(pos[0], pos[1])
if configs.misc.COLORED == True:
dst.addstr(screenpos[1], screenpos[0], '+',
color.color(7, 0))
else:
dst.addstr(screenpos[1], screenpos[0], '+')
self.drawPos = []
def drawAll(self, dst):
self.drawAllFlag = False
for h in range(self.h):
for w in range(self.w):
pos = (self.pos[0] + (self.w/2 - w),
self.pos[1] + (self.h/2 - h))
if (pos[0] < 0) or (pos[1] < 0) or \
(pos[0] >= LEVEL_WIDTH) or \
(pos[1] >= LEVEL_HEIGHT):
continue
if configs.misc.COLORED == True:
dst.addstr(self.y + self.h - h,
self.x + self.w - w,
'+', color.color(7, 0))
else:
dst.addstr(self.y + self.h - h,
self.x + self.w - w,
'+')