def save(dest = ""): if event.end_game: return level_set = os.path.split(level_dir)[-1] save_path = os.path.join(savegame.save_path, current_level+".yaml") savegame.set_current_level(level_set, current_level) event_obj = event.get_yaml_object() decal_obj = serialize.YamlDecalInvisList( [d.obj_id for d in decal.decals if not d.visible] ) save_list = physics.body_update_list + decal.decals level_obj = serialize.YamlLevelData(level_dir, current_level) yaml_list = [level_obj, event_obj] serialize.save( level_dir, current_level, save_list, save_path, yaml_list ) if dest != "": savegame.save_to(dest)
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()