batframework 1.0.8a2__py3-none-any.whl → 1.0.8a4__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 +53 -50
- batFramework/action.py +126 -99
- batFramework/actionContainer.py +53 -9
- batFramework/animatedSprite.py +117 -73
- batFramework/audioManager.py +69 -26
- batFramework/camera.py +259 -69
- batFramework/constants.py +16 -54
- batFramework/cutscene.py +39 -29
- batFramework/cutsceneBlocks.py +36 -43
- batFramework/dynamicEntity.py +17 -9
- batFramework/easingController.py +58 -0
- batFramework/entity.py +48 -97
- batFramework/enums.py +113 -0
- batFramework/fontManager.py +65 -0
- batFramework/gui/__init__.py +10 -2
- batFramework/gui/button.py +9 -78
- batFramework/gui/clickableWidget.py +221 -0
- batFramework/gui/constraints/__init__.py +1 -0
- batFramework/gui/constraints/constraints.py +730 -0
- batFramework/gui/container.py +174 -32
- batFramework/gui/debugger.py +131 -43
- batFramework/gui/dialogueBox.py +99 -0
- batFramework/gui/draggableWidget.py +40 -0
- batFramework/gui/image.py +54 -18
- batFramework/gui/indicator.py +38 -21
- batFramework/gui/interactiveWidget.py +177 -13
- batFramework/gui/label.py +292 -74
- batFramework/gui/layout.py +219 -60
- batFramework/gui/meter.py +71 -0
- batFramework/gui/radioButton.py +84 -0
- batFramework/gui/root.py +134 -38
- batFramework/gui/shape.py +259 -57
- batFramework/gui/slider.py +230 -0
- batFramework/gui/style.py +10 -0
- batFramework/gui/styleManager.py +48 -0
- batFramework/gui/textInput.py +137 -0
- batFramework/gui/toggle.py +103 -51
- batFramework/gui/widget.py +329 -254
- batFramework/manager.py +40 -19
- batFramework/object.py +114 -0
- batFramework/particle.py +101 -0
- batFramework/renderGroup.py +67 -0
- batFramework/resourceManager.py +100 -0
- batFramework/scene.py +281 -123
- batFramework/sceneManager.py +141 -108
- batFramework/scrollingSprite.py +114 -0
- batFramework/sprite.py +51 -0
- batFramework/stateMachine.py +2 -2
- batFramework/tileset.py +46 -0
- batFramework/time.py +123 -58
- batFramework/transition.py +195 -124
- batFramework/utils.py +87 -151
- batframework-1.0.8a4.dist-info/LICENCE +21 -0
- batframework-1.0.8a4.dist-info/METADATA +55 -0
- batframework-1.0.8a4.dist-info/RECORD +58 -0
- batFramework/debugger.py +0 -48
- batFramework/easing.py +0 -71
- batFramework/gui/constraints.py +0 -204
- batFramework/gui/frame.py +0 -19
- batFramework/particles.py +0 -77
- batFramework/transitionManager.py +0 -0
- batframework-1.0.8a2.dist-info/METADATA +0 -58
- batframework-1.0.8a2.dist-info/RECORD +0 -42
- {batframework-1.0.8a2.dist-info → batframework-1.0.8a4.dist-info}/WHEEL +0 -0
- {batframework-1.0.8a2.dist-info → batframework-1.0.8a4.dist-info}/top_level.txt +0 -0
batFramework/sceneManager.py
CHANGED
@@ -1,165 +1,198 @@
|
|
1
1
|
import batFramework as bf
|
2
2
|
import pygame
|
3
|
+
from typing import Self
|
3
4
|
|
4
5
|
|
6
|
+
def swap(lst, index1, index2):
|
7
|
+
lst[index1], lst[index2] = lst[index2], lst[index1]
|
8
|
+
|
5
9
|
|
6
10
|
class SceneManager:
|
7
|
-
def __init__(self
|
8
|
-
self.
|
9
|
-
self.
|
11
|
+
def __init__(self) -> None:
|
12
|
+
self.scenes: list[bf.Scene] = []
|
13
|
+
self.shared_events = {pygame.WINDOWRESIZED}
|
10
14
|
|
11
|
-
self.transitions: list[bf.BaseTransition] = []
|
12
|
-
self.set_sharedVar("is_debugging_func", lambda: self._debugging)
|
13
|
-
self.set_sharedVar("in_transition", False)
|
14
15
|
self.set_sharedVar("in_cutscene", False)
|
16
|
+
self.set_sharedVar("player_has_control", True)
|
17
|
+
self.old_player_control = True
|
18
|
+
self.debug_mode: bf.enums.debugMode = bf.debugMode.HIDDEN
|
19
|
+
self.current_transitions: dict[str, bf.transition.Transition] = {}
|
15
20
|
|
16
|
-
|
17
|
-
for index,s in enumerate(
|
18
|
-
s.set_manager(self)
|
21
|
+
def init_scenes(self, *initial_scenes:bf.Scene):
|
22
|
+
for index, s in enumerate(initial_scenes):
|
19
23
|
s.set_scene_index(index)
|
20
|
-
|
21
|
-
|
24
|
+
for s in reversed(initial_scenes):
|
25
|
+
self.add_scene(s)
|
26
|
+
# self.scenes = list(initial_scenes)
|
27
|
+
self.set_scene(initial_scenes[0].get_name())
|
22
28
|
self.update_scene_states()
|
23
29
|
|
30
|
+
def set_shared_event(self, event: pygame.Event) -> None:
|
31
|
+
"""
|
32
|
+
Add an event that will be propagated to all active scenes, not just the one on top.
|
33
|
+
"""
|
34
|
+
self.shared_events.add(event)
|
35
|
+
|
24
36
|
def print_status(self):
|
37
|
+
"""
|
38
|
+
Print some information about the current state of the scenes.
|
39
|
+
"""
|
25
40
|
print("-" * 40)
|
26
|
-
print(
|
27
|
-
|
41
|
+
print(
|
42
|
+
"\n".join(
|
43
|
+
f" {s.name:<30}\t{'Active' if s.active else 'Inactive'}\t{'Visible' if s.visible else 'Invisible'}\tindex= {s.scene_index}"
|
44
|
+
for s in self.scenes
|
45
|
+
)
|
46
|
+
)
|
47
|
+
print(f"[Debugging] = {self.debug_mode}")
|
28
48
|
print("---SHARED VARIABLES---")
|
29
|
-
|
49
|
+
for name, value in self.shared_variables.items():
|
30
50
|
print(f"[{str(name)} = {str(value)}]")
|
31
|
-
for name, value in self.sharedVarDict.items()
|
32
|
-
]
|
33
51
|
print("-" * 40)
|
34
52
|
|
35
|
-
def set_sharedVar(self, name, value) ->
|
36
|
-
|
37
|
-
return True
|
53
|
+
def set_sharedVar(self, name, value) -> None:
|
54
|
+
bf.ResourceManager().set_sharedVar(name,value)
|
38
55
|
|
39
|
-
def get_sharedVar(self, name):
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
return self._scenes[0]
|
56
|
+
def get_sharedVar(self, name, error_value=None):
|
57
|
+
return bf.ResourceManager().get_sharedVar(name, error_value)
|
58
|
+
|
59
|
+
def get_current_scene_name(self) -> str:
|
60
|
+
"""get the name of the current scene"""
|
61
|
+
return self.scenes[0].get_name()
|
62
|
+
|
63
|
+
def get_current_scene(self) -> bf.Scene:
|
64
|
+
return self.scenes[0]
|
49
65
|
|
50
66
|
def update_scene_states(self):
|
51
|
-
self.active_scenes = [s for s in reversed(self.
|
52
|
-
self.visible_scenes = [s for s in reversed(self.
|
67
|
+
self.active_scenes = [s for s in reversed(self.scenes) if s.active]
|
68
|
+
self.visible_scenes = [s for s in reversed(self.scenes) if s.visible]
|
53
69
|
|
54
70
|
def add_scene(self, scene: bf.Scene):
|
55
|
-
if scene in self.
|
71
|
+
if scene in self.scenes and not self.has_scene(scene.name):
|
56
72
|
return
|
57
73
|
scene.set_manager(self)
|
58
74
|
scene.do_when_added()
|
59
|
-
self.
|
75
|
+
self.scenes.insert(0, scene)
|
60
76
|
|
61
77
|
def remove_scene(self, name: str):
|
62
|
-
self.
|
78
|
+
self.scenes = [s for s in self.scenes if s.name != name]
|
63
79
|
|
64
80
|
def has_scene(self, name):
|
65
|
-
return any(name == scene.
|
81
|
+
return any(name == scene.name for scene in self.scenes)
|
66
82
|
|
67
83
|
def get_scene(self, name):
|
68
84
|
if not self.has_scene(name):
|
69
85
|
return None
|
70
|
-
for scene in self.
|
71
|
-
if scene.
|
86
|
+
for scene in self.scenes:
|
87
|
+
if scene.name == name:
|
72
88
|
return scene
|
73
89
|
|
74
|
-
def
|
75
|
-
if
|
76
|
-
return
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
)
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
self.
|
100
|
-
self.
|
101
|
-
|
102
|
-
|
103
|
-
|
90
|
+
def get_scene_at(self, index: int) -> bf.Scene | None:
|
91
|
+
if index < 0 or index >= len(self.scenes):
|
92
|
+
return None
|
93
|
+
return self.scenes[index]
|
94
|
+
|
95
|
+
def transition_to_scene(
|
96
|
+
self,
|
97
|
+
scene_name: str,
|
98
|
+
transition: bf.transition.Transition = bf.transition.Fade(0.1),
|
99
|
+
index: int = 0,
|
100
|
+
):
|
101
|
+
target_scene = self.get_scene(scene_name)
|
102
|
+
if not target_scene:
|
103
|
+
print(f"Scene '{scene_name}' does not exist")
|
104
|
+
return
|
105
|
+
if len(self.scenes) == 0 or index >= len(self.scenes) or index < 0:
|
106
|
+
return
|
107
|
+
source_surface = bf.const.SCREEN.copy()
|
108
|
+
dest_surface = bf.const.SCREEN.copy()
|
109
|
+
|
110
|
+
# self.draw(source_surface)
|
111
|
+
target_scene.draw(dest_surface)
|
112
|
+
target_scene.do_on_enter_early()
|
113
|
+
self.get_scene_at(index).do_on_exit_early()
|
114
|
+
self.current_transitions = {"scene_name": scene_name, "transition": transition}
|
115
|
+
transition.set_start_callback(lambda: self._start_transition(target_scene))
|
116
|
+
transition.set_end_callback(lambda: self._end_transition(scene_name, index))
|
117
|
+
transition.set_source(source_surface)
|
118
|
+
transition.set_dest(dest_surface)
|
119
|
+
transition.start()
|
120
|
+
|
121
|
+
def _start_transition(self, target_scene: bf.Scene):
|
122
|
+
target_scene.set_active(True)
|
123
|
+
target_scene.set_visible(True)
|
124
|
+
self.old_player_control = bool(self.get_sharedVar("player_has_control"))
|
125
|
+
self.set_sharedVar("player_has_control", False)
|
126
|
+
|
127
|
+
def _end_transition(self, scene_name, index):
|
128
|
+
self.set_scene(scene_name, index, True)
|
129
|
+
self.current_transitions.clear()
|
130
|
+
|
131
|
+
def set_scene(self, scene_name, index=0, ignore_early: bool = False):
|
132
|
+
target_scene = self.get_scene(scene_name)
|
133
|
+
if not target_scene:
|
134
|
+
print(f"'{scene_name}' does not exist")
|
135
|
+
return
|
136
|
+
if len(self.scenes) == 0 or index >= len(self.scenes) or index < 0:
|
137
|
+
return
|
104
138
|
|
105
|
-
|
106
|
-
|
139
|
+
# switch
|
140
|
+
if not ignore_early:
|
141
|
+
self.scenes[index].do_on_exit_early()
|
142
|
+
self.scenes[index].on_exit()
|
143
|
+
# re-insert scene at index 0
|
144
|
+
self.scenes.remove(target_scene)
|
145
|
+
self.scenes.insert(index, target_scene)
|
146
|
+
_ = [s.set_scene_index(i) for i, s in enumerate(self.scenes)]
|
147
|
+
if not ignore_early:
|
148
|
+
self.scenes[index].do_on_enter_early()
|
149
|
+
target_scene.on_enter()
|
107
150
|
|
108
|
-
|
151
|
+
self.set_sharedVar("player_has_control", self.old_player_control)
|
109
152
|
|
110
|
-
#switch
|
111
|
-
old_scene.on_exit()
|
112
|
-
self.remove_scene(name)
|
113
|
-
self._scenes.insert(index,target_scene)
|
114
|
-
_ = [s.set_scene_index(i) for i,s in enumerate(self._scenes)]
|
115
|
-
target_scene.on_enter()
|
116
153
|
|
154
|
+
def cycle_debug_mode(self):
|
155
|
+
current_index = self.debug_mode.value
|
156
|
+
next_index = (current_index + 1) % len(bf.debugMode)
|
157
|
+
return bf.debugMode(next_index)
|
117
158
|
|
118
159
|
def process_event(self, event: pygame.Event):
|
119
|
-
if self.transitions:
|
120
|
-
return
|
121
160
|
keys = pygame.key.get_pressed()
|
122
161
|
if (
|
123
162
|
keys[pygame.K_LCTRL]
|
163
|
+
and keys[pygame.K_LSHIFT]
|
124
164
|
and event.type == pygame.KEYDOWN
|
125
|
-
and event.key == pygame.K_d
|
126
165
|
):
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
166
|
+
if event.key == pygame.K_d:
|
167
|
+
self.debug_mode = self.cycle_debug_mode()
|
168
|
+
self.set_sharedVar("debug_mode", self.debug_mode)
|
169
|
+
return
|
170
|
+
if event.key == pygame.K_p:
|
171
|
+
self.print_status()
|
172
|
+
return
|
173
|
+
if event.type in self.shared_events:
|
174
|
+
[s.process_event(event) for s in self.scenes]
|
175
|
+
else:
|
176
|
+
self.scenes[0].process_event(event)
|
136
177
|
|
137
178
|
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
|
147
179
|
for scene in self.active_scenes:
|
148
180
|
scene.update(dt)
|
149
181
|
self.do_update(dt)
|
150
182
|
|
151
183
|
def do_update(self, dt: float):
|
152
|
-
|
184
|
+
pass
|
153
185
|
|
154
186
|
def draw(self, surface) -> None:
|
155
|
-
|
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:
|
187
|
+
for scene in self.visible_scenes:
|
165
188
|
scene.draw(surface)
|
189
|
+
if self.current_transitions:
|
190
|
+
self._draw_transition(surface)
|
191
|
+
|
192
|
+
def _draw_transition(self, surface):
|
193
|
+
self.current_transitions["transition"].set_source(surface)
|
194
|
+
tmp = bf.const.SCREEN.copy()
|
195
|
+
self.get_scene(self.current_transitions["scene_name"]).draw(tmp)
|
196
|
+
self.current_transitions["transition"].set_dest(tmp)
|
197
|
+
self.current_transitions["transition"].draw(surface)
|
198
|
+
return
|
@@ -0,0 +1,114 @@
|
|
1
|
+
from typing import Self, Iterator
|
2
|
+
from pygame.math import Vector2
|
3
|
+
import batFramework as bf
|
4
|
+
import pygame
|
5
|
+
|
6
|
+
|
7
|
+
class ScrollingSprite(bf.Sprite):
|
8
|
+
def __init__(
|
9
|
+
self,
|
10
|
+
data: pygame.Surface | str,
|
11
|
+
size: None | tuple[int, int] = None,
|
12
|
+
convert_alpha: bool = True,
|
13
|
+
):
|
14
|
+
self.scroll_value = Vector2(0, 0)
|
15
|
+
self.auto_scroll = Vector2(0, 0)
|
16
|
+
|
17
|
+
# Use integer values for the starting points, converted from floating point scroll values
|
18
|
+
|
19
|
+
super().__init__(data, size, convert_alpha)
|
20
|
+
self.original_width, self.original_height = self.original_surface.get_size()
|
21
|
+
|
22
|
+
def get_debug_outlines(self):
|
23
|
+
yield from super().get_debug_outlines()
|
24
|
+
for r in self._get_mosaic_rect_list():
|
25
|
+
yield r.move(*self.rect.topleft)
|
26
|
+
|
27
|
+
def set_image(
|
28
|
+
self, data: pygame.Surface | str, size: None | tuple[int, int] = None
|
29
|
+
) -> Self:
|
30
|
+
super().set_image(data, size)
|
31
|
+
self.original_width, self.original_height = self.original_surface.get_size()
|
32
|
+
return self
|
33
|
+
|
34
|
+
def set_autoscroll(self, x: float, y: float) -> Self:
|
35
|
+
self.auto_scroll.update(x, y)
|
36
|
+
return self
|
37
|
+
|
38
|
+
def set_scroll(self, x: float = None, y: float = None) -> Self:
|
39
|
+
self.scroll_value.update(
|
40
|
+
x if x else self.scroll_value.x, y if y else self.scroll_value.y
|
41
|
+
)
|
42
|
+
return self
|
43
|
+
|
44
|
+
def scroll(self, x: float, y: float) -> Self:
|
45
|
+
self.scroll_value += x, y
|
46
|
+
return self
|
47
|
+
|
48
|
+
def update(self, dt: float) -> None:
|
49
|
+
if self.auto_scroll:
|
50
|
+
self.scroll(*self.auto_scroll * dt)
|
51
|
+
original_width, original_height = self.original_surface.get_size()
|
52
|
+
|
53
|
+
# Use integer values for the starting points, converted from floating point scroll values
|
54
|
+
|
55
|
+
if self.scroll_value.x > self.original_width:
|
56
|
+
self.scroll_value.x -= self.original_width
|
57
|
+
if self.scroll_value.y > self.original_height:
|
58
|
+
self.scroll_value.y -= self.original_height
|
59
|
+
|
60
|
+
super().update(dt)
|
61
|
+
|
62
|
+
def set_size(self, size: tuple[int | None, int | None]) -> Self:
|
63
|
+
size = list(size)
|
64
|
+
if size[0] is None:
|
65
|
+
size[0] = self.rect.w
|
66
|
+
if size[1] is None:
|
67
|
+
size[1] = self.rect.h
|
68
|
+
|
69
|
+
self.surface = pygame.Surface(size).convert_alpha()
|
70
|
+
self.rect = self.surface.get_frect(topleft=self.rect.topleft)
|
71
|
+
return self
|
72
|
+
|
73
|
+
def _get_mosaic_rect_list(self) -> Iterator[pygame.Rect]:
|
74
|
+
# Use integer values for the starting points, converted from floating point scroll values
|
75
|
+
start_x = int(self.scroll_value.x % self.original_width)
|
76
|
+
start_y = int(self.scroll_value.y % self.original_height)
|
77
|
+
|
78
|
+
# Adjust start_x and start_y to begin tiling off-screen to the top-left, covering all visible area
|
79
|
+
if start_x != 0:
|
80
|
+
start_x -= self.original_width
|
81
|
+
if start_y != 0:
|
82
|
+
start_y -= self.original_height
|
83
|
+
|
84
|
+
# Set the region in which to tile
|
85
|
+
end_x = self.rect.w
|
86
|
+
end_y = self.rect.h
|
87
|
+
|
88
|
+
# Starting y_position for the inner loop
|
89
|
+
y_position = start_y
|
90
|
+
|
91
|
+
# if self.rect.w-1 < self.scroll_value.x < self.rect.w+1 : print(self.scroll_value.x,int(self.scroll_value.x % self.rect.w),start_x,self.rect.w,original_width)
|
92
|
+
# Generate all necessary rectangles
|
93
|
+
x = start_x
|
94
|
+
while x < end_x:
|
95
|
+
y = y_position
|
96
|
+
while y < end_y:
|
97
|
+
yield pygame.Rect(x, y, self.original_width, self.original_height)
|
98
|
+
y += self.original_height
|
99
|
+
x += self.original_width
|
100
|
+
return self
|
101
|
+
|
102
|
+
def draw(self, camera: bf.Camera) -> None:
|
103
|
+
if not (
|
104
|
+
self.visible
|
105
|
+
and (self.surface is not None)
|
106
|
+
and camera.rect.colliderect(self.rect)
|
107
|
+
):
|
108
|
+
return
|
109
|
+
# self.surface.fill((0, 0, 0, 0))
|
110
|
+
camera.surface.fblits(
|
111
|
+
[(self.original_surface, r.move(self.rect.x-camera.rect.x,self.rect.y-camera.rect.y)) for r in self._get_mosaic_rect_list()]
|
112
|
+
)
|
113
|
+
# camera.surface.blit(self.surface, camera.world_to_screen(self.rect))
|
114
|
+
return
|
batFramework/sprite.py
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
import batFramework as bf
|
2
|
+
import pygame
|
3
|
+
from typing import Self
|
4
|
+
|
5
|
+
|
6
|
+
class Sprite(bf.Entity):
|
7
|
+
def __init__(
|
8
|
+
self,
|
9
|
+
path=None,
|
10
|
+
size: None | tuple[int, int] = None,
|
11
|
+
convert_alpha: bool = True,
|
12
|
+
):
|
13
|
+
self.original_surface: pygame.Surface = None
|
14
|
+
|
15
|
+
super().__init__(convert_alpha=convert_alpha)
|
16
|
+
if path is not None:
|
17
|
+
self.from_path(path)
|
18
|
+
if size is not None and self.original_surface:
|
19
|
+
self.set_size(self.original_surface.get_size())
|
20
|
+
|
21
|
+
def set_size(self, size: tuple[float, float]) -> Self:
|
22
|
+
if size == self.rect.size:
|
23
|
+
return self
|
24
|
+
self.rect.size = size
|
25
|
+
self.surface = pygame.Surface(
|
26
|
+
(int(self.rect.w), int(self.rect.h)), self.surface_flags
|
27
|
+
)
|
28
|
+
if self.convert_alpha:
|
29
|
+
self.surface = self.surface.convert_alpha()
|
30
|
+
self.surface.fill((0, 0, 0, 0 if self.convert_alpha else 255))
|
31
|
+
self.surface.blit(
|
32
|
+
pygame.transform.scale(self.original_surface, self.rect.size), (0, 0)
|
33
|
+
)
|
34
|
+
return self
|
35
|
+
|
36
|
+
def from_path(self, path: str) -> Self:
|
37
|
+
tmp = bf.ResourceManager().get_image(path, self.convert_alpha)
|
38
|
+
if tmp is None:
|
39
|
+
return self
|
40
|
+
self.original_surface = tmp
|
41
|
+
size = self.original_surface.get_size()
|
42
|
+
self.set_size(size)
|
43
|
+
return self
|
44
|
+
|
45
|
+
def from_surface(self, surface: pygame.Surface) -> Self:
|
46
|
+
if surface is None:
|
47
|
+
return self
|
48
|
+
self.original_surface = surface
|
49
|
+
size = self.original_surface.get_size()
|
50
|
+
self.set_size(size)
|
51
|
+
return self
|
batFramework/stateMachine.py
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
import batFramework as bf
|
2
2
|
|
3
3
|
|
4
|
-
class StateMachine:
|
5
|
-
...
|
4
|
+
class StateMachine: ...
|
6
5
|
|
7
6
|
|
8
7
|
class State:
|
@@ -44,6 +43,7 @@ class StateMachine:
|
|
44
43
|
self.current_state.on_exit()
|
45
44
|
self.current_state = self.states[state_name]
|
46
45
|
self.current_state.on_enter()
|
46
|
+
|
47
47
|
def get_current_state(self) -> State:
|
48
48
|
return self.current_state
|
49
49
|
|
batFramework/tileset.py
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
import pygame
|
2
|
+
from .resourceManager import ResourceManager
|
3
|
+
import batFramework as bf
|
4
|
+
|
5
|
+
|
6
|
+
class Tileset:
|
7
|
+
def __init__(self, source: pygame.Surface, tilesize: tuple[int, int]) -> None:
|
8
|
+
self.surface = source
|
9
|
+
self.tile_size = tilesize
|
10
|
+
self.tile_width = source.get_width() // tilesize[0]
|
11
|
+
self.tile_height = source.get_height() // tilesize[1]
|
12
|
+
# Create flipped versions of the tileset surface
|
13
|
+
self.flipped_surfaces = {
|
14
|
+
(False, False): self.surface,
|
15
|
+
(False, True): pygame.transform.flip(self.surface, False, True),
|
16
|
+
(True, False): pygame.transform.flip(self.surface, True, False),
|
17
|
+
(True, True): pygame.transform.flip(self.surface, True, True),
|
18
|
+
}
|
19
|
+
|
20
|
+
# Split each of the surfaces into tiles
|
21
|
+
self.tile_dict = {}
|
22
|
+
for flip_state, surf in self.flipped_surfaces.items():
|
23
|
+
tiles = bf.utils.split_surface(surf, self.tile_size)
|
24
|
+
for coord, tile in tiles.items():
|
25
|
+
if coord not in self.tile_dict:
|
26
|
+
self.tile_dict[coord] = {}
|
27
|
+
self.tile_dict[coord][flip_state] = tile
|
28
|
+
|
29
|
+
def __str__(self) -> str:
|
30
|
+
num_tiles = 0
|
31
|
+
if self.tile_dict:
|
32
|
+
num_tiles = len(self.tile_dict.values())
|
33
|
+
return f"{num_tiles} tiles | Tile size : {self.tile_size}"
|
34
|
+
|
35
|
+
def get_tile(
|
36
|
+
self, x: int, y: int, flipX=False, flipY=False
|
37
|
+
) -> pygame.Surface | None:
|
38
|
+
if flipX:
|
39
|
+
x = self.tile_width - 1 - x
|
40
|
+
if flipY:
|
41
|
+
y = self.tile_height - 1 - y
|
42
|
+
tile_data = self.tile_dict.get((x, y), None)
|
43
|
+
if tile_data is None:
|
44
|
+
return None
|
45
|
+
|
46
|
+
return tile_data.get((flipX, flipY), None)
|