-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
401 lines (352 loc) · 13.4 KB
/
helpers.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
import copy
import decimal
import math
from enum import Enum
import random
from settings import *
from assets.colors import colors
from pygame.key import *
from pygame.locals import *
import pygame
class Blocks(Enum):
AIR = -1
GRASS = 0
DIRT = 1
STONE = 2
ANDESITE = 3
DIORITE = 4
BEDROCK = 5
LOG = 6
PLANKS = 7
PLATFORM = 8
WALL = 9
TREE = 10
IRON_ORE = 11
BIRD_SPAWN = 12
class TerrainPoints(Enum):
CAVE = 0
ANDESITE = 1
DIORITE = 2
IRON = 3
BIRD_SPAWN = 4
class Entity(pygame.sprite.Sprite):
def __init__(self, x, y, dx = 0, dy = 0):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.dx = dx
self.dy = dy
self.gravityVal = 0.05
self.suffocationDamage = False
self.suffocationDelay = -1
self.haveCollisions = True
def update(self, app, *args):
self.y -= self.dy
self.gravity(app)
self.checkForCollision(app)
self.x += self.dx
self.dx *= 0.7
if abs(self.dx) < 0.005:
self.dx = 0
if hasattr(self, "tick"):
self.tick(app, *args)
self.rect.x = getPixX(app, self.x)
if hasattr(self, "width"):
self.rect.x -= self.width / 2
self.rect.y = getPixY(app, self.y)
if hasattr(self, "height"):
self.rect.y -= self.height / 2
def gravity(self, app):
self.dy += self.gravityVal
def checkForCollision(self, app):
if not self.haveCollisions: return
blockLeft = getBlockFromCoords(app, math.floor(self.x) - 1, math.ceil(self.y))
blockRight = getBlockFromCoords(app, math.ceil(self.x), math.ceil(self.y))
blockTopLeft = getBlockFromCoords(app, math.floor(self.x), math.floor(self.y + 1))
blockTopRight = getBlockFromCoords(app, math.ceil(self.x), math.floor(self.y + 1))
if blockTopLeft and blockTopLeft.solid:
blockTop = blockTopLeft
elif blockTopRight and blockTopRight.solid:
blockTop = blockTopRight
else:
blockTop = None
onGround = isOnGround(app, self.x, self.y - self.dy)
# Right Collision
if 0 < self.dx:
topBlock = blockTopLeft and blockTopLeft.solid
diagBlock = getBlockFromCoords(app, math.ceil(self.x), math.ceil(self.y) + 1)
freeDiagBlock = not diagBlock or not (diagBlock.solid == 1)
sneak = self.sneak if hasattr(self, "sneak") else False
if (blockRight and
blockRight.solid == 1 and
not topBlock and
freeDiagBlock and
onGround and
not sneak and
self.dx > 0.01): # prevent chaining the one jump
self.x = blockRight.x
self.y = blockRight.y + 1
self.dx = 0.01
elif blockRight and blockRight.solid == 1:
self.dx = 0
self.x = blockRight.x - 0.9
# Left Collision
elif self.dx < 0:
topBlock = blockTop and blockTop.solid == 1
diagBlock = getBlockFromCoords(app, math.floor(self.x) - 1, math.ceil(self.y) + 1)
freeDiagBlock = not diagBlock or not (diagBlock.solid == 1)
sneak = self.sneak if hasattr(self, "sneak") else False
if (blockLeft and
blockLeft.solid == 1 and
not topBlock
and freeDiagBlock and
onGround and
not sneak and
self.dx < -0.01): # prevent chaining the one jump
self.x = blockLeft.x + 1
self.y = blockLeft.y + 1
self.dx = -0.01
elif (blockLeft and
blockLeft.solid == 1 and
self.x + self.dx < blockLeft.x + 1):
self.dx = 0
self.x = blockLeft.x + 1.1
# Gravity Collision
if isOnGround(app, self.x, self.y) and self.dy > 0:
onGround = True
self.dy = 0
groundLeft = getGround(app, math.floor(self.x), self.y)
groundRight = getGround(app, math.floor(self.x + 0.8), self.y)
if self.x + 0.8 == math.floor(self.x + 0.8):
groundRight = groundLeft
self.y = max(groundLeft, groundRight)
# Top Collision
if self.dy != 0 and blockTop and blockTop.solid == 1:
if blockTopLeft:
leftOverlap = hasOverlap(
(blockTopLeft.x, blockTopLeft.y,
blockTopLeft.x + 1, blockTopLeft.y + 1),
(self.x, self.y - self.dy, self.x + 0.8, self.y - self.dy + 0.8)
)
else:
leftOverlap = False
if blockTopRight:
rightOverlap = hasOverlap(
(blockTopRight.x, blockTopRight.y,
blockTopRight.x + 1, blockTopRight.y + 1),
(self.x, self.y - self.dy, self.x + 0.8, self.y - self.dy + 0.8)
)
else:
rightOverlap = False
if leftOverlap and blockTopLeft.solid == 1:
self.dy = 0
self.y = blockTopLeft.y - 1
if rightOverlap and blockTopRight.solid == 1:
self.dy = 0
self.y = blockTopRight.y - 1
###############################################################################
# HELPER FUNCS
def almostEqual(d1, d2, epsilon=10**-7): # helper-fn
# note: use math.isclose() outside 15-112 with Python version 3.5 or later
return (abs(d2 - d1) < epsilon)
def getBlockFromCoords(app, x, y):
for chunk in app.game.loaded:
if chunk.inChunk(x) and 0 <= y <= BUILD_HEIGHT and (x, y) in chunk.blocks:
return chunk.blocks[(x, y)]
return None
def isOnGround(app, x, y):
groundLeft = getGround(app, math.floor(x), y)
groundRight = getGround(app, math.floor(x + 0.8), y)
if y - groundLeft <= 0.4 or y - groundRight <= 0.4:
return True
return False
def getCoordsFromPix(app, xPix, yPix):
blockY = math.ceil(-(yPix - app.height * TERRAIN_HEIGHT) / UNIT_WH) + app.player.y
for chunk in app.game.loaded:
for b in range(CHUNK_SIZE):
if (b + chunk.x, blockY) in chunk.blocks:
block = chunk.blocks[(b + chunk.x, blockY)]
blockX = ((block.x - app.player.x) * UNIT_WH) + (app.width // 2)
if blockX <= xPix and xPix < blockX + UNIT_WH:
return block.x, block.y
def getPixFromCoords(app, x, y):
return getPixX(app, x), getPixY(app, y)
def getPixX(app, x):
return ((x - app.player.x) * UNIT_WH) + (app.width // 2)
def getPixY(app, y):
return (app.height * TERRAIN_HEIGHT) + ((app.player.y - y) * UNIT_WH)
def getGround(app, x, y):
for chunk in app.game.loaded:
if chunk.inChunk(x):
for r in range(math.ceil(y), -1, -1):
if (x, r) in chunk.blocks and chunk.blocks[(x, r)].solid:
if chunk.blocks[(x, r)].isHalf and y <= r:
return chunk.blocks[(x, r)].y
else:
return chunk.blocks[(x, r)].y + 1
return -1
def generateChunks(app):
while min(app.game.loaded).index < min(app.game.chunks.values()).index + 2:
app.game.generateChunk(app, False)
while max(app.game.loaded).index > max(app.game.chunks.values()).index - 2:
app.game.generateChunk(app, True)
def moveBlock(app, block, canvas):
x, y = getPixFromCoords(app, block.x, block.y)
if block.type == Blocks.AIR:
return
image = block.image
if image == None:
return
canvas.moveto(image, x, y)
def checkBackground(app):
width = app.background.get_width()
newBackgrounds = []
for i in range(app.game.bgX[0] - width, app.width, width):
newBackgrounds.append(i)
app.game.bgX = newBackgrounds
def roundHalfUp(d): # helper-fn
# Round to nearest with ties going away from zero.
rounding = decimal.ROUND_HALF_UP
# See other rounding options here:
# https://docs.python.org/3/library/decimal.html#rounding-modes
return int(decimal.Decimal(d).to_integral_value(rounding=rounding))
def getImage(app, name):
if name not in app.images:
return None
return app.images[name]
def withinBounds(x1, y1, x2, y2, x, y):
return x1 <= x <= x2 and y1 <= y <= y2
# create a "height map" for a chunk with a start y and end y
# check readme for link to algo
def generateTerrain(y1, y2, displace=1, length=0):
if 2**(length + 1) >= CHUNK_SIZE:
return [y1, y2]
displacement = random.randint(0, displace)
multiply = -1 if random.randint(0, 1) else 1
midpoint = int(((y1 + y2) / 2) + displacement * multiply)
length += 1
return (generateTerrain(y1, midpoint, int(displace * 0.5), length) +
generateTerrain(midpoint, y2, int(displace * 0.5), length))
def belowSurface(app, y):
if y <= GROUND_LEVEL - GRASS_LEVEL - TERRAIN_VARIATION:
return True
return False
def distSurface(y):
return GROUND_LEVEL - y
def getSurface(app, x):
for chunk in app.game.loaded:
if chunk.inChunk(x):
for r in range(GROUND_LEVEL - GRASS_LEVEL - TERRAIN_VARIATION,
GROUND_LEVEL - GRASS_LEVEL + TERRAIN_VARIATION):
if (x, r) in chunk.blocks and chunk.blocks[(x, r)].type == Blocks.AIR:
if (x, r + 1) not in chunk.blocks:
return r
return -1
def getBackgroundColor(time):
lastR, lastG, lastB = colors[math.floor(time)]
nextR, nextG, nextB = colors[math.floor(time) + 1]
percentage = (time - math.floor(time)) * 100
r = lastR + (nextR - lastR) * percentage / 100
g = lastG + (nextG - lastG) * percentage / 100
b = lastB + (nextB - lastB) * percentage / 100
return "#%02x%02x%02x" % (int(r), int(g), int(b))
def hasOverlap(r1, r2):
x1, y1, x2, y2 = r1
x3, y3, x4, y4 = r2
return (x1 <= x3 <= x2 or x1 <= x4 <= x2) and (y1 <= y3 <= y2 or y1 <= y4 <= y2)
def isOverEnemy(app, x, y):
chunkIndex = app.game.getChunkIndex(x)
chunk = app.game.getChunk(app, chunkIndex)
for mob in chunk.mobs:
width = mob.width / UNIT_WH
height = mob.height / UNIT_WH
margin = 0.4
if withinBounds(mob.x - width / 2 - margin, mob.y - height / 2 - margin,
mob.x + width / 2 + margin, mob.y + height / 2, x, y):
return mob
return False
def canBeMade(app, recipe):
items = {}
for inv in app.player.inventory:
if not inv: continue
if inv.name in items.keys():
items[inv.name] += inv.count
else:
items[inv.name] = inv.count
for name, count in recipe["ingredients"].items():
if name not in items or items[name] < count:
return False
return True
def makeRecipe(app, recipe):
for item, count in recipe["ingredients"].items():
app.player.removeItem(app, item, count)
app.player.pickUp(app, copy.deepcopy(recipe["output"]))
def numCanCraft(app, recipe):
if not canBeMade(app, recipe): return
items = {}
for inv in app.player.inventory:
if not inv: continue
if inv.name in items.keys():
items[inv.name] += inv.count
else:
items[inv.name] = inv.count
# get maximum number of recipe that can be made
maxCount = 0
for name, count in recipe["ingredients"].items():
if name not in items:
return 0
maxCount += items[name] // count
return maxCount
def nearbySolid(app, x, y):
solid = []
block = getBlockFromCoords(app, x - 1, y)
if block and block.solid == 1:
solid.append(block)
block = getBlockFromCoords(app, x + 1, y)
if block and block.solid == 1:
solid.append(block)
block = getBlockFromCoords(app, x, y - 1)
if block and block.solid == 1:
solid.append(block)
block = getBlockFromCoords(app, x, y + 1)
if block and block.solid == 1:
solid.append(block)
return solid
def nearbyAir(app, x, y):
air = []
block = getBlockFromCoords(app, x - 1, y)
if block and block.type == Blocks.AIR:
air.append(block)
block = getBlockFromCoords(app, x + 1, y)
if block and block.type == Blocks.AIR:
air.append(block)
block = getBlockFromCoords(app, x, y - 1)
if block and block.type == Blocks.AIR:
air.append(block)
block = getBlockFromCoords(app, x, y + 1)
if block and block.type == Blocks.AIR:
air.append(block)
return air
def getPath(app, x1, y1, x2, y2, depth = 0, path = [], maxDepth = 5):
if path == []:
path = [(x1, y1)]
if x1 == x2 and y1 == y2:
return path
if depth > maxDepth:
return False
block1 = getBlockFromCoords(app, x1, y1)
if not block1: return False
block2 = getBlockFromCoords(app, x2, y2)
if not block2: return False
for air in nearbyAir(app, x1, y1):
if (air.x, air.y) in path: continue
path.append((air.x, air.y))
if getPath(app, air.x, air.y, x2, y2, depth + 1, path, maxDepth):
return path
path.pop()
return False
def keyIsNumber(k):
nums = [pygame.K_0, pygame.K_1, pygame.K_2, pygame.K_3, pygame.K_4, pygame.K_5, pygame.K_6, pygame.K_7, pygame.K_8, pygame.K_9]
return k in nums
def giveItem(app, item):
app.player.pickUp(app, item)