-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
148 lines (113 loc) · 4.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from BTP.BTP import *
import BTP.BTP
import os
import sys
from core import ComponentObject, TextureAtlas, ObjectBaseAtlas
from components import *
from utility import DungeonScreens, DungeonActionTypes, TILE_SIZE
from screens import Menu, Game, MapCreator, Loading
class Dungeon(Win):
ASSETS_DIR = "./assets/"
def __init__(self) -> None:
super().__init__()
print(BTP.BTP.__doc__)
self.texture_atlas = TextureAtlas()
self.objects_atlas = ObjectBaseAtlas()
self.object_base: list[ComponentObject] = [
Character,
Doors,
Floor,
Wall,
Coin,
Chest,
SingleItem,
Flask,
Weapon,
Hearts
]
self.loading = Loading(self, self.objects_atlas)
self.menu = Menu(self, self.objects_atlas)
self.game = Game(self, self.objects_atlas)
self.map_creator = MapCreator(self, self.objects_atlas)
self.state = DungeonScreens.MENU
self.no_assets = False
def on_ready(self) -> None:
if self.no_assets:
return
for obj in self.objects_atlas.objects:
if isinstance(obj, ComponentObject):
obj.on_ready(self)
obj.accepted_actions = DungeonActionTypes.all()
self.camera_follow_rect(
Vec(),
Vec(TILE_SIZE),
0.0, 0.0, 0.0
)
self.map_creator.on_ready()
self.game.on_ready()
def on_close(self) -> None:
pass
def on_draw_background(self, dt: float) -> None:
pass
def on_draw(self, dt: float) -> None:
match self.state:
case DungeonScreens.MAP_CREATOR:
self.map_creator.on_draw(dt)
case DungeonScreens.GAME:
self.game.on_draw(dt)
def on_draw_ui(self, dt: float) -> None:
if self.is_loading():
return
if self.no_assets:
self.loading.on_draw_error("Assets not found")
return
match self.state:
case DungeonScreens.MENU:
self.state = self.menu.on_draw_ui(dt).name
if self.state == DungeonScreens.MAP_CREATOR:
self.game.close_game()
self.map_creator.clear_map()
self.map_creator.load_map(self.menu.get_selected_map())
self.map_creator.start_update_thread()
self.map_creator.force_update_view()
elif self.state == DungeonScreens.GAME:
self.map_creator.stop_update_thread()
self.game.new_game(self.menu.get_selected_map())
case DungeonScreens.MAP_CREATOR:
self.state = self.map_creator.on_draw_ui(dt).name
if self.state == DungeonScreens.MENU:
self.menu.reset_input(self.menu.get_first_map())
case DungeonScreens.GAME:
self.state = self.game.on_draw_ui(dt).name
if self.state == DungeonScreens.MENU:
self.menu.reset_input(self.menu.get_first_map())
def on_draw_loading(self, dt: float) -> None:
self.loading.on_draw(dt)
def on_load(self) -> None:
try:
if not os.path.exists(Dungeon.ASSETS_DIR):
self.no_assets = True
return
files = os.listdir(Dungeon.ASSETS_DIR)
for filename in files:
texture_path = os.path.abspath(Dungeon.ASSETS_DIR + filename)
texture_id = self.load_image(texture_path)
self.texture_atlas.add(filename, texture_id)
for type in self.object_base:
self.objects_atlas.register(type)
for texture in self.texture_atlas.textures:
self.objects_atlas.add(texture)
self.menu.on_load()
except Exception as e:
print(e)
# time.sleep(3)
def main(args):
size = (0,0)
fullscreen = True
if "--debug" in args:
size = (1280,960)
fullscreen = False
Dungeon().start(*size, "Dungeon - BTP v{} | {}".format(BTP.BTP.__version__,BTP.BTP.__libvers__), fullscreen)
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))