-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.py
96 lines (74 loc) · 2.5 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
import math
from pyglet import gl
from pyglet.clock import schedule_once, unschedule
import pyglet
# projectile, object, geometry
class Entity:
bolt = pyglet.resource.image("bolt.png")
bolt.anchor_x = bolt.width // 2
bolt.anchor_y = bolt.height // 2
blowup = pyglet.resource.image("blowoup.png")
blowup.anchor_x = bolt.width // 2
blowup.anchor_y = bolt.height // 2
def __init__(self, world, etype, x, y, **parameters):
self.active = True
self.world = world
self.collidable = False
self.blockable = False
self.dangerous = False
self.entitytype = etype
self.texture = None
self.vertexlist = None
if etype == "geometry":
self.collidable = True
self.blockable = True
self.x, self.y = x, y
self.w, self.h = parameters["dimensions"]
elif etype == "projectile":
self.dangerous = True
self.collidable = True
self.x, self.y = x, y
self.speed = parameters["speed"]
self.angle = parameters["angle"]
self.sx, self.sy = math.cos(self.angle) * self.speed, math.sin(self.angle) * self.speed
schedule_once(self.deactivate, 4.0)
self.texture = pyglet.sprite.Sprite(Entity.bolt, batch=world.batch, group=world.layers[3])
elif etype == "eprojectile":
self.dangerous = False
self.collidable = True
self.x, self.y = x, y
self.speed = parameters["speed"]
self.angle = parameters["angle"]
self.sx, self.sy = math.cos(self.angle) * self.speed, math.sin(self.angle) * self.speed
schedule_once(self.deactivate, 2.0)
self.texture = pyglet.sprite.Sprite(Entity.bolt, batch=world.batch, group=world.layers[3])
elif etype == "smallexplosion":
world.play("explosion")
self.dangerous = False
self.collidable = False
self.x, self.y = x, y
self.scale=.2
self.rotation=0
schedule_once(self.deactivate, .4)
self.texture = pyglet.sprite.Sprite(Entity.blowup, batch=world.batch, group=world.layers[3])
self.texture.x = self.x
self.texture.y = self.y
def deactivate(self, dt):
if dt == -1:
unschedule(self.deactivate)
self.active = False
if self.texture:
self.texture.delete()
def update(self, dt):
if self.entitytype == "projectile" or self.entitytype == "eprojectile":
self.x, self.y = self.x + self.sx * dt, self.y + self.sy * dt
try:
self.texture.x, self.texture.y = self.x, self.y
except AttributeError:
pass
if self.entitytype == "smallexplosion":
try:
self.texture.rotation += 5 * dt
self.texture.scale += dt * 2
except AttributeError:
pass