Exemplo n.º 1
0
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()
Exemplo n.º 2
0
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()