Пример #1
0
    def init_ambush_list(self, ambush_init_list):
        for ambush_init in ambush_init_list:
            # only add monsters to ambush, when hero enter ambush, add it to enemy group
            ambush = Ambush(
                ambush_init["ambush"]["pos"],
                sfg.Ambush.SURROUND_AREA_WIDTH,
                sfg.Ambush.ENTER_AREA_WIDTH,
                ambush_init["ambush"]["type"],
            )

            for monster_init in ambush_init["monsters"]:
                monster_id, pos, direct = monster_init["id"], monster_init["pos"], monster_init["direction"]
                monster_setting = sfg.SPRITE_SETTING_MAPPING[monster_id]
                EnemyClass = ENEMY_CLASS_MAPPING[monster_id]
                monster = EnemyClass(monster_setting, pos, direct)
                ambush.add(monster)

            ambush.init_sprite_status()
            self.ambush_list.append(ambush)
Пример #2
0
def run(chapter):
    clock = pygame.time.Clock()

    map_setting = util.load_map_setting(chapter)

    screen = pygame.display.set_mode(sfg.Screen.SIZE)
    pygame.display.set_caption("Renne Map Editor")
    camera = Camera(screen, map_size=map_setting["size"])
    game_world = GameWorld()
    game_map = GameMap(chapter, map_setting)

    # load hero
    hero = Renne(sfg.Renne, map_setting["hero"]["pos"], map_setting["hero"]["direction"])
    game_world.add_object(hero)

    # load monsters
    monster_init_list = map_setting.get("monsters", [])
    for monster_init in monster_init_list:
        monster_id, pos, direct = monster_init["id"], monster_init["pos"], monster_init["direction"]
        monster = Enemy(sfg.SPRITE_SETTING_MAPPING[monster_id], pos, direct)
        game_world.add_object(monster)

    # load static objects
    chapter_static_objects = map_setting.get("static_objects", [])
    for static_obj_init in chapter_static_objects:
        t, p = static_obj_init["id"], static_obj_init["pos"]
        static_obj = StaticObject(sfg.STATIC_OBJECT_SETTING_MAPPING[t], p)
        game_world.add_object(static_obj)

    # hack an attrbute ambush into game_world for easy saving
    game_world.ambush_list = []
    # some monsters is in ambush list
    ambush_init_list = map_setting.get("ambush_list", [])
    for ambush_init in ambush_init_list:
        ambush = Ambush(ambush_init["ambush"]["pos"], sfg.Ambush.SURROUND_AREA_WIDTH,
            sfg.Ambush.ENTER_AREA_WIDTH, ambush_init["ambush"]["type"])
        for monster_init in ambush_init["monsters"]:
            monster_id, pos, direct = monster_init["id"], monster_init["pos"], monster_init["direction"]
            monster = Enemy(sfg.SPRITE_SETTING_MAPPING[monster_id], pos, direct)
            game_world.add_object(monster)
            ambush.add(monster)
        game_world.ambush_list.append(ambush)

    running = True
    key_vec = Vector2()
    selected_object = None
    while running:
        mouse_pos = pygame.mouse.get_pos()
        map_pos_for_mouse = get_map_pos_for_mouse(camera.rect, mouse_pos)

        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                running = False

            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    if object_can_be_shift(selected_object) \
                        and put_down_selected_object(selected_object, game_world):
                        selected_object = None
                    elif isinstance(selected_object, Ambush) \
                        and put_down_ambush(selected_object, game_world):
                        selected_object = None

                if event.key == sfg.MapEditor.KEY_STATIC_OBJECT:
                    # static object
                    selected_object = selected_object_shift(selected_object, cfg.GameObject.TYPE_STATIC)
                elif event.key == sfg.MapEditor.KEY_ENEMY:
                    # enemy
                    selected_object = selected_object_shift(selected_object, cfg.GameObject.TYPE_DYNAMIC)
                elif event.key == sfg.MapEditor.KEY_AMBUSH:
                    # ambush
                    selected_object = mouse_ambush_toggle(selected_object)

                if event.key == sfg.MapEditor.KEY_ERASE_SELECTED_OBJECT:
                    selected_object = None

                if event.key == sfg.MapEditor.KEY_TURN_DIRECTION:
                    if isinstance(selected_object, GameSprite):
                        selected_object = turn_sprite_direction(selected_object)

                key_mods = pygame.key.get_mods()
                if key_mods & KMOD_CTRL and event.key == sfg.MapEditor.KEY_CTRL_SAVE:
                    # ctrl+s to save map setting
                    change_map_setting(map_setting, game_world, game_map)
                    util.save_map_setting(chapter, map_setting)
                    print "save chapter %s map setting" % chapter

                if key_mods & KMOD_ALT:

                    # debug draw switch
                    if event.key == sfg.MapEditor.KEY_ALT_SWITCH_POS:
                        DEBUG_DRAW["pos"] = not DEBUG_DRAW["pos"]
                    elif event.key == sfg.MapEditor.KEY_ALT_SWITCH_AREA:
                        DEBUG_DRAW["area"] = not DEBUG_DRAW["area"]
                    elif event.key == sfg.MapEditor.KEY_ALT_SWITCH_WAYPOINT:
                        DEBUG_DRAW["waypoints"] = not DEBUG_DRAW["waypoints"]
                    elif event.key == sfg.MapEditor.KEY_ALT_SWITCH_BLOCK_POINT:
                        DEBUG_DRAW["block_points"] = not DEBUG_DRAW["block_points"]

            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    # left click
                    if selected_object is None:
                        # pick up "this" unit if the mouse is over it
                        selected_object = select_unit(map_pos_for_mouse, game_world)
                    else:
                        # put down the current selected unit if no "collision" happen
                        if (isinstance(selected_object, GameSprite) or \
                            isinstance(selected_object, StaticObject)) \
                            and put_down_selected_object(selected_object, game_world):
                            if pygame.key.get_mods() & KMOD_CTRL:
                                selected_object = create_new_instance(selected_object)
                            else:
                                selected_object = None

                        elif isinstance(selected_object, Ambush) \
                            and put_down_ambush(selected_object, game_world):
                            selected_object = None

                    change_map_setting(map_setting, game_world, game_map)


        pressed_keys = pygame.key.get_pressed()
        key_vec.x = key_vec.y = 0.0
        if pressed_keys[K_LEFT]:
           key_vec.x -= 1.0
        if pressed_keys[K_RIGHT]:
            key_vec.x += 1.0
        if pressed_keys[K_UP]:
            key_vec.y -= 1.0
        if pressed_keys[K_DOWN]:
            key_vec.y += 1.0

        time_passed = clock.tick(sfg.FPS)
        passed_seconds = time_passed * 0.001

        camera.screen_move(key_vec, sfg.MapEditor.SCREEN_MOVE_SPEED, passed_seconds)
        if selected_object is not None:
            if isinstance(selected_object, GameSprite) \
                or isinstance(selected_object, StaticObject):
                set_selected_object_follow_mouse(map_pos_for_mouse, selected_object)
            elif isinstance(selected_object, Ambush):
                set_ambush_follow_mouse(map_pos_for_mouse, selected_object)

        game_map.draw(camera)

        # draw ambush before all objects, because they are "close" to the floor
        for ambush in game_world.ambush_list:
            ambush.draw(camera)

        for sp in sorted(game_world.yield_all_objects(), key=lambda sp: sp.pos.y):
            if sp.setting.GAME_OBJECT_TYPE == cfg.GameObject.TYPE_DYNAMIC:
                sp.animation.adjust_rect()
                # select current image for corresponding direction
                sp.animation.image = sp.animation.sprite_image_contoller.get_surface(
                    cfg.SpriteAction.STAND)[sp.direction]
                sp.animation.draw_shadow(camera)

            sp.draw(camera)

        if selected_object is not None:
            if isinstance(selected_object, GameSprite) \
                or isinstance(selected_object, StaticObject):
                # render selected_object
                selected_object_name = sfg.Font.ARIAL_32.render(
                    selected_object.setting.NAME, True, pygame.Color("black"))
                camera.screen.blit(selected_object_name, (5, 5))
                if isinstance(selected_object, GameSprite):
                    selected_object.animation.draw_shadow(camera)
                    selected_object.animation.image = selected_object.animation.sprite_image_contoller.get_surface(
                        cfg.SpriteAction.STAND)[selected_object.direction]
                selected_object.draw(camera)
                draw_block_points_for_selected_object(camera, selected_object)
            
            elif isinstance(selected_object, Ambush):
                camera.screen.blit(sfg.Font.ARIAL_32.render("Ambush", True, pygame.Color("black")), (5, 5))
                selected_object.draw(camera)

        # debug drawings
        for sp in game_world.yield_all_objects():
            if DEBUG_DRAW["pos"]:
                debug_tools.draw_pos(camera, sp)
            if DEBUG_DRAW["area"]:
                debug_tools.draw_area(camera, sp)

        if DEBUG_DRAW["waypoints"]:
            debug_tools.draw_waypoins(camera, game_map.waypoints)
        if DEBUG_DRAW["block_points"]:
            debug_tools.draw_block_points(camera, game_map.block_points)

        pygame.display.update()