batframework 1.0.7__py3-none-any.whl → 1.0.8a2__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.
- batFramework/__init__.py +47 -41
- batFramework/action.py +20 -42
- batFramework/actionContainer.py +4 -43
- batFramework/animatedSprite.py +65 -98
- batFramework/audioManager.py +25 -39
- batFramework/camera.py +56 -226
- batFramework/constants.py +48 -32
- batFramework/cutscene.py +24 -32
- batFramework/cutsceneBlocks.py +33 -30
- batFramework/debugger.py +48 -0
- batFramework/dynamicEntity.py +7 -8
- batFramework/easing.py +23 -28
- batFramework/entity.py +52 -89
- batFramework/gui/__init__.py +1 -3
- batFramework/gui/button.py +58 -124
- batFramework/gui/constraints.py +90 -163
- batFramework/gui/container.py +18 -29
- batFramework/gui/debugger.py +42 -106
- batFramework/gui/frame.py +9 -15
- batFramework/gui/image.py +17 -36
- batFramework/gui/indicator.py +14 -20
- batFramework/gui/interactiveWidget.py +12 -63
- batFramework/gui/label.py +50 -113
- batFramework/gui/layout.py +51 -66
- batFramework/gui/root.py +29 -70
- batFramework/gui/shape.py +41 -34
- batFramework/gui/toggle.py +29 -37
- batFramework/gui/widget.py +131 -174
- batFramework/manager.py +14 -21
- batFramework/particles.py +20 -41
- batFramework/scene.py +72 -173
- batFramework/sceneManager.py +80 -40
- batFramework/stateMachine.py +0 -1
- batFramework/time.py +51 -62
- batFramework/transition.py +154 -0
- batFramework/utils.py +150 -3
- batframework-1.0.8a2.dist-info/METADATA +58 -0
- batframework-1.0.8a2.dist-info/RECORD +42 -0
- {batframework-1.0.7.dist-info → batframework-1.0.8a2.dist-info}/WHEEL +1 -1
- batFramework/enums.py +0 -14
- batFramework/fontManager.py +0 -57
- batFramework/gui/dialogueBox.py +0 -70
- batFramework/gui/slider.py +0 -5
- batFramework/gui/textInput.py +0 -88
- batFramework/resourceManager.py +0 -72
- batFramework/sprite.py +0 -33
- batFramework/tileset.py +0 -64
- batframework-1.0.7.dist-info/LICENCE +0 -21
- batframework-1.0.7.dist-info/METADATA +0 -55
- batframework-1.0.7.dist-info/RECORD +0 -50
- {batframework-1.0.7.dist-info → batframework-1.0.8a2.dist-info}/top_level.txt +0 -0
batFramework/scene.py
CHANGED
@@ -1,132 +1,86 @@
|
|
1
1
|
from __future__ import annotations
|
2
2
|
import re
|
3
|
-
from typing import TYPE_CHECKING,
|
4
|
-
|
3
|
+
from typing import TYPE_CHECKING,Any
|
5
4
|
if TYPE_CHECKING:
|
6
5
|
from .manager import Manager
|
7
|
-
from .sceneManager import SceneManager
|
8
6
|
import pygame
|
9
7
|
import batFramework as bf
|
10
8
|
|
11
9
|
|
12
10
|
class Scene:
|
13
|
-
def __init__(self, name
|
14
|
-
"""
|
15
|
-
Initialize the Scene object.
|
16
|
-
|
17
|
-
Args:
|
18
|
-
name: Name of the scene.
|
19
|
-
enable_alpha (bool, optional): Enable alpha channel for the scene surfaces. Defaults to True.
|
20
|
-
"""
|
21
|
-
self.scene_index = 0
|
11
|
+
def __init__(self, name, enable_alpha=True) -> None:
|
22
12
|
self._name = name
|
23
|
-
self.manager: Manager | None = None
|
24
13
|
self._active = False
|
25
14
|
self._visible = False
|
26
15
|
self._world_entities: list[bf.Entity] = []
|
27
16
|
self._hud_entities: list[bf.Entity] = []
|
17
|
+
self.manager: Manager | None = None
|
28
18
|
self.actions: bf.ActionContainer = bf.ActionContainer()
|
29
|
-
self.
|
30
|
-
self.
|
31
|
-
self.hud_camera: bf.Camera = bf.Camera(
|
19
|
+
self.camera: bf.Camera = bf.Camera()
|
20
|
+
self.scene_index = 0
|
21
|
+
self.hud_camera: bf.Camera = bf.Camera()
|
22
|
+
if enable_alpha:
|
23
|
+
self.camera.surface = self.camera.surface.convert_alpha()
|
24
|
+
self.hud_camera.surface = self.camera.surface.convert_alpha()
|
25
|
+
|
26
|
+
self.camera.set_clear_color((0, 0, 0))
|
27
|
+
self.hud_camera.set_clear_color((0, 0, 0, 0))
|
32
28
|
|
33
|
-
self.root: bf.Root = bf.Root(
|
29
|
+
self.root : bf.Root = bf.Root()
|
34
30
|
self.root.set_center(*self.hud_camera.get_center())
|
35
31
|
self.add_hud_entity(self.root)
|
36
32
|
self.blit_calls = 0
|
37
33
|
|
38
|
-
|
39
|
-
def get_world_entity_count(self)->int:
|
40
|
-
return len(self._world_entities)
|
41
|
-
|
42
|
-
|
43
|
-
def get_hud_entity_count(self)->int:
|
44
|
-
return len(self._hud_entities) + self.root.count_children_recursive() -1
|
45
|
-
|
46
|
-
|
47
|
-
def set_scene_index(self, index: int):
|
48
|
-
"""Set the scene index."""
|
34
|
+
def set_scene_index(self,index):
|
49
35
|
self.scene_index = index
|
50
36
|
|
51
|
-
def get_scene_index(self)
|
52
|
-
"""Get the scene index."""
|
37
|
+
def get_scene_index(self):
|
53
38
|
return self.scene_index
|
54
39
|
|
55
|
-
def set_sharedVar(self, name
|
56
|
-
|
57
|
-
Set a shared variable in the manager.
|
58
|
-
|
59
|
-
Args:
|
60
|
-
name: Name of the shared variable.
|
61
|
-
value: Value to set.
|
62
|
-
|
63
|
-
Returns:
|
64
|
-
bool: True if setting was successful, False otherwise.
|
65
|
-
"""
|
66
|
-
if not self.manager:
|
67
|
-
return False
|
40
|
+
def set_sharedVar(self, name, value)->bool:
|
41
|
+
if not self.manager : return False
|
68
42
|
return self.manager.set_sharedVar(name, value)
|
69
43
|
|
70
|
-
def get_sharedVar(self, name
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
Args:
|
75
|
-
name: Name of the shared variable.
|
76
|
-
|
77
|
-
Returns:
|
78
|
-
Any: Value of the shared variable.
|
79
|
-
"""
|
80
|
-
if not self.manager:
|
81
|
-
return error_value
|
82
|
-
return self.manager.get_sharedVar(name,error_value)
|
44
|
+
def get_sharedVar(self, name)->Any:
|
45
|
+
if not self.manager : return False
|
46
|
+
return self.manager.get_sharedVar(name)
|
83
47
|
|
84
48
|
def do_when_added(self):
|
85
49
|
pass
|
86
50
|
|
87
|
-
def set_clear_color(self, color: pygame.Color
|
88
|
-
"""Set the clear color for the camera."""
|
51
|
+
def set_clear_color(self, color: pygame.Color|tuple):
|
89
52
|
self.camera.set_clear_color(color)
|
53
|
+
# self.hud_camera.set_clear_color(color)
|
90
54
|
|
91
55
|
def set_manager(self, manager_link: Manager):
|
92
|
-
"""Set the manager link for the scene."""
|
93
56
|
self.manager = manager_link
|
94
|
-
self.manager.update_scene_states()
|
95
57
|
|
96
58
|
def set_visible(self, value: bool):
|
97
|
-
"""Set the visibility of the scene."""
|
98
59
|
self._visible = value
|
99
|
-
if self.manager:
|
100
|
-
self.manager.update_scene_states()
|
60
|
+
if self.manager : self.manager.update_scene_states()
|
101
61
|
|
102
62
|
def set_active(self, value):
|
103
|
-
"""Set the activity of the scene."""
|
104
63
|
self._active = value
|
105
|
-
if self.manager:
|
106
|
-
self.manager.update_scene_states()
|
64
|
+
if self.manager : self.manager.update_scene_states()
|
107
65
|
|
108
66
|
def is_active(self) -> bool:
|
109
|
-
"""Check if the scene is active."""
|
110
67
|
return self._active
|
111
68
|
|
112
69
|
def is_visible(self) -> bool:
|
113
|
-
"""Check if the scene is visible."""
|
114
70
|
return self._visible
|
115
71
|
|
116
72
|
def get_name(self) -> str:
|
117
|
-
"""Get the name of the scene."""
|
118
73
|
return self._name
|
119
74
|
|
120
75
|
def add_world_entity(self, *entity: bf.Entity):
|
121
|
-
"""Add world entities to the scene."""
|
122
76
|
for e in entity:
|
123
|
-
self._world_entities
|
124
|
-
|
125
|
-
|
126
|
-
|
77
|
+
if e not in self._world_entities:
|
78
|
+
|
79
|
+
self._world_entities.append(e)
|
80
|
+
e.parent_scene = self
|
81
|
+
e.do_when_added()
|
127
82
|
|
128
83
|
def remove_world_entity(self, *entity: bf.Entity):
|
129
|
-
"""Remove world entities from the scene."""
|
130
84
|
for e in entity:
|
131
85
|
if e not in self._world_entities:
|
132
86
|
return False
|
@@ -136,65 +90,38 @@ class Scene:
|
|
136
90
|
return True
|
137
91
|
|
138
92
|
def add_hud_entity(self, *entity: bf.Entity):
|
139
|
-
"""Add HUD entities to the scene."""
|
140
93
|
for e in entity:
|
141
|
-
self._hud_entities
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
return True
|
94
|
+
if e not in self._hud_entities:
|
95
|
+
self._hud_entities.append(e)
|
96
|
+
e.parent_scene = self
|
97
|
+
e.do_when_added()
|
146
98
|
|
147
99
|
def remove_hud_entity(self, *entity: bf.Entity):
|
148
|
-
"""Remove HUD entities from the scene."""
|
149
100
|
for e in entity:
|
150
101
|
if e in self._hud_entities:
|
151
102
|
e.do_when_removed()
|
152
103
|
e.parent_scene = None
|
153
104
|
self._hud_entities.remove(e)
|
154
105
|
|
155
|
-
def
|
156
|
-
"""Add actions to the scene."""
|
106
|
+
def add_action(self, *action):
|
157
107
|
self.actions.add_action(*action)
|
158
108
|
|
159
|
-
def add_early_actions(self, *action):
|
160
|
-
"""Add actions to the scene."""
|
161
|
-
self.early_actions.add_action(*action)
|
162
|
-
|
163
109
|
def get_by_tags(self, *tags):
|
164
|
-
|
165
|
-
res = [
|
110
|
+
return [
|
166
111
|
entity
|
167
112
|
for entity in self._world_entities + self._hud_entities
|
168
|
-
if any(entity.
|
113
|
+
if any(entity.has_tag(t) for t in tags)
|
169
114
|
]
|
170
|
-
res.extend(list(self.root.get_by_tags(*tags)))
|
171
|
-
return res
|
172
115
|
|
173
116
|
def get_by_uid(self, uid) -> bf.Entity | None:
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
for entity in entities:
|
183
|
-
if entity.uid == uid:
|
184
|
-
return entity
|
185
|
-
return None
|
186
|
-
|
187
|
-
def _recursive_search_by_uid(self, uid, widget) -> bf.Entity | None:
|
188
|
-
"""Recursively search for entity by uid in the widget's children."""
|
189
|
-
if widget.uid == uid:
|
190
|
-
return widget
|
191
|
-
|
192
|
-
for child in widget.children:
|
193
|
-
res = self._recursive_search_by_uid(uid, child)
|
194
|
-
if res is not None:
|
195
|
-
return res
|
196
|
-
|
197
|
-
return None
|
117
|
+
return next(
|
118
|
+
(
|
119
|
+
entity
|
120
|
+
for entity in self._world_entities + self._hud_entities
|
121
|
+
if entity.uid == uid
|
122
|
+
),
|
123
|
+
None,
|
124
|
+
)
|
198
125
|
|
199
126
|
# called before process event
|
200
127
|
def do_early_process_event(self, event: pygame.Event) -> bool:
|
@@ -207,91 +134,77 @@ class Scene:
|
|
207
134
|
Propagates event to child events. Calls early process event first, if returns False then stops. Processes scene's action_container, then custom do_handle_event function.
|
208
135
|
Finally resets the action_container, and propagates to all child entities. if any of them returns True, the propagation is stopped.
|
209
136
|
"""
|
210
|
-
|
137
|
+
if self.get_sharedVar("in_transition"):
|
138
|
+
return
|
211
139
|
if self.do_early_process_event(event):
|
212
140
|
return
|
213
|
-
self.
|
214
|
-
self.do_handle_actions()
|
141
|
+
self.actions.process_event(event)
|
215
142
|
self.do_handle_event(event)
|
216
|
-
for entity in self.
|
143
|
+
for entity in self._world_entities + self._hud_entities:
|
217
144
|
if entity.process_event(event):
|
218
|
-
|
219
|
-
self.actions.
|
220
|
-
|
221
|
-
|
222
|
-
def do_handle_actions(self) -> None:
|
223
|
-
"""Handle actions within the scene."""
|
224
|
-
pass
|
145
|
+
break
|
146
|
+
self.actions.reset()
|
225
147
|
|
226
148
|
def do_handle_event(self, event: pygame.Event):
|
227
149
|
"""called inside process_event but before resetting the scene's action container and propagating event to child entities of the scene"""
|
228
150
|
pass
|
229
151
|
|
230
152
|
def update(self, dt):
|
231
|
-
"""Update the scene. Do NOT override"""
|
232
153
|
for entity in self._world_entities + self._hud_entities:
|
233
154
|
entity.update(dt)
|
234
155
|
self.do_update(dt)
|
235
156
|
self.camera.update(dt)
|
236
157
|
self.hud_camera.update(dt)
|
237
|
-
self.actions.reset()
|
238
|
-
self.early_actions.reset()
|
239
158
|
|
240
159
|
def do_update(self, dt):
|
241
|
-
"""Specific update within the scene."""
|
242
160
|
pass
|
243
161
|
|
244
162
|
def debug_entity(self, entity: bf.Entity, camera: bf.Camera):
|
163
|
+
# return
|
245
164
|
if not entity.visible:
|
246
165
|
return
|
247
|
-
# bounding_box = entity.get_bounding_box()
|
248
|
-
# if bounding_box is None : return
|
249
166
|
for data in entity.get_bounding_box():
|
250
|
-
if data
|
251
|
-
if isinstance(data, pygame.FRect) or isinstance(data, pygame.Rect):
|
167
|
+
if isinstance(data,pygame.FRect):
|
252
168
|
rect = data
|
253
|
-
color = entity.
|
169
|
+
color = entity._debug_color
|
254
170
|
else:
|
255
171
|
rect = data[0]
|
256
172
|
color = data[1]
|
173
|
+
if not isinstance(color,pygame.Color): color = pygame.Color(color)
|
257
174
|
|
258
|
-
pygame.draw.rect(camera.surface, color, camera.transpose(rect), 1)
|
259
|
-
|
260
|
-
def sort_entities(self) -> None:
|
261
|
-
"""Sort entities within the scene based on their rendering order."""
|
262
|
-
self._world_entities.sort(key=lambda e: e.render_order)
|
263
|
-
self._hud_entities.sort(key=lambda e: e.render_order)
|
175
|
+
pygame.draw.rect(camera.surface, color , camera.transpose(rect), 1)
|
264
176
|
|
265
177
|
def draw(self, surface: pygame.Surface):
|
178
|
+
self._world_entities.sort(key=lambda e: (e.z_depth,e.render_order))
|
179
|
+
self._hud_entities.sort(key=lambda e: (e.z_depth,e.render_order))
|
180
|
+
|
266
181
|
total_blit_calls = 0
|
267
|
-
if not self.manager : return
|
268
182
|
self.camera.clear()
|
269
183
|
self.hud_camera.clear()
|
270
184
|
|
271
|
-
show_outlines = self.manager._debugging in [2, 3]
|
272
|
-
show_only_visible_outlines = self.manager._debugging != 3
|
273
|
-
|
274
185
|
|
275
|
-
|
276
|
-
|
186
|
+
total_blit_calls += sum(
|
187
|
+
entity.draw(self.camera) for entity in self._world_entities
|
188
|
+
)
|
277
189
|
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
in self._world_entities if not show_only_visible_outlines\
|
282
|
-
or (show_only_visible_outlines and entity.visible)]
|
190
|
+
if self.manager and self.manager._debugging == 2:
|
191
|
+
for entity in self._world_entities:
|
192
|
+
self.debug_entity(entity, self.camera)
|
283
193
|
|
284
|
-
total_blit_calls += sum(
|
194
|
+
total_blit_calls += sum(
|
195
|
+
entity.draw(self.hud_camera) for entity in self._hud_entities
|
196
|
+
)
|
197
|
+
if self.manager and self.manager._debugging == 2:
|
198
|
+
for entity in self._hud_entities:
|
199
|
+
self.debug_entity(entity, self.hud_camera)
|
285
200
|
|
286
|
-
if show_outlines:
|
287
|
-
[self.debug_entity(entity, self.hud_camera) for entity\
|
288
|
-
in self._hud_entities if not show_only_visible_outlines\
|
289
|
-
or (show_only_visible_outlines and entity.visible)]
|
290
201
|
|
291
202
|
self.do_early_draw(surface)
|
292
203
|
self.camera.draw(surface)
|
204
|
+
self.do_post_world_draw(surface)
|
293
205
|
self.hud_camera.draw(surface)
|
294
206
|
self.do_final_draw(surface)
|
207
|
+
|
295
208
|
self.blit_calls = total_blit_calls
|
296
209
|
|
297
210
|
def do_early_draw(self, surface: pygame.Surface):
|
@@ -306,22 +219,8 @@ class Scene:
|
|
306
219
|
def on_enter(self):
|
307
220
|
self.set_active(True)
|
308
221
|
self.set_visible(True)
|
309
|
-
self.root.clear_hovered()
|
310
|
-
self.root.clear_focused()
|
311
|
-
self.root.build()
|
312
|
-
self.do_on_enter()
|
313
222
|
|
314
223
|
def on_exit(self):
|
315
|
-
self.root.clear_hovered()
|
316
|
-
self.root.clear_focused()
|
317
224
|
self.set_active(False)
|
318
225
|
self.set_visible(False)
|
319
226
|
self.actions.hard_reset()
|
320
|
-
self.early_actions.hard_reset()
|
321
|
-
self.do_on_exit()
|
322
|
-
|
323
|
-
def do_on_enter(self)->None:
|
324
|
-
pass
|
325
|
-
|
326
|
-
def do_on_exit(self)->None:
|
327
|
-
pass
|
batFramework/sceneManager.py
CHANGED
@@ -2,18 +2,19 @@ import batFramework as bf
|
|
2
2
|
import pygame
|
3
3
|
|
4
4
|
|
5
|
+
|
5
6
|
class SceneManager:
|
6
7
|
def __init__(self, *initial_scenes: bf.Scene) -> None:
|
7
|
-
self._debugging
|
8
|
-
self.
|
8
|
+
self._debugging = 0
|
9
|
+
self.sharedVarDict = {}
|
9
10
|
|
10
|
-
self.
|
11
|
-
self.set_sharedVar("
|
11
|
+
self.transitions: list[bf.BaseTransition] = []
|
12
|
+
self.set_sharedVar("is_debugging_func", lambda: self._debugging)
|
13
|
+
self.set_sharedVar("in_transition", False)
|
12
14
|
self.set_sharedVar("in_cutscene", False)
|
13
|
-
self.set_sharedVar("player_has_control", True)
|
14
15
|
|
15
16
|
self._scenes: list[bf.Scene] = list(initial_scenes)
|
16
|
-
for index,
|
17
|
+
for index,s in enumerate(self._scenes):
|
17
18
|
s.set_manager(self)
|
18
19
|
s.set_scene_index(index)
|
19
20
|
s.do_when_added()
|
@@ -22,26 +23,28 @@ class SceneManager:
|
|
22
23
|
|
23
24
|
def print_status(self):
|
24
25
|
print("-" * 40)
|
25
|
-
print([(s._name,
|
26
|
+
print([(s._name, s._active, s._visible,s.scene_index) for s in self._scenes])
|
26
27
|
print(f"[Debugging] = {self._debugging}")
|
27
28
|
print("---SHARED VARIABLES---")
|
28
|
-
|
29
|
+
_ = [
|
29
30
|
print(f"[{str(name)} = {str(value)}]")
|
31
|
+
for name, value in self.sharedVarDict.items()
|
32
|
+
]
|
30
33
|
print("-" * 40)
|
31
34
|
|
32
35
|
def set_sharedVar(self, name, value) -> bool:
|
33
|
-
self.
|
36
|
+
self.sharedVarDict[name] = value
|
34
37
|
return True
|
35
38
|
|
36
|
-
def get_sharedVar(self, name
|
37
|
-
if name not in self.
|
38
|
-
return
|
39
|
-
return self.
|
40
|
-
|
41
|
-
def get_current_scene_name(self)
|
42
|
-
return self._scenes[0].
|
43
|
-
|
44
|
-
def get_current_scene(self)
|
39
|
+
def get_sharedVar(self, name):
|
40
|
+
if name not in self.sharedVarDict:
|
41
|
+
return None
|
42
|
+
return self.sharedVarDict[name]
|
43
|
+
|
44
|
+
def get_current_scene_name(self)-> str:
|
45
|
+
return self._scenes[0].name
|
46
|
+
|
47
|
+
def get_current_scene(self)->bf.Scene:
|
45
48
|
return self._scenes[0]
|
46
49
|
|
47
50
|
def update_scene_states(self):
|
@@ -68,51 +71,79 @@ class SceneManager:
|
|
68
71
|
if scene._name == name:
|
69
72
|
return scene
|
70
73
|
|
71
|
-
def get_scene_at(self,index:int)->bf.Scene|None:
|
72
|
-
if index < 0 or index >= len(self._scenes) : return None
|
73
|
-
return self._scenes[index]
|
74
|
-
|
75
74
|
def transition_to_scene(self, dest_scene_name, transition, **kwargs):
|
76
|
-
self.
|
75
|
+
if not self.has_scene(dest_scene_name):
|
76
|
+
return False
|
77
|
+
source_surf = pygame.Surface(bf.const.RESOLUTION,pygame.SRCALPHA).convert_alpha()
|
78
|
+
dest_surf = pygame.Surface(bf.const.RESOLUTION,pygame.SRCALPHA).convert_alpha()
|
79
|
+
|
80
|
+
index = kwargs.pop("index",0)
|
81
|
+
#draw the surfaces
|
82
|
+
source_scenes = [s for s in self.visible_scenes if s.scene_index >= index and s._visible]
|
83
|
+
# source_scenes = self.visible_scenes
|
84
|
+
_ = [s.draw(source_surf) for s in source_scenes]
|
85
|
+
# self._scenes[index].draw(source_surf)
|
86
|
+
|
87
|
+
# pygame.image.save_extended:(source_surf,"source_surface.png")
|
88
|
+
self.get_scene(dest_scene_name).draw(dest_surf)
|
89
|
+
# pygame.image.save_extended(dest_surf,"dest_surface.png")
|
90
|
+
|
91
|
+
# print(f"start transition from {self._scenes[index]._name} to {dest_scene_name}")
|
92
|
+
|
93
|
+
instance: bf.BaseTransition = transition(
|
94
|
+
source_surf, dest_surf, **kwargs
|
95
|
+
)
|
96
|
+
instance.set_scene_index(index)
|
97
|
+
instance.set_source_name(self._scenes[index]._name)
|
98
|
+
instance.set_dest_name(dest_scene_name)
|
99
|
+
self.transitions.append(instance)
|
100
|
+
self.set_sharedVar("in_transition", True)
|
101
|
+
|
102
|
+
def set_scene(self, name,index=0):
|
103
|
+
if len(self._scenes)==0 or not self.has_scene(name) or index>=len(self._scenes):return
|
77
104
|
|
78
|
-
def set_scene(self, name, index=0):
|
79
105
|
target_scene = self.get_scene(name)
|
80
|
-
if (
|
81
|
-
len(self._scenes) == 0
|
82
|
-
or not target_scene
|
83
|
-
or index >= len(self._scenes)
|
84
|
-
or index < 0
|
85
|
-
):
|
86
|
-
return
|
87
|
-
|
88
106
|
old_scene = self._scenes[index]
|
89
|
-
|
107
|
+
|
108
|
+
# print(f"switch from {old_scene._name} to {target_scene._name} in index {index}")
|
109
|
+
|
110
|
+
#switch
|
90
111
|
old_scene.on_exit()
|
91
112
|
self.remove_scene(name)
|
92
|
-
self._scenes.insert(index,
|
93
|
-
_ = [s.set_scene_index(i) for i,
|
113
|
+
self._scenes.insert(index,target_scene)
|
114
|
+
_ = [s.set_scene_index(i) for i,s in enumerate(self._scenes)]
|
94
115
|
target_scene.on_enter()
|
95
116
|
|
117
|
+
|
96
118
|
def process_event(self, event: pygame.Event):
|
119
|
+
if self.transitions:
|
120
|
+
return
|
97
121
|
keys = pygame.key.get_pressed()
|
98
122
|
if (
|
99
123
|
keys[pygame.K_LCTRL]
|
100
124
|
and event.type == pygame.KEYDOWN
|
101
125
|
and event.key == pygame.K_d
|
102
126
|
):
|
103
|
-
self._debugging = (self._debugging + 1) %
|
104
|
-
self.set_sharedVar("debugging_mode",self._debugging)
|
127
|
+
self._debugging = (self._debugging + 1) % 3
|
105
128
|
return
|
106
|
-
|
129
|
+
elif (
|
107
130
|
keys[pygame.K_LCTRL]
|
108
131
|
and event.type == pygame.KEYDOWN
|
109
132
|
and event.key == pygame.K_p
|
110
133
|
):
|
111
134
|
self.print_status()
|
112
|
-
return
|
113
135
|
self._scenes[0].process_event(event)
|
114
136
|
|
115
137
|
def update(self, dt: float) -> None:
|
138
|
+
if self.transitions:
|
139
|
+
transition = self.transitions[0]
|
140
|
+
transition.update(dt)
|
141
|
+
if transition.has_ended():
|
142
|
+
self.set_sharedVar("in_transition", False)
|
143
|
+
self.set_scene(transition.dest_scene_name,transition.index)
|
144
|
+
|
145
|
+
self.transitions.pop(0)
|
146
|
+
return
|
116
147
|
for scene in self.active_scenes:
|
117
148
|
scene.update(dt)
|
118
149
|
self.do_update(dt)
|
@@ -121,5 +152,14 @@ class SceneManager:
|
|
121
152
|
return
|
122
153
|
|
123
154
|
def draw(self, surface) -> None:
|
124
|
-
|
155
|
+
transition_scene_index = -1
|
156
|
+
visible_scenes = self.visible_scenes.copy()
|
157
|
+
if self.transitions:
|
158
|
+
transition_scene_index = self.transitions[0].index
|
159
|
+
|
160
|
+
if transition_scene_index >=0:
|
161
|
+
self.transitions[0].draw(surface)
|
162
|
+
visible_scenes = [s for s in visible_scenes if s.scene_index < transition_scene_index]
|
163
|
+
|
164
|
+
for scene in visible_scenes:
|
125
165
|
scene.draw(surface)
|