Beispiel #1
0
    def init(self):
        SoundManager.se_suppress()
        self.game_event = ObservableEvent()

        self.game_event += self.on_game_event

        n = len(self.game.players)
        ports = self.char_portraits = [
            GameCharacterPortrait(parent=self, color=color,
                                  x=x, y=y, tag_placement=tp)
            for x, y, tp, color in self.gcp_location[:n]
        ]

        pl = self.game.players
        shift = pl.index(self.game.me)
        for i, c in enumerate(ports):
            self.game_event += c.on_game_event
            p = pl[(shift + i) % n]
            c.player = p
            c.update()

        ports[0].equipcard_area.selectable = True  # it's TheChosenOne

        self.begin_select_player()
        self.end_select_player()
        self.skill_box = SkillSelectionBox(
            parent=self, x=161, y=9, width=70, height=22*6-4
        )

        SoundManager.switch_bgm('thb-bgm_game')

        self.more_init()
Beispiel #2
0
    def __init__(self, game, *a, **k):
        self.selection_change = ObservableEvent()

        self.game = game

        Control.__init__(self, can_focus=True, *a, **k)

        self.keystrokes = '\x00'
        self.char_portraits = None

        self.deck_indicator = DeckIndicator(
            parent=self,
            x=30,
            y=660,
            width=50,
            height=25,
        )

        self.handcard_area = HandCardArea(
            parent=self,
            view=self,
            x=238,
            y=9,
            zindex=3,
            width=93 * 5 + 42,
            height=145,
        )

        self.deck_area = PortraitCardArea(
            parent=self,
            width=1,
            height=1,
            x=self.width // 2,
            y=self.height // 2,
            zindex=4,
        )

        self.btn_afk = OptionButton(parent=self,
                                    zindex=1,
                                    conf=(
                                        (u'让⑨帮你玩', Colors.blue, False),
                                        (u'⑨在帮你玩', Colors.orange, True),
                                    ),
                                    **r2d((30, 625, 75, 25)))

        self.gameintro_icon = GameIntroIcon(parent=self,
                                            game=game,
                                            **r2d((690, 630, 25, 25)))

        self.dropcard_area = DropCardArea(
            parent=self,
            x=0,
            y=324,
            zindex=3,
            width=820,
            height=125,
        )

        class Animations(pyglet.graphics.Batch, Control):
            def __init__(self, **k):
                pyglet.graphics.Batch.__init__(self)
                Control.__init__(self,
                                 x=0,
                                 y=0,
                                 width=0,
                                 height=0,
                                 zindex=2,
                                 **k)
                self.animations = []

            def delete(self):
                Control.delete(self)
                for a in self.animations:
                    a.delete()

            def hit_test(self, x, y):
                return False

        self.animations = Animations(parent=self)
        self.selecting_player = 0
        self.action_params = {}
