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 ): 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( 71 'light', 72 attrs={ 73 'position': tuple(pt), 74 'radius': 0.1, 75 'color': (1.0, 0.1, 0.1), 76 'lights_volumes': False, 77 }, 78 ) 79 scl = float(spawn_time) / 3.75 80 min_val = 0.4 81 max_val = 0.7 82 ba.playsound(self._spawner_sound, position=self._light.position) 83 ba.animate( 84 self._light, 85 'intensity', 86 { 87 0.0: 0.0, 88 0.25 * scl: max_val, 89 0.500 * scl: min_val, 90 0.750 * scl: max_val, 91 1.000 * scl: min_val, 92 1.250 * scl: 1.1 * max_val, 93 1.500 * scl: min_val, 94 1.750 * scl: 1.2 * max_val, 95 2.000 * scl: min_val, 96 2.250 * scl: 1.3 * max_val, 97 2.500 * scl: min_val, 98 2.750 * scl: 1.4 * max_val, 99 3.000 * scl: min_val, 100 3.250 * scl: 1.5 * max_val, 101 3.500 * scl: min_val, 102 3.750 * scl: 2.0, 103 4.000 * scl: 0.0, 104 }, 105 ) 106 ba.timer(spawn_time, self._spawn) 107 108 def _spawn(self) -> None: 109 ba.timer(1.0, self._light.delete) 110 if self._spawn_callback is not None: 111 self._spawn_callback() 112 if self._send_spawn_message: 113 # only run if our activity still exists 114 activity = ba.getactivity() 115 if activity is not None: 116 activity.handlemessage( 117 self.SpawnMessage(self, self._data, self._pt) 118 )
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 ): 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 = ba.getsound('swip2') 68 self._data = data 69 self._pt = pt 70 # create a light where the spawn will happen 71 self._light = ba.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 ba.playsound(self._spawner_sound, position=self._light.position) 84 ba.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 ba.timer(spawn_time, self._spawn) 108 109 def _spawn(self) -> None: 110 ba.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 = ba.getactivity() 116 if activity is not None: 117 activity.handlemessage( 118 self.SpawnMessage(self, self._data, self._pt) 119 )
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 ): 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 = ba.getsound('swip2') 68 self._data = data 69 self._pt = pt 70 # create a light where the spawn will happen 71 self._light = ba.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 ba.playsound(self._spawner_sound, position=self._light.position) 84 ba.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 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.