def activate(self): density = 0.01 mass = density inertia = pymunk.moment_for_circle(mass, 0, self.radius) self._tractor_body = pymunk.Body(mass, inertia) self._tractor_body.position = self._body.position self._tractor_body._get_block = ref(self) self._tractor_shape = pymunk.Circle(self._tractor_body, self.radius) self._tractor_shape.collision_type = COLLISION_TYPES["tractor"] self._tractor_shape.sensor = True self._tractor_link = PivotJoint(self._body, self._tractor_body, (0, 0), (0, 0)) SPACE.add(self._tractor_body, self._tractor_shape, self._tractor_link)
def activate(self): density = 0.01 mass = density inertia = pymunk.moment_for_circle(mass, 0, self.radius) self._shield_body = pymunk.Body(mass, inertia) self._shield_body.position = self._body.position self._shield_shape = pymunk.Circle(self._shield_body, self.radius) self._shield_shape.elasticity = 0.5 self._shield_shape.friction= 0.0 self._shield_shape.collision_type = COLLISION_TYPES["shield"] self._shield_link = PivotJoint(self._body, self._shield_body, (0, 0), (0, 0)) SPACE.add(self._shield_body, self._shield_shape, self._shield_link)
def weld_to(self, block): pj = PivotJoint(self._body, block._body, (self._body.position + block._body.position) / 2) gj = GearJoint(self._body, block._body, 0, 1) SPACE.add(pj, gj) # add weak references to each other block._adjacent_blocks.add(self) self._adjacent_blocks.add(block) # and to the joint self._joints.add(pj) block._joints.add(pj) self._joints.add(gj) block._joints.add(gj)
def __init__(self): # load images base_path = "images/blocks/{}.png".format(self.image) self.img = load_image(base_path, self.image_anchor) # create collision object w = h = BLOCK_SIZE inertia = pymunk.moment_for_box(1, w, h) self._body = pymunk.Body(1, inertia) self._body.position = Vec2d(0, 0) self._shape = pymunk.Poly.create_box(self._body, (w, h), radius=0.2) self._shape.collision_type = COLLISION_TYPES['ghost'] self._shape.sensor = True self._shape._get_block = ref(self) SPACE.add(self._body, self._shape) # what can it connect to? self.valid_welds = []
def __init__(self, source=None): # physics r = 4 mass = 0.01 inertia = pymunk.moment_for_circle(mass, 0, r) self._body = pymunk.Body(mass, inertia) p = Vec2d(random.uniform(0, BLOCK_SIZE), 0) p.rotate(random.uniform(0, 2 * math.pi)) self._body.position = source._body.position self._body.velocity = source._body.velocity + p self._shape = pymunk.Circle(self._body, r) self._shape.elasticity = 1.0 self._shape.friction= 0.0 self._shape.collision_type = COLLISION_TYPES["resource"] self._shape._get_resource = ref(self) self._shape.sensor = True SPACE.add(self._body, self._shape) SPACE.resources.add(self)
def __init__(self, source=None, damage=1): source_body = source._body # physics r = 3 mass = 0.01 inertia = pymunk.moment_for_circle(mass, 0, r) self._body = pymunk.Body(mass, inertia) p = Vec2d(BLOCK_SIZE, 0) p.angle = source_body.angle + math.pi / 2 + source.direction * math.pi / 2 self._body.position = source_body.position + p self._body.velocity = source_body.velocity + p * BLOCK_SIZE * 2 self._shape = pymunk.Circle(self._body, r) self._shape.collision_type = COLLISION_TYPES["blaster"] self._shape._get_projectile = ref(self) SPACE.add(self._body, self._shape) SPACE.projectiles.add(self) # SFX if settings.SOUND: random.choice(BLASTER_SFX).play()
def __init__(self, point): self._joints = WeakSet() self._adjacent_blocks = WeakSet() # load images base_path = "images/blocks/{}.png".format(self.image) damaged_path = "images/blocks/{}_damaged.png".format(self.image) destroyed_path = "images/blocks/{}_destroyed.png".format(self.image) self.img = load_image(base_path, self.image_anchor) self.img_damaged = load_image(damaged_path, self.image_anchor) self.img_destroyed = load_image(destroyed_path, self.image_anchor) w = h = BLOCK_SIZE # using (density * 1 ** 2) for these because our units are BLOCK_SIZE inertia = pymunk.moment_for_box(self.material.density, w, h) self._body = pymunk.Body(self.material.density, inertia) self._body.position = point self._shape = pymunk.Poly.create_box(self._body, (w, h)) self._shape.elasticity = self.material.elasticity self._shape.friction = self.material.friction self._shape.collision_type = self.material.collision_type self._shape._get_block = ref(self) SPACE.add(self._body, self._shape) SPACE.register_block(self)
def __init__(self, point, radius, velocity=Vec2d(0, 0), damage=0): self.radius = radius self.damage = damage inertia = pymunk.moment_for_circle(pymunk.inf, 0, radius) self._body = pymunk.Body(pymunk.inf, inertia) self._body.position = point self._body.velocity = velocity self._shape = pymunk.Circle(self._body, radius) self._shape.collision_type = COLLISION_TYPES['explosion'] self._shape._get_explosion = ref(self) SPACE.add(self._body, self._shape) SPACE.register_explosion(self) # play SFX if settings.SOUND: sound = random.choice(EXPLOSION_SFX) # TODO: 3D sound #volume = (500 - (SPACE.camera_lock()._body.position - self._body.position).length) / 500 #if volume > 0: # print volume # sound.volume = random.choice([1.0, .5, .25]) sound.play()