-
Notifications
You must be signed in to change notification settings - Fork 0
/
hit.py
285 lines (228 loc) · 9.26 KB
/
hit.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
__author__ = 'Ecialo'
from pyglet import image
import cocos
from cocos import collision_model as cm
from cocos import euclid as eu
import math
import geometry as gm
import Box2D as b2
import movable_object as mova
import collides as coll
import box
from registry.group import CHOP, STAB, SWING, MISSLE, LINE, RECTANGLE
from registry.box2d import *
from registry.metric import pixels_to_tiles
from registry.utility import EMPTY_LIST
def shape_to_cshape(shape):
"""
Transform geometry shape to collision shape
"""
if isinstance(shape, gm.Rectangle):
return cm.AARectShape(shape.pc, shape.h_width, shape.h_height)
else:
c = shape.p + shape.v/2
return cm.AARectShape(c, abs(shape.v.x/2), abs(shape.v.y/2))
class Swing(cocos.draw.Line):
def __init__(self, stp, endp, master, hit_pattern=CHOP):
self.master = master
owner = self.master.owner
stp = owner.from_global_to_self(stp)
endp = owner.from_global_to_self(endp)
self.fight_group = master.owner.fight_group | SWING
self.base_fight_group = master.owner.fight_group
super(Swing, self).__init__(stp, endp, (0, 255, 0, 255))
self.completed = False
self._time_to_complete = 0.0
self._color_c = 0.0
self.b2fixture = None
self.cshape = None
self.trace = None
self.contacts = []
self.hit_pattern = hit_pattern
self.features = set()
effects = property(lambda self: filter(None, map(lambda eff: eff(self), self.master.effects)))
def update(self, dt):
if self.time_to_complete <= 0.0:
self.complete()
else:
self.time_to_complete -= dt
def uncompleteness(self):
return self.time_to_complete/self.master.swing_time
def _change_time_to_complete(self, time):
"""
Update time to complete with representable color
"""
self._time_to_complete = time
val = int(time * self._color_c)
self.color = (255 - val, val, 0, 255)
time_to_complete = property(lambda self: self._time_to_complete,
_change_time_to_complete)
def set_time_to_complete(self, time):
"""
Init time and set color of line to dangerous red
"""
self._time_to_complete = time
self._color_c = 255.0 / time
def on_begin_contact(self, fixture):
fixcat = fixture.filterData.categoryBits
if fixcat & B2BODYPART:
self.contacts.append(fixture.userData)
elif fixcat & B2SWING:
fixture.userData.collide(self)
def on_end_contact(self, fixture):
if fixture.filterData.categoryBits & B2ACTOR:
self.contacts.remove(fixture.body.userData)
def collide(self, other):
other._collide_slash(self)
def _collide_slash(self, other):
coll.collide_slash_slash(self, other)
def _collide_actor(self, other):
coll.collide_actor_slash(other, self)
def _collide_hit_zone(self, other):
coll.collide_slash_hit_zone(self, other)
def aim(self, vec):
end = self.start + vec.normalize()*self.master.length
self.end = end
def perform(self, time):
"""
Morph line into real collideable figure.
"""
#Define geometry and time data
actor = self.master.owner
v1 = actor.b2body.GetLocalPoint(pixels_to_tiles((self.start.x, self.start.y)))
v2 = actor.b2body.GetLocalPoint(pixels_to_tiles((self.end.x, self.end.y)))
if v2.x <= v1.x:
v1, v2 = v2, v1
vlen = math.sqrt((v2.x-v1.x)*(v2.x-v1.x)+(v2.y-v1.y)*(v2.y-v1.y))
vcent = ((v1.x+v2.x)/2.0, (v1.y+v2.y)/2.0)
vangle = math.asin((v2.y-v1.y)/vlen)
v = self.end - self.start
start = gm.Point2(self.start.x, self.start.y)
self.trace = gm.LineSegment2(start, v)
self.cshape = shape_to_cshape(self.trace)
self.set_time_to_complete(time)
self.b2fixture = actor.b2body.CreateFixture(b2.b2FixtureDef(shape=b2.b2PolygonShape(box=(vlen, 0,
vcent, vangle)),
isSensor=True, userData=self))
self.b2fixture.filterData.categoryBits = B2SWING
self.b2fixture.filterData.maskBits = B2HITZONE | B2SWING | B2BODYPART
actor.world.addEventHandler(self.b2fixture, self.on_begin_contact, self.on_end_contact)
self.schedule(self.update)
def set_batch(self, _):
pass
def complete(self, parried=False):
"""
End life of this line
"""
if self.completed:
return
self.completed = True
self.unschedule(self.update)
if not parried:
for bodypart in self.contacts:
bodypart.collide(self)
self.fight_group = -1
self.base_fight_group = -1
self.master.owner.world.destroy_fixture(self.b2fixture)
self.master.complete()
self.kill()
def _move(self, vec):
"""
Move end and start points of line
"""
self.position += vec
if self.trace is not None:
self.trace.p += vec
self.cshape.center += vec
def non_gravity_update(self, dt):
start_point = self.cshape.center.copy()
super(Hit_Zone, self).update(dt)
v = self.cshape.center - start_point
self.trace.p += v
class Hit_Zone(mova.Movable_Object):
def __init__(self, master, img, vector, speed, position, hit_shape,
effects=EMPTY_LIST):
if hit_shape is RECTANGLE:
trace = gm.Rectangle(gm.Point2(0, 0), eu.Vector2(img.width, img.height))
trace.pc = position
elif hit_shape is LINE:
start_point = gm.Point2(position[0] - img.width/2, position[1] - img.height/2)
v = vector/abs(vector)*img.width
trace = gm.LineSegment2(start_point, v)
#TODO add rotation
self.hit_shape = hit_shape
self.trace = trace
cshape = shape_to_cshape(self.trace)
v = vector/abs(vector) if abs(vector) != 0 else eu.Vector2(0,0)
v *= speed
mova.Movable_Object.__init__(self, img, cshape, position, v.y, v.x)
self.master = master
self.fight_group = master.owner.fight_group | MISSLE
self.base_fight_group = master.owner.fight_group
self.features = set()
self.hit_pattern = STAB
self.completed = False
self.time_to_complete = 0.1
self.time = 0.1
if effects is not EMPTY_LIST:
self.effects = filter(None, map(lambda eff: eff(self), effects))
else:
self.effects = filter(None, map(lambda eff: eff(self), self.master.effects))
self.schedule(self.update)
def setup_b2body(self):
super(Hit_Zone, self).setup_b2body()
cshape = self.cshape
hit_shape = self.hit_shape
img = self.image
if hit_shape is RECTANGLE:
rx, ry = pixels_to_tiles((cshape.rx, cshape.ry))
self.b2body.CreateFixture(b2.b2FixtureDef(shape=b2.b2PolygonShape(box=(rx, ry)), isSensor=True,
userData=self))
elif hit_shape is LINE:
r = pixels_to_tiles(img.width/2.0)
self.b2body.CreateFixture(b2.b2FixtureDef(shape=b2.b2PolygonShape(box=(r, 0)),
isSensor=True, userData=self))
self.b2body.gravityScale = 0
self.b2body.fixtures[-1].filterData.categoryBits = B2HITZONE
self.b2body.fixtures[-1].filterData.maskBits = B2HITZONE | \
B2SWING | B2LEVEL | B2BODYPART
self.world.addEventHandler(self.b2body.fixtures[-1], self.on_begin_contact, self.on_end_contact)
def update(self, dt):
self.time -= dt
def uncompleteness(self):
return max(0.0, self.time)/self.time_to_complete
def complete(self, parried=False):
if self.completed:
return
self.completed = True
self.world.destroy_body(self.b2body)
self.master.destroy_missile(self)
def on_begin_contact(self, fixture):
fixcat = fixture.filterData.categoryBits
if fixcat & B2LEVEL:
self.complete()
else:
fixture.userData.collide(self)
def on_end_contact(self, fixture):
pass
def collide(self, other):
other._collide_hit_zone(self)
def _collide_hit_zone(self, other):
coll.collide_hit_zone_hit_zone(self, other)
def _collide_actor(self, other):
coll.collide_actor_hit_zone(other, self)
def _collide_slash(self, other):
coll.collide_slash_hit_zone(other, self)
def show_hitboxes(self):
shape = self.trace.copy()
shape.pc = (0, 0)
self.add(box.Box(shape, (255, 0, 0, 255)), z=5)
class Invisible_Hit_Zone(Hit_Zone):
def __init__(self, master, width, height, v, speed, position, effects=EMPTY_LIST):
img = image.SolidColorImagePattern((0, 0, 0, 0)).create_image(width, height)
Hit_Zone.__init__(self, master, img, v, speed, position, RECTANGLE, effects)
#TODO: move to box2d
class Missile(Hit_Zone):
update = non_gravity_update
def uncompleteness(self):
return 0.5