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 b2drop(self): self.cshape.center = eu.Vector2(self.position[0], self.position[1]) rx, ry = pixels_to_tiles((self.cshape.rx, self.cshape.ry)) self.b2body.CreateFixture(b2.b2FixtureDef(shape=b2.b2PolygonShape(box=(rx, ry)), userData=self)) self.b2body.fixtures[-1].filterData.categoryBits = B2ITEM self.b2body.fixtures[-1].filterData.maskBits = B2LEVEL self.b2body.fixtures[-1].friction = 10 x, y = pixels_to_tiles((self.cshape.center.x, self.cshape.center.y)) self.b2body.position = (x, y) self.b2body.linearVelocity.x = self.master.b2body.linearVelocity.x + pixels_to_tiles(randint(-500, 500)) self.b2body.linearVelocity.y = self.master.b2body.linearVelocity.y + pixels_to_tiles(randint(-100, 100))
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 move_to(self, x, y): """ Place Actor to x, y. """ old = self.cshape.center.copy() vec = eu.Vector2(int(x), int(y)) self.position = vec self.cshape.center = vec self.b2body.position = pixels_to_tiles((vec.x, vec.y))
def __init__(self, img, cshape=None, position=(0, 0), vertical_speed=0.0, horizontal_speed=0.0): super(Movable_Object, self).__init__(img, position) self.image = img self.cshape = cshape if cshape: self.cshape.center = eu.Vector2(*position) self.b2body = None self.setup_b2body() self.b2body.linearVelocity = pixels_to_tiles((horizontal_speed, vertical_speed)) self.b2body.position = position self.ground_count = 0 self.on_ground = False