-
Notifications
You must be signed in to change notification settings - Fork 0
/
playerobject.py
75 lines (59 loc) · 2.43 KB
/
playerobject.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
from globalconst import *
from gameobjects import *
from particles import *
from graphics import *
from sound import *
import math
import verlet
class PlayerObject(GameObject):
def __init__(self, x, y, tile=None, particleColor = (255,128,0)):
GameObject.__init__(self, x, y, tile)
playSound("spawn")
self.boostLoopTimer = 0
self.particleColor = particleColor
def getThrust(self, power =65.0):
r = -math.radians(self.rotation) + 0.5 * math.pi
return [math.cos(r) * self.ydir * power, math.sin(r) * self.ydir * power]
def update(self, gamestate):
self.rotation = math.fmod(self.rotation + self.rotationDir * ROTATION_CHANGE, 360)
a = self.getThrust()
gravity = 10.0
a[1] += gravity
pos = [self.x, self.y]
verlet.integrate1(pos, self.v, a, 1 / FPS)
verlet.integrate2(pos, self.v, a, 1 / FPS)
self.x, self.y = pos
# collision with level
cx, cy = int(self.x // TILE_W), int(self.y // TILE_H)
level = gamestate.level
self.checkTileCollision(level, cx -1, cy -1)
self.checkTileCollision(level, cx +0, cy -1)
self.checkTileCollision(level, cx +1, cy -1)
self.checkTileCollision(level, cx -1, cy)
self.checkTileCollision(level, cx +0, cy)
self.checkTileCollision(level, cx +1, cy)
self.checkTileCollision(level, cx -1, cy +1)
self.checkTileCollision(level, cx +0, cy +1)
self.checkTileCollision(level, cx +1, cy +1)
def updateLocal(self, gamestate):
thrust = self.getThrust()
if thrust[0] != 0 or thrust[1] != 0:
self.boostLoopTimer-=1.0/60.0
if self.boostLoopTimer<=0:
self.boostLoopTimer=0.099
playSound("boost-loop")
particleDirMult = -3.0
thrust[0] *= particleDirMult / FPS
thrust[1] *= particleDirMult / FPS
particlesCreate(self.x,self.y,thrust[0] + self.v[0]/FPS,thrust[1] + self.v[1]/FPS,0.5,self.particleColor,1)
def checkTileCollision(self, level, cx, cy):
if cx < 0 or cy < 0 or cx >= len(level[0]) or cy >= len(level):
return
debugTiles.append((cx, cy))
tileId = level[cy][cx]
if tileId == ' ':
return False
tile = getTiles()[tileId]
if checkPixelTileCollision(self, tileId, cx, cy):
debugTiles.append((cx, cy))
print('coll!')