Beispiel #3
0
class THBattleUI(Control):
    portrait_location = [
        (60, 290, Colors.blue),
        (250, 440, Colors.orange),
        (450, 440, Colors.blue),
        (640, 290, Colors.orange),
        (450, 140, Colors.blue),
        (250, 140, Colors.orange),
    ]

    gcp_location = [
        (3, 1, 'me', Colors.blue),
        (669, 280, 'left', Colors.orange),
        (515, 500, 'bottom', Colors.blue),
        (335, 500, 'bottom', Colors.orange),
        (155, 500, 'bottom', Colors.blue),
        (3, 280, 'right', Colors.orange),
    ]

    def __init__(self, game, *a, **k):
        self.selection_change = ObservableEvent()

        self.game = game

        Control.__init__(self, can_focus=True, *a, **k)

        self.keystrokes = '\x00'
        self.char_portraits = None

        self.deck_indicator = DeckIndicator(
            parent=self,
            x=30,
            y=660,
            width=50,
            height=25,
        )

        self.handcard_area = HandCardArea(
            parent=self,
            view=self,
            x=238,
            y=9,
            zindex=3,
            width=93 * 5 + 42,
            height=145,
        )

        self.deck_area = PortraitCardArea(
            parent=self,
            width=1,
            height=1,
            x=self.width // 2,
            y=self.height // 2,
            zindex=4,
        )

        self.btn_afk = OptionButton(parent=self,
                                    zindex=1,
                                    conf=(
                                        (u'让⑨帮你玩', Colors.blue, False),
                                        (u'⑨在帮你玩', Colors.orange, True),
                                    ),
                                    **r2d((30, 625, 75, 25)))

        self.gameintro_icon = GameIntroIcon(parent=self,
                                            game=game,
                                            **r2d((690, 630, 25, 25)))

        self.dropcard_area = DropCardArea(
            parent=self,
            x=0,
            y=324,
            zindex=3,
            width=820,
            height=125,
        )

        class Animations(pyglet.graphics.Batch, Control):
            def __init__(self, **k):
                pyglet.graphics.Batch.__init__(self)
                Control.__init__(self,
                                 x=0,
                                 y=0,
                                 width=0,
                                 height=0,
                                 zindex=2,
                                 **k)
                self.animations = []

            def delete(self):
                Control.delete(self)
                for a in self.animations:
                    a.delete()

            def hit_test(self, x, y):
                return False

        self.animations = Animations(parent=self)
        self.selecting_player = 0
        self.action_params = {}

    @property
    def afk(self):
        return self.btn_afk.value

    def init(self):
        SoundManager.se_suppress()
        self.game_event = ObservableEvent()
        self.process_game_event = self.game_event.notify

        self.game_event += self.on_game_event

        n = len(self.game.players)
        ports = self.char_portraits = [
            GameCharacterPortrait(parent=self,
                                  color=color,
                                  x=x,
                                  y=y,
                                  tag_placement=tp)
            for x, y, tp, color in self.gcp_location[:n]
        ]

        pl = self.game.players
        shift = pl.index(self.game.me)
        for i, c in enumerate(ports):
            self.game_event += c.on_game_event
            p = pl[(shift + i) % n]
            c.player = p
            c.update()

        ports[0].equipcard_area.selectable = True  # it's TheChosenOne

        self.begin_select_player()
        self.end_select_player()
        self.skill_box = SkillSelectionBox(parent=self,
                                           x=161,
                                           y=9,
                                           width=70,
                                           height=22 * 6 - 4)

        SoundManager.switch_bgm('thb-bgm_game')

        self.more_init()

    def more_init(self):
        pass

    def set_live(self):
        SoundManager.se_unsuppress()
        self.update_portraits_hard()
        self.update_handcard_area()
        self.refresh_input_state()

    def player2portrait(self, p):
        from gamepack.thb.characters.baseclasses import Character
        if isinstance(p, Character):
            p = p.player

        for port in self.char_portraits:
            if port.player == p:
                break
        else:
            raise ValueError(p)
        return port

    def refresh_input_state(self):
        self.action_params = {}

        g = self.game
        skills = getattr(g.me, 'skills', None)
        if skills is not None:
            skills = [(s, i, s.ui_meta.clickable(g))
                      for i, s in enumerate(skills)
                      if not getattr(s.ui_meta, 'no_display', False)]

            skills.sort(key=lambda i: -i[2])

            self.skill_box.set_skills(skills)

    def update_portraits(self):
        for port in self.char_portraits:
            port.update()

    def update_portraits_hard(self):
        g = self.game
        for p in g.players:
            port = self.player2portrait(p)
            port.update_identity(p)
            port.clear_equip_sprites()
            if not hasattr(p, 'equips'): continue
            port.add_equip_sprites(p.equips)
            effects.update_tags(self, p)
            port.update()

    def update_handcard_area(self):
        g = self.game
        me = g.me
        if not hasattr(me, 'cards'): return

        hca = self.handcard_area

        for c in hca.cards[:]:
            c.delete()

        for c in list(me.cards) + list(me.showncards):
            cs = CardSprite(c, parent=hca)
            cs.associated_card = c

        hca.update()

    def on_message(self, _type, *args):
        if _type == 'player_change':
            for i, pd in enumerate(args[0]):
                p = self.game.players[i]
                port = self.player2portrait(p)
                port.dropped = (pd['state'] in ('dropped', 'fleed'))
                port.fleed = (pd['state'] == 'fleed')
                port.update()

    def on_game_event(self, evt_type, arg):
        if evt_type == 'action_before' and isinstance(arg, actions.PlayerTurn):
            self.current_player = arg.target

        elif evt_type in ('game_begin', 'switch_character'):
            self.update_portraits()

        elif evt_type == 'action_after':
            act = arg
            meta = getattr(act, 'ui_meta', None)
            if meta and getattr(meta, 'update_portrait', None):
                pl = set()
                if act.source:
                    pl.add(act.source)

                if hasattr(act, 'target_list'):
                    pl.update(act.target_list)
                elif act.target:
                    pl.add(act.target)

                for p in pl:
                    self.player2portrait(p).update()

        effects.handle_event(self, evt_type, arg)
        inputs.handle_event(self, evt_type, arg)

    def more_on_message(self, _type, args):
        pass

    def on_text(self, text):
        # The easter egg
        ks = self.keystrokes
        ks = (ks + text)[:40]
        self.keystrokes = ks

        from gamepack.thb.characters.baseclasses import Character

        for c in Character.character_classes.itervalues():
            try:
                alter = c.ui_meta.figure_image_alter
            except:
                continue

            for i in xrange(len(ks)):
                if L(alter).decrypt(ks[-i:]):
                    SoundManager.play('c-sound-input')

    def draw(self):
        self.draw_subcontrols()

    def ray(self, f, t):
        if f == t: return
        sp = self.player2portrait(f)
        dp = self.player2portrait(t)
        x0, y0 = sp.x + sp.width / 2, sp.y + sp.height / 2
        x1, y1 = dp.x + dp.width / 2, dp.y + dp.height / 2
        Ray(x0, y0, x1, y1, parent=self, zindex=10)

    def prompt(self, s):
        self.prompt_raw(u'|B|cff0000ff>> |r' + unicode(s) + u'\n')

    def prompt_raw(self, s):
        self.parent and self.parent.events_box.append(s)

    def begin_select_player(self, disables=[]):
        # if self.selecting_player: return
        self.selecting_player = True
        # self.selected_players = []
        for p in self.game.players:
            port = self.player2portrait(p)

            if p in disables:
                port.disabled = True
                port.selected = False
                try:
                    self.selected_players.remove(p)
                except ValueError:
                    pass
            else:
                port.disabled = False

    def get_selected_players(self):
        return self.selected_players

    def set_selected_players(self, players):
        for p in self.char_portraits:
            p.selected = False

        for p in players:
            self.player2portrait(p).selected = True

        self.selected_players = players[:]

    def end_select_player(self):
        # if not self.selecting_player: return
        self.selecting_player = False
        self.selected_players = []
        for p in self.char_portraits:
            p.selected = False
            p.disabled = False

    def get_selected_cards(self):
        return [
            cs.associated_card
            for cs in self.handcard_area.cards if cs.hca_selected
        ] + [
            cs.associated_card
            for cs in self.player2portrait(self.game.me).equipcard_area.cards
            if cs.selected
        ]

    def get_selected_skills(self):
        skills = self.game.me.skills
        return sorted([skills[i] for i in self.skill_box.get_selected_index()],
                      key=lambda s: s.sort_index)

    def reset_selected_skills(self):
        self.skill_box.reset()

    def get_action_params(self):
        return self.action_params

    def on_mouse_click(self, x, y, button, modifier):
        c = self.control_frompoint1_recursive(x, y)
        if isinstance(c, GameCharacterPortrait
                      ) and self.selecting_player and not c.disabled:
            char = c.character
            if not char: return True
            sel = c.selected
            psel = self.selected_players
            if sel:
                c.selected = False
                psel.remove(char)
            else:
                c.selected = True
                psel.append(char)

            self.selection_change.notify()
        return True

    def get_game_screen(self):
        assert self.parent
        return self.parent

    @staticmethod
    def show_result(g):
        ResultPanel(g, parent=Overlay.cur_overlay)
