bastd.actor.popuptext

Defines Actor(s).

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Defines Actor(s)."""
  4
  5from __future__ import annotations
  6
  7import random
  8from typing import TYPE_CHECKING
  9
 10import ba
 11
 12if TYPE_CHECKING:
 13    from typing import Any, Sequence
 14
 15
 16class PopupText(ba.Actor):
 17    """Text that pops up above a position to denote something special.
 18
 19    category: Gameplay Classes
 20    """
 21
 22    def __init__(
 23        self,
 24        text: str | ba.Lstr,
 25        position: Sequence[float] = (0.0, 0.0, 0.0),
 26        color: Sequence[float] = (1.0, 1.0, 1.0, 1.0),
 27        random_offset: float = 0.5,
 28        offset: Sequence[float] = (0.0, 0.0, 0.0),
 29        scale: float = 1.0,
 30    ):
 31        """Instantiate with given values.
 32
 33        random_offset is the amount of random offset from the provided position
 34        that will be applied. This can help multiple achievements from
 35        overlapping too much.
 36        """
 37        super().__init__()
 38        if len(color) == 3:
 39            color = (color[0], color[1], color[2], 1.0)
 40        pos = (
 41            position[0] + offset[0] + random_offset * (0.5 - random.random()),
 42            position[1] + offset[1] + random_offset * (0.5 - random.random()),
 43            position[2] + offset[2] + random_offset * (0.5 - random.random()),
 44        )
 45
 46        self.node = ba.newnode(
 47            'text',
 48            attrs={
 49                'text': text,
 50                'in_world': True,
 51                'shadow': 1.0,
 52                'flatness': 1.0,
 53                'h_align': 'center',
 54            },
 55            delegate=self,
 56        )
 57
 58        lifespan = 1.5
 59
 60        # scale up
 61        ba.animate(
 62            self.node,
 63            'scale',
 64            {
 65                0: 0.0,
 66                lifespan * 0.11: 0.020 * 0.7 * scale,
 67                lifespan * 0.16: 0.013 * 0.7 * scale,
 68                lifespan * 0.25: 0.014 * 0.7 * scale,
 69            },
 70        )
 71
 72        # translate upward
 73        self._tcombine = ba.newnode(
 74            'combine',
 75            owner=self.node,
 76            attrs={'input0': pos[0], 'input2': pos[2], 'size': 3},
 77        )
 78        ba.animate(
 79            self._tcombine, 'input1', {0: pos[1] + 1.5, lifespan: pos[1] + 2.0}
 80        )
 81        self._tcombine.connectattr('output', self.node, 'position')
 82
 83        # fade our opacity in/out
 84        self._combine = ba.newnode(
 85            'combine',
 86            owner=self.node,
 87            attrs={
 88                'input0': color[0],
 89                'input1': color[1],
 90                'input2': color[2],
 91                'size': 4,
 92            },
 93        )
 94        for i in range(4):
 95            ba.animate(
 96                self._combine,
 97                'input' + str(i),
 98                {
 99                    0.13 * lifespan: color[i],
100                    0.18 * lifespan: 4.0 * color[i],
101                    0.22 * lifespan: color[i],
102                },
103            )
104        ba.animate(
105            self._combine,
106            'input3',
107            {
108                0: 0,
109                0.1 * lifespan: color[3],
110                0.7 * lifespan: color[3],
111                lifespan: 0,
112            },
113        )
114        self._combine.connectattr('output', self.node, 'color')
115
116        # kill ourself
117        self._die_timer = ba.Timer(
118            lifespan, ba.WeakCall(self.handlemessage, ba.DieMessage())
119        )
120
121    def handlemessage(self, msg: Any) -> Any:
122        assert not self.expired
123        if isinstance(msg, ba.DieMessage):
124            if self.node:
125                self.node.delete()
126        else:
127            super().handlemessage(msg)
class PopupText(ba._actor.Actor):
 17class PopupText(ba.Actor):
 18    """Text that pops up above a position to denote something special.
 19
 20    category: Gameplay Classes
 21    """
 22
 23    def __init__(
 24        self,
 25        text: str | ba.Lstr,
 26        position: Sequence[float] = (0.0, 0.0, 0.0),
 27        color: Sequence[float] = (1.0, 1.0, 1.0, 1.0),
 28        random_offset: float = 0.5,
 29        offset: Sequence[float] = (0.0, 0.0, 0.0),
 30        scale: float = 1.0,
 31    ):
 32        """Instantiate with given values.
 33
 34        random_offset is the amount of random offset from the provided position
 35        that will be applied. This can help multiple achievements from
 36        overlapping too much.
 37        """
 38        super().__init__()
 39        if len(color) == 3:
 40            color = (color[0], color[1], color[2], 1.0)
 41        pos = (
 42            position[0] + offset[0] + random_offset * (0.5 - random.random()),
 43            position[1] + offset[1] + random_offset * (0.5 - random.random()),
 44            position[2] + offset[2] + random_offset * (0.5 - random.random()),
 45        )
 46
 47        self.node = ba.newnode(
 48            'text',
 49            attrs={
 50                'text': text,
 51                'in_world': True,
 52                'shadow': 1.0,
 53                'flatness': 1.0,
 54                'h_align': 'center',
 55            },
 56            delegate=self,
 57        )
 58
 59        lifespan = 1.5
 60
 61        # scale up
 62        ba.animate(
 63            self.node,
 64            'scale',
 65            {
 66                0: 0.0,
 67                lifespan * 0.11: 0.020 * 0.7 * scale,
 68                lifespan * 0.16: 0.013 * 0.7 * scale,
 69                lifespan * 0.25: 0.014 * 0.7 * scale,
 70            },
 71        )
 72
 73        # translate upward
 74        self._tcombine = ba.newnode(
 75            'combine',
 76            owner=self.node,
 77            attrs={'input0': pos[0], 'input2': pos[2], 'size': 3},
 78        )
 79        ba.animate(
 80            self._tcombine, 'input1', {0: pos[1] + 1.5, lifespan: pos[1] + 2.0}
 81        )
 82        self._tcombine.connectattr('output', self.node, 'position')
 83
 84        # fade our opacity in/out
 85        self._combine = ba.newnode(
 86            'combine',
 87            owner=self.node,
 88            attrs={
 89                'input0': color[0],
 90                'input1': color[1],
 91                'input2': color[2],
 92                'size': 4,
 93            },
 94        )
 95        for i in range(4):
 96            ba.animate(
 97                self._combine,
 98                'input' + str(i),
 99                {
100                    0.13 * lifespan: color[i],
101                    0.18 * lifespan: 4.0 * color[i],
102                    0.22 * lifespan: color[i],
103                },
104            )
105        ba.animate(
106            self._combine,
107            'input3',
108            {
109                0: 0,
110                0.1 * lifespan: color[3],
111                0.7 * lifespan: color[3],
112                lifespan: 0,
113            },
114        )
115        self._combine.connectattr('output', self.node, 'color')
116
117        # kill ourself
118        self._die_timer = ba.Timer(
119            lifespan, ba.WeakCall(self.handlemessage, ba.DieMessage())
120        )
121
122    def handlemessage(self, msg: Any) -> Any:
123        assert not self.expired
124        if isinstance(msg, ba.DieMessage):
125            if self.node:
126                self.node.delete()
127        else:
128            super().handlemessage(msg)

Text that pops up above a position to denote something special.

category: Gameplay Classes

PopupText( text: str | ba._language.Lstr, position: Sequence[float] = (0.0, 0.0, 0.0), color: Sequence[float] = (1.0, 1.0, 1.0, 1.0), random_offset: float = 0.5, offset: Sequence[float] = (0.0, 0.0, 0.0), scale: float = 1.0)
 23    def __init__(
 24        self,
 25        text: str | ba.Lstr,
 26        position: Sequence[float] = (0.0, 0.0, 0.0),
 27        color: Sequence[float] = (1.0, 1.0, 1.0, 1.0),
 28        random_offset: float = 0.5,
 29        offset: Sequence[float] = (0.0, 0.0, 0.0),
 30        scale: float = 1.0,
 31    ):
 32        """Instantiate with given values.
 33
 34        random_offset is the amount of random offset from the provided position
 35        that will be applied. This can help multiple achievements from
 36        overlapping too much.
 37        """
 38        super().__init__()
 39        if len(color) == 3:
 40            color = (color[0], color[1], color[2], 1.0)
 41        pos = (
 42            position[0] + offset[0] + random_offset * (0.5 - random.random()),
 43            position[1] + offset[1] + random_offset * (0.5 - random.random()),
 44            position[2] + offset[2] + random_offset * (0.5 - random.random()),
 45        )
 46
 47        self.node = ba.newnode(
 48            'text',
 49            attrs={
 50                'text': text,
 51                'in_world': True,
 52                'shadow': 1.0,
 53                'flatness': 1.0,
 54                'h_align': 'center',
 55            },
 56            delegate=self,
 57        )
 58
 59        lifespan = 1.5
 60
 61        # scale up
 62        ba.animate(
 63            self.node,
 64            'scale',
 65            {
 66                0: 0.0,
 67                lifespan * 0.11: 0.020 * 0.7 * scale,
 68                lifespan * 0.16: 0.013 * 0.7 * scale,
 69                lifespan * 0.25: 0.014 * 0.7 * scale,
 70            },
 71        )
 72
 73        # translate upward
 74        self._tcombine = ba.newnode(
 75            'combine',
 76            owner=self.node,
 77            attrs={'input0': pos[0], 'input2': pos[2], 'size': 3},
 78        )
 79        ba.animate(
 80            self._tcombine, 'input1', {0: pos[1] + 1.5, lifespan: pos[1] + 2.0}
 81        )
 82        self._tcombine.connectattr('output', self.node, 'position')
 83
 84        # fade our opacity in/out
 85        self._combine = ba.newnode(
 86            'combine',
 87            owner=self.node,
 88            attrs={
 89                'input0': color[0],
 90                'input1': color[1],
 91                'input2': color[2],
 92                'size': 4,
 93            },
 94        )
 95        for i in range(4):
 96            ba.animate(
 97                self._combine,
 98                'input' + str(i),
 99                {
100                    0.13 * lifespan: color[i],
101                    0.18 * lifespan: 4.0 * color[i],
102                    0.22 * lifespan: color[i],
103                },
104            )
105        ba.animate(
106            self._combine,
107            'input3',
108            {
109                0: 0,
110                0.1 * lifespan: color[3],
111                0.7 * lifespan: color[3],
112                lifespan: 0,
113            },
114        )
115        self._combine.connectattr('output', self.node, 'color')
116
117        # kill ourself
118        self._die_timer = ba.Timer(
119            lifespan, ba.WeakCall(self.handlemessage, ba.DieMessage())
120        )

Instantiate with given values.

random_offset is the amount of random offset from the provided position that will be applied. This can help multiple achievements from overlapping too much.

def handlemessage(self, msg: Any) -> Any:
122    def handlemessage(self, msg: Any) -> Any:
123        assert not self.expired
124        if isinstance(msg, ba.DieMessage):
125            if self.node:
126                self.node.delete()
127        else:
128            super().handlemessage(msg)

General message handling; can be passed any message object.

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