bastd.actor.spawner

Defines some lovely Actor(s).

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Defines some lovely Actor(s)."""
  4
  5from __future__ import annotations
  6
  7from typing import TYPE_CHECKING
  8
  9import ba
 10
 11if TYPE_CHECKING:
 12    from typing import Any, Sequence, Callable
 13
 14
 15# FIXME: Should make this an Actor.
 16class Spawner:
 17    """Utility for delayed spawning of objects.
 18
 19    Category: **Gameplay Classes**
 20
 21    Creates a light flash and sends a Spawner.SpawnMessage
 22    to the current activity after a delay.
 23    """
 24
 25    class SpawnMessage:
 26        """Spawn message sent by a Spawner after its delay has passed.
 27
 28        Category: **Message Classes**
 29        """
 30
 31        spawner: Spawner
 32        """The ba.Spawner we came from."""
 33
 34        data: Any
 35        """The data object passed by the user."""
 36
 37        pt: Sequence[float]
 38        """The spawn position."""
 39
 40        def __init__(
 41                self,
 42                spawner: Spawner,
 43                data: Any,
 44                pt: Sequence[float],  # pylint: disable=invalid-name
 45        ):
 46            """Instantiate with the given values."""
 47            self.spawner = spawner
 48            self.data = data
 49            self.pt = pt  # pylint: disable=invalid-name
 50
 51    def __init__(
 52            self,
 53            data: Any = None,
 54            pt: Sequence[float] = (0, 0, 0),  # pylint: disable=invalid-name
 55            spawn_time: float = 1.0,
 56            send_spawn_message: bool = True,
 57            spawn_callback: Callable[[], Any] | None = None):
 58        """Instantiate a Spawner.
 59
 60        Requires some custom data, a position,
 61        and a spawn-time in seconds.
 62        """
 63        self._spawn_callback = spawn_callback
 64        self._send_spawn_message = send_spawn_message
 65        self._spawner_sound = ba.getsound('swip2')
 66        self._data = data
 67        self._pt = pt
 68        # create a light where the spawn will happen
 69        self._light = ba.newnode('light',
 70                                 attrs={
 71                                     'position': tuple(pt),
 72                                     'radius': 0.1,
 73                                     'color': (1.0, 0.1, 0.1),
 74                                     'lights_volumes': False
 75                                 })
 76        scl = float(spawn_time) / 3.75
 77        min_val = 0.4
 78        max_val = 0.7
 79        ba.playsound(self._spawner_sound, position=self._light.position)
 80        ba.animate(
 81            self._light, 'intensity', {
 82                0.0: 0.0,
 83                0.25 * scl: max_val,
 84                0.500 * scl: min_val,
 85                0.750 * scl: max_val,
 86                1.000 * scl: min_val,
 87                1.250 * scl: 1.1 * max_val,
 88                1.500 * scl: min_val,
 89                1.750 * scl: 1.2 * max_val,
 90                2.000 * scl: min_val,
 91                2.250 * scl: 1.3 * max_val,
 92                2.500 * scl: min_val,
 93                2.750 * scl: 1.4 * max_val,
 94                3.000 * scl: min_val,
 95                3.250 * scl: 1.5 * max_val,
 96                3.500 * scl: min_val,
 97                3.750 * scl: 2.0,
 98                4.000 * scl: 0.0
 99            })
100        ba.timer(spawn_time, self._spawn)
101
102    def _spawn(self) -> None:
103        ba.timer(1.0, self._light.delete)
104        if self._spawn_callback is not None:
105            self._spawn_callback()
106        if self._send_spawn_message:
107            # only run if our activity still exists
108            activity = ba.getactivity()
109            if activity is not None:
110                activity.handlemessage(
111                    self.SpawnMessage(self, self._data, self._pt))
class Spawner:
 17class Spawner:
 18    """Utility for delayed spawning of objects.
 19
 20    Category: **Gameplay Classes**
 21
 22    Creates a light flash and sends a Spawner.SpawnMessage
 23    to the current activity after a delay.
 24    """
 25
 26    class SpawnMessage:
 27        """Spawn message sent by a Spawner after its delay has passed.
 28
 29        Category: **Message Classes**
 30        """
 31
 32        spawner: Spawner
 33        """The ba.Spawner we came from."""
 34
 35        data: Any
 36        """The data object passed by the user."""
 37
 38        pt: Sequence[float]
 39        """The spawn position."""
 40
 41        def __init__(
 42                self,
 43                spawner: Spawner,
 44                data: Any,
 45                pt: Sequence[float],  # pylint: disable=invalid-name
 46        ):
 47            """Instantiate with the given values."""
 48            self.spawner = spawner
 49            self.data = data
 50            self.pt = pt  # pylint: disable=invalid-name
 51
 52    def __init__(
 53            self,
 54            data: Any = None,
 55            pt: Sequence[float] = (0, 0, 0),  # pylint: disable=invalid-name
 56            spawn_time: float = 1.0,
 57            send_spawn_message: bool = True,
 58            spawn_callback: Callable[[], Any] | None = None):
 59        """Instantiate a Spawner.
 60
 61        Requires some custom data, a position,
 62        and a spawn-time in seconds.
 63        """
 64        self._spawn_callback = spawn_callback
 65        self._send_spawn_message = send_spawn_message
 66        self._spawner_sound = ba.getsound('swip2')
 67        self._data = data
 68        self._pt = pt
 69        # create a light where the spawn will happen
 70        self._light = ba.newnode('light',
 71                                 attrs={
 72                                     'position': tuple(pt),
 73                                     'radius': 0.1,
 74                                     'color': (1.0, 0.1, 0.1),
 75                                     'lights_volumes': False
 76                                 })
 77        scl = float(spawn_time) / 3.75
 78        min_val = 0.4
 79        max_val = 0.7
 80        ba.playsound(self._spawner_sound, position=self._light.position)
 81        ba.animate(
 82            self._light, 'intensity', {
 83                0.0: 0.0,
 84                0.25 * scl: max_val,
 85                0.500 * scl: min_val,
 86                0.750 * scl: max_val,
 87                1.000 * scl: min_val,
 88                1.250 * scl: 1.1 * max_val,
 89                1.500 * scl: min_val,
 90                1.750 * scl: 1.2 * max_val,
 91                2.000 * scl: min_val,
 92                2.250 * scl: 1.3 * max_val,
 93                2.500 * scl: min_val,
 94                2.750 * scl: 1.4 * max_val,
 95                3.000 * scl: min_val,
 96                3.250 * scl: 1.5 * max_val,
 97                3.500 * scl: min_val,
 98                3.750 * scl: 2.0,
 99                4.000 * scl: 0.0
100            })
101        ba.timer(spawn_time, self._spawn)
102
103    def _spawn(self) -> None:
104        ba.timer(1.0, self._light.delete)
105        if self._spawn_callback is not None:
106            self._spawn_callback()
107        if self._send_spawn_message:
108            # only run if our activity still exists
109            activity = ba.getactivity()
110            if activity is not None:
111                activity.handlemessage(
112                    self.SpawnMessage(self, self._data, self._pt))

Utility for delayed spawning of objects.

Category: Gameplay Classes

Creates a light flash and sends a Spawner.SpawnMessage to the current activity after a delay.

Spawner( data: Any = None, pt: Sequence[float] = (0, 0, 0), spawn_time: float = 1.0, send_spawn_message: bool = True, spawn_callback: Optional[Callable[[], Any]] = None)
 52    def __init__(
 53            self,
 54            data: Any = None,
 55            pt: Sequence[float] = (0, 0, 0),  # pylint: disable=invalid-name
 56            spawn_time: float = 1.0,
 57            send_spawn_message: bool = True,
 58            spawn_callback: Callable[[], Any] | None = None):
 59        """Instantiate a Spawner.
 60
 61        Requires some custom data, a position,
 62        and a spawn-time in seconds.
 63        """
 64        self._spawn_callback = spawn_callback
 65        self._send_spawn_message = send_spawn_message
 66        self._spawner_sound = ba.getsound('swip2')
 67        self._data = data
 68        self._pt = pt
 69        # create a light where the spawn will happen
 70        self._light = ba.newnode('light',
 71                                 attrs={
 72                                     'position': tuple(pt),
 73                                     'radius': 0.1,
 74                                     'color': (1.0, 0.1, 0.1),
 75                                     'lights_volumes': False
 76                                 })
 77        scl = float(spawn_time) / 3.75
 78        min_val = 0.4
 79        max_val = 0.7
 80        ba.playsound(self._spawner_sound, position=self._light.position)
 81        ba.animate(
 82            self._light, 'intensity', {
 83                0.0: 0.0,
 84                0.25 * scl: max_val,
 85                0.500 * scl: min_val,
 86                0.750 * scl: max_val,
 87                1.000 * scl: min_val,
 88                1.250 * scl: 1.1 * max_val,
 89                1.500 * scl: min_val,
 90                1.750 * scl: 1.2 * max_val,
 91                2.000 * scl: min_val,
 92                2.250 * scl: 1.3 * max_val,
 93                2.500 * scl: min_val,
 94                2.750 * scl: 1.4 * max_val,
 95                3.000 * scl: min_val,
 96                3.250 * scl: 1.5 * max_val,
 97                3.500 * scl: min_val,
 98                3.750 * scl: 2.0,
 99                4.000 * scl: 0.0
100            })
101        ba.timer(spawn_time, self._spawn)

Instantiate a Spawner.

Requires some custom data, a position, and a spawn-time in seconds.

class Spawner.SpawnMessage:
26    class SpawnMessage:
27        """Spawn message sent by a Spawner after its delay has passed.
28
29        Category: **Message Classes**
30        """
31
32        spawner: Spawner
33        """The ba.Spawner we came from."""
34
35        data: Any
36        """The data object passed by the user."""
37
38        pt: Sequence[float]
39        """The spawn position."""
40
41        def __init__(
42                self,
43                spawner: Spawner,
44                data: Any,
45                pt: Sequence[float],  # pylint: disable=invalid-name
46        ):
47            """Instantiate with the given values."""
48            self.spawner = spawner
49            self.data = data
50            self.pt = pt  # pylint: disable=invalid-name

Spawn message sent by a Spawner after its delay has passed.

Category: Message Classes

Spawner.SpawnMessage(spawner: bastd.actor.spawner.Spawner, data: Any, pt: Sequence[float])
41        def __init__(
42                self,
43                spawner: Spawner,
44                data: Any,
45                pt: Sequence[float],  # pylint: disable=invalid-name
46        ):
47            """Instantiate with the given values."""
48            self.spawner = spawner
49            self.data = data
50            self.pt = pt  # pylint: disable=invalid-name

Instantiate with the given values.

The ba.Spawner we came from.

data: Any

The data object passed by the user.

pt: Sequence[float]

The spawn position.