Beispiel #4
0
 def __init__(self, *a, **k):
     dict.__init__(self, *a, **k)
     dict.__setattr__(self, 'setting_change', ObservableEvent())
Beispiel #5
0
    def __init__(self, game, *a, **k):
        self.selection_change = ObservableEvent()

        self.game = game

        Control.__init__(self, can_focus=True, *a, **k)

        self.keystrokes = '\x00'
        self.char_portraits = None

        self.deck_indicator = DeckIndicator(
            parent=self, x=30, y=660, width=50, height=25,
        )

        self.handcard_area = HandCardArea(
            parent=self, view=self, x=238, y=9, zindex=3,
            width=93*5+42, height=145,
        )

        self.deck_area = PortraitCardArea(
            parent=self, width=1, height=1,
            x=self.width//2, y=self.height//2, zindex=4,
        )

        self.btn_afk = OptionButton(
            parent=self, zindex=1, conf=(
                (u'让⑨帮你玩', Colors.blue, False),
                (u'⑨在帮你玩', Colors.orange, True),
            ), **r2d((30, 625, 75, 25))
        )

        self.gameintro_icon = GameIntroIcon(
            parent=self, game=game,
            **r2d((690, 630, 25, 25))
        )

        self.dropcard_area = DropCardArea(
            parent=self, x=0, y=324, zindex=3,
            width=820, height=125,
        )

        class Animations(pyglet.graphics.Batch, Control):
            def __init__(self, **k):
                pyglet.graphics.Batch.__init__(self)
                Control.__init__(
                    self, x=0, y=0,
                    width=0, height=0, zindex=2,
                    **k
                )
                self.animations = []

            def delete(self):
                Control.delete(self)
                for a in self.animations:
                    a.delete()

            def hit_test(self, x, y):
                return False

        self.animations = Animations(parent=self)
        self.selecting_player = 0
        self.action_params = {}
