bascenev1lib.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 bascenev1 as bs
 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 bascenev1.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],
 45        ):
 46            """Instantiate with the given values."""
 47            self.spawner = spawner
 48            self.data = data
 49            self.pt = pt
 50
 51    def __init__(
 52        self,
 53        *,
 54        data: Any = None,
 55        pt: Sequence[float] = (0, 0, 0),
 56        spawn_time: float = 1.0,
 57        send_spawn_message: bool = True,
 58        spawn_callback: Callable[[], Any] | None = None,
 59    ):
 60        """Instantiate a Spawner.
 61
 62        Requires some custom data, a position,
 63        and a spawn-time in seconds.
 64        """
 65        self._spawn_callback = spawn_callback
 66        self._send_spawn_message = send_spawn_message
 67        self._spawner_sound = bs.getsound('swip2')
 68        self._data = data
 69        self._pt = pt
 70        # create a light where the spawn will happen
 71        self._light = bs.newnode(
 72            'light',
 73            attrs={
 74                'position': tuple(pt),
 75                'radius': 0.1,
 76                'color': (1.0, 0.1, 0.1),
 77                'lights_volumes': False,
 78            },
 79        )
 80        scl = float(spawn_time) / 3.75
 81        min_val = 0.4
 82        max_val = 0.7
 83        self._spawner_sound.play(position=self._light.position)
 84        bs.animate(
 85            self._light,
 86            'intensity',
 87            {
 88                0.0: 0.0,
 89                0.25 * scl: max_val,
 90                0.500 * scl: min_val,
 91                0.750 * scl: max_val,
 92                1.000 * scl: min_val,
 93                1.250 * scl: 1.1 * max_val,
 94                1.500 * scl: min_val,
 95                1.750 * scl: 1.2 * max_val,
 96                2.000 * scl: min_val,
 97                2.250 * scl: 1.3 * max_val,
 98                2.500 * scl: min_val,
 99                2.750 * scl: 1.4 * max_val,
100                3.000 * scl: min_val,
101                3.250 * scl: 1.5 * max_val,
102                3.500 * scl: min_val,
103                3.750 * scl: 2.0,
104                4.000 * scl: 0.0,
105            },
106        )
107        bs.timer(spawn_time, self._spawn)
108
109    def _spawn(self) -> None:
110        bs.timer(1.0, self._light.delete)
111        if self._spawn_callback is not None:
112            self._spawn_callback()
113        if self._send_spawn_message:
114            # only run if our activity still exists
115            activity = bs.getactivity()
116            if activity is not None:
117                activity.handlemessage(
118                    self.SpawnMessage(self, self._data, self._pt)
119                )
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 bascenev1.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],
 46        ):
 47            """Instantiate with the given values."""
 48            self.spawner = spawner
 49            self.data = data
 50            self.pt = pt
 51
 52    def __init__(
 53        self,
 54        *,
 55        data: Any = None,
 56        pt: Sequence[float] = (0, 0, 0),
 57        spawn_time: float = 1.0,
 58        send_spawn_message: bool = True,
 59        spawn_callback: Callable[[], Any] | None = None,
 60    ):
 61        """Instantiate a Spawner.
 62
 63        Requires some custom data, a position,
 64        and a spawn-time in seconds.
 65        """
 66        self._spawn_callback = spawn_callback
 67        self._send_spawn_message = send_spawn_message
 68        self._spawner_sound = bs.getsound('swip2')
 69        self._data = data
 70        self._pt = pt
 71        # create a light where the spawn will happen
 72        self._light = bs.newnode(
 73            'light',
 74            attrs={
 75                'position': tuple(pt),
 76                'radius': 0.1,
 77                'color': (1.0, 0.1, 0.1),
 78                'lights_volumes': False,
 79            },
 80        )
 81        scl = float(spawn_time) / 3.75
 82        min_val = 0.4
 83        max_val = 0.7
 84        self._spawner_sound.play(position=self._light.position)
 85        bs.animate(
 86            self._light,
 87            'intensity',
 88            {
 89                0.0: 0.0,
 90                0.25 * scl: max_val,
 91                0.500 * scl: min_val,
 92                0.750 * scl: max_val,
 93                1.000 * scl: min_val,
 94                1.250 * scl: 1.1 * max_val,
 95                1.500 * scl: min_val,
 96                1.750 * scl: 1.2 * max_val,
 97                2.000 * scl: min_val,
 98                2.250 * scl: 1.3 * max_val,
 99                2.500 * scl: min_val,
100                2.750 * scl: 1.4 * max_val,
101                3.000 * scl: min_val,
102                3.250 * scl: 1.5 * max_val,
103                3.500 * scl: min_val,
104                3.750 * scl: 2.0,
105                4.000 * scl: 0.0,
106            },
107        )
108        bs.timer(spawn_time, self._spawn)
109
110    def _spawn(self) -> None:
111        bs.timer(1.0, self._light.delete)
112        if self._spawn_callback is not None:
113            self._spawn_callback()
114        if self._send_spawn_message:
115            # only run if our activity still exists
116            activity = bs.getactivity()
117            if activity is not None:
118                activity.handlemessage(
119                    self.SpawnMessage(self, self._data, self._pt)
120                )

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        *,
 55        data: Any = None,
 56        pt: Sequence[float] = (0, 0, 0),
 57        spawn_time: float = 1.0,
 58        send_spawn_message: bool = True,
 59        spawn_callback: Callable[[], Any] | None = None,
 60    ):
 61        """Instantiate a Spawner.
 62
 63        Requires some custom data, a position,
 64        and a spawn-time in seconds.
 65        """
 66        self._spawn_callback = spawn_callback
 67        self._send_spawn_message = send_spawn_message
 68        self._spawner_sound = bs.getsound('swip2')
 69        self._data = data
 70        self._pt = pt
 71        # create a light where the spawn will happen
 72        self._light = bs.newnode(
 73            'light',
 74            attrs={
 75                'position': tuple(pt),
 76                'radius': 0.1,
 77                'color': (1.0, 0.1, 0.1),
 78                'lights_volumes': False,
 79            },
 80        )
 81        scl = float(spawn_time) / 3.75
 82        min_val = 0.4
 83        max_val = 0.7
 84        self._spawner_sound.play(position=self._light.position)
 85        bs.animate(
 86            self._light,
 87            'intensity',
 88            {
 89                0.0: 0.0,
 90                0.25 * scl: max_val,
 91                0.500 * scl: min_val,
 92                0.750 * scl: max_val,
 93                1.000 * scl: min_val,
 94                1.250 * scl: 1.1 * max_val,
 95                1.500 * scl: min_val,
 96                1.750 * scl: 1.2 * max_val,
 97                2.000 * scl: min_val,
 98                2.250 * scl: 1.3 * max_val,
 99                2.500 * scl: min_val,
100                2.750 * scl: 1.4 * max_val,
101                3.000 * scl: min_val,
102                3.250 * scl: 1.5 * max_val,
103                3.500 * scl: min_val,
104                3.750 * scl: 2.0,
105                4.000 * scl: 0.0,
106            },
107        )
108        bs.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 bascenev1.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],
46        ):
47            """Instantiate with the given values."""
48            self.spawner = spawner
49            self.data = data
50            self.pt = pt

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

Category: Message Classes

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

Instantiate with the given values.

spawner: Spawner

The bascenev1.Spawner we came from.

data: Any

The data object passed by the user.

pt: Sequence[float]

The spawn position.