bascenev1lib.actor.onscreentimer

Defines Actor(s).

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Defines Actor(s)."""
  4from __future__ import annotations
  5
  6from typing import TYPE_CHECKING
  7import logging
  8
  9from typing_extensions import override
 10import bascenev1 as bs
 11
 12if TYPE_CHECKING:
 13    from typing import Any
 14
 15
 16class OnScreenTimer(bs.Actor):
 17    """A handy on-screen timer.
 18
 19    category: Gameplay Classes
 20
 21    Useful for time-based games where time increases.
 22    """
 23
 24    def __init__(self) -> None:
 25        super().__init__()
 26        self._starttime_ms: int | None = None
 27        self.node = bs.newnode(
 28            'text',
 29            attrs={
 30                'v_attach': 'top',
 31                'h_attach': 'center',
 32                'h_align': 'center',
 33                'color': (1, 1, 0.5, 1),
 34                'flatness': 0.5,
 35                'shadow': 0.5,
 36                'position': (0, -70),
 37                'scale': 1.4,
 38                'text': '',
 39            },
 40        )
 41        self.inputnode = bs.newnode(
 42            'timedisplay', attrs={'timemin': 0, 'showsubseconds': True}
 43        )
 44        self.inputnode.connectattr('output', self.node, 'text')
 45
 46    def start(self) -> None:
 47        """Start the timer."""
 48        tval = int(bs.time() * 1000.0)
 49        assert isinstance(tval, int)
 50        self._starttime_ms = tval
 51        self.inputnode.time1 = self._starttime_ms
 52        bs.getactivity().globalsnode.connectattr(
 53            'time', self.inputnode, 'time2'
 54        )
 55
 56    def has_started(self) -> bool:
 57        """Return whether this timer has started yet."""
 58        return self._starttime_ms is not None
 59
 60    def stop(self, endtime: int | float | None = None) -> None:
 61        """End the timer.
 62
 63        If 'endtime' is not None, it is used when calculating
 64        the final display time; otherwise the current time is used.
 65        """
 66        if endtime is None:
 67            endtime = bs.time()
 68
 69        if self._starttime_ms is None:
 70            logging.warning(
 71                'OnScreenTimer.stop() called without first calling start()'
 72            )
 73        else:
 74            endtime_ms = int(endtime * 1000)
 75            self.inputnode.timemax = endtime_ms - self._starttime_ms
 76
 77    def getstarttime(self) -> float:
 78        """Return the scene-time when start() was called.
 79
 80        Time will be returned in seconds if timeformat is SECONDS or
 81        milliseconds if it is MILLISECONDS.
 82        """
 83        val_ms: Any
 84        if self._starttime_ms is None:
 85            print('WARNING: getstarttime() called on un-started timer')
 86            val_ms = int(bs.time() * 1000.0)
 87        else:
 88            val_ms = self._starttime_ms
 89        assert isinstance(val_ms, int)
 90        return 0.001 * val_ms
 91
 92    @property
 93    def starttime(self) -> float:
 94        """Shortcut for start time in seconds."""
 95        return self.getstarttime()
 96
 97    @override
 98    def handlemessage(self, msg: Any) -> Any:
 99        # if we're asked to die, just kill our node/timer
100        if isinstance(msg, bs.DieMessage):
101            if self.node:
102                self.node.delete()
class OnScreenTimer(bascenev1._actor.Actor):
 17class OnScreenTimer(bs.Actor):
 18    """A handy on-screen timer.
 19
 20    category: Gameplay Classes
 21
 22    Useful for time-based games where time increases.
 23    """
 24
 25    def __init__(self) -> None:
 26        super().__init__()
 27        self._starttime_ms: int | None = None
 28        self.node = bs.newnode(
 29            'text',
 30            attrs={
 31                'v_attach': 'top',
 32                'h_attach': 'center',
 33                'h_align': 'center',
 34                'color': (1, 1, 0.5, 1),
 35                'flatness': 0.5,
 36                'shadow': 0.5,
 37                'position': (0, -70),
 38                'scale': 1.4,
 39                'text': '',
 40            },
 41        )
 42        self.inputnode = bs.newnode(
 43            'timedisplay', attrs={'timemin': 0, 'showsubseconds': True}
 44        )
 45        self.inputnode.connectattr('output', self.node, 'text')
 46
 47    def start(self) -> None:
 48        """Start the timer."""
 49        tval = int(bs.time() * 1000.0)
 50        assert isinstance(tval, int)
 51        self._starttime_ms = tval
 52        self.inputnode.time1 = self._starttime_ms
 53        bs.getactivity().globalsnode.connectattr(
 54            'time', self.inputnode, 'time2'
 55        )
 56
 57    def has_started(self) -> bool:
 58        """Return whether this timer has started yet."""
 59        return self._starttime_ms is not None
 60
 61    def stop(self, endtime: int | float | None = None) -> None:
 62        """End the timer.
 63
 64        If 'endtime' is not None, it is used when calculating
 65        the final display time; otherwise the current time is used.
 66        """
 67        if endtime is None:
 68            endtime = bs.time()
 69
 70        if self._starttime_ms is None:
 71            logging.warning(
 72                'OnScreenTimer.stop() called without first calling start()'
 73            )
 74        else:
 75            endtime_ms = int(endtime * 1000)
 76            self.inputnode.timemax = endtime_ms - self._starttime_ms
 77
 78    def getstarttime(self) -> float:
 79        """Return the scene-time when start() was called.
 80
 81        Time will be returned in seconds if timeformat is SECONDS or
 82        milliseconds if it is MILLISECONDS.
 83        """
 84        val_ms: Any
 85        if self._starttime_ms is None:
 86            print('WARNING: getstarttime() called on un-started timer')
 87            val_ms = int(bs.time() * 1000.0)
 88        else:
 89            val_ms = self._starttime_ms
 90        assert isinstance(val_ms, int)
 91        return 0.001 * val_ms
 92
 93    @property
 94    def starttime(self) -> float:
 95        """Shortcut for start time in seconds."""
 96        return self.getstarttime()
 97
 98    @override
 99    def handlemessage(self, msg: Any) -> Any:
100        # if we're asked to die, just kill our node/timer
101        if isinstance(msg, bs.DieMessage):
102            if self.node:
103                self.node.delete()

A handy on-screen timer.

category: Gameplay Classes

Useful for time-based games where time increases.

OnScreenTimer()
25    def __init__(self) -> None:
26        super().__init__()
27        self._starttime_ms: int | None = None
28        self.node = bs.newnode(
29            'text',
30            attrs={
31                'v_attach': 'top',
32                'h_attach': 'center',
33                'h_align': 'center',
34                'color': (1, 1, 0.5, 1),
35                'flatness': 0.5,
36                'shadow': 0.5,
37                'position': (0, -70),
38                'scale': 1.4,
39                'text': '',
40            },
41        )
42        self.inputnode = bs.newnode(
43            'timedisplay', attrs={'timemin': 0, 'showsubseconds': True}
44        )
45        self.inputnode.connectattr('output', self.node, 'text')

Instantiates an Actor in the current bascenev1.Activity.

node
inputnode
def start(self) -> None:
47    def start(self) -> None:
48        """Start the timer."""
49        tval = int(bs.time() * 1000.0)
50        assert isinstance(tval, int)
51        self._starttime_ms = tval
52        self.inputnode.time1 = self._starttime_ms
53        bs.getactivity().globalsnode.connectattr(
54            'time', self.inputnode, 'time2'
55        )

Start the timer.

def has_started(self) -> bool:
57    def has_started(self) -> bool:
58        """Return whether this timer has started yet."""
59        return self._starttime_ms is not None

Return whether this timer has started yet.

def stop(self, endtime: int | float | None = None) -> None:
61    def stop(self, endtime: int | float | None = None) -> None:
62        """End the timer.
63
64        If 'endtime' is not None, it is used when calculating
65        the final display time; otherwise the current time is used.
66        """
67        if endtime is None:
68            endtime = bs.time()
69
70        if self._starttime_ms is None:
71            logging.warning(
72                'OnScreenTimer.stop() called without first calling start()'
73            )
74        else:
75            endtime_ms = int(endtime * 1000)
76            self.inputnode.timemax = endtime_ms - self._starttime_ms

End the timer.

If 'endtime' is not None, it is used when calculating the final display time; otherwise the current time is used.

def getstarttime(self) -> float:
78    def getstarttime(self) -> float:
79        """Return the scene-time when start() was called.
80
81        Time will be returned in seconds if timeformat is SECONDS or
82        milliseconds if it is MILLISECONDS.
83        """
84        val_ms: Any
85        if self._starttime_ms is None:
86            print('WARNING: getstarttime() called on un-started timer')
87            val_ms = int(bs.time() * 1000.0)
88        else:
89            val_ms = self._starttime_ms
90        assert isinstance(val_ms, int)
91        return 0.001 * val_ms

Return the scene-time when start() was called.

Time will be returned in seconds if timeformat is SECONDS or milliseconds if it is MILLISECONDS.

starttime: float
93    @property
94    def starttime(self) -> float:
95        """Shortcut for start time in seconds."""
96        return self.getstarttime()

Shortcut for start time in seconds.

@override
def handlemessage(self, msg: Any) -> Any:
 98    @override
 99    def handlemessage(self, msg: Any) -> Any:
100        # if we're asked to die, just kill our node/timer
101        if isinstance(msg, bs.DieMessage):
102            if self.node:
103                self.node.delete()

General message handling; can be passed any message object.

Inherited Members
bascenev1._actor.Actor
autoretain
on_expire
expired
exists
is_alive
activity
getactivity