tinyecs-compsys-sdl2 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,237 @@
1
+ from collections.abc import Sequence
2
+ from dataclasses import dataclass
3
+
4
+ import glm
5
+ import pygame
6
+ import pygame._sdl2 as sdl2
7
+ import tinyecs as ecs
8
+
9
+ from aenum import extend_enum
10
+ from glm import vec2
11
+ from pgcooldown import LerpThing
12
+ from tinyecs import EntityID, EntityComponentsBundle
13
+ from tinyecs.compsys import Comp
14
+ from tinyecs.compsys.dynamics import PRSA
15
+
16
+
17
+ type Vector = pygame.Vector2 | glm.vec2 | Sequence[float]
18
+
19
+ extend_enum(Comp, 'CONTAINER')
20
+ extend_enum(Comp, 'GRAVITY')
21
+ extend_enum(Comp, 'LIFETIME')
22
+ extend_enum(Comp, 'SPLINE')
23
+ extend_enum(Comp, 'SPRITE')
24
+ extend_enum(Comp, 'SPRITE_ANIM')
25
+ extend_enum(Comp, 'TARGET')
26
+ extend_enum(Comp, 'TINT')
27
+ extend_enum(Comp, 'TINT_COLOR')
28
+ extend_enum(Comp, 'TINT_SPRITE')
29
+ extend_enum(Comp, 'WANTS_MOUSE')
30
+ extend_enum(Comp, 'WORLD')
31
+
32
+ use_radians = True
33
+
34
+
35
+ class TSprite:
36
+ def __init__(self,
37
+ texture: sdl2.Texture,
38
+ srcrect: pygame.Rect,
39
+ scale: float = 1.0,
40
+ angle: float = 0.0,
41
+ anchor: vec2 | None = None,
42
+ flip_x: bool = False,
43
+ flip_y: bool = False,
44
+ layer: int = 0):
45
+ """A sprite based on an sdl2 Texture.
46
+
47
+ :param texture: The actual texture
48
+ :param srcrect: The source coordinates on the texture, assuming it's an atlas
49
+ :param scale: The base scale of the sprite. This is the basis that
50
+ scaling during renering will be applie to self.texture = texture
51
+ :param angle: The base angle of the sprite, e.g. the direction it's on the sprite sheet.
52
+ :param anchor: The pivot point, defaults to center
53
+ :param flip_x: Sprite must be flipped horizontally
54
+ :param flip_y: Sprite must be flipped vertically
55
+ :param layer: An optional sprite layer
56
+
57
+ """
58
+ self.texture = texture
59
+ self.srcrect = srcrect
60
+ self.scale = scale
61
+
62
+ if scale != 1:
63
+ self.dstrect = self.srcrect.scale_by(scale).move_to(topleft=(0, 0))
64
+ else:
65
+ self.dstrect = srcrect.move_to(topleft=(0, 0))
66
+
67
+ if isinstance(anchor, str):
68
+ self.anchor = vec2(getattr(self.dstrect, anchor))
69
+ else:
70
+ self.anchor = anchor
71
+
72
+ self.flip_x = flip_x
73
+ self.flip_y = flip_y
74
+ self.angle = angle
75
+
76
+ self.layer = layer
77
+
78
+ def __iter__(self):
79
+ yield 'texture', self.texture
80
+ yield 'srcrect', self.srcrect
81
+ yield 'scale', self.scale
82
+ yield 'angle', self.angle
83
+ yield 'anchor', self.anchor
84
+ yield 'flip_x', self.flip_x
85
+ yield 'flip_y', self.flip_y
86
+
87
+ def __replace__(self, /, **kwargs):
88
+ """Clone the sprite while also changing parameters.
89
+
90
+ .. note:: Except ``texture`` and ``srcrect``, the argument list is the
91
+ same as instantiating a :class:`TSprite`.
92
+ """
93
+ existing = dict(self)
94
+ existing.update(kwargs)
95
+ return TSprite(**existing)
96
+
97
+ def __repr__(self):
98
+ return f'TSprite({self.texture}, srcrect={self.srcrect}, dstrect={self.dstrect}, anchor={self.anchor}, layer={self.layer}, flip_x={self.flip_x}, flip_y={self.flip_y})'
99
+
100
+
101
+ @dataclass
102
+ class SpriteAnim:
103
+ """A component holding a sprite animation.
104
+
105
+ :param anim: A list of TSprite instances
106
+ :param timer: A LerpThing to control the sprite switching
107
+
108
+ """
109
+ anim: list[TSprite]
110
+ timer: LerpThing
111
+
112
+
113
+ def sys_bounce(eid: EntityID, prsa: PRSA, sprite: TSprite, momentum: Vector, world: pygame.Rect) -> None:
114
+ """Bounce the sprite within the given container.
115
+
116
+ """
117
+
118
+ rect = sprite.dstrect.move_to(center=prsa.pos)
119
+ if prsa.scale != 1:
120
+ rect.scale_by_ip(prsa.scale)
121
+
122
+ if rect.left < world.left:
123
+ rect.left = world.left - rect.left
124
+ momentum.x = -momentum.x
125
+ elif rect.right > world.right:
126
+ rect.right = 2 * world.right - rect.right
127
+ momentum.x = -momentum.x
128
+ if rect.top < world.top:
129
+ rect.top = world.top - rect.top
130
+ momentum.y = -momentum.y
131
+ elif rect.bottom > world.bottom:
132
+ rect.bottom = 2 * world.bottom - rect.bottom
133
+ momentum.y = -momentum.y
134
+
135
+
136
+ def sys_container(eid: EntityID, prsa: PRSA, container: pygame.Rect) -> None:
137
+ """Kill the entity if it has left the given boundaries.
138
+
139
+ :param eid: The entity ID
140
+
141
+ """
142
+
143
+ if not container.collidepoint(prsa.pos):
144
+ ecs.add_component(eid, Comp.IS_KILLED, True)
145
+
146
+
147
+ def _draw_sprite(sprite: TSprite, prsa: PRSA) -> None:
148
+ """Draw a sprite transformed and positioned by the PRSA.
149
+
150
+ :param sprite: The TSprite component holding the texture
151
+ :param prsa: The PRSA transform object of the entity
152
+
153
+ The sprite is positioned relative to its center. The optional ``anchor``
154
+ attribute in the sprite is honored and used as offset to the center.
155
+ """
156
+
157
+ texture = sprite.texture
158
+
159
+ bkp_alpha = texture.alpha
160
+ texture.alpha = prsa.alpha
161
+
162
+ anchor = sprite.anchor or sprite.dstrect.center
163
+ dstrect = sprite.dstrect.move_to(center=glm.round(prsa.pos + sprite.dstrect.center - anchor))
164
+
165
+ if prsa.scale != 1:
166
+ dstrect.scale_by_ip(prsa.scale)
167
+
168
+ angle = prsa.rotation + sprite.angle
169
+ if use_radians:
170
+ angle = glm.degrees(angle)
171
+
172
+ texture.draw(srcrect=sprite.srcrect,
173
+ dstrect=dstrect,
174
+ angle=angle,
175
+ origin=sprite.anchor,
176
+ flip_x=sprite.flip_x,
177
+ flip_y=sprite.flip_y)
178
+
179
+ texture.alpha = bkp_alpha
180
+
181
+
182
+ def sys_bulk_draw_sprites(entities: EntityComponentsBundle, *, layered=False):
183
+ """Bulk-draw sprites transformed and positioned by the PRSA.
184
+
185
+ :param eid: A list of ``tinyecs.EntityComponentsBundle`` to loop over
186
+ :param layered: Flag if the ``layer`` attribute in the sprite should be honored
187
+
188
+ """
189
+
190
+ if layered:
191
+ sprites = sorted(entities, key=lambda e: e[1].layer)
192
+ else:
193
+ sprites = entities
194
+
195
+ for eid, sprite, prsa in sprites:
196
+ _draw_sprite(sprite, prsa)
197
+
198
+
199
+ def sys_draw_sprite(eid: EntityID, sprite: TSprite, prsa: PRSA) -> None:
200
+ """Draw a sprite transformed and positioned by the PRSA.
201
+
202
+ :param eid: The entity ID
203
+ :param sprite: The TSprite component holding the texture
204
+ :param prsa: The PRSA transform object of the entity
205
+
206
+ """
207
+
208
+ _draw_sprite(sprite, prsa)
209
+
210
+
211
+ def sys_sprite_anim(eid: EntityID, sprite_anim: SpriteAnim) -> None:
212
+ """Based on the timer in ``anim``, update ``sprite``.
213
+
214
+ :param eid: The entity ID
215
+ :param sprite_anim: The sprite animation
216
+
217
+ """
218
+
219
+ if sprite_anim.timer.finished():
220
+ sprite = sprite_anim.anim[-1]
221
+ else:
222
+ sprite = sprite_anim.anim[int(sprite_anim.timer)]
223
+ ecs.update_component(eid, Comp.SPRITE, sprite)
224
+
225
+
226
+ def sys_mouse(eid: EntityID, prsa: PRSA, wants_mouse: bool, *, mouse: Vector) -> None:
227
+ """Apply the mouse position to the PRSA.
228
+
229
+ :param eid: The entity ID
230
+ :param prsa: The PRSA transform object of the entity
231
+ :param wants_mouse: To only use entities that want a mouse position
232
+ :param mouse: The current mouse position
233
+
234
+ .. note: The mouse position must be provided as kwarg when running this system.
235
+ """
236
+
237
+ prsa.pos = vec2(mouse)
@@ -0,0 +1,68 @@
1
+ Metadata-Version: 2.4
2
+ Name: tinyecs-compsys-sdl2
3
+ Version: 0.0.1
4
+ Summary: pygame-ce SDL2 component and system library for tinyecs
5
+ Author-email: Michael Lamertz <michael.lamertz@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: homepage, https://forgejo.indieandmore.photography/dickerdackel/tinyecs-compsys-sdl2.git
8
+ Project-URL: bugtracker, https://forgejo.indieandmore.photography/dickerdackel/tinyecs-compsys-sdl2/issues
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Topic :: Games/Entertainment
12
+ Classifier: Topic :: Software Development :: Libraries :: pygame
13
+ Description-Content-Type: text/x-rst
14
+ License-File: LICENSE
15
+ Requires-Dist: aenum
16
+ Requires-Dist: pgcooldown
17
+ Requires-Dist: pygame-ce
18
+ Requires-Dist: pyglm
19
+ Requires-Dist: tinyecs>=0.5.0
20
+ Requires-Dist: tinyecs-compsys
21
+ Requires-Dist: tinyecs-compsys-dynamics
22
+ Dynamic: license-file
23
+
24
+ tinyecs-compsys-sdl2
25
+ ====================
26
+
27
+ Components for pygame-ce's sdl2 rendering.
28
+
29
+ Synopsis
30
+ --------
31
+
32
+ .. code:: python
33
+
34
+ from tinyecs.compsys.sdl2 import ...
35
+
36
+ Usage
37
+ -----
38
+
39
+ This package contains pygame-ce sdl2 components and systems.
40
+
41
+ Installation
42
+ ------------
43
+
44
+ Installation instructions + requirements (should come automagically with
45
+ pyproject.toml)
46
+
47
+ .. code:: console
48
+
49
+ pip install tinyecs[sdl2]
50
+ # or
51
+ pip install tinyecs
52
+ pip install tinyecs-compsys
53
+ pip install tinyecs-compsys-sdl2
54
+
55
+ Support / Contributing
56
+ ----------------------
57
+
58
+ Issues can be opened on `github issues`_
59
+
60
+ .. _github issues: https://forgejo.indieandmore.photography/dickerdackel/tinyecs-compsys/issues
61
+
62
+
63
+ License
64
+ -------
65
+
66
+ This software is provided under the MIT license.
67
+
68
+ See LICENSE file for details.
@@ -0,0 +1,6 @@
1
+ tinyecs/compsys/sdl2/__init__.py,sha256=sAMe0UEcpY_K-HbIoPdtsKpcrEzEgw8bHkKrCz9WfkA,7165
2
+ tinyecs_compsys_sdl2-0.0.1.dist-info/licenses/LICENSE,sha256=Ov4pwsg4S7PmyeyUVABYYBtwaBxK1LdI8Y9--FajuMk,1072
3
+ tinyecs_compsys_sdl2-0.0.1.dist-info/METADATA,sha256=c1cTEjOYVSHJaC5HcyFNGX1lW7z_lwkNEEEMjlMrh-s,1678
4
+ tinyecs_compsys_sdl2-0.0.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
5
+ tinyecs_compsys_sdl2-0.0.1.dist-info/top_level.txt,sha256=lMaXjtnpZvawjp5Re5jZOY_Yki_mB1QkM_PTRUig0O4,8
6
+ tinyecs_compsys_sdl2-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Michael Lamertz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ tinyecs