def _acquire_singlebody_now(self, singlebody, parent_unit): #Calculate the new velocity vector for the big body vx1, vy1 = self.body.velocity.x, self.body.velocity.y vx2, vy2 = singlebody.body.velocity.x, singlebody.body.velocity.y m1, m2 = self.body.mass, singlebody.body.mass vx3 = (m1*vx1+m2*vx2)/(m1+m2) vy3 = (m1*vy1+m2*vy2)/(m1+m2) #Find the body-local offset of the new unit new_offset = self.body.world_to_local(singlebody.body.position) #Define a shortcut to eliminate ambiguity new_unit = singlebody.unit new_unit.parent_unit = parent_unit #Kill the old body and shape physics.space.remove(singlebody.body) singlebody.unit.remove_shapes() physics.body_update_list.remove(singlebody) #Add the new unit to the big body self.units.append(new_unit) #Initialize it with the proper parent (big body) new_unit.initialize(self, collision_type=physics.PLAYER) new_unit.set_offset(new_offset.x,new_offset.y) #Give it the proper local rotation new_unit.local_angle = (new_unit.rotation+self.angle_degrees) new_unit.local_angle_target = new_unit.local_angle #Tell the physics engine about it new_unit.add_shapes() #Fix the big body self.update_center_of_mass() self.body.velocity=(vx3,vy3) if hasattr(singlebody.unit, 'obj_id'): if singlebody.unit.obj_id in event.attach_funcs.keys(): for func in event.attach_funcs[singlebody.unit.obj_id]: try: func(singlebody.unit.obj_id) except: func() event.update_player_units(self.units) new_unit.attach()
def load(level_name, keep_config=False, keep_velocity=False): global current_level, player reset() current_level = level_name path = os.path.join(level_dir, level_name+".yaml") stream = file(path, 'r') yaml_objects = yaml.load(stream) stream.close() load_geometry(yaml_objects) load_yaml_objects(yaml_objects) shapeloaders.add_line(0, 0, width, 0) shapeloaders.add_line(width, 0, width, height) shapeloaders.add_line(width, height, 0, height) shapeloaders.add_line(0, height, 0, 0) physics.space.resize_static_hash() physics.space.resize_active_hash() if not keep_config: init_player(player_start_x, player_start_y, math.pi/2, player_config) player.body.angle = player_start_angle else: player.body.position.x = player_start_x player.body.position.y = player_start_y if not keep_velocity: player.body.velocity.x = 0.0 player.body.velocity.y = 0.0 physics.body_update_list.append(player) physics.unit_update_list.extend(player.units) physics.space.add(player.body) for unit in player.units: if hasattr(unit, 'update_patients_now'): unit.update_patients_now = True unit.add_shapes() unit.batch = None unit.batch = batch unit.migrate() resources.wall_sound = resources.metal_against_metal2 event.update_player_units(player.units) level_module = __import__(level_name) if hasattr(level_module, 'init'): level_module.init()
def _release_unit_now(self, unit): #Find the new body's global coordinates and rotation physics.unit_update_list.remove(unit) new_body_position = self.body.local_to_world(unit.circle.center) new_body_rotation = math.degrees(self.body.angle) - unit.local_angle angle_to_unit = self.body.angle angle_to_unit += math.atan2(unit.offset[1], unit.offset[0]) extra_angle = angle_to_unit + math.pi/2 extra = self.body.angular_velocity * math.sqrt(unit.offset_dist_sq) new_body_velocity = ( self.body.velocity.x + extra * math.cos(extra_angle), self.body.velocity.y + extra * math.sin(extra_angle) ) unit.remove_shapes() #Create a new body, pass it the unit new_body = SingleBody(new_body_position, new_body_rotation, unit) new_body.body.velocity = new_body_velocity new_body.body.angular_velocity = self.body.angular_velocity unit.parent = None if unit.active: unit.active_timer = 1.0 unit.deactivate() unit.release() #Fix self self.update_center_of_mass() if hasattr(unit, 'obj_id'): if unit.obj_id in event.release_funcs.keys(): for func in event.release_funcs[unit.obj_id]: try: func(unit.obj_id) except TypeError: func() event.update_player_units(self.units)
def __init__(self, pos=(0.0,0.0), rot=0.0, child_units=[]): mass = 0.0 inertia = 0.0 self.units = [] offset_r = 0.0 for this_unit in child_units: offset_r = math.sqrt(this_unit.offset[0]**2+this_unit.offset[1]**2) mass += this_unit.mass inertia += pymunk.moment_for_circle( this_unit.mass, 0, this_unit.radius, this_unit.offset ) self.body = pymunk.Body(mass, inertia) self.units = child_units for this_unit in self.units: this_unit.initialize(self,collision_type=physics.PLAYER) this_unit.add_shapes() physics.body_update_list.append(self) physics.unit_update_list.extend(self.units) physics.space.add(self.body) self.update_center_of_mass() self.body.angle = rot self.body.position = pos self.x, self.y = self.body.position.x, self.body.position.y self.angle_degrees = math.degrees(self.body.angle) self.to_acquire = [] self.parent_units = [] self.to_release = [] self.attachable = False event.update_player_units(self.units)
def load_save_from_path(path, keep_config=False, keep_velocity=False): global player, current_level reset() event.init() stream = file(path, 'r') yaml_objects = [obj for obj in yaml.load(stream) if obj != None] stream.close() level_module = None target_queue = [] if player != None: for unit in player.units: unit.batch = None for obj in yaml_objects: try: getattr(saveloaders, obj.yaml_tag[3:])(obj) except: if obj.yaml_tag == u"!i_LevelData": change_level_set(obj.level_dir) current_level = obj.level_name savegame.set_current_level(obj.level_dir, current_level) level_module = __import__(current_level) path = os.path.join(level_dir, obj.level_name+".yaml") stream = file(path, 'r') yaml_geometry = yaml.load(stream) stream.close() load_geometry(yaml_geometry) elif obj.yaml_tag == u"!i_GlueBody": if not obj.is_player or not keep_config: unit_list = [saveloaders.unit_from_dict(u) for u in obj.units] new_gluebody = body.GlueBody( obj.position, obj.angle, unit_list ) new_gluebody.attachable = obj.attachable new_gluebody.body.velocity = obj.velocity new_gluebody.body.angular_velocity = obj.angular_velocity if obj.is_player: player = new_gluebody for unit in unit_list: if hasattr(unit, 'update_patients_now'): unit.update_patients_now = True if obj.is_player and keep_config: player.body.position = obj.position if not keep_velocity: player.body.velocity.x = 0.0 player.body.velocity.y = 0.0 physics.body_update_list.append(player) physics.unit_update_list.extend(player.units) physics.space.add(player.body) for unit in player.units: unit.add_shapes() unit.batch = batch unit.migrate() if unit.using_sound: unit.init_sound(unit.sound, unit.loop_sound) elif obj.yaml_tag == u"!i_Turret": new_turret = saveloaders.make_turret(obj) new_turret.active = obj.active target_queue.append((new_turret, obj.target)) if not obj.visible: new_turret.make_invisible() else: print "Did not load", obj for obj, target in target_queue: if target > 0: obj.target = event.get_object(target) event.update_player_units(player.units) resources.wall_sound = resources.metal_against_metal2 if hasattr(level_module, 'on_load'): level_module.on_load()