-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.py
212 lines (196 loc) · 5.8 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
import pygame, math, geom
from vector import Vector
mp = [0,0]
class Entity:
entities = []
bulletIds = []
bombIds = []
agentIds = []
trackIds = []
nextId = 0
def __init__(self, type, xywh = None, xyr = None):
if xywh != None:
self.rect = pygame.Rect(xywh[0],xywh[1],xywh[2],xywh[3])
self.isRect = True
elif xyr != None:
self.circle = [xyr[0], xyr[1], xyr[2]]
self.isRect = False
else:
print("No location specified for entity")
self.type = type
self.ID = Entity.nextId
Entity.nextId += 1
Entity.addEntity(self)
@staticmethod
def tickAll(keys, mousePos, player, level, delta):
global mp
mp = mousePos
for e in Entity.entities:
if e == None:
continue
if e.type == Entity.PLAYER:
e.tick(keys, mousePos,level, delta)
elif e.type == Entity.BULLET or e.type == Entity.BOMB:
e.tick(level,delta)
elif e.type == Entity.XPL:
e.tick(delta)
elif e.type >= 5:
e.tick(player, level, delta)
@staticmethod
def drawAll(ts, surface, delta):
Entity.drawBombs(surface)
for e in Entity.entities:
if e == None or e.type == Entity.BOMB or e.type == Entity.TRACK:
continue
if e.type == 0 or e.type >= 5:
e.draw(ts, surface, delta)
continue
e.draw(surface)
@staticmethod
def drawBombs(surface):
for bId in Entity.bombIds:
b = Entity.entities[bId]
b.draw(surface)
@staticmethod
def drawTracks(surface):
for tId in Entity.trackIds:
t = Entity.entities[tId]
t.draw(surface)
def tick(self, delta):
pass
def draw(self, surface):
pass
def destroy(self):
Entity.removeEntity(self)
def collideEntity(self, e):
if self.isRect != e.isRect:
if self.isRect:
return geom.rectCircCollision(self.rect, e.circle)
return geom.rectCircCollision(e.rect, self.circle)
elif self.isRect:
return self.rect.colliderect(e.rect)
return geom.circCircCollision(self.circle, e.circle)
@staticmethod
def addEntity(e):
if e.type == Entity.BULLET:
Entity.bulletIds.append(e.ID)
elif e.type == Entity.BOMB:
Entity.bombIds.append(e.ID)
elif e.type == Entity.TRACK:
Entity.trackIds.append(e.ID)
elif e.type >= 5:
Entity.agentIds.append(e.ID)
Entity.entities.append(e)
@staticmethod
def removeEntity(e):
try:
if e.type == Entity.BULLET:
Entity.bulletIds.remove(e.ID)
elif e.type == Entity.BOMB:
Entity.bombIds.remove(e.ID)
elif e.type == Entity.TRACK:
Entity.trackIds.remove(e.ID)
elif e.type >= 5:
Entity.agentIds.remove(e.ID)
except ValueError:
pass
Entity.entities[e.ID] = None
def getW(self):
if self.isRect:
return self.rect.w
else:
return None
def getH(self):
if self.isRect:
return self.rect.h
else:
return None
def setW(self, w):
if self.isRect:
self.rect.w = w
def setH(self, h):
if self.isRect:
self.rect.h = h
def getR(self):
if self.isRect:
return None
else:
return self.circle[2]
def setR(self, r):
if not self.isRect:
self.circle[2] = r
def getX(self):
if self.isRect:
return self.rect.x
else:
return self.circle[0]
def getY(self):
if self.isRect:
return self.rect.y
else:
return self.circle[1]
def setX(self, x):
if self.isRect:
self.rect.x = x
else:
self.circle[0] = x
def setY(self, y):
if self.isRect:
self.rect.y = y
else:
self.circle[1] = y
def getLoc(self):
return (self.getX(), self.getY())
def getLocVect(self):
return Vector(self.getX(), self.getY())
def getSquare(self):
loc = self.getLoc()
return (math.floor(loc[0]/30), math.floor(loc[1]/30))
@staticmethod
def clear(l):
for e in Entity.entities:
if not e:
continue
if e.type == 1 or e.type == 2:
e.destroy()
elif e.type == 3:
e.safeDestroy()
@staticmethod
def typeToColour(type):
if type == 0:
return "Blue"
elif type == 5:
return "Brown"
elif type == 6:
return "Grey"
elif type == 7:
return "Teal"
elif type == 8:
return "Yellow"
elif type == 9:
return "Red"
elif type == 10:
return "Green"
elif type == 12:
return "Red"
PLAYER = 0
BULLET = 1
TRACK = 2
BOMB = 3
XPL = 4
TURRET = 5
SOLDIER = 6
HUNTER = 7
BOMBER = 8
ROVER = 9#Slightly lighter than player 2
SNIPER = 10
WARRIOR = 11
SENTRY = 12
SCOUT = 13
SPY = 14
ASSASIN = 15
ELITE = 16
SPAWNER = 17 #Slightly lighter than player 1
TACTICIAN = 18
GENERAL = 19
DESTROYER = 20