Example #1
0
class ToonFPSGui:

    def __init__(self, base):
        self.base = base
        self.noAmmoLabel = None
        self.ammo_gui = None
        self.hp_meter = None
        self.crosshair = None
        self.stats_container = None
        self.stats_bg = None
        self.stats_lbl = None
        self.kills_lbl = None
        self.deaths_lbl = None
        self.points_lbl = None
        return

    def load(self):
        self.ammo_gui = loader.loadModel('phase_4/models/minigames/gun_ammo_gui.egg')
        self.ammo_gui.setScale(0.15)
        self.ammo_gui.setPos(0.38, 0, 0.1)
        self.hp_meter = DirectWaitBar(text=str(self.base.hp), text_roll=-90, text_scale=0.2, text_pos=(-0.025,
                                                                                                       0), relief=DGG.RAISED, barColor=(1,
                                                                                                                                        0,
                                                                                                                                        0,
                                                                                                                                        1), range=self.base.max_hp, value=self.base.hp, parent=base.a2dBottomRight, scale=0.4, pos=(-0.12,
                                                                                                                                                                                                                                    0,
                                                                                                                                                                                                                                    0.2), frameSize=(-0.4,
                                                                                                                                                                                                                                                     0.4,
                                                                                                                                                                                                                                                     -0.2,
                                                                                                                                                                                                                                                     0.2))
        self.hp_meter.setR(-90)
        self.hp_meter.hide()
        self.crosshair = getCrosshair()
        font = CIGlobals.getToonFont()
        box = DGG.getDefaultDialogGeom()
        if self.base.__class__.__name__ == 'GunGameToonFPS':
            self.stats_container = DirectFrame(parent=base.a2dTopLeft, pos=(0.3, 0.2,
                                                                            -0.185))
            self.stats_bg = OnscreenImage(image=box, color=(1, 1, 0.75, 1), scale=(0.5,
                                                                                   0.3,
                                                                                   0.3), parent=self.stats_container)
            self.stats_lbl = OnscreenText(font=font, text='Stats', pos=(-0.01, 0.08,
                                                                        0), parent=self.stats_container)
            self.kills_lbl = OnscreenText(font=font, text='Kills: ' + str(self.base.kills), pos=(-0.235,
                                                                                                 0.025,
                                                                                                 0), scale=0.055, parent=self.stats_container, align=TextNode.ALeft)
            self.deaths_lbl = OnscreenText(font=font, text='Deaths: ' + str(self.base.deaths), pos=(-0.235,
                                                                                                    -0.04,
                                                                                                    0), scale=0.055, parent=self.stats_container, align=TextNode.ALeft)
            self.points_lbl = OnscreenText(font=font, text='Points: ' + str(self.base.points), pos=(-0.235,
                                                                                                    -0.105,
                                                                                                    0), scale=0.055, parent=self.stats_container, align=TextNode.ALeft)
            self.stats_container.hide()
            del font
            del box

    def start(self):
        self.ammo_gui.reparentTo(base.a2dBottomLeft)
        self.crosshair.show()
        self.hp_meter.show()
        if self.base.__class__.__name__ == 'GunGameToonFPS':
            self.stats_container.show()

    def end(self):
        self.ammo_gui.reparentTo(hidden)
        if self.base.__class__.__name__ == 'GunGameToonFPS':
            self.stats_container.hide()
        self.crosshair.hide()
        self.hp_meter.hide()

    def cleanup(self):
        self.ammo_gui.removeNode()
        self.ammo_gui = None
        self.hp_meter.destroy()
        self.hp_meter = None
        self.crosshair.destroy()
        self.crosshair = None
        self.deleteNoAmmoLabel()
        self.deleteStatsGui()
        return

    def deleteStatsGui(self):
        if self.stats_container:
            self.stats_container.destroy()
            self.stats_container = None
        if self.stats_bg:
            self.stats_bg.destroy()
            self.stats_bg = None
        if self.stats_lbl:
            self.stats_lbl.destroy()
            self.stats_lbl = None
        if self.kills_lbl:
            self.kills_lbl.destroy()
            self.kills_lbl = None
        if self.deaths_lbl:
            self.deaths_lbl.destroy()
            self.deaths_lbl = None
        if self.points_lbl:
            self.points_lbl.destroy()
            self.points_lbl = None
        return

    def updateStats(self):
        self.kills_lbl['text'] = 'Kills: ' + str(self.base.kills)
        self.deaths_lbl['text'] = 'Deaths: ' + str(self.base.deaths)
        self.points_lbl['text'] = 'Points: ' + str(self.base.points)

    def deleteNoAmmoLabel(self):
        if self.noAmmoLabel:
            self.noAmmoLabel.destroy()
            self.noAmmoLabel = None
        return

    def adjustAmmoGui(self):
        self.ammo_gui.find('**/bar_' + str(self.base.ammo + 1)).hide()

    def adjustHpMeter(self):
        self.hp_meter['text'] = str(self.base.hp)
        self.hp_meter['value'] = self.base.hp
        if self.base.hp <= 40:
            self.hp_meter['barColor'] = (1, 0, 0, 1)
        else:
            self.hp_meter['barColor'] = (1, 1, 1, 1)

    def resetAmmo(self):
        for bar in self.ammo_gui.findAllMatches('**/bar_*'):
            bar.show()

    def notifyNoAmmo(self):
        self.deleteNoAmmoLabel()
        self.noAmmoLabel = DirectLabel(text='Press R to reload!', relief=None, text_scale=0.1, text_pos=(0,
                                                                                                         0.5,
                                                                                                         0), text_fg=(1,
                                                                                                                      1,
                                                                                                                      1,
                                                                                                                      1), text_shadow=(0,
                                                                                                                                       0,
                                                                                                                                       0,
                                                                                                                                       1))
        return
