components.py (4138B)
1 import abc 2 3 from dataclasses import dataclass 4 from typing import Any, Callable, Generator, List, Tuple 5 6 from ..common import * 7 8 9 class Component(metaclass=abc.ABCMeta): 10 """ 11 Defines the interface for a 'Component', with a unique id. 12 """ 13 @classmethod 14 def __subclasshook__(cls, subclass): 15 return ( 16 hasattr(subclass, 'cid') and type(subclass.cid) is type(int) 17 ) 18 19 @abc.abstractmethod 20 def cid(self) -> int: 21 """ 22 Returns a unique id for this component class. 23 """ 24 raise NotImplementedError 25 26 27 class ComponentBase: 28 """ 29 Implements functionality common to each component. 30 """ 31 def __hash__(self) -> int: 32 return self.cid 33 34 35 @dataclass 36 @AutoId.component 37 class Transform2D(ComponentBase): 38 """ 39 Holds the position (pixels), rotation (radians). 40 """ 41 px: float 42 py: float 43 theta: float 44 45 46 @dataclass 47 @AutoId.component 48 class Collider2D(ComponentBase): 49 """ 50 Holds the size of the AABB in the x and y planes. 51 """ 52 sx: float 53 sy: float 54 55 56 @dataclass 57 @AutoId.component 58 class Collision(ComponentBase): 59 """ 60 Stores a collision between 2 entities. 61 """ 62 colliding_entity: int 63 64 65 @dataclass 66 @AutoId.component 67 class Velocity2D(ComponentBase): 68 """ 69 Holds the velocity (pixels per second) of an entity. 70 """ 71 vx: float 72 vy: float 73 74 75 @dataclass 76 @AutoId.component 77 class ScreenElement(ComponentBase): 78 """ 79 Holds a handle to some screen element. 80 """ 81 handle: int 82 vertices: List[int] 83 84 85 @dataclass 86 @AutoId.component 87 class StaleTag(ComponentBase): 88 """ 89 Marks an entity as being stale, and marks it to be be disposed of. 90 """ 91 pass 92 93 94 @dataclass 95 @AutoId.component 96 class EdgeHarm(ComponentBase): 97 """ 98 Harms a given entity once it reaches the given y-coordinate. 99 """ 100 target: int 101 py: int 102 103 104 @dataclass 105 @AutoId.component 106 class UserInput(ComponentBase): 107 """ 108 Receives input from peripherals. 109 """ 110 speed: int 111 112 113 @dataclass 114 @AutoId.component 115 class Score(ComponentBase): 116 """ 117 Stores the current score that an entity has accrued. 118 """ 119 count: int 120 121 122 @dataclass 123 @AutoId.component 124 class Lives(ComponentBase): 125 """ 126 Stores the number of lives that a given entitiy has. 127 """ 128 count: int 129 130 131 @dataclass 132 @AutoId.component 133 class Lifespan(ComponentBase): 134 """ 135 Gives an entity a finite lifespan, after which it gets destroyed. 136 """ 137 ttl: float 138 139 140 @dataclass 141 @AutoId.component 142 class PlayerTag(ComponentBase): 143 """ 144 Marks an entity as a player. 145 """ 146 pass 147 148 149 @dataclass 150 @AutoId.component 151 class EnemyTag(ComponentBase): 152 """ 153 Marks an entity as an enemy. 154 """ 155 pass 156 157 158 @dataclass 159 class BulletData: 160 """ 161 Common bullet data. 162 """ 163 bullet_size: int 164 bullet_speed: int 165 bullet_vertices: List[int] 166 bullet_colours: List[str] 167 bullet_colour_idx: int 168 169 170 @dataclass 171 @AutoId.component 172 class BulletTag(ComponentBase): 173 """ 174 Marks an entity as a bullet. 175 """ 176 pass 177 178 179 @dataclass 180 @AutoId.component 181 class LinearBulletEmitter(ComponentBase): 182 """ 183 Emits bullets in a straight line. 184 """ 185 data: BulletData 186 direction: int 187 188 189 @dataclass 190 @AutoId.component 191 class FireLinearBulletEmitter(ComponentBase): 192 """ 193 Used to emit a bullet from a linear emitter. 194 """ 195 pass 196 197 198 @dataclass 199 @AutoId.component 200 class RadialBulletEmitter(ComponentBase): 201 """ 202 Emits bullets in an arc. 203 """ 204 data: BulletData 205 206 bullet_count: int 207 bullet_arc_offset: float ## in radians 208 209 210 @dataclass 211 @AutoId.component 212 class FireRadialBulletEmitter(ComponentBase): 213 """ 214 Used to emit a bullet from a radial emitter. 215 """ 216 pass 217 218 219 @dataclass 220 @AutoId.component 221 class Spawner(ComponentBase): 222 """ 223 Holds information for an entity spawner. 224 """ 225 spawn_generator: Generator[Tuple[float, Tuple[int, int]], None, None] 226 instantiate: Callable[[Tuple[int, int], Any, Any], int] 227 228 229 @dataclass 230 @AutoId.component 231 class EnemyEmitterCooldown(ComponentBase): 232 """ 233 Gives an enemy bullet emitter a cooldown. 234 """ 235 min_cooldown: float 236 max_cooldown: float 237