-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.py
executable file
·228 lines (182 loc) · 6.98 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
# game entities
import texture
import control
import vectors
import cell
import math
import sprite as sprclass
from texture import Texture
from functools import partial
#from control import *
from definitions import *
# the position of all entities is defined as their centre.
# rotation is stored separately, also defined about their centre.
# so collision detection will have to calculate the position
# of an entity's vertices
# superclass
class Entity:
entities = []
players = []
# we need cells, collision stuff, etc
def __init__(self, pos = None, rot = 0, vel = (0, 0), size = None, \
ctlfunc = None, interact = True, player = False, sprite = None, colour = (255, 255, 255), health = 100, owner = None):
self.age = 0
self.expiry = -1
self.health = health
self.owner = owner
self.pos = pos # centre of entity. if this remains None, interact should probably remain False. god help you if it doesn't
self.rot = rot
self.vel = vel
self.sprite = sprite
if not size:
if sprite:
self.size = len(self.sprite[0]), len(self.sprite)
else:
self.size = (0, 0)
else:
self.size = size
if not ctlfunc:
self.ctlfunc = control.Control(self)
else:
self.ctlfunc = ctlfunc
# collision check flag
if not self.pos:
self.interact = False
else:
self.interact = interact
self.player = player
if self.player:
Entity.players.append(self)
self.colour = colour
self.cells = []
if self.interact:
cell.add(self)
Entity.entities.append(self)
def delete(self):
if self.interact:
cell.delete(self)
Entity.entities.remove(self)
if self.player:
Entity.players.remove(self)
# return the local vertices, described counterclockwise, of the sprite
# rectangle rotated about its centre and the origin
def _localVertices(self):
w, h = self.size[0], self.size[1]
rot = self.rot
radius = vectors.length((0, 0), (w, h)) / 2
# top right corner
# ______
# | /|
# | / |
# | /__|
# |a |
# | |
# |______|
a = math.asin(h/(2 * radius))
#c = (radius * round(cos(rot + a), 2), radius * round(sin(rot + a), 2))
c = vectors.component(radius, rot + a)
v = [(c[0], c[1])]
# top left corner
#c = (radius * round(cos(rot + pi - a), 2), radius * round(sin(rot + pi - a), 2))
c = vectors.component(radius, rot + (math.pi - a))
v.append((c[0], c[1]))
# bottom left corner
#c = (radius * round(cos(rot + pi + a), 2), radius * round(sin(rot + pi + a), 2))
c = vectors.component(radius, rot + (math.pi + a))
v.append((c[0], c[1]))
# bottom right corner
#c = (radius * round(cos(rot - a), 2), radius * round(sin(rot - a), 2))
c = vectors.component(radius, rot - a)
v.append((c[0], c[1]))
return (list(map(lambda t: (round(t[0], 2), round(t[1], 2)), v)))
# orient the above in the scene
def vertices(self):
if(self.size[0] == 1 and self.size[1] == 1): # a bullet or something
v = [(0, 0)]
else:
v = self._localVertices()
return (list(map(lambda a: (a[0] + self.pos[0], a[1] + self.pos[1]), v)))
# projectile locations
def projloc(self, weapon):
right_mid = vectors.component(self.size[0] / 2, self.rot) # front middle.. tat tat tat
if weapon == "WEAP_BULLET":
return(self.pos[0] + right_mid[0], self.pos[1] + right_mid[1])
else:
return(self.pos)
# naughty ones, don't use this function. only the collision detector needs to
def _move(self, pos):
self.pos = pos
cell.delete(self)
cell.add(self)
# primary objects
class entPlane(Entity):
def __init__(self, pos, rot = 0, vel = (0, 0), player = False, sprite = None, size = None, expiry = -1, interact = True, health = HEALTH_PLANE, owner =
None):
self.age = 0
self.expiry = expiry
self.health = health
self.owner = owner
self.pos = pos
# if rotation and velocity is given, use only the magnitude of the velocity
if rot:
self.rot = ROT_PLANE_INT * round(rot / ROT_PLANE_INT)
else:
self.rot = ROT_PLANE_INT * round(vectors.anglex(vel) / ROT_PLANE_INT)
self.vel = vectors.component(vectors.length(vel), self.rot)
self.ctlfunc = control.ctlPlane(self)
self.player = player
if self.player:
Entity.players.append(self)
self.interact = interact
#if not Texture.textures.has_key("OBJ_PLANE"):
# Texture("OBJ_PLANE", IMGDIR + "OBJ_PLANE" + IMGEXT)
#self.texture = Texture.textures["OBJ_PLANE"]
#self.size = (40, 40)#(self.image.w, self.image.h)
if not sprite:
self.sprite = sprclass.plane
else:
self.sprite = sprite
if not size:
self.size = len(self.sprite[0]), len(self.sprite)
else:
self.size = size
self.cells = []
if self.interact:
cell.add(self)
Entity.entities.append(self)
class entTank(Entity):
pass
# projectiles
class entBullet(Entity):
def __init__(self, pos, vel = None, rot = 0, owner = None, sprite = None, size = None, expiry = EXPIRY_BULLET, health = 100):
self.age = 0
self.expiry = expiry
self.health = health
self.pos = pos
self.rot = rot
self.player = False
self.interact = True
if not vel:
if owner: self.vel = vector.component(SPD_BULLET, owner.rot)
else: self.vel = (SPD_BULLET, 0)
else:
self.vel = vel
self.owner = owner
self.ctlfunc = control.Control(self)
#if not Image.images.has_key("OBJ_BULLET"):
# Image("OBJ_BULLET", IMGDIR + "OBJ_BULLET" + IMGEXT)
#self.image = Image.images["OBJ_BULLET"]
#self.size = (self.image.w, self.image.h)
if not sprite:
self.sprite = sprclass.bullet
else:
self.sprite = sprite
if not size:
self.size = len(self.sprite[0]), len(self.sprite)
else:
self.size = size
self.cells = []
cell.add(self)
Entity.entities.append(self)
types = { "ENTITY": Entity, "PLANE": entPlane, "PLANE_PLAYER": partial(entPlane, player = True), "BULLET": entBullet }
#types = ["ENTITY", "PLANE", "PLANE_PLAYER", "BULLET"]