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

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:
107    def handlemessage(self, msg: Any) -> Any:
108        assert not self.expired
109        if isinstance(msg, ba.DieMessage):
110            if self.node:
111                self.node.delete()
112        else:
113            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