-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_objects.pyx
351 lines (284 loc) · 11.1 KB
/
game_objects.pyx
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
# cython: profile=True
# cython: language_level=3
# cython: infer_types=True
from . import global_values as g
from . import utilities as util
from . import graphics as gfx
import pygame as p
import inspect
import math as m
# abstact parent class for basically anything in the game
class Game_Object():
def __init__(self, rect, **_kwargs):
self.rect = p.Rect(rect)
# x, y, width, height attributes are needed since pygame rect can only store int values
self.x = self.rect.x
self.y = self.rect.y
self.old_x = self.x
self.old_y = self.y
self.width = self.rect.w
self.height = self.rect.h
self.old_width = self.width
self.old_height = self.height
self.mask_collision = False
# velocity attributes
self.vx = 0
self.vy = 0
self.real_vx = 0
self.real_vy = 0
self.vx_keep = 0
self.vy_keep = 0
self.max_v = None
max_v = 3
self.max_vx = max_v
self.max_vy = max_v
self.set_max_vx(self.max_vx)
self.set_max_vy(self.max_vy)
# once a velocity component (absolutely) drops below this point, it will be set to 0
self.min_v_dropoff = 0.001
self.min_v = None
self.static = False
# graphics attributes
self.visible = True
self.graphics = None
self.surface = None
self.cache_surfaces = True
self.angle = 0
self.draw_bias = 0
self.temp = False
from . import events
self.pipe = events.Pipe(str(self))
kwargs = {"parent": None}
kwargs.update(_kwargs)
# parent/children attributes
self.parent = None
if kwargs["parent"]:
self.set_parent(kwargs["parent"])
self.children = []
self.__dict__.update(**kwargs)
if "max_v" in kwargs.keys():
self.set_max_v(kwargs["max_v"])
if "max_vx" in kwargs.keys():
self.set_max_vx(kwargs["max_vx"])
if "max_vy" in kwargs.keys():
self.set_max_vy(kwargs["max_vy"])
self.v_direction = util.get_angle(0, 0, self.vx, self.vy)
self.v_mag = util.get_distance(0, 0, self.vx, self.vy)
self.update_mask()
self.deleted = False
self.class_aliases = set()
for cls in inspect.getmro(self.__class__):
self.class_aliases.add("class_"+cls.__name__)
self.id = g.game_object_next_id
g.game_object_next_id += 1
if not self.temp:
self.add_to_game()
def add_to_game(self):
for alias in self.class_aliases:
if alias in g.game_objects.keys():
g.game_objects[alias].append(self)
else:
g.game_objects.update({alias: [self]})
def set_max_v(self, value):
self.max_v = value
if self.max_v is not None:
self.set_max_vx(value)
self.set_max_vy(value)
def set_max_vx(self, value):
self.max_vx = value
if self.max_vx is not None:
self.max_positive_vx = value
self.max_negative_vx = -value
else:
self.max_positive_vx = None
self.max_negative_vx = None
def set_max_vy(self, value):
self.max_vy = value
if self.max_vy is not None:
self.max_positive_vy = value
self.max_negative_vy = -value
else:
self.max_positive_vy = None
self.max_negative_vy = None
def set_parent(self, parent):
self.parent = parent
if self not in self.parent.children:
self.parent.children.append(self)
def remove_parent(self):
if self in self.parent.children:
self.parent.children.remove(self)
self.parent = None
def remove_child(self, child):
child.remove_parent()
# make the centerpoint of the Game_Object a given point
def center(self, center):
if isinstance(center, Game_Object):
center = center.rect.center
self.rect.center = center
self.x = self.rect.x
self.y = self.rect.y
# get distance/angle between game object and other things
def get_distance(self, x, y):
dist = util.get_distance(self.rect.centerx, self.rect.centery, x, y)
return dist
def get_distance_game_obj(self, game_obj):
dist = util.get_distance(self.rect.centerx, self.rect.centery, game_obj.rect.centerx, game_obj.rect.centery)
return dist
def get_angle(self, x, y):
angle = util.get_angle(self.rect.centerx, self.rect.centery, x, y)
return angle
def get_angle_game_obj(self, game_obj):
angle = util.get_angle(self.rect.centerx, self.rect.centery, game_obj.rect.centerx, game_obj.rect.centery)
return angle
# set rect values based off of position and dimensions
def update_rect(self):
self.rect.x = int(self.x)
self.rect.y = int(self.y)
self.rect.width = int(self.width)
self.rect.height = int(self.height)
# "offical" way to set x and y coords
# could be overwritten by game objects wishing to use it
def set_x(self, x):
self.x = x
def set_y(self, y):
self.y = y
# set x and y coords based on a rectangle (the Game_Object's rectangle if no arguments are specified)
def set_from_rect(self, rect=None, update_children=True, keep_decimal_component=True):
old_x = self.x
old_y = self.y
if not rect:
rect = self.rect
if keep_decimal_component:
self.x = rect.x+(self.x % 1)
self.y = rect.y+(self.y % 1)
self.width = rect.width+(self.width % 1)
self.height = rect.height+(self.height % 1)
else:
self.x = rect.x
self.y = rect.y
self.width = rect.width
self.height = rect.height
self.rect = rect.copy()
if update_children:
change_x = self.x-old_x
change_y = self.y-old_y
self.update_children(old_x, old_y)
self.update_rect()
def update_from_parent(self, change_x, change_y):
self.set_x(self.x+change_x)
self.set_y(self.y+change_y)
def update_children(self, change_x, change_y):
for child in self.children:
child.update_from_parent(change_x, change_y)
def update_mask(self):
if self.mask_collision and self.surface:
self.collision_mask = p.mask.from_surface(self.surface)
else:
self.collision_mask = p.mask.Mask((self.width, self.height))
self.collision_mask.fill()
def clamp_velocity(self):
# if velocity is very low set it to 0
if abs(self.vx) <= self.min_v_dropoff:
self.vx = 0
if abs(self.vy) <= self.min_v_dropoff:
self.vy = 0
# gradient must be maintained while capping velocity
if self.vx or self.vy:
self.v_direction = util.get_angle(0, 0, self.vx, self.vy)
self.v_mag = util.get_magnitude(self.vx, self.vy)
# cap velocity if it is too high
if self.max_v is not None:
if self.v_mag > self.max_v: # too high
self.vx = m.cos(self.v_direction)*self.max_v
self.vy = m.sin(self.v_direction)*self.max_v
else:
if self.vx > self.max_positive_vx:
self.vx = self.max_positive_vx
if self.vx < self.max_negative_vx:
self.vx = self.max_negative_vx
if self.vy > self.max_positive_vy:
self.vy = self.max_positive_vy
if self.vy < self.max_negative_vy:
self.vy = self.max_negative_vy
if self.min_v is not None:
if self.v_mag < self.min_v: # too low
self.vx = m.cos(self.v_direction)*self.min_v
self.vy = m.sin(self.v_direction)*self.min_v
else:
if self.max_v is not None:
if abs(self.vx) > self.max_v:
self.vx = self.max_v*(self.vx/abs(self.vx))
if abs(self.vy) > self.max_v:
self.vy = self.max_v*(self.vy/abs(self.vy))
else:
if self.vx > self.max_positive_vx:
self.vx = self.max_positive_vx
if self.vx < self.max_negative_vx:
self.vx = self.max_negative_vx
if self.vy > self.max_positive_vy:
self.vy = self.max_positive_vy
if self.vy < self.max_negative_vy:
self.vy = self.max_negative_vy
def slow_velocity(self):
self.vx *= self.vx_keep
self.vy *= self.vy_keep
def update_position_and_velocity(self):
self.clamp_velocity()
self.move(self.vx, self.vy)
self.slow_velocity()
def update(self):
if self.width != self.old_width or self.height != self.old_height:
self.update_mask()
if not self.static:
# update position and velocity
self.update_position_and_velocity()
else:
pass
self.update_rect()
# get all the g.events targetting this entity
# kinda weird IDK if it will work
def get_events(self, event_type=None, full=False):
obtained_events = []
for event in g.events:
if event in self.pipe.events:
obtained_events.append(event)
else:
if full:
if self in event.__dict__.values():
obtained_events.append(self)
else:
if hasattr(event, "entity") and event.entity == self:
obtained_events.append(self)
elif hasattr(event, "target") and event.target == self:
obtained_events.append(self)
return obtained_events
def clear_events(self, event_type=None, full=False):
for event in self.get_events(event_type, full=full):
event.delete()
# this should be overwritten by classes utilising more complex
def move(self, ax, ay):
self.x += ax
self.y += ay
self.update_rect()
def draw(self):
if self.graphics:
self.surface = gfx.get_surface(self.graphics)
if self.surface:
# TODO, test whether pygame will accept non-whole numbers in scale instructions
draw_surface = gfx.scale_surface(self.surface, (self.rect.w, self.rect.h), cache=self.cache_surfaces)
gfx.draw_rotated_surface(draw_surface, self.rect.topleft, self.angle, cx=0.5, cy=0.5, ox=0.5, oy=0.5)
def set_old_properties(self):
self.old_x = self.x
self.old_y = self.y
self.old_width = self.width
self.old_height = self.height
def delete(self, reason=None):
if not self.deleted:
self.deleted = True
if not self.temp:
for alias in self.class_aliases:
g.game_objects[alias].remove(self)
self.clear_events()
self.pipe.delete()
if self.parent:
self.parent.children.remove(self)