Example #2
0
class TrainGUI:
    """The locomotive state GUI."""

    def __init__(self):
        self._weapon_buts = {}
        self._turn_snd = loader.loadSfx(  # noqa: F821
            "sounds/train/railroad_switch.ogg"
        )

        frame = DirectFrame(
            parent=base.a2dBottomRight,  # noqa: F821
            frameSize=(-0.09, 0.09, -0.28, 0.28),
            pos=(-0.09, 0, 0.28),
            frameTexture=GUI_PIC + "metal1.png",
        )
        frame.setTransparency(TransparencyAttrib.MAlpha)
        DirectFrame(  # an icon for the locomotive durability
            parent=frame,
            frameSize=(-0.023, 0.023, -0.023, 0.023),
            pos=(0.05, 0, 0.24),
            frameTexture=GUI_PIC + "train.png",
        ).setTransparency(TransparencyAttrib.MAlpha)

        DirectFrame(  # an icon for the locomotive speed
            parent=frame,
            frameSize=(-0.028, 0.028, -0.023, 0.023),
            pos=(-0.012, 0, 0.24),
            frameTexture=GUI_PIC + "speed.png",
        ).setTransparency(TransparencyAttrib.MAlpha)

        self._durability = DirectWaitBar(
            parent=frame,
            frameSize=(-0.225, 0.225, -0.002, 0.002),
            frameColor=(0.35, 0.35, 0.35, 1),
            range=1000,
            value=1000,
            barColor=(0.42, 0.42, 0.8, 1),
            pos=(0.05, 0, -0.025),
        )
        self._durability.setR(-90)
        self._speed = DirectWaitBar(
            parent=frame,
            frameSize=(-0.225, 0.225, -0.002, 0.002),
            frameColor=(0.35, 0.35, 0.35, 1),
            range=1,
            value=1,
            barColor=(1, 0.63, 0, 0.6),
            pos=(-0.012, 0, -0.025),
        )
        self._speed.setR(-90)

        DirectLabel(  # speed gauge scale
            parent=frame,
            pos=(-0.05, 0, 0.19),
            frameSize=(-0.25, 0.25, -0.01, 0.01),
            frameColor=(0, 0, 0, 0),
            text="40-\n\n-\n\n-\n\n-\n\n20-\n\n-\n\n-\n\n-\n\n0-",
            text_scale=0.028,
            text_fg=SILVER_COL,
        )
        frame_miles = DirectFrame(
            frameSize=(-0.115, 0.115, -0.06, 0.05),
            pos=(0, 0, -0.95),
            frameTexture=GUI_PIC + "metal1.png",
            sortOrder=-1,
        )
        frame_miles.set_transparency(TransparencyAttrib.MAlpha)

        but = DirectButton(
            parent=frame_miles,
            frameSize=(-0.015, 0.015, -0.025, 0.025),
            frameTexture=GUI_PIC + "grenade.png",
            pos=(-0.075, 0, 0.015),
            relief="flat",
        )
        but.setTransparency(TransparencyAttrib.MAlpha)
        but.bind(DGG.ENTER, self._highlight_weapon_but, extraArgs=[but])
        but.bind(DGG.EXIT, self._dehighlight_weapon_but, extraArgs=[but])

        self._weapon_buts["Grenade Launcher"] = {
            "but": but,
            "reload_step": 0,
            "dis_command": None,
            "reloading_len": 13,
            "frame": DirectFrame(
                parent=frame_miles,
                frameColor=(0, 0, 0, 0.25),
                pos=(-0.075, 0, -0.01),
                frameSize=(-0.013, 0.013, 0, 0.05),
            ),
        }

        but = DirectButton(
            parent=frame_miles,
            frameSize=(-0.015, 0.015, -0.025, 0.025),
            frameTexture=GUI_PIC + "machine_gun.png",
            pos=(0, 0, 0.015),
            relief="flat",
        )
        but.setTransparency(TransparencyAttrib.MAlpha)
        but.bind(DGG.ENTER, self._highlight_weapon_but, extraArgs=[but])
        but.bind(DGG.EXIT, self._dehighlight_weapon_but, extraArgs=[but])

        self._weapon_buts["Machine Gun"] = {
            "but": but,
            "reload_step": 0,
            "dis_command": None,
            "reloading_len": 22,
            "frame": DirectFrame(
                parent=frame_miles,
                frameColor=(0, 0, 0, 0.25),
                pos=(0, 0, -0.01),
                frameSize=(-0.013, 0.013, 0, 0.05),
            ),
        }

        but = DirectButton(
            parent=frame_miles,
            frameSize=(-0.015, 0.015, -0.025, 0.025),
            frameTexture=GUI_PIC + "cluster_rocket.png",
            pos=(0.075, 0, 0.015),
            relief="flat",
        )
        but.setTransparency(TransparencyAttrib.MAlpha)
        but.bind(DGG.ENTER, self._highlight_weapon_but, extraArgs=[but])
        but.bind(DGG.EXIT, self._dehighlight_weapon_but, extraArgs=[but])

        self._weapon_buts["Cluster Howitzer"] = {
            "but": but,
            "reload_step": 0,
            "dis_command": None,
            "reloading_len": 45,
            "frame": DirectFrame(
                parent=frame_miles,
                frameColor=(0, 0, 0, 0.25),
                pos=(0.075, 0, -0.01),
                frameSize=(-0.013, 0.013, 0, 0.05),
            ),
        }

        self._miles_meter = DirectLabel(
            parent=frame_miles,
            text="0000000",
            text_font=base.main_font,  # noqa: F821
            frameSize=(0.1, 0.1, 0.15, 0.15),
            text_scale=(0.033, 0.031),
            text_fg=RUST_COL,
            pos=(0, 0, -0.04),
        )
        taskMgr.doMethodLater(  # noqa: F821
            0.25, self._update_speed, "update_speed_indicator"
        )
        self._fork_lab = None

    def _dehighlight_weapon_but(self, button, _):
        """Dehighlight weapon button.

        Args:
            button (panda3d.gui.DirectGui.DirectButton):
                Button to dehighlight.
        """
        if "hover_" in button["frameTexture"]:
            button["frameTexture"] = button["frameTexture"].replace("hover_", "")

    def _highlight_weapon_but(self, button, _):
        """Hightlight weapon button.

        Args:
            button (panda3d.gui.DirectGui.DirectButton):
                Button to highlight.
        """
        if button["command"] is not None:
            button["frameTexture"] = (
                GUI_PIC + "hover_" + button["frameTexture"].split("/")[-1]
            )

    def _show_reloading(self, weapon, length, task):
        """Show reloading shadow on the weapon button.

        Args:
            weapon (str): The reloading weapon name.
            length (int): Reloading length in seconds.
        """
        self._weapon_buts[weapon]["frame"]["frameSize"] = (
            -0.013,
            0.013,
            0,
            0.05
            - self._weapon_buts[weapon]["reload_step"]
            / (self._weapon_buts[weapon]["reloading_len"] * 20),
        )
        self._weapon_buts[weapon]["reload_step"] += 1

        if self._weapon_buts[weapon]["reload_step"] == length:
            self._weapon_buts[weapon]["reload_step"] = 0
            self._weapon_buts[weapon]["frame"]["frameSize"] = (0, 0, 0, 0)

            self._weapon_buts[weapon]["but"]["command"] = self._weapon_buts[weapon][
                "dis_command"
            ]
            self._weapon_buts[weapon]["dis_command"] = None
            return task.done

        return task.again

    def _turn_on_fork(self, fork):
        """Turn the locomotive on the next fork.

        Args:
            fork (world.block.Block): Fork block to turn on.
        """
        self._turn_snd.play()
        base.train.do_turn = 1  # noqa: F821
        base.ignore("t")  # noqa: F821
        fork.load_additional_surface()
        self.hide_turning_ability()

    def _update_speed(self, task):
        """Update the Train speed GUI indicator."""
        self._speed["value"] = base.train.ctrl.current_speed  # noqa: F821
        return task.again

    def activate_weapon(self, weapon, command):
        """
        Erase shadow on the reloaded weapon button
        and register shooting method.

        Args:
            weapon (str): The weapon name.
            command (method): The method, originally bind to the button.
        """
        self._weapon_buts[weapon]["frame"]["frameSize"] = (0, 0, 0, 0)
        self._weapon_buts[weapon]["but"]["command"] = command

    def hide_turning_ability(self):
        """Hide turning GUI."""
        if self._fork_lab is None:
            return

        self._fork_lab.destroy()
        self._fork_lab = None

    def increase_max_duration(self):
        """Increase maximum of the locomotive Durability indicator."""
        self._durability["range"] = 1500

    def make_shot(self, weapon):
        """Disable the weapon button until reloading complete.

        Args:
            weapon (str): The reloaded weapon name.
        """
        self._weapon_buts[weapon]["dis_command"] = self._weapon_buts[weapon]["but"][
            "command"
        ]
        self._weapon_buts[weapon]["but"]["command"] = None

        taskMgr.doMethodLater(  # noqa: F821
            1,
            self._show_reloading,
            "show_reloading_" + weapon,
            extraArgs=[weapon, self._weapon_buts[weapon]["reloading_len"]],
            appendTask=True,
        )

    def show_turning_ability(self, fork, branch, invert):
        """Show a notification about turning ability.

        If there is a fork in the next 2 miles, the player can
        use an ability to turn, or ignore the fork and move in
        the default direction.

        Args:
            fork (world.block.Block):
                Fork block to which the player is approaching.
            branch (str): Branch side indicator: "l" or "r".
            invert (bool):
                True, if the player is approaching the fork
                from opposite direction of the world.
        """
        base.train.do_turn = 0  # noqa: F821
        text = ""

        if branch is None:
            text = base.labels.FORKS[2]  # noqa: F821
        else:
            if fork.name == "l_fork":
                if invert or branch == "l":
                    text = base.labels.FORKS[0]  # noqa: F821
                else:
                    text = base.labels.FORKS[1]  # noqa: F821
            elif fork.name == "r_fork":
                if invert or branch == "r":
                    text = base.labels.FORKS[1]  # noqa: F821
                else:
                    text = base.labels.FORKS[0]  # noqa: F821

        base.accept("t", self._turn_on_fork, [fork])  # noqa: F821

        self._fork_lab = DirectLabel(
            pos=(0, 0, -0.75),
            text_scale=0.04,
            text_fg=SILVER_COL,
            text_font=base.main_font,  # noqa: F821
            frameColor=(0, 0, 0, 0.4),
            text=text,
        )

    def update_indicators(self, **params):
        """Update the Train GUI with the given parameters.

        Args:
            params (dict): New parameters values.
        """
        if "durability" in params.keys():
            self._durability["value"] = params["durability"]

    def update_miles(self, new_miles):
        """Update the miles meter widget.

        Args:
            new_miles (int): New milesmeter value.
        """
        self._miles_meter["text"] = (
            # mi
            str(new_miles).rjust(7, "0")
            + base.labels.RESOURCES[7]  # noqa: F821
        )