forked from ClanGenOfficial/clangen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·285 lines (228 loc) · 8.98 KB
/
Copy pathmain.py
File metadata and controls
executable file
·285 lines (228 loc) · 8.98 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# ==== DO NOT MOVE THIS IMPORT!
# ==== DO NOT ADD ANYTHING BEFORE THIS IMPORT!
import init # isort: skip
# Load game
import logging
import threading
import pygame
import scripts.game_structure.screen_settings
from scripts.cat.sprites.load_sprites import sprites
from scripts.clan import Afterlife, clan_class
from scripts.debug_console import debug_mode
from scripts.game_input import INPUT_ACTION_PRESSED
from scripts.game_structure import constants, game
from scripts.game_structure.audio.audio_manager import AudioManager
from scripts.game_structure.discord_rpc import _DiscordRPC
from scripts.game_structure.game.save_load import read_clans
from scripts.game_structure.game.settings import game_setting_get
from scripts.game_structure.game.switches import (
Switch,
switch_get_value,
switch_set_value,
)
from scripts.game_structure.load_cat import load_cats, version_convert
from scripts.game_structure.screen_settings import MANAGER, screen, screen_scale
from scripts.game_input import controller_manager, keyboard_manager
# import all screens for initialization (Note - must be done after pygame_gui manager is created)
from scripts.screens import all_screens
from scripts.screens.enums import GameScreen
from scripts.ui.windows.save_check import SaveCheckWindow
from scripts.housekeeping.quit_game import quit_game
# P Y G A M E
clock = pygame.time.Clock()
pygame.display.set_icon(pygame.image.load("resources/images/icon.png"))
game.rpc = _DiscordRPC("1076277970060185701", daemon=True)
game.rpc.start()
game.rpc.start_rpc.set()
# LOAD cats & clan
finished_loading = False
controller_manager.init()
def load_data():
global finished_loading
# load audio
try:
if not getattr(game, "audio", None):
game.audio = AudioManager()
pygame.mixer.pre_init(buffer=44100)
pygame.mixer.init()
# loading sounds here bc they depend on mixer being initialized
game.audio.sound.load_sounds()
except pygame.error:
print("Failed to initialize audio. Audio will be disabled.")
game.audio.disabled = True
game.audio.muted = True
# load in the spritesheets
sprites.load_all()
clan_list = read_clans()
if clan_list:
switch_set_value(Switch.clan_list, clan_list)
switch_set_value(Switch.clan_save_id, clan_list[0])
try:
game.starclan = Afterlife()
game.dark_forest = Afterlife()
load_cats()
version_info = clan_class.load_clan()
version_convert(version_info)
game.load_events()
except Exception as e:
logging.exception("File failed to load")
if not switch_get_value(Switch.error_message):
switch_set_value(
Switch.error_message, "There was an error loading the cats file!"
)
switch_set_value(Switch.traceback, e)
scripts.screens.screens_core.screens_core.rebuild_core()
finished_loading = True
images = []
def loading_animation(scale: float = 1):
# Load images, adjust color
color = pygame.Surface((200 * scale, 210 * scale))
if game_setting_get("dark mode"):
color.fill(constants.CONFIG["theme"]["light_mode_background"])
else:
color.fill(constants.CONFIG["theme"]["dark_mode_background"])
if len(images) == 0:
for i in range(1, 11):
im = pygame.transform.scale_by(
pygame.image.load(f"resources/images/loading_animate/startup/{i}.png"),
screen_scale,
)
im.blit(color, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
images.append(im)
del im
# Cleanup
del color
x = screen.get_width() / 2
y = screen.get_height() / 2
i = 0
total_frames = len(images)
while not finished_loading:
clock.tick(8) # Loading screen is 8FPS
if game_setting_get("dark mode"):
screen.fill(constants.CONFIG["theme"]["dark_mode_background"])
else:
screen.fill(constants.CONFIG["theme"]["light_mode_background"])
screen.blit(
images[i], (x - images[i].get_width() / 2, y - images[i].get_height() / 2)
)
i += 1
if i >= total_frames:
i = 0
for event in pygame.event.get():
controller_manager.process_event(event)
if event.type == pygame.QUIT:
quit_game(savesettings=False)
pygame.display.update()
def load_game():
"""
Performs the functions needed to load the game.
This function is ran when the game loads and whenever the player
switches clans.
"""
global finished_loading
game.cur_events_list.clear()
game.patrol_cats.clear()
game.patrolled.clear()
game.updated_afterlife_cats.clear()
game.clan = None
game.starclan = None
game.dark_forest = None
switch_set_value(Switch.switch_clan, False)
finished_loading = False
loading_thread = threading.Thread(target=load_data)
loading_thread.start()
loading_animation(screen_scale)
# loading thread should be done by now, so just join it for safety.
loading_thread.join()
del loading_thread
load_game()
all_screens.get_screen(GameScreen.START).screen_switches()
# dev screen info now lives in scripts/screens/screens_core
fps = switch_get_value(Switch.fps)
if game_setting_get("custom cursor"):
MANAGER.set_active_cursor(constants.CUSTOM_CURSOR)
else:
MANAGER.set_active_cursor(constants.DEFAULT_CURSOR)
while 1:
time_delta = clock.tick(fps) / 1000.0
if switch_get_value(Switch.switch_clan):
load_game()
# have to manually reload errors because it only happens when screen is switched to
game.all_screens[GameScreen.START].reload_errors()
# Draw screens
# This occurs before events are handled to stop pygame_gui buttons from blinking.
game.all_screens[game.current_screen].on_use()
# EVENTS
for event in pygame.event.get():
if event.type == INPUT_ACTION_PRESSED and debug_mode.debug_menu.visible:
pass
else:
consumed = MANAGER.process_events(event)
# todo ...shouldn't this be `get_switch(Switch.cur_screen)`?
if not consumed:
all_screens.get_screen(
game.current_screen.replace(" ", "_")
).handle_event(event)
if not game.audio.disabled and not game.audio.muted:
game.audio.sound.handle_sound_events(event)
if event.type == pygame.QUIT:
# Don't display if on the start screen or there is no clan.
if (
switch_get_value(Switch.cur_screen)
in (
GameScreen.START,
GameScreen.SWITCH_CLAN,
GameScreen.SETTINGS,
GameScreen.MAKE_CLAN_CHOOSE_MODE,
GameScreen.MAKE_CLAN_CHOOSE_CARDS,
GameScreen.MAKE_CLAN_CHOOSE_NAME,
GameScreen.MAKE_CLAN_CHOOSE_CATS,
GameScreen.MAKE_CLAN_CHOOSE_SYMBOL,
GameScreen.MAKE_CLAN_CLAN_CREATED,
)
or not game.clan
):
quit_game(savesettings=False)
else:
SaveCheckWindow(switch_get_value(Switch.cur_screen), False, None)
# MOUSE CLICK
if event.type == pygame.MOUSEBUTTONDOWN:
game.clicked = True
if MANAGER.visual_debug_active:
_ = pygame.mouse.get_pos()
if game_setting_get("fullscreen"):
print(f"(x: {_[0]}, y: {_[1]})")
else:
print(f"(x: {_[0] * screen_scale}, y: {_[1] * screen_scale})")
del _
# F2 turns toggles visual debug mode for pygame_gui, allowed for easier bug fixes.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_F2:
MANAGER.print_layer_debug()
elif event.key == pygame.K_F3:
debug_mode.toggle_debug_mode()
elif event.key == pygame.K_F11:
scripts.game_structure.screen_settings.toggle_fullscreen(
source_screen=all_screens.screen_dict[
switch_get_value(Switch.cur_screen).replace(" ", "_")
],
show_confirm_dialog=False,
)
controller_manager.process_event(event)
keyboard_manager.process_event(event)
MANAGER.update(time_delta)
# update
game.update_game()
if game.switch_screens:
all_screens.get_screen(
game.last_screen_forupdate.replace(" ", "_")
).exit_screen()
all_screens.get_screen(game.current_screen.replace(" ", "_")).screen_switches()
game.switch_screens = False
debug_mode.pre_update(clock)
# END FRAME
MANAGER.draw_ui(screen)
debug_mode.post_update(screen)
pygame.display.update()
if not game.audio.disabled and not game.audio.muted:
game.audio.start()