Beispiel #6
0
class THBattleUI(Control):
    portrait_location = [
        (60,  290, Colors.blue),
        (250, 440, Colors.orange),
        (450, 440, Colors.blue),
        (640, 290, Colors.orange),
        (450, 140, Colors.blue),
        (250, 140, Colors.orange),
    ]

    gcp_location = [
        (3,   1,   'me',     Colors.blue),
        (669, 280, 'left',   Colors.orange),
        (515, 500, 'bottom', Colors.blue),
        (335, 500, 'bottom', Colors.orange),
        (155, 500, 'bottom', Colors.blue),
        (3,   280, 'right',  Colors.orange),
    ]

    def __init__(self, game, *a, **k):
        self.selection_change = ObservableEvent()

        self.game = game

        Control.__init__(self, can_focus=True, *a, **k)

        self.keystrokes = '\x00'
        self.char_portraits = None

        self.deck_indicator = DeckIndicator(
            parent=self, x=30, y=660, width=50, height=25,
        )

        self.handcard_area = HandCardArea(
            parent=self, view=self, x=238, y=9, zindex=3,
            width=93*5+42, height=145,
        )

        self.deck_area = PortraitCardArea(
            parent=self, width=1, height=1,
            x=self.width//2, y=self.height//2, zindex=4,
        )

        self.btn_afk = OptionButton(
            parent=self, zindex=1, conf=(
                (u'让⑨帮你玩', Colors.blue, False),
                (u'⑨在帮你玩', Colors.orange, True),
            ), **r2d((30, 625, 75, 25))
        )

        self.gameintro_icon = GameIntroIcon(
            parent=self, game=game,
            **r2d((690, 630, 25, 25))
        )

        self.dropcard_area = DropCardArea(
            parent=self, x=0, y=324, zindex=3,
            width=820, height=125,
        )

        class Animations(pyglet.graphics.Batch, Control):
            def __init__(self, **k):
                pyglet.graphics.Batch.__init__(self)
                Control.__init__(
                    self, x=0, y=0,
                    width=0, height=0, zindex=2,
                    **k
                )
                self.animations = []

            def delete(self):
                Control.delete(self)
                for a in self.animations:
                    a.delete()

            def hit_test(self, x, y):
                return False

        self.animations = Animations(parent=self)
        self.selecting_player = 0
        self.action_params = {}

    @property
    def afk(self):
        return self.btn_afk.value

    def process_game_event(self, evt, data):
        self.game_event.notify(evt, data)
        return data

    def init(self):
        SoundManager.se_suppress()
        self.game_event = ObservableEvent()

        self.game_event += self.on_game_event

        n = len(self.game.players)
        ports = self.char_portraits = [
            GameCharacterPortrait(parent=self, color=color,
                                  x=x, y=y, tag_placement=tp)
            for x, y, tp, color in self.gcp_location[:n]
        ]

        pl = self.game.players
        shift = pl.index(self.game.me)
        for i, c in enumerate(ports):
            self.game_event += c.on_game_event
            p = pl[(shift + i) % n]
            c.player = p
            c.update()

        ports[0].equipcard_area.selectable = True  # it's TheChosenOne

        self.begin_select_player()
        self.end_select_player()
        self.skill_box = SkillSelectionBox(
            parent=self, x=161, y=9, width=70, height=22*6-4
        )

        SoundManager.switch_bgm('thb-bgm_game')

        self.more_init()

    def more_init(self):
        pass

    def set_live(self):
        SoundManager.se_unsuppress()
        self.update_portraits_hard()
        self.update_handcard_area()
        self.refresh_input_state()

    def player2portrait(self, p):
        from thb.characters.baseclasses import Character
        if isinstance(p, Character):
            p = p.player

        for port in self.char_portraits:
            if port.player == p:
                break
        else:
            raise ValueError(p)
        return port

    def refresh_input_state(self):
        self.action_params = {}

        g = self.game
        skills = getattr(g.me, 'skills', None)
        if skills is not None:
            skills = [
                (s, i, s.ui_meta.clickable(g))
                for i, s in enumerate(skills)
                if not getattr(s.ui_meta, 'no_display', False)
            ]

            skills.sort(key=lambda i: -i[2])

            self.skill_box.set_skills(skills)

    def update_portraits(self):
        for port in self.char_portraits:
            port.update()

    def update_portraits_hard(self):
        g = self.game
        for p in g.players:
            port = self.player2portrait(p)
            port.update_identity(p)
            port.clear_equip_sprites()
            if not hasattr(p, 'equips'): continue
            port.add_equip_sprites(p.equips)
            effects.update_tags(self, p)
            port.update()

    def update_handcard_area(self):
        g = self.game
        me = g.me
        if not hasattr(me, 'cards'): return

        hca = self.handcard_area

        for c in hca.cards[:]:
            c.delete()

        for c in list(me.cards) + list(me.showncards):
            cs = CardSprite(c, parent=hca)
            cs.associated_card = c

        hca.update()

    def on_message(self, _type, *args):
        if _type == 'player_change':
            for i, pd in enumerate(args[0]):
                p = self.game.players[i]
                port = self.player2portrait(p)
                port.dropped = (pd['state'] in ('dropped', 'fleed'))
                port.fleed = (pd['state'] == 'fleed')
                port.update()

    def on_game_event(self, evt_type, arg):
        if evt_type == 'action_before' and isinstance(arg, actions.PlayerTurn):
            self.current_player = arg.target

        elif evt_type in ('game_begin', 'switch_character'):
            self.update_portraits()

        elif evt_type == 'action_after':
            act = arg
            meta = getattr(act, 'ui_meta', None)
            if meta and getattr(meta, 'update_portrait', None):
                pl = set()
                if act.source:
                    pl.add(act.source)

                if hasattr(act, 'target_list'):
                    pl.update(act.target_list)
                elif act.target:
                    pl.add(act.target)

                for p in pl:
                    self.player2portrait(p).update()

        effects.handle_event(self, evt_type, arg)
        inputs.handle_event(self, evt_type, arg)

    def more_on_message(self, _type, args):
        pass

    def on_text(self, text):
        # The easter egg
        ks = self.keystrokes
        ks = (ks + text)[:40]
        self.keystrokes = ks

        from thb.characters.baseclasses import Character

        for c in Character.character_classes.itervalues():
            try:
                alter = c.ui_meta.figure_image_alter
            except:
                continue

            for i in xrange(len(ks)):
                if L(alter).decrypt(ks[-i:]):
                    SoundManager.play('c-sound-input')

    def draw(self):
        self.draw_subcontrols()

    def ray(self, f, t):
        if f == t: return
        sp = self.player2portrait(f)
        dp = self.player2portrait(t)
        x0, y0 = sp.x + sp.width/2, sp.y + sp.height/2
        x1, y1 = dp.x + dp.width/2, dp.y + dp.height/2
        Ray(x0, y0, x1, y1, parent=self, zindex=10)

    def prompt(self, s):
        self.prompt_raw(u'|B|cff0000ff>> |r' + unicode(s) + u'\n')

    def prompt_raw(self, s):
        self.parent and self.parent.events_box.append(s)

    def begin_select_player(self, disables=[]):
        # if self.selecting_player: return
        self.selecting_player = True
        # self.selected_players = []
        for p in self.game.players:
            port = self.player2portrait(p)

            if p in disables:
                port.disabled = True
                port.selected = False
                try:
                    self.selected_players.remove(p)
                except ValueError:
                    pass
            else:
                port.disabled = False

    def get_selected_players(self):
        return self.selected_players

    def set_selected_players(self, players):
        for p in self.char_portraits:
            p.selected = False

        for p in players:
            self.player2portrait(p).selected = True

        self.selected_players = players[:]

    def end_select_player(self):
        # if not self.selecting_player: return
        self.selecting_player = False
        self.selected_players = []
        for p in self.char_portraits:
            p.selected = False
            p.disabled = False

    def get_selected_cards(self):
        return [
            cs.associated_card
            for cs in self.handcard_area.cards
            if cs.hca_selected
        ] + [
            cs.associated_card
            for cs in self.player2portrait(self.game.me).equipcard_area.cards
            if cs.selected
        ]

    def get_selected_skills(self):
        skills = self.game.me.skills
        return sorted([
            skills[i] for i in self.skill_box.get_selected_index()
        ], key=lambda s: s.sort_index)

    def reset_selected_skills(self):
        self.skill_box.reset()

    def get_action_params(self):
        return self.action_params

    def on_mouse_click(self, x, y, button, modifier):
        c = self.control_frompoint1_recursive(x, y)
        if isinstance(c, GameCharacterPortrait) and self.selecting_player and not c.disabled:
            char = c.character
            if not char: return True
            sel = c.selected
            psel = self.selected_players
            if sel:
                c.selected = False
                psel.remove(char)
            else:
                c.selected = True
                psel.append(char)

            self.selection_change.notify()
        return True

    def get_game_screen(self):
        assert self.parent
        return self.parent

    @staticmethod
    def show_result(g):
        ResultPanel(g, parent=Overlay.cur_overlay)