-
Notifications
You must be signed in to change notification settings - Fork 0
/
actors.py
644 lines (579 loc) · 23.7 KB
/
actors.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
import Box2D as box2d
from globals.types import Point
import globals
import ui
import drawing
import cmath
import math
import os
import game_view
import modes
import random
class StaticTriangle(object):
def __init__(self,physics,vertices):
self.physics = physics
self.bodydef = box2d.b2BodyDef()
self.triangle = drawing.Triangle(globals.ground_buffer)
#I don't think it matters much, but set the position to the average of the 3 points
midpoint = ((vertices[0] + vertices[1] + vertices[2])*self.physics.scale_factor)/3.0
self.bodydef.position = tuple(midpoint)
self.shape = box2d.b2PolygonDef()
self.shape.setVertices([ list(vertex*self.physics.scale_factor - midpoint) for vertex in vertices ])
self.body = physics.world.CreateBody(self.bodydef)
self.shape.density = 1
self.shape.friction = 0.5
self.body.CreateShape(self.shape)
self.PhysUpdate()
def GetPos(self):
return Point(*self.body.position)/self.physics.scale_factor
def PhysUpdate(self):
for i in xrange(3):
vertex = self.shape.vertices[i]
screen_coords = Point(*self.body.GetWorldPoint(vertex))/self.physics.scale_factor
self.triangle.vertex[i] = (screen_coords.x,screen_coords.y,10)
self.triangle.tc[i] = (screen_coords.x/64.,screen_coords.y/64.)
class StaticBox(object):
isBullet = False
mass = 1
filter_group = None
static = True
health = 500
def __init__(self,physics,bl,tr,tc = None):
#Hardcode the dirt texture since right now all static things are dirt. I know I know.
self.dead = False
self.tc = tc
if tc is not None:
self.InitPolygons(tc)
self.visible = True
else:
self.visible = False
self.physics = physics
self.bodydef = box2d.b2BodyDef()
midpoint = (tr - bl)*0.5*physics.scale_factor
self.bodydef.position = tuple((bl*physics.scale_factor) + midpoint)
self.shape = self.CreateShape(midpoint)
if not self.static:
self.shape.userData = self
if self.filter_group is not None:
self.shape.filter.groupIndex = self.filter_group
self.bodydef.isBullet = self.isBullet
self.body = physics.world.CreateBody(self.bodydef)
self.shape.density = self.mass
self.shape.friction = 0.7
self.shapeI = self.body.CreateShape(self.shape)
self.child_joint = None
self.parent_joint = None
self.PhysUpdate()
def Destroy(self):
if self.static:
#Don't ever destroy static things
return
if self.dead:
return
if self.parent_joint:
#We're attached, so get rid of that before killing us
self.parent_joint.UnGrapple()
self.parent_joint = None
self.shape.ClearUserData()
self.physics.world.DestroyBody(self.body)
self.dead = True
self.quad.Disable()
def Damage(self,amount):
#can't damage static stuff
return
def CreateShape(self,midpoint):
if self.dead:
return
shape = box2d.b2PolygonDef()
shape.SetAsBox(*midpoint)
return shape
def InitPolygons(self,tc):
if self.dead:
return
self.triangle1 = drawing.Triangle(globals.ground_buffer)
self.triangle2 = drawing.Triangle(globals.ground_buffer)
#self.triangles = self.triangle1,self.triangle2
def GetPos(self):
if self.dead:
return
return Point(*self.body.position)/self.physics.scale_factor
def GetAngle(self):
if self.dead:
return
return self.body.angle
def PhysUpdate(self):
if self.dead:
return
if not self.visible:
return
for triangle,vertex_list in ((self.triangle1,(0,1,2)),(self.triangle2,(2,3,0))):
v = 0
for i in vertex_list:
vertex = self.shape.vertices[i]
screen_coords = Point(*self.body.GetWorldPoint(vertex))/self.physics.scale_factor
triangle.vertex[v] = (screen_coords.x,screen_coords.y,10)
triangle.tc[v] = (screen_coords.x/64.,screen_coords.y/64.)
v += 1
class DynamicBox(StaticBox):
static = False
health = 30
def __init__(self,physics,bl,tr,tc):
super(DynamicBox,self).__init__(physics,bl,tr,tc)
self.body.SetMassFromShapes()
physics.AddObject(self)
def InitPolygons(self,tc):
if self.dead:
return
self.quad = drawing.Quad(globals.quad_buffer,tc = tc)
def PhysUpdate(self):
if self.dead:
return
#Just set the vertices
for i,vertex in enumerate(self.shape.vertices):
screen_coords = Point(*self.body.GetWorldPoint(vertex))/self.physics.scale_factor
self.quad.vertex[(i+3)%4] = (screen_coords.x,screen_coords.y,10)
def Damage(self,amount):
if globals.game_view.ship.state in modes.ShipStates.TUTORIAL:
return
self.health -= amount
if self.health < 0:
self.Destroy()
class Crate(DynamicBox):
def Damage(self,amount):
if globals.game_view.ship.state in modes.ShipStates.TUTORIAL:
return
self.health -= amount
if self.health < 0:
index = None
globals.sounds.explode.play()
if hasattr(globals.current_view.mode,'BoxDestroyed'):
globals.current_view.mode.BoxDestroyed(self)
self.Destroy()
class Bullet(DynamicBox):
pass
class PlayerBullet(Bullet):
isBullet = True
damage = 10
bullet_shape = Point(20,20)
def __init__(self,parent,physics,bl,tr,tc,filter_group,mass):
self.parent = parent
self.filter_group = filter_group
self.mass = mass
super(PlayerBullet,self).__init__(physics,bl,tr,tc)
class SeekingMissile(Bullet):
isBullet = True
damage = 20
bullet_shape = Point(20,20)
def __init__(self,parent,physics,bl,tr,tc,filter_group,mass):
self.parent = parent
self.target = globals.game_view.ship
self.filter_group = filter_group
self.mass = mass
super(SeekingMissile,self).__init__(physics,bl,tr,globals.game_view.atlas.TextureCoords(os.path.join(globals.dirs.sprites,'beam.png')))
def Destroy(self):
if self.parent:
self.parent.DeleteBullet(self)
super(SeekingMissile,self).Destroy()
def PhysUpdate(self):
super(SeekingMissile,self).PhysUpdate()
#Apply a thrust in the direction of the target
target_pos = self.target.GetPos()
self.pos = self.GetPos()
if not target_pos or not self.pos:
return
diff = target_pos - self.pos
direction = self.GetAngle() + (math.pi/2)
distance,angle = cmath.polar(complex(diff.x,diff.y))
#angle = (angle - (math.pi/2) - self.GetAngle())%(math.pi*2)
angle = (angle - direction)%(math.pi*2)
if angle < (math.pi):
#need to turn right, but by how much?
amount = angle/math.pi
else:
amount = -(2*math.pi - angle)/math.pi
desired_av = 10*amount
f = 1 - abs(amount)
current_speed = Point(*self.body.linearVelocity)
if f < 0.8 or current_speed.length() > 80:
desired_velocity = 0
#Apply a force in the direction we're not going to slow us down
force = Point(-current_speed.x,-current_speed.y)
self.body.ApplyForce(tuple(force),self.body.position)
else:
desired_velocity = 1
thrust = 20
vector = cmath.rect(thrust,direction)
self.body.ApplyForce((vector.real,vector.imag),self.body.position)
torque = (desired_av - self.body.angularVelocity)
self.body.ApplyTorque(torque)
class ShootingThing(DynamicBox):
wait_for_bullets = False
straight_up = False
def Fire(self,pos):
diff = pos - self.GetPos()
distance,angle = cmath.polar(complex(diff.x,diff.y))
angle = (angle - (math.pi/2) - self.GetAngle())%(math.pi*2)
#0 = pi*2 is straight ahead, pi is behind.
#so 0.75 pi to 1.25 pi is disallowed
if angle <= self.min_shoot and angle >= self.max_shoot:
return
if distance < self.min_shoot_distance:
#If you aim too close then the shots go wild
return
if distance > self.max_shoot_distance:
return
if self.cooldown > self.t:
return
self.sound.play()
self.fired = True
self.cooldown = self.t + self.cooldown_time
#if distance >= self.max_distance:
# return
for offset in self.cannon_positions:
if self.wait_for_bullets and len(self.bullets) >= self.max_bullets:
continue
bpos = Point(*self.body.GetWorldPoint(tuple(offset*self.physics.scale_factor)))/self.physics.scale_factor
bullet = self.bullet_type(self,self.physics,bpos,bpos+self.bullet_type.bullet_shape,tc = self.parent.atlas.TextureCoords(os.path.join(globals.dirs.sprites,'blast.png')),filter_group = self.filter_group,mass = self.bullet_mass)
#print dir(bullet.body)
if self.straight_up:
pass
else:
bullet.body.linearVelocity = tuple(Point(*self.body.linearVelocity) + (pos - bpos).unit_vector()*self.bullet_velocity)
self.bullets.append(bullet)
if len(self.bullets) > self.max_bullets:
bullet = self.bullets.pop(0)
bullet.Destroy()
class Trex(ShootingThing):
mass = 1
health = 100
cooldown_time = 1000
filter_group = -2
min_shoot = 1.2*math.pi
max_shoot = 0.8*math.pi
min_shoot_distance = 0
max_shoot_distance = 2000
max_bullets = 2
bullet_mass = 0.04
bullet_velocity = 80
bullet_type = PlayerBullet
cannon_positions = [Point(20,5)]
sprite = 'trex.png'
def __init__(self,parent,physics,bl,tr):
self.parent = parent
tc = self.parent.atlas.TextureCoords(os.path.join(globals.dirs.sprites,self.sprite))
super(Trex,self).__init__(physics,bl,tr,tc)
self.cooldown = 0
self.bullets = []
self.sound = globals.sounds.dino_blast
self.sound.set_volume(0.01)
self.death_sounds = globals.sounds.dino_die1,globals.sounds.dino_die2,globals.sounds.dino_die3
def Damage(self,amount):
if self.dead:
return
self.parent.ship.AddScore(amount)
if globals.game_view.ship.state in [modes.ShipStates.TUTORIAL_MOVEMENT,
modes.ShipStates.TUTORIAL_SHOOTING,
modes.ShipStates.TUTORIAL_GRAPPLE,
modes.ShipStates.TUTORIAL_TOWING]:
return
self.health -= amount
if self.health < 0:
random.choice(self.death_sounds).play()
#globals.current_view.mode.BoxDestroyed(self)
self.parent.RemoveTrex(self)
self.Destroy()
def Update(self,t):
self.t = t
if self.dead:
return
#If the player is above us in a 180 degree arc we'll try shooting at it
player_pos = self.parent.ship.GetPos()
if not player_pos:
return
self.Fire(player_pos)
class Stegosaurus(Trex):
bullet_type = SeekingMissile
bullet_velocity = 10
cooldown_time = 5000
max_bullets = 1
wait_for_bullets = True
sprite = 'steg.png'
def DeleteBullet(self,bullet):
index = None
for i,item in enumerate(self.bullets):
if item is bullet:
index = i
break
if index is not None:
del self.bullets[index]
class PlayerShip(ShootingThing):
max_shoot = 0.5*math.pi
min_shoot = 1.5*math.pi
max_distance = 300
max_grapple = 175
min_shoot_distance = 30
max_shoot_distance = 60000
mass = 3
filter_group = -1
cooldown_time = 400
health = 10000000
max_bullets = 10
bullet_mass = 0.08
bullet_velocity = 200
bullet_type = PlayerBullet
cannon_positions = [Point(20,5),Point(-20,5)]
def __init__(self,parent,physics,bl,tr,tc):
self.parent = parent
relative_bl = parent.GetRelative(tr + Point(10,10))
relative_tr = parent.GetRelative(tr + Point(400,150))
self.text = ui.TextBox(parent = parent,
bl = relative_bl,
tr = relative_tr,
text = ' ',
textType = drawing.texture.TextTypes.WORLD_RELATIVE,
scale = 2)
self.grapple_quad = drawing.Quad(globals.quad_buffer,tc = parent.atlas.TextureCoords(os.path.join(globals.dirs.sprites,'grapple.png')))
self.beam_quad = drawing.Quad(globals.quad_buffer,tc = parent.atlas.TextureCoords(os.path.join(globals.dirs.sprites,'beam.png')))
self.beam_quad.Disable()
self.beam = False
self.beam_power = 0
self.beam_fired = False
self.last_beam_update = 0
self.grapple_quad.Disable()
self.letter_duration = 30
self.text_start = None
self.fired = False
self.joint = False
self.t = 0
self.grappled = False
self.detached = False
super(PlayerShip,self).__init__(physics,bl,tr,tc)
self.bullets = []
self.cooldown = 0
self.text_limit = None
self.sound = globals.sounds.blast
self.sound.set_volume(0.4)
self.health_text = ui.TextBox(parent = globals.screen_root,
bl = Point(0,0.9),
tr = Point(0.2,0.95) ,
text = ' ',
scale = 2)
self.score_text = ui.TextBox(parent = globals.screen_root,
bl = Point(0,0.85),
tr = Point(0.2,0.9) ,
text = ' ',
scale = 2)
self.beam_power_text = ui.TextBox(parent = globals.screen_root,
bl = Point(0,0.8),
tr = Point(0.3,0.85) ,
text = ' ',
scale = 2)
self.number_enemies_text = ui.TextBox(parent = globals.screen_root,
bl = Point(0,0.75),
tr = Point(0.3,0.80) ,
text = ' ',
scale = 2)
self.AdjustBeamPower(1000)
self.SetHealth(500)
globals.sounds.hurt.set_volume(0.2)
self.SetScore(0)
def Update(self,t = None):
self.t = t
selfpos = self.GetPos()
if not selfpos:
return
self.text.SetPos(self.parent.GetRelative(selfpos))
if self.text_start == None and t is not None:
self.text_start = t+self.text_wait
if t is not None:
elapsed = t - self.text_start
if elapsed > len(self.text.text)*self.letter_duration:
self.text.EnableChars()
if self.text_limit is not None and elapsed > self.text_limit:
self.SetText(' ',wait=0)
elif elapsed > 0:
num_enabled = int(float(elapsed)/self.letter_duration)
self.text.EnableChars(num_enabled)
self.AdjustBeamPower(3)
if self.beam:
self.AdjustBeamPower(-10)
if self.beam_power <= 0:
self.StopBeam()
if self.beam:
self.beam_fired = True
self.beam_quad.Enable()
angle = self.GetAngle()
unit_vector = cmath.rect(1000,angle + math.pi/2)
unit_vector = Point(unit_vector.real,unit_vector.imag)
i = 0
left_part = Point(*self.body.GetWorldPoint(tuple(Point(-2,0.5))))/self.parent.physics.scale_factor
right_part = Point(*self.body.GetWorldPoint(tuple(Point(2,0.5))))/self.parent.physics.scale_factor
left_distant = left_part + unit_vector
right_distant = right_part + unit_vector
for i,vertex in enumerate((left_part,left_distant,right_distant,right_part,)):
self.beam_quad.vertex[i] = (vertex.x,vertex.y,20)
#Are we damaging anybody
for item in self.parent.enemies + self.parent.game_mode.ooze_boxes:
targets = [item]
if hasattr(item,'bullets'):
targets.extend(item.bullets)
for enemy in targets:
enemy_pos = enemy.GetPos()
if not enemy_pos:
continue
signs = []
for a,b in ((left_part,left_distant),(right_part,right_distant)):
sign = (b.x - a.x)*(enemy_pos.y-a.y) - (b.y - a.y)*(enemy_pos.x-a.x)
signs.append(sign)
if signs[0]*signs[-1] < 0:
#i.e the two signs were different, i.e the thing was on the different side of each line
#i.e it's between the lines
#Is it in front though?
a = left_part
b = right_part
sign = (b.x - a.x)*(enemy_pos.y-a.y) - (b.y - a.y)*(enemy_pos.x-a.x)
if sign > 0:
distance = (enemy_pos - left_part).length()
if distance < 600:
self.AddScore(10)
enemy.Damage(5)
else:
self.beam_quad.Disable()
#Set the vertices of the grapple_quad
if not self.grappled:
return
#It's not quite as simple as converting physics vertices to quad vertices since the physics engine
#isn't keeping track of the vertices we want to use. Instead get them from the bodies in question
for i,(body,offset) in enumerate(((self.body,Point(-0.2,0)),
(self.body,Point(0.2,0)),
(self.touching.GetBody(),self.contact + Point(-0.2,0)),
(self.touching.GetBody(),self.contact + Point(0.2,0)))):
screen_coords = Point(*body.GetWorldPoint(tuple(offset)))/self.parent.physics.scale_factor
self.grapple_quad.vertex[i] = (screen_coords.x,screen_coords.y,10)
def UnGrapple(self):
self.physics.world.DestroyJoint(self.joint)
self.joint = None
self.detached = True
self.grapple_quad.Disable()
self.touching = None
self.contact = None
self.grappled = False
if self.child_joint:
self.child_joint.parent_joint = None
self.child_joint = None
globals.sounds.ungrapple.play()
def FireBeam(self):
if self.beam == False:
globals.sounds.beam.play()
self.beam = True
def StopBeam(self):
if self.beam == True:
globals.sounds.beam.stop()
self.beam = False
def Grapple(self,pos):
if self.joint:
self.UnGrapple()
return
diff = pos - self.GetPos()
distance,angle = cmath.polar(complex(diff.x,diff.y))
angle = (angle - (math.pi/2) - self.GetAngle())%(math.pi*2)
#0 = pi*2 is straight ahead, pi is behind.
#so 0.75 pi to 1.25 pi is allowed
if not (angle <= self.min_shoot and angle >= self.max_shoot):
return
if distance > self.max_grapple:
return
#We need to determine if this point is in any objects...
#We'll create a small AABB around the point, and get the list of potentially intersecting shapes,
#then test each one to see if the point is inside it
aabb = box2d.b2AABB()
phys_pos = pos*self.physics.scale_factor
#First of all make sure it's not inside us
trans = box2d.b2XForm()
trans.SetIdentity()
p = phys_pos - Point(*self.body.position)
if self.shapeI.TestPoint(trans,tuple(p)):
return
aabb.lowerBound.Set(phys_pos.x-0.1,phys_pos.y-0.1)
aabb.upperBound.Set(phys_pos.x+0.1,phys_pos.y+0.1)
(count,shapes) = self.physics.world.Query(aabb,10)
for shape in shapes:
trans = box2d.b2XForm()
trans.SetIdentity()
p = phys_pos - Point(*shape.GetBody().position)
if shape.TestPoint(trans,tuple(p)):
self.touching = shape
self.contact = p
break
else:
self.touching = None
self.contact = None
if not self.touching:
return
globals.sounds.grapple.play()
#Tell the other body that it's in a joint with us so that
target = self.touching.userData
if target == None:
self.touching = None
self.contact = None
return
target.parent_joint = self
self.child_joint = target
joint = box2d.b2DistanceJointDef()
joint.Initialize(self.body,self.touching.GetBody(),self.body.GetWorldCenter(),tuple(phys_pos))
joint.collideConnected = True
self.joint = self.physics.world.CreateJoint(joint)
self.grappled = True
self.grapple_quad.Enable()
def SetText(self,text,wait = 1000,limit = None):
self.text.SetText(text)
self.text.EnableChars(0)
self.text_start = None
self.text_wait = wait
if limit is not None:
self.text_limit = limit
def SetHealth(self,value):
if value < 0:
value = 0
self.health = value
self.health_text.SetText('health: %d' % self.health)
def SetScore(self,value):
self.score = value
self.score_text.SetText('score: %d' % self.score)
def SetEnemies(self,value):
self.num_enemies = value
self.number_enemies_text.SetText('Dinosaurs : %s' % self.num_enemies)
def AddScore(self,value):
if globals.game_view.ship.state in modes.ShipStates.TUTORIAL:
return
self.SetScore(self.score + value)
def AdjustBeamPower(self,amount):
self.beam_power += amount
if self.beam_power < 0:
self.beam_power = 0
if self.beam_power > 1000:
self.beam_power = 1000
if self.t - self.last_beam_update > 100:
self.beam_power_text.SetText('beam power : %d' % self.beam_power)
self.last_beam_update = self.t
def Damage(self,amount):
if self.dead:
return
if globals.game_view.ship.state != modes.ShipStates.DESTROY_DINOS:
return
self.SetHealth(self.health - amount)
globals.sounds.hurt.play()
if self.health <= 0:
globals.sounds.player_die.play()
#globals.current_view.mode.BoxDestroyed(self)
self.parent.mode = modes.GameOver(self.parent,win = False,score = self.score)
def Disable(self):
self.health_text.Disable()
self.score_text.Disable()
self.number_enemies_text.Disable()
self.beam_power_text.Disable()
def Enable(self):
self.health_text.Enable()
self.score_text.Enable()
self.number_enemies_text.Enable()
self.beam_power_text.Enable()