-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.py
494 lines (434 loc) · 22 KB
/
Entity.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import math
from cmu_graphics import *
from Vector import *
class Entity:
def __init__(self, x, y, width, height, level=0):
self.x = x
self.y = y
self.width = width
self.height = height
self.level = level
self.jumping = False
self.falling = True
self.positions = []
self.previousPositions = []
self.timer = 0
self.maxJumpHeight = 20
self.speed = 3
self.reachFallPortion = False
self.gravity = 1.3
self.rotateAngle = 0
self.index = 1
self.direction = 'right'
self.backgroundX = 0
self.isAttacking = False
self.looksAttacking = False
self.previousAttackTime = 0
self.attackWidth = 350
self.attackHeight = 70
self.maxHealth = 5
self.currentHealth = 5
self.damageTook = 0
self.healthList = [True]*self.maxHealth
self.healthX = 100 # center of left-most health circle
self.healthXInterval = 70
self.healthY = 100
self.healthRadius = 10
self.yesHealthColor = 'black'
self.noHealthColor = 'grey'
self.holdingUp = False
self.holdingDown = False
self.isCollidingWithOval = False
self.isKilled = False
self.freezeDuration = 10
self.freezeEverything = False
self.stopFreeze = False
self.isInvincible = False
self.invincibleDuration = 60
self.knockBackY = 50
self.knockBackX = 100
self.enemyCollisionDirection = None
self.collidedEnemy = None
self.startFallDuration = 3
self.playerAttackDamage = 10
self.dashing = False
self.dashDuration = 5
self.dashDistance = 100
self.attackDirection = None
self.dashingPositions = []
self.test = False
self.cornersAttack = []
self.cornersEnemy = []
self.projectedAttack = []
self.projectedEnemy = []
self.fourPointsAttack1 = []
self.fourPointsAttack2 = []
self.fourPointsEnemy1 = []
self.fourPointsEnemy2 = []
self.twoPointsAttack1 = []
self.twoPointsAttack2 = []
self.twoPointsEnemy1 = []
self.twoPointsEnemy2 = []
self.projectionCollisions = 0
self.infinity = 10*15
self.epsilon = 10*-15
self.playerAttackKnockBackDistanceHorizontal = 0
self.enemyAttackKnockBackDistanceHorizontal = 100
self.enemyKnockBackDistanceVertical = 100
self.isPogoing = False
self.timerPogo = 0
self.positionsPogo = []
self.isPogoingWhileJumping = True
self.timerPogoJumping = 0
self.isCollidingWithRect = False
self.isCollidingWithAnything = False
self.terrainCollisionsDict = dict()
self.isPogoingOnGround = False
self.moving = False
self.spriteX = 20
self.totalScrollX = 0
self.dashesLeft = 1
self.doubleJumping = False
self.doubleTimer = 0
self.isKnockBack = True
self.hazardLimit = 700
self.spriteCounterAttack = 0
self.resource = 0
self.resourceGain = 10
self.resourceCost = 50
self.resourceMax = 100
self.smoothAngleAdjustment = 0.05 # smooth transition when going from curved surface to flat terrain
def move(self, direction):
if self.isCollidingWithAnything:
self.moving =True
self.x += direction*self.speed
def jump(self):
self.positions.append(self.y)
if self.jumping == True and self.y >= self.positions[0]:
if self.reachMax():
self.reachFallPortion = True
else:
self.reachFallPortion = False
self.positions.append(self.y)
newPosition = -(self.timer - self.y - (self.maxJumpHeight)**0.5) + self.maxJumpHeight
self.y = newPosition
def doubleJump(self):
if self.jumping == True:
newPosition = -(self.doubleTimer - self.y - (self.maxJumpHeight*1.7)**0.5) + self.maxJumpHeight*1.7
self.y = newPosition
elif self.jumping == False:
newPosition = -(self.doubleTimer - self.y - (self.maxJumpHeight*0.1)**0.5) + self.maxJumpHeight*0.1
self.y = newPosition
def pogoJump(self):
self.dashesLeft = 1
if self.falling and not self.isCollidingWithAnything and not self.isPogoingOnGround:
self.isPogoingWhileJumping = True
elif self.isCollidingWithAnything or self.isPogoingOnGround:
self.isPogoingWhileJumping = False
self.isPogoingOnGround = True
self.jumping = False
newPosition = -(self.timerPogo - self.y - (self.maxJumpHeight*1)**0.5) + self.maxJumpHeight*1
self.y = newPosition
def pogoJumpWhileJumping(self):
newPosition = -(self.timerPogoJumping - self.y - (self.maxJumpHeight*1.2)**0.5) + self.maxJumpHeight*1.2
self.y = newPosition
def fall(self):
if self.falling == True and self.jumping == False and self.isPogoing == False:
self.y += -self.timer*self.gravity
elif self.falling == True and (self.jumping == True or self.isPogoing == True):
self.y += -self.timer*self.gravity/2
def reachMax(self):
if self.timer*(1+self.gravity) >= (self.maxJumpHeight)**0.5 + self.maxJumpHeight:
return True
return False
def getPlayerVertices(self):
self.leftX = self.x
self.rightX = self.x + self.width
self.topY = -self.y
self.bottomY = -self.y + self.height
self.middleX = (self.rightX + self.leftX)/2
self.middleY = (self.topY + self.bottomY)/2
self.longRadius = self.height/2
theta = (self.rotateAngle/180)*math.pi
self.deltaX = (math.sin(theta))*self.longRadius
self.deltaY = self.longRadius - (math.cos(theta))*self.longRadius
self.orientationX = self.middleX - self.deltaX
self.orientationY = self.bottomY - self.deltaY
self.attackAppearDuration = 2
self.timeBetweenAttacks = 20
def getMiddleXFromOrientation(self, orientationY):
self.longRadius = self.height/2
theta = (self.rotateAngle/180)*math.pi # math.sin and math.cos uses radians, not degrees
self.deltaX = (math.sin(theta))*self.longRadius
self.deltaY = self.longRadius - (math.cos(theta))*self.longRadius
return orientationY + self.deltaY
def resetAngle(self):
if self.index >= abs(self.rotateAngle):
self.rotateAngle = 0
self.index = 1
else:
self.rotateAngle = self.rotateAngle/self.index
def updateHealth(self, amount): # for player
if self.currentHealth == -amount:
self.isKilled = True
elif self.currentHealth >= 1 and amount < 0 and self.isInvincible == False:
self.currentHealth += amount
self.damageTook = self.maxHealth - self.currentHealth
self.healthList = [True]*self.currentHealth + [False]*self.damageTook
self.isInvincible = True
self.freezeEverything = True
elif self.currentHealth >= 1 and amount > 0 and self.currentHealth != self.maxHealth:
self.currentHealth += amount
self.damageTook = self.maxHealth - self.currentHealth
self.healthList = [True]*self.currentHealth + [False]*self.damageTook
def knockBack(self, collisionDirection):
if not -self.y >= self.hazardLimit - 100:
self.y += self.knockBackY
if collisionDirection == 'right':
self.x -= self.knockBackX
elif collisionDirection == 'left':
self.x += self.knockBackX
elif collisionDirection in ['up', 'down']:
if self.x <= self.collidedEnemy.x:
self.x -= self.knockBackX
elif self.x > self.collidedEnemy.x:
self.x += self.knockBackX
def checkColliding(self, terrain):
self.getPlayerVertices()
if terrain.type == 'Rectangle':
return self.checkCollidingRect(terrain)
elif terrain.type == 'outerOval':
return self.checkCollidingOuterOval(terrain)
def checkCollidingRect(self, terrain):
terrain.getTerrainVertices()
if ((self.leftX > terrain.leftX) and (self.leftX < terrain.rightX) or
(self.rightX > terrain.leftX) and (self.rightX < terrain.rightX)):
if self.bottomY >= terrain.topY and self.bottomY <= terrain.bottomY:
if len(self.previousPositions) <= 2:
return (True, 'down', terrain.topY)
previousX, previousY = self.previousPositions[-2]
if previousY + self.height <= terrain.topY:
return (True, 'down', terrain.topY)
if ((self.bottomY > terrain.topY and self.bottomY < terrain.bottomY) or
(self.topY > terrain.topY and self.topY < terrain.bottomY)):
if self.rightX >= terrain.leftX and self.rightX <= terrain.rightX:
return (True, 'right', terrain.leftX)
if ((self.bottomY > terrain.topY and self.bottomY < terrain.bottomY) or
(self.topY > terrain.topY and self.topY < terrain.bottomY)):
if self.leftX <= terrain.rightX and self.leftX >= terrain.leftX:
return (True, 'left', terrain.rightX)
return False, None, None
def initializeVariables(self):
self.cornersAttack = []
self.cornersEnemy = []
self.projectedEnemy = []
self.projectedAttack = []
self.fourPointsAttack1 = []
self.fourPointsAttack2 = []
self.fourPointsEnemy1 = []
self.fourPointsEnemy2 = []
def getRotatedCorners(self, enemy):
topY = -self.attackY
leftX = self.attackX
bottomY = -(self.attackY - self.height)
rightX = self.attackX + self.attackWidth
middleX = (leftX + rightX)/2
middleY = (topY + bottomY)/2
attackAngle = self.rotateAngle
self.vectorAttackX = Vector(middleX, middleY, attackAngle)
self.vectorAttackY = Vector(middleX, middleY, attackAngle+90)
if self.rotateAngle == 0:
shiftAttackX = self.attackWidth/2
shiftAttackY = self.attackHeight/2
self.vectorAttackRightX = Vector(middleX+shiftAttackX, middleY, attackAngle)
self.vectorAttackLeftX = Vector(middleX-shiftAttackX, middleY, attackAngle)
self.vectorAttackRightY = Vector(middleX+shiftAttackY, middleY, attackAngle+90)
self.vectorAttackLeftY = Vector(middleX-shiftAttackY, middleY, attackAngle+90)
self.cornersAttack=[(self.attackX, -self.attackY),
(self.attackX, -self.attackY+self.attackHeight),
(self.attackX+self.attackWidth, -self.attackY),
(self.attackX+self.attackWidth, -self.attackY+self.attackHeight)]
else:
shiftAttackX = (self.attackWidth/2)/(math.cos(attackAngle*math.pi/180)+0.00001) # 0.00001 to avoid division by zero errors
shiftAttackY = (self.attackHeight/2)/(math.sin(attackAngle*math.pi/180)+0.00001)
self.vectorAttackRightX = Vector(middleX+shiftAttackX, middleY, attackAngle)
self.vectorAttackLeftX = Vector(middleX-shiftAttackX, middleY, attackAngle)
self.vectorAttackRightY = Vector(middleX+shiftAttackY, middleY, attackAngle+90)
self.vectorAttackLeftY = Vector(middleX-shiftAttackY, middleY, attackAngle+90)
self.cornersAttack = [
(self.vectorAttackLeftX.getIntersection(self.vectorAttackRightY)),
(self.vectorAttackLeftX.getIntersection(self.vectorAttackLeftY)),
(self.vectorAttackRightX.getIntersection(self.vectorAttackRightY)),
(self.vectorAttackRightX.getIntersection(self.vectorAttackLeftY))
]
enemy.middleX = (enemy.leftX + enemy.rightX)/2
enemy.middleY = (enemy.topY + enemy.bottomY)/2
self.vectorEnemyX = Vector(enemy.middleX, enemy.middleY, enemy.rotateAngle)
self.vectorEnemyY = Vector(enemy.middleX, enemy.middleY, enemy.rotateAngle+90)
if enemy.rotateAngle == 0:
shiftEnemyX = enemy.width/2
shiftEnemyY = enemy.height/2
self.vectorEnemyRightX = Vector(enemy.middleX + shiftEnemyX, enemy.middleY, enemy.rotateAngle)
self.vectorEnemyLeftX = Vector(enemy.middleX - shiftEnemyX, enemy.middleY, enemy.rotateAngle)
self.vectorEnemyRightY = Vector(enemy.middleX + shiftEnemyY, enemy.middleY, enemy.rotateAngle + 90)
self.vectorEnemyLeftY = Vector(enemy.middleX - shiftEnemyY, enemy.middleY, enemy.rotateAngle + 90)
self.cornersEnemy=[(enemy.x, -enemy.y),
(enemy.x, -enemy.y+enemy.height),
(enemy.x+enemy.width, -enemy.y),
(enemy.x+enemy.width, -enemy.y+enemy.height)]
else:
shiftEnemyX = (enemy.width/2) / (math.cos(enemy.rotateAngle*math.pi/180)+0.00001)
shiftEnemyY = (enemy.height/2) / (math.sin(enemy.rotateAngle*math.pi/180)+0.00001)
self.vectorEnemyRightX = Vector(enemy.middleX + shiftEnemyX, enemy.middleY, enemy.rotateAngle)
self.vectorEnemyLeftX = Vector(enemy.middleX - shiftEnemyX, enemy.middleY, enemy.rotateAngle)
self.vectorEnemyRightY = Vector(enemy.middleX + shiftEnemyY, enemy.middleY, enemy.rotateAngle + 90)
self.vectorEnemyLeftY = Vector(enemy.middleX - shiftEnemyY, enemy.middleY, enemy.rotateAngle + 90)
self.cornersEnemy = [
self.vectorEnemyLeftX.getIntersection(self.vectorEnemyRightY),
self.vectorEnemyLeftX.getIntersection(self.vectorEnemyLeftY),
self.vectorEnemyRightX.getIntersection(self.vectorEnemyRightY),
self.vectorEnemyRightX.getIntersection(self.vectorEnemyLeftY)
]
def projectCorners(self, enemy):
for vector in [self.vectorEnemyX,
self.vectorEnemyY]:
for (x, y) in self.cornersAttack:
self.projectedAttack.append(vector.getProjection(x, y))
for vector in [self.vectorAttackX,
self.vectorAttackY]:
for (x, y) in self.cornersEnemy:
self.projectedEnemy.append(vector.getProjection(x, y))
self.fourPointsAttack1 = self.projectedAttack[:4]
self.fourPointsAttack2 = self.projectedAttack[4:]
self.fourPointsEnemy1 = self.projectedEnemy[:4]
self.fourPointsEnemy2 = self.projectedEnemy[4:]
def countLineSegmentOverlap(self, enemy):
if self.rotateAngle == 0:
self.twoPointsAttack1 = [self.fourPointsAttack1[0], self.fourPointsAttack1[3]]
self.twoPointsAttack2 = [self.fourPointsAttack2[0], self.fourPointsAttack2[3]]
else:
self.twoPointsAttack1 = [max(self.fourPointsAttack1), min(self.fourPointsAttack1)]
self.twoPointsAttack2 = [max(self.fourPointsAttack2), min(self.fourPointsAttack2)]
if enemy.rotateAngle == 0:
self.twoPointsEnemy1 = [self.fourPointsEnemy1[0], self.fourPointsEnemy1[3]]
self.twoPointsEnemy2 = [self.fourPointsEnemy2[0], self.fourPointsEnemy2[3]]
else:
self.twoPointsEnemy1 = [max(self.fourPointsEnemy1), min(self.fourPointsEnemy1)]
self.twoPointsEnemy2 = [max(self.fourPointsEnemy2), min(self.fourPointsEnemy2)]
# 0 top left, 1 bottom left, 2 top right, 3 bottom right
(x0, y0) = self.cornersAttack[0]
(x1, y1) = self.cornersAttack[1]
(x2, y2) = self.cornersAttack[2]
(x3, y3) = self.cornersAttack[3]
referencePointAttack1X1 = ((x0+x2)/2, (y0+y2)/2)
referencePointAttack1X2 = ((x1+x3)/2, (y1+y3)/2)
referencePointAttack2X1 = ((x0+x1)/2, (y0+y1)/2)
referencePointAttack2X2 = ((x2+x3)/2, (y2+y3)/2)
(x0, y0) = self.cornersEnemy[0]
(x1, y1) = self.cornersEnemy[1]
(x2, y2) = self.cornersEnemy[2]
(x3, y3) = self.cornersEnemy[3]
referencePointEnemy1X1 = ((x0+x2)/2, (y0+y2)/2)
referencePointEnemy1X2 = ((x1+x3)/2, (y1+y3)/2)
referencePointEnemy2X1 = ((x0+x1)/2, (y0+y1)/2)
referencePointEnemy2X2 = ((x2+x3)/2, (y2+y3)/2)
[(endX1, endY1), (endX2, endY2)] = self.twoPointsEnemy1
x0, y0 = referencePointAttack1X1[0], referencePointAttack1X1[1]
x1, y1 = referencePointAttack1X2[0], referencePointAttack1X2[1]
if ((x0 <= endX1 and x0 >= endX2) or (x0 >= endX1 and x0 <= endX2) or
(x1 <= endX1 and x1 >= endX2) or (x1 >= endX1 and x1 <= endX2) or
(endX1 <= x0 and endX1 >= x1) or (endX1 >= x0 and endX1 <= x1) or
(endX2 <= x0 and endX2 >= x1) or (endX2 >= x0 and endX2 <= x1)):
self.projectionCollisions += 1
[(endX1, endY1), (endX2, endY2)] = self.twoPointsEnemy2
x0, y0 = referencePointAttack2X1[0], referencePointAttack2X1[1]
x1, y1 = referencePointAttack2X2[0], referencePointAttack2X2[1]
if ((x0 <= endX1 and x0 >= endX2) or (x0 >= endX1 and x0 <= endX2) or
(x1 <= endX1 and x1 >= endX2) or (x1 >= endX1 and x1 <= endX2) or
(endX1 <= x0 and endX1 >= x1) or (endX1 >= x0 and endX1 <= x1) or
(endX2 <= x0 and endX2 >= x1) or (endX2 >= x0 and endX2 <= x1)):
self.projectionCollisions += 1
[(endX1, endY1), (endX2, endY2)] = self.twoPointsAttack1
x0, y0 = referencePointEnemy1X1[0], referencePointEnemy1X1[1]
x1, y1 = referencePointEnemy1X2[0], referencePointEnemy1X2[1]
if ((x0 <= endX1 and x0 >= endX2) or (x0 >= endX1 and x0 <= endX2) or
(x1 <= endX1 and x1 >= endX2) or (x1 >= endX1 and x1 <= endX2) or
(endX1 <= x0 and endX1 >= x1) or (endX1 >= x0 and endX1 <= x1) or
(endX2 <= x0 and endX2 >= x1) or (endX2 >= x0 and endX2 <= x1)):
self.projectionCollisions += 1
[(endX1, endY1), (endX2, endY2)] = self.twoPointsAttack2
x0, y0 = referencePointEnemy2X1[0], referencePointEnemy2X1[1]
x1, y1 = referencePointEnemy2X2[0], referencePointEnemy2X2[1]
if ((x0 <= endX1 and x0 >= endX2) or (x0 >= endX1 and x0 <= endX2) or
(x1 <= endX1 and x1 >= endX2) or (x1 >= endX1 and x1 <= endX2) or
(endX1 <= x0 and endX1 >= x1) or (endX1 >= x0 and endX1 <= x1) or
(endX2 <= x0 and endX2 >= x1) or (endX2 >= x0 and endX2 <= x1)):
self.projectionCollisions += 1
def rotatingAxisAlgorithm(self, enemy):
if self.projectionCollisions == 4:
self.projectionCollisions = 0
return True
else:
self.projectionCollisions = 0
return False
def checkAttackColliding(self, enemy):
self.initializeVariables()
self.getRotatedCorners(enemy)
self.projectCorners(enemy)
self.countLineSegmentOverlap(enemy)
return self.rotatingAxisAlgorithm(enemy)
def checkCollidingOuterOval(self, terrain):
if (self.jumping == True or self.isPogoing == True) and self.reachFallPortion == False:
return False, None, None
else:
self.getPlayerVertices()
terrain.getTerrainVertices()
if ((self.orientationX-terrain.x)/(terrain.width/2))**2 + ((self.orientationY-terrain.y)/(terrain.height/2))**2 < 1:
self.setAngle(terrain)
self.getPlayerVertices()
if ((self.orientationX-terrain.x)/(terrain.width/2))**2 + ((self.orientationY-terrain.y)/(terrain.height/2))**2 < 1:
return (True, 'down', self.orientationX)
if ((self.middleX-terrain.x)/(terrain.width/2))**2 + ((self.bottomY-terrain.y)/(terrain.height/2))**2 < 1:
return (True, 'down', self.orientationX)
return False, None, None
def setAngle(self, terrain):
xPartialDerivative = 2*(self.middleX - terrain.x)/((terrain.width/2)**2)
yPartialDerivative = 2*(self.bottomY-terrain.y)/((terrain.height/2)**2)
if yPartialDerivative == 0:
self.rotateAngle = 0
return
alpha = math.atan(xPartialDerivative/(yPartialDerivative))*180/math.pi
if alpha < 45 and alpha >= 0:
self.rotateAngle = -alpha
elif alpha >= 45:
self.rotateAngle = -(90-alpha)
elif alpha <= -45:
self.rotateAngle = (90+alpha)
elif alpha < 0 and alpha > -45:
self.rotateAngle = -alpha
def isCollidingRect(self, other):
other.getPlayerVertices()
if ((self.leftX >= other.leftX) and (self.leftX <= other.rightX) or
(self.rightX >= other.leftX) and (self.rightX <= other.rightX)):
if self.bottomY >= other.topY and self.bottomY <= other.bottomY:
if len(self.previousPositions) <= 2:
return (True, 'down', other.topY)
previousX, previousY = self.previousPositions[-2]
if previousY + self.height <= other.topY:
return (True, 'down', other.topY)
if ((self.bottomY >= other.topY and self.bottomY <= other.bottomY) or
(self.topY >= other.topY and self.topY <= other.bottomY)):
if self.rightX >= other.leftX and self.rightX <= other.rightX:
return (True, 'right', other.leftX)
if ((self.bottomY >= other.topY and self.bottomY <= other.bottomY) or
(self.topY >= other.topY and self.topY <= other.bottomY)):
if self.leftX <= other.rightX and self.leftX >= other.leftX:
return (True, 'left', other.rightX)
return False, None, None
def takeDamageEnemy(self, damage):
actualDamage = min(self.health, damage)
self.health -= damage
if self.health == 0:
self.isKilled = True
def distance(self, x1, y1, x2, y2):
return ((x1-x2)**2 + (y1-y2)**2)**0.5