This repository has been archived by the owner on Jan 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.py
423 lines (348 loc) · 16.5 KB
/
world.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
from constants import W_WALLDIST
import math
import file
import pack
class Level(object):
def __init__(self, info=None):
self.finished = False
if info is not None:
#Load level by string or by dictionary
if isinstance(info, str):
data = file.loadJson(info)
elif isinstance(info, dict):
data = info
self.name = data['name']
#Must convert to list because it will be reused
self.ceilingcolor = data['ceilingcolor']
self.floorcolor = data['floorcolor']
self.outerwall = data['outerwall']
self.pack = pack.Pack(data['pack'])
self.map = data['map']
player = data['player']
self.player = Player(player[0], player[1], player[2], player[3], player[4], player[5], player[6], player[7], player[8], player[9], player[10])
self.player.level = self
self.statics = []
for static in data['statics']:
self.addEntity(Static(static[0], static[1], static[2]))
self.items = []
for item in data['items']:
self.addEntity(Item(item[0], item[1], item[2], item[3]))
self.enemies = []
for enemy in data['enemies']:
self.addEntity(Enemy(enemy[0], enemy[1], enemy[2], enemy[3], enemy[4], enemy[5], enemy[6], enemy[7], enemy[8], enemy[9]))
self.projectiles = []
else:
self.name = "Untitled"
self.ceilingcolor = (235, 235, 235)
self.floorcolor = (75, 75, 75)
self.outerwall = 1
self.pack = pack.Pack("Dungeon")
self.loadEmpty(10, 10)
self.player = Player(0.5, 0.5, 0, 4, 5, 100, 1, 0.1, 4, 5, 1)
self.player.level = self
self.statics = []
self.items = []
self.enemies = []
self.projectiles = []
def getDictionary(self):
return {"name": self.name,
"ceilingcolor": self.ceilingcolor,
"floorcolor": self.floorcolor,
"outerwall": self.outerwall,
"pack": self.pack.name,
"map": self.map,
"player": (self.player.x, self.player.y, self.player.direction, self.player.speed, self.player.sprintspeed, self.player.health, self.player.regen, self.player.cooldown, self.player.projectilespeed, self.player.projectiledamage, self.player.projectiletexture),
"statics": list(map(lambda static: (static.x, static.y, static.texture), self.statics)),
"items": list(map(lambda item: (item.x, item.y, item.texture, item.function), self.items)),
"enemies": list(map(lambda enemy: (enemy.x, enemy.y, enemy.direction, enemy.texture, enemy.speed, enemy.health, enemy.cooldown, enemy.projectilespeed, enemy.projectiledamage, enemy.projectiletexture), self.enemies))}
def copy(self):
return Level(self.getDictionary())
def save(self, path):
file.saveJson(path, self.getDictionary())
def changeSize(self, width, height):
currentWidth = self.getWidth()
currentHeight = self.getHeight()
if height != currentHeight:
if height < currentHeight:
del self.map[height:]
else:
self.map += [[0 for _ in range(width)] for _ in range(height - currentHeight)]
if width != currentWidth:
if width < currentWidth:
for stripe in self.map:
del stripe[width:]
else:
for stripe in self.map:
stripe += [0 for _ in range(width - currentWidth)]
for sprite in self.statics + self.items + self.enemies + self.projectiles:
if sprite.x > width or sprite.y > height:
self.removeEntity(sprite)
def loadEmpty(self, width, height):
self.map = [[0 for _ in range(width)] for _ in range(height)]
def getWidth(self):
return len(self.map[0])
def getHeight(self):
return len(self.map)
def inBounds(self, x, y):
return (0 <= x < self.getWidth() and 0 <= y < self.getHeight())
def setElement(self, x, y, value):
#Check to see if the requested x and y values are inside the map
if self.inBounds(x, y):
self.map[y][x] = value
return True
return False
def getElement(self, x, y):
#Check to see if the requested x and y values are inside the map
if self.inBounds(x, y):
return self.map[y][x]
return self.outerwall
def getRelativeElement(self, x, y, deltaX, deltaY):
return self.getElement(math.floor(x + deltaX), math.floor(y + deltaY))
def addEntity(self, entity):
if entity.level is None:
entity.level = self
if entity.variation == 0:
self.statics.append(entity)
elif entity.variation == 1:
self.items.append(entity)
elif entity.variation == 2:
self.enemies.append(entity)
elif entity.variation == 3:
self.projectiles.append(entity)
def removeEntity(self, entity):
if entity is not None:
if entity.variation == 0:
self.statics.remove(entity)
del entity
elif entity.variation == 1:
self.items.remove(entity)
del entity
elif entity.variation == 2:
self.enemies.remove(entity)
del entity
elif entity.variation == 3:
self.projectiles.remove(entity)
del entity
def getEntities(self):
return self.statics + self.items + self.enemies + self.projectiles + [self.player]
def getEntity(self, x, y, variation, area=0.5):
#Should never be used repeatedly, Inefficient.
result = None
if variation == -1:
for sprite in self.getEntities():
if sprite.collideCircle(x, y, area):
result = sprite
break
else:
if variation == 0:
for static in self.statics:
if static.collideCircle(x, y, area):
result = static
break
elif variation == 1:
for item in self.items:
if item.collideCircle(x, y, area):
result = item
break
elif variation == 2:
for enemy in self.enemies:
if enemy.collideCircle(x, y, area):
result = enemy
break
elif variation == 3:
for projectile in self.projectiles:
if projectile.collideCircle(x, y, area):
result = projectile
break
return result
def update(self, delta):
self.player.update(delta)
for item in self.items:
item.update(delta)
for enemy in self.enemies:
enemy.update(delta)
for projectile in self.projectiles:
projectile.update(delta)
class Entity(object):
def __init__(self, x, y, direction):
self.x = x
self.y = y
self.direction = direction
self.level = None
#Level is set by the level class, breaks if no level is attached
def collideCircle(self, x, y, size=0.5):
'''
Checks collision within a circle
Uses Pythagorean theorem
'''
return abs(math.sqrt(((self.x - x) ** 2) + ((self.y - y) ** 2))) < size
def collideAABB(self, x, y, size=0.5):
'''
Checks collision with axis aligned bounding boxes
'''
return self.x - size <= x <= self.x + size and self.y - size <= y <= self.y + size
def move(self, deltaX, deltaY, collision=True, legdist=W_WALLDIST, fast=True, independent=True):
'''
deltaX: Amount to move on the x-axis
deltaY: Amount to move on the y-axis
collision: Considers collision
fast: Checks only sides that entity is moving towards
legdist: Closest distance the entity can get to the wall
independent: If collision should be checked for both x and y axis, or only once (To use, fast must be false)
'''
if collision:
if fast:
#Checks to see if the x movement is inside wall
if(self.level.getRelativeElement(self.x, self.y, math.copysign(legdist, deltaX) + deltaX, legdist) == 0 and
self.level.getRelativeElement(self.x, self.y, math.copysign(legdist, deltaX) + deltaX, -legdist) == 0):
self.x += deltaX
#Checks to see if the y movement is inside wall
if(self.level.getRelativeElement(self.x, self.y, legdist, math.copysign(legdist, deltaY) + deltaY) == 0 and
self.level.getRelativeElement(self.x, self.y, -legdist, math.copysign(legdist, deltaY) + deltaY) == 0):
self.y += deltaY
else:
if independent:
#Checks to see if the x movement is inside wall
if(self.level.getRelativeElement(self.x, self.y, legdist + deltaX, legdist) == 0 and
self.level.getRelativeElement(self.x, self.y, legdist + deltaX, -legdist) == 0 and
self.level.getRelativeElement(self.x, self.y, -legdist + deltaX, legdist) == 0 and
self.level.getRelativeElement(self.x, self.y, -legdist + deltaX, -legdist) == 0):
self.x += deltaX
#Checks to see if the y movement is inside wall
if(self.level.getRelativeElement(self.x, self.y, legdist, legdist + deltaY) == 0 and
self.level.getRelativeElement(self.x, self.y, -legdist, legdist + deltaY) == 0 and
self.level.getRelativeElement(self.x, self.y, legdist, -legdist + deltaY) == 0 and
self.level.getRelativeElement(self.x, self.y, -legdist, -legdist + deltaY) == 0):
self.y += deltaY
else:
#Check to see if both the x and y together are inside a wall
if(self.level.getRelativeElement(self.x, self.y, legdist + deltaX, legdist + deltaY) == 0 and
self.level.getRelativeElement(self.x, self.y, legdist + deltaX, -legdist + deltaY) == 0 and
self.level.getRelativeElement(self.x, self.y, -legdist + deltaX, legdist + deltaY) == 0 and
self.level.getRelativeElement(self.x, self.y, -legdist + deltaX, -legdist + deltaY) == 0):
self.x += deltaX
self.y += deltaY
else:
self.x += deltaX
self.y += deltaY
def walk(self, distance, collision=True):
'''
Moves in the entity's current direction
'''
#Check movement distance on each axis
direction = math.radians(self.direction)
self.move(math.cos(direction) * distance, math.sin(direction) * distance, collision)
def strafe(self, distance, collision=True):
'''
Moves perpendicular to the entity's current direction
'''
#Check movement distance on each axis
direction = math.radians(self.direction)
self.move(-math.sin(direction) * distance, math.cos(direction) * distance, collision)
def rotate(self, degree):
self.direction = (self.direction + degree) % 360
class TexturedEntity(Entity):
def __init__(self, x, y, direction, texture):
super().__init__(x, y, direction)
self.texture = texture
class CharacterEntity(Entity):
def __init__(self, x, y, direction, speed, health, cooldown, projectilespeed, projectiledamage, projectiletexture):
super().__init__(x, y, direction)
self.speed = speed
self.maxhealth = health
self.health = health
self.dead = False
self.cooldown = cooldown
self.cooldowntime = 0
self.projectilespeed = projectilespeed
self.projectiledamage = projectiledamage
self.projectiletexture = projectiletexture
def update(self, delta):
self.cooldowntime += delta
def heal(self, amount):
self.health = min(self.maxhealth, self.health + amount)
def damage(self, amount):
if self.health - amount > 0:
self.health -= amount
else:
self.dead = True
self.health = 0
def shoot(self, level):
if self.cooldowntime > self.cooldown:
self.cooldowntime = 0
newProjectile = Projectile(self.x, self.y, self.direction, self.projectiletexture, self)
newProjectile.walk(1, False)
level.addEntity(newProjectile)
'''
SPRITE VARIATIONS:
0 = STATIC
1 = ITEM
2 = ENEMY
3 = PROJECTILE
'''
class Player(CharacterEntity):
variation = -1
def __init__(self, x, y, direction, speed, sprintspeed, health, regen, cooldown, projectilespeed, projectiledamage, projectiletexture):
super().__init__(x, y, direction, speed, health, cooldown, projectilespeed, projectiledamage, projectiletexture)
self.sprintspeed = sprintspeed
self.regen = regen
self.cooldowntime = 0
def update(self, delta):
super().update(delta)
if not self.dead:
self.heal(self.regen * delta)
class Static(TexturedEntity):
variation = 0
def __init__(self, x, y, texture):
super().__init__(x, y, 0, texture)
class Item(TexturedEntity):
variation = 1
def __init__(self, x, y, texture, function):
super().__init__(x, y, 0, texture)
self.function = function
def update(self, delta):
if self.collideAABB(self.level.player.x, self.level.player.y, 0.5):
if self.function == 0:
self.level.finished = True
self.level.removeEntity(self)
elif self.function == 1:
self.level.player.heal(50)
self.level.removeEntity(self)
class Enemy(CharacterEntity):
variation = 2
def __init__(self, x, y, direction, texture, speed, health, cooldown, projectilespeed, projectiledamage, projectiletexture):
super().__init__(x, y, direction, speed, health, cooldown, projectilespeed, projectiledamage, projectiletexture)
self.texture = texture
self.cooldowntime = 0
def update(self, delta):
super().update(delta)
if self.dead:
self.level.removeEntity(self)
self.rotate(min(10, math.degrees(math.atan2(self.level.player.y - self.y, self.level.player.x - self.x)) - self.direction))
if not self.collideCircle(self.level.player.x, self.level.player.y, 4) and self.collideCircle(self.level.player.x, self.level.player.y, 12):
self.walk(self.speed * delta)
if self.cooldowntime > self.cooldown:
self.cooldowntime = 0
newProjectile = Projectile(self.x, self.y, self.direction, 2, self)
newProjectile.walk(1, False)
self.level.addEntity(newProjectile)
class Projectile(TexturedEntity):
variation = 3
def __init__(self, x, y, direction, texture, spawn):
super().__init__(x, y, direction, texture)
self.spawn = spawn
self.speed = spawn.projectilespeed
self.damage = spawn.projectiledamage
def update(self, delta):
self.walk(self.speed * delta, False)
if self.level.getElement(math.floor(self.x), math.floor(self.y)) != 0:
self.level.removeEntity(self)
elif self.level.player != self.spawn and self.collideAABB(self.level.player.x, self.level.player.y):
self.level.player.damage(self.damage)
self.level.removeEntity(self)
else:
for enemy in self.level.enemies:
if enemy != self.spawn and self.collideAABB(enemy.x, enemy.y):
enemy.damage(self.damage)
self.level.removeEntity(self)
break