[go: up one dir, main page]

Menu

[r313]: / kalma / map.py  Maximize  Restore  History

Download this file

323 lines (261 with data), 10.5 kB

  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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import pygame
from pygame.locals import *
import math
import time
import os
import obj
import zombie
import spawn
from locals import *
import renderer
class Tile:
tilesets = []
wallsets = []
def __init__(self, tileset, type, leftwall, rightwall):
self.tileset = tileset
self.type = type
self.leftwall = leftwall
self.rightwall = rightwall
def load_tilesets():
f = open(os.path.join("tiles", "tiles.txt"))
num_tilesets = int(f.readline().strip())
for i in range(num_tilesets):
line = f.readline().strip()
Tile.tilesets.append(pygame.image.load(os.path.join("tiles", line + "floor.png")).convert_alpha())
Tile.wallsets.append(pygame.image.load(os.path.join("tiles", line + "wall.png")).convert_alpha())
class Map:
def __init__(self, width, height):
# Make sure the width and height are positive integers.
try:
width = int(width)
except:
width = 1
if width < 1:
width = 1
try:
height = int(height)
except:
height = 1
if height < 1:
height = 1
self.width = int(width)
self.height = int(height)
# grid stores the basic tiles, objects stores all game objects. Spawnpoints stores routes to other levels.
self.grid = []
self.objects = []
self.spawnpoints = []
for i in range(width*height):
self.grid.append(Tile(0,0,0,0))
def get_tile(self, pos):
x = int(math.floor(pos[0]))
y = int(math.floor(pos[1]))
if x < 0 or y < 0 or x >= self.width or y >= self.height:
return Tile(0,0,0,0)
return self.grid[int(math.floor(pos[0])) + int(math.floor(pos[1])) * self.width]
def draw(self, screen, offset, characters):
i = 0
rend = renderer.Renderer()
for y in range(self.height):
pos = [offset[0] - y * TILEWIDTH/2, offset[1] + y * TILEHEIGHT/2]
for x in range(self.width):
if self.grid[i].type > 0:
rect = pygame.Rect((self.grid[i].type - 1) * TILEWIDTH, 0, TILEWIDTH, TILEHEIGHT)
if (Variables.vdict["psychomode"]):
screen.blit(Tile.tilesets[5], pos, rect)
else:
screen.blit(Tile.tilesets[self.grid[i].tileset], pos, rect)
pos[0] += TILEWIDTH/2
pos[1] += TILEHEIGHT/2
i += 1
x,y = 0,0
while y < self.height or x < self.width:
if y < self.height and x < self.width:
rend.add_walls(self.get_tile((x,y)), (x, y))
x += 1
y -= 1
if y < 0 or x > self.width:
y = y + x + 1
x = 0
for object in self.objects:
rend.add_object(object)
rend.draw(screen, offset)
def update(self):
self.check_collisions()
for object in self.objects:
object.update(self)
#Old style wall rendering was here. Got removed in revision 290.
def check_collisions(self):
for o in self.objects:
if o.objtype == TYPE_OBJECT or o.dead or o.is_walkover():
continue
rect = o.get_rect()
rect.bottom += o.current_animation.dir[1]
rect.top += o.current_animation.dir[1]
rect.left += o.current_animation.dir[0]
rect.right += o.current_animation.dir[0]
stopped = False
# Tile 7 is a door. You don't collide with doors.
for x in range(int(math.ceil(rect.left)),int(math.ceil(rect.right))):
tile = self.get_tile((x, int(math.floor(rect.top))))
if not tile.type or (tile.leftwall and tile.leftwall != 7):
# This "try:" is a hack to make characters and zombies stop, but there's nothing like this
# for objects in general yet. Perhaps I'll make a general "you collided"
# function
try:
o.stop()
stopped = True
break
except:
pass
tile = self.get_tile((x, int(math.floor(rect.bottom))))
if not tile.type or (tile.leftwall and tile.leftwall != 7):
try:
o.stop()
stopped = True
break
except:
pass
if stopped:
continue
for y in range(int(math.ceil(rect.top)), int(math.ceil(rect.bottom))):
tile = self.get_tile((int(math.floor(rect.left)), y))
if not tile.type or (tile.rightwall and tile.rightwall != 7):
try:
o.stop()
stopped = True
break
except:
pass
tile = self.get_tile((int(math.floor(rect.right)), y))
if not tile.type or (tile.rightwall and tile.rightwall != 7):
try:
o.stop()
stopped = True
break
except:
pass
if stopped:
continue
for o2 in self.objects:
if o2.is_walkover(): # or o2.objtype == TYPE_CHARACTER or o2.objtype == TYPE_ENEMY:
continue
if o != o2:
rect2 = o2.get_rect()
rect2.bottom += o2.current_animation.dir[1]
rect2.top += o2.current_animation.dir[1]
rect2.left += o2.current_animation.dir[0]
rect2.right += o2.current_animation.dir[0]
if (rect.left >= rect2.left and rect.left <= rect2.right) or (rect.right >= rect2.left and rect.right <= rect2.right):
if rect.bottom >= rect2.top and rect.bottom <= rect2.bottom:
try:
o.stop()
stopped = True
except:
pass
elif rect.top >= rect2.top and rect.top <= rect2.bottom:
try:
o.stop()
stopped = True
except:
pass
#Selects an object pointed by the mouse. First prefers characters, then zombies, then ordinary items.
def select_object(self, offset, pos):
lastfound = None
lastenemy = None
lastchar = None
for o in self.objects:
spos = get_screen_pos(offset, o.pos)
#print spos
#print pos
rect = pygame.Rect(spos[0] - o.current_animation.center[0], spos[1] - o.current_animation.center[1],
o.current_animation.surface.get_width() / o.current_animation.frames, o.current_animation.surface.get_height())
if rect.collidepoint(pos[0], pos[1]):
if o.objtype == TYPE_CHARACTER:
lastchar = o
if o.objtype == TYPE_ENEMY:
lastenemy = o
if o.objtype == TYPE_SPAWNPOINT:
return o
lastfound = o
if lastchar:
return lastchar
if lastenemy:
return lastenemy
if lastfound:
return lastfound
return None
#This function returns a spawn point from a map
def getspawn(self, name):
result = (0,0)
for sp in self.spawnpoints:
if sp.spawn_to == name:
return sp.pos
return (0,0)
# This function takes parameters x and y in the tile coordinates, and returns coordinates on the screen.
# Offset is just a pair of numbers that is added to the result.
def get_screen_pos(offset, pos):
x,y = pos
return [offset[0] + x * TILEWIDTH/2 - y * TILEWIDTH/2 + TILEWIDTH/2, offset[1] + x * TILEHEIGHT/2 + y * TILEHEIGHT/2]
def get_map_pos(offset, pos):
x,y = pos
pos = (float(x - offset[0]), float(y - offset[1]))
#x = offset[0] + retx * TILEWIDTH/2 - rety * TILEWIDTH/2 + TILEWIDTH/2
#y = offset[1] + retx * TILEHEIGHT/2 + rety * TILEHEIGHT/2
#retx = 2*x/TILEWIDTH - 2*offset[0]/TILEWIDTH + rety - 1
#rety = 2*y/TILEHEIGHT - 2*offset[1]/TILEHEIGHT - retx
# retx = pos[0]/TILEWIDTH + pos[1]/TILEHEIGHT
retx = pos[1]/TILEHEIGHT + pos[0]/TILEWIDTH - 0.5
rety = -pos[0]/TILEWIDTH + pos[1]/TILEHEIGHT + 0.5
return (retx, rety)
# This function reads and parses a map file
def load_map(name):
filename = os.path.join("maps", name + ".txt")
f = open(filename)
str = f.readline()
strs = str.split()
#print strs
width = int(strs[0])
height = int(strs[1])
map = Map(width, height)
map.name = name
# Loads the spawnpoints from the file
num_spawnpoints = int(f.readline().strip())
for i in range(num_spawnpoints):
line = f.readline()
parts = line.split(" ")
spname,rx,ry = parts
spx = float(rx)
spy = float(ry)
sp = spawn.Spawnpoint(spname, (spx, spy))
map.spawnpoints.append(sp)
if sp.spawn_to != "default":
map.objects.append(sp)
num_enemies = int(f.readline().strip())
for i in range(num_enemies):
line = f.readline()
parts = line.split(" ")
objectname,rx,ry = parts
objectx = float(rx)
objecty = float(ry)
map.objects.append(zombie.Zombie(objectname, (objectx, objecty)))
# Loads the objects from the file and adds them to the objects array
num_objects = int(f.readline().strip())
for i in range(num_objects):
line = f.readline()
parts = line.split(" ")
objectname,rx,ry = parts
objectx = float(rx)
objecty = float(ry)
map.objects.append(obj.Object(objectname, (objectx, objecty)))
# Loads tiles from the file and adds them to the grid
i = 0
#print map.width, map.height
for tile in f.read().split():
map.grid[i].tileset = ord(tile[0])-65
map.grid[i].leftwall = int(tile[1])
map.grid[i].type = int(tile[2])
map.grid[i].rightwall = int(tile[3])
i += 1
f.close()
return map
# Testing code got removed in revision 290.