-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.py
More file actions
640 lines (514 loc) · 24.3 KB
/
resources.py
File metadata and controls
640 lines (514 loc) · 24.3 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
import pygame
import settings
from expertise import Expertise
from error_codes import Error_codes
class __CharactersCollection:
""" Collection de personnages utilisée par l'objet global characters_collection (voir plus bas). """
def __init__(self) -> None:
self.__surfaces = None
def init(self) -> Error_codes:
"""
Initialise l'instance unique de resources.characters_collection.
La méthode init() permet d'éviter de ralentir l'importation du module avec des entrées/sorties. Elle permet
aussi de diminuer l'impact d'importations multiples et de gérer les erreurs à un seul endroit, une fois les
importations terminées.
:return: le code de succes si l'initialisation s'est bien passée, le code d'erreur sinon
"""
# charge l'image contenant tous les personnages
try:
characters_sheet = pygame.image.load(
settings.CHARACTERS_FILENAME).convert_alpha()
except:
return Error_codes.IMG_CHAR
if not characters_sheet:
return Error_codes.IMG_CHAR
# découpe la surface de personnages en surfaces individuelles (une pour chaque personnage)
height = characters_sheet.get_height()
width = characters_sheet.get_width() / settings.NB_CHARACTERS
self.__surfaces = []
for i in range(settings.NB_CHARACTERS):
character_surface = pygame.Surface(
(width, height), pygame.SRCALPHA)
source_area = pygame.Rect(i * width, 0, width, height)
character_surface.blit(characters_sheet, (0, 0), source_area)
self.__surfaces.append(character_surface)
return Error_codes.SUCCES
def get(self, character_id: int) -> pygame.Surface or None:
"""
Retourne la surface correspondant à l'identifiant de personnage (character_id).
:param character_id: identifiant de personnage
:return: la surface si disponible, None sinon
"""
assert self.__surfaces
if 0 <= character_id < settings.NB_CHARACTERS:
return self.__surfaces[character_id]
return None
class __CharactersIconCollection:
"""
Collection d'icônes pour les personnages utilisée par les fleches de directions
(voir plus bas).
"""
def __init__(self) -> None:
self.__incident_surfaces = None
def init(self) -> Error_codes:
"""
Initialise l'instance unique de resources.characters_icons_collection.
La méthode init() permet d'éviter de ralentir l'importation du module avec des entrées/sorties. Elle permet
aussi de diminuer l'impact d'importations multiples et de gérer les erreurs à un seul endroit, une fois les
importations terminées.
:return: le code de succes si l'initialisation s'est bien passée, le code d'erreur sinon
"""
# charge l'image contenant tout les icones
try:
icons_sheet = pygame.image.load(
settings.CHARACTERS_ICON_FILENAME).convert_alpha()
except:
return Error_codes.IMG_ICONES_CHAR
if not icons_sheet:
return Error_codes.IMG_ICONES_CHAR
# s'assure que les icones soient carrés
if icons_sheet.get_width() % icons_sheet.get_height() != 0:
return Error_codes.SQUARES_ICONES_CHAR
# découpe la surface d'icones en surfaces individuelles (une pour chaque icone)
height = width = icons_sheet.get_height()
self.__surfaces = []
for i in range(icons_sheet.get_width() // width):
icon_surface = pygame.Surface((width, height), pygame.SRCALPHA)
source_area = pygame.Rect(i * width, 0, width, height)
icon_surface.blit(icons_sheet, (0, 0), source_area)
self.__surfaces.append(icon_surface)
return Error_codes.SUCCES
def get(self, icon_id: int) -> pygame.Surface or None:
"""
Retourne la surface correspondant à l'icone (icon_id) pour un incident d'une
expertise donnée (expertise).
:param icon_id: identifiant de l'icone
:return: la surface si disponible, None sinon
"""
assert self.__surfaces
if 0 <= icon_id < len(self.__surfaces):
return self.__surfaces[icon_id]
return None
class __ProgressBarCollection:
"Collection de barres pour la progression du travail du personnage utilisée par l'objet global progress_bar_collection"
def __init__(self) -> None:
self.__surfaces = None
def init(self) -> Error_codes:
"""
Initialise l'instance unique de resources.progress_bar_collection.
La méthode init() permet d'éviter de ralentir l'importation du module avec des entrées/sorties. Elle permet
aussi de diminuer l'impact d'importations multiples et de gérer les erreurs à un seul endroit, une fois les
importations terminées.
:return: le code de succes si l'initialisation s'est bien passée, le code d'erreur sinon
"""
# charge l'image contenant toutes les barres
try:
progress_bar_sheet = pygame.image.load(
settings.PROGRESS_BAR_FILENAME).convert_alpha()
except:
return Error_codes.IMG_PROGRESS_BAR
if not progress_bar_sheet:
return Error_codes.IMG_PROGRESS_BAR
# s'assure que les surfaces des barres soient carrées
if progress_bar_sheet.get_width() % progress_bar_sheet.get_height() != 0:
return Error_codes.SQUARES_PROGRESS_BAR
# découpe la surface de tuiles en surfaces individuelles (une pour chaque tuile)
height = width = progress_bar_sheet.get_height()
self.__surfaces = []
for i in range(progress_bar_sheet.get_width() // width):
progress_bar_surface = pygame.Surface(
(width, height), pygame.SRCALPHA)
source_area = pygame.Rect(i * width, 0, width, height)
progress_bar_surface.blit(progress_bar_sheet, (0, 0), source_area)
self.__surfaces.append(progress_bar_surface)
return Error_codes.SUCCES
def get(self, bar_id: int) -> pygame.Surface or None:
"""
Retourne la surface correspondant à l'identifiant de barre (bar_id).
:param bar_id: identifiant de barre
:return: la surface si disponible, None sinon
"""
assert self.__surfaces
if 0 <= bar_id < len(self.__surfaces):
return self.__surfaces[bar_id]
return None
class __TilesCollection:
""" Collection de tuiles utilisée par l'objet global tiles_collection (voir plus bas). """
def __init__(self) -> None:
self.__surfaces = None
def init(self) -> Error_codes:
"""
Initialise l'instance unique de resources.tiles_collection.
La méthode init() permet d'éviter de ralentir l'importation du module avec des entrées/sorties. Elle permet
aussi de diminuer l'impact d'importations multiples et de gérer les erreurs à un seul endroit, une fois les
importations terminées.
:return: le code de succes si l'initialisation s'est bien passée, le code d'erreur sinon
"""
# charge l'image contenant toutes les tuiles
try:
tiles_sheet = pygame.image.load(settings.TILES_FILENAME).convert()
except:
return Error_codes.IMG_TILES
if not tiles_sheet:
return Error_codes.IMG_TILES
# s'assure que les tuiles soient carrées
if tiles_sheet.get_width() % tiles_sheet.get_height() != 0:
return Error_codes.SQUARES_TILES
# découpe la surface de tuiles en surfaces individuelles (une pour chaque tuile)
height = width = tiles_sheet.get_height()
self.__surfaces = []
for i in range(tiles_sheet.get_width() // width):
tile_surface = pygame.Surface((width, height))
source_area = pygame.Rect(i * width, 0, width, height)
tile_surface.blit(tiles_sheet, (0, 0), source_area)
self.__surfaces.append(tile_surface)
return Error_codes.SUCCES
def get(self, tile_id: int) -> pygame.Surface or None:
"""
Retourne la surface correspondant à l'identifiant de tuile (tile_id).
:param tile_id: identifiant de tuile
:return: la surface si disponible, None sinon
"""
assert self.__surfaces
if 0 <= tile_id < len(self.__surfaces):
return self.__surfaces[tile_id]
return None
def tile_size(self) -> int:
"""
Retourne les dimensions d'une image de tuile.
:return: Dimensions d'une tuile (largeur, hauteur)
"""
assert self.__surfaces and len(self.__surfaces) > 0
return self.__surfaces[0].get_size()
def pixel_pos_to_tile_pos(self, pixel_position: tuple) -> tuple:
"""
Convertit une coordonnée de pixels en coordonnée de tuile
:param pixel_position: Position (coordonnée) en pixels
:return: Position (coordonnée) de la tuile
"""
assert self.__surfaces and len(self.__surfaces) > 0
pixel_x, pixel_y = pixel_position
tile_x = pixel_x % self.__surfaces[0].get_width()
tile_y = pixel_y % self.__surfaces[0].get_height()
return tile_x, tile_y
def tile_pos_to_pixel_pos(self, tile_position: tuple) -> tuple:
"""
Convertit une coordonnée de tuile en coordonnée de pixels
:param tile_position: Position (coordonnée) de la tuile
:return: Position (coordonnée) en pixels
"""
assert self.__surfaces and len(self.__surfaces) > 0
tile_x, tile_y = tile_position
pixel_x = tile_x * self.__surfaces[0].get_width()
pixel_y = tile_y * self.__surfaces[0].get_height()
return pixel_x, pixel_y
def tile_pos_to_center_pixel_pos(self, tile_position: tuple) -> tuple:
"""
Convertit une coordonnée de tuile en coordonnée de pixels pour le centre de la tuile
:param tile_position: Position (coordonnée) de la tuile
:return: Position (coordonnée) du centre de la tuile en pixels
"""
assert self.__surfaces and len(self.__surfaces) > 0
pixel_x, pixel_y = self.tile_pos_to_pixel_pos(tile_position)
center_x = pixel_x + (self.__surfaces[0].get_width() / 2)
center_y = pixel_y + (self.__surfaces[0].get_height() / 2)
return center_x, center_y
class __AssetsCollection:
""" Collection d'actifs (assets) utilisée par l'objet global assets_collection (voir plus bas). """
def __init__(self) -> None:
self.__surfaces = None
def init(self) -> Error_codes:
"""
Initialise l'instance unique de resources.assets_collection.
La méthode init() permet d'éviter de ralentir l'importation du module avec des entrées/sorties. Elle permet
aussi de diminuer l'impact d'importations multiples et de gérer les erreurs à un seul endroit, une fois les
importations terminées.
:return: le code de succes si l'initialisation s'est bien passée, le code d'erreur sinon
"""
# charge l'image contenant tous les actifs
try:
assets_sheet = pygame.image.load(
settings.ASSETS_FILENAME).convert_alpha()
except:
return Error_codes.IMG_ASSETS
if not assets_sheet:
return Error_codes.IMG_ASSETS
# s'assure que les actifs soient carrés
if assets_sheet.get_width() % assets_sheet.get_height() != 0:
return Error_codes.SQUARES_ASSETS
# découpe la surface d'actifs en surfaces individuelles (une pour chaque actif)
height = width = assets_sheet.get_height()
self.__surfaces = []
for i in range(assets_sheet.get_width() // width):
asset_surface = pygame.Surface((width, height), pygame.SRCALPHA)
source_area = pygame.Rect(i * width, 0, width, height)
asset_surface.blit(assets_sheet, (0, 0), source_area)
self.__surfaces.append(asset_surface)
return Error_codes.SUCCES
def get(self, asset_id: int) -> pygame.Surface or None:
"""
Retourne la surface correspondant à l'identifiant de l'actif (asset_id).
:param asset_id: identifiant d'actif
:return: la surface si disponible, None sinon
"""
assert self.__surfaces
if 0 <= asset_id < len(self.__surfaces):
return self.__surfaces[asset_id]
return None
class __Arrow:
""" image utilisée par l'objet global arrow (voir plus bas). """
def __init__(self) -> None:
self.__surfaces = None
def init(self) -> Error_codes:
"""
Initialise l'instance unique de resources.arrow.
La méthode init() permet d'éviter de ralentir l'importation du module avec des entrées/sorties. Elle permet
aussi de diminuer l'impact d'importations multiples et de gérer les erreurs à un seul endroit, une fois les
importations terminées.
:return: le code de succes si l'initialisation s'est bien passée, le code d'erreur sinon
"""
# charge l'image contenant la fleche
try:
arrow_sheet = pygame.image.load(
settings.ARROW_FILENAME).convert_alpha()
except:
return Error_codes.IMG_ARROW
if not arrow_sheet:
return Error_codes.IMG_ARROW
height = width = arrow_sheet.get_height()
asset_surface = pygame.Surface((width, height), pygame.SRCALPHA)
asset_surface.blit(arrow_sheet, (0, 0),)
self.__surface = asset_surface
return Error_codes.SUCCES
def get(self) -> pygame.Surface:
"""
Retourne la surface de la fleche
:return: la surface
"""
assert self.__surface
return self.__surface
class __IncidentsCollection:
"""
Collection de minuteries et d'icônes pour les incidents utilisée par l'objet global incidents_collection
(voir plus bas).
"""
def __init__(self) -> None:
self.__incident_surfaces = None
def init(self) -> Error_codes:
"""
Initialise l'instance unique de resources.incidents_collection.
La méthode init() permet d'éviter de ralentir l'importation du module avec des entrées/sorties. Elle permet
aussi de diminuer l'impact d'importations multiples et de gérer les erreurs à un seul endroit, une fois les
importations terminées.
:return: le code de succes si l'initialisation s'est bien passée, le code d'erreur sinon
"""
# Chargement de l'image contenant toutes images reliées aux incidents:
# 17 images de minuterie (pleine à terminée) et 7 images pour les icônes
try:
incidents_sheet = pygame.image.load(
settings.INCIDENTS_FILENAME).convert_alpha()
except:
return Error_codes.IMG_INCIDENTS
if not incidents_sheet:
return Error_codes.IMG_INCIDENTS
# Vérifications que les images soient carrées
if incidents_sheet.get_width() % incidents_sheet.get_height() != 0:
return Error_codes.SQUARES_INCIDENTS
# Découpage de la surface chargée en surfaces individuelles (une pour chaque image de minuterie)
height = width = incidents_sheet.get_height()
timer_surfaces = []
for i in range(settings.NB_INCIDENT_TIMER_IMAGES):
timer_surface = pygame.Surface((width, height), pygame.SRCALPHA)
source_area = pygame.Rect(i * width, 0, width, height)
timer_surface.blit(incidents_sheet, (0, 0), source_area)
timer_surfaces.append(timer_surface)
# Découpage de la surface chargée en surfaces individuelles (une pour chaque icône d'incident)
icons_surfaces = []
for i in range(settings.NB_INCIDENT_TIMER_IMAGES, settings.NB_INCIDENT_TIMER_IMAGES + settings.NB_SKILLS):
icon_surface = pygame.Surface((width, height), pygame.SRCALPHA)
source_area = pygame.Rect(i * width, 0, width, height)
icon_surface.blit(incidents_sheet, (0, 0), source_area)
icons_surfaces.append(icon_surface)
# Construction des surfaces combinées (minuterie + icône)
self.__incident_surfaces = []
for skill_index in range(settings.NB_SKILLS):
series = []
for timer_index in range(len(timer_surfaces)):
combined_surface = timer_surfaces[timer_index].copy()
combined_surface.blit(icons_surfaces[skill_index], (0, 0))
series.append(combined_surface)
self.__incident_surfaces.append(series)
return Error_codes.SUCCES
def get(self, timer_id: int, expertise: Expertise) -> pygame.Surface or None:
"""
Retourne la surface correspondant à la minuterie (timer_id) pour un incident d'une
expertise donnée (expertise).
:param timer_id: identifiant de l'image de minuterie
:param expertise: expertise sollicitée par l'incident
:return: la surface si disponible, None sinon
"""
assert self.__incident_surfaces
expertise_id = int(expertise)
if 0 <= timer_id < len(self.__incident_surfaces[expertise_id]):
return self.__incident_surfaces[expertise_id][timer_id]
return None
class __SoundsCollection:
""" Collection de sons utilisée par l'objet global sounds_collection (voir plus bas). """
def __init__(self) -> None:
self.__surfaces = None
self.__sounds = None
def init(self) -> Error_codes:
"""
Initialise l'instance unique de resources.sounds_collection.
La méthode init() permet d'éviter de ralentir l'importation du module avec des entrées/sorties. Elle permet
aussi de diminuer l'impact d'importations multiples et de gérer les erreurs à un seul endroit, une fois les
importations terminées.
:return: le code de succes si l'initialisation s'est bien passée, le code d'erreur sinon
"""
self.__sounds = {}
try:
sound = pygame.mixer.Sound(settings.PHONE_RING_SOUND_FILENAME)
except:
return Error_codes.SOUND_PHONE
if not sound:
return Error_codes.SOUND_PHONE
self.__sounds['HELPDESK-PHONE-RING'] = sound
try:
sound = pygame.mixer.Sound(settings.PHONE_HANGUP_SOUND_FILENAME)
except:
return Error_codes.SOUND_HANGUP
if not sound:
return Error_codes.SOUND_HANGUP
self.__sounds['HELPDESK-PHONE-HANGUP'] = sound
try:
sound = pygame.mixer.Sound(settings.SOLVE_SOUND_FILENAME)
except:
return Error_codes.SOUND_SOLVE
if not sound:
return Error_codes.SOUND_SOLVE
self.__sounds['INCIDENT-SOLVE'] = sound
try:
sound = pygame.mixer.Sound(settings.FAILURE_SOUND_FILENAME)
except:
return Error_codes.SOUND_FAIL
if not sound:
return Error_codes.SOUND_FAIL
self.__sounds['INCIDENT-FAIL'] = sound
try:
sound = pygame.mixer.Sound(settings.OFFICE_AMBIENCE_SOUND)
except:
return Error_codes.SOUND_AMBIENCE
if not sound:
return Error_codes.SOUND_AMBIENCE
sound.set_volume(0.10)
self.__sounds['OFFICE-AMBIENCE'] = sound
try:
sound = pygame.mixer.Sound(settings.BACKGROUND_MUSIC)
except:
return Error_codes.SOUND_MUSIC
if not sound:
return Error_codes.SOUND_MUSIC
sound.set_volume(0.25)
self.__sounds['BACKGROUND-MUSIC'] = sound
try:
sound = pygame.mixer.Sound(settings.SQUEAKY_TILE_SOUND_FILENAME)
except:
return Error_codes.SOUND_SQUEAK
if not sound:
return Error_codes.SOUND_SQUEAK
sound.set_volume(0.3)
self.__sounds['SQUEAKY_TOY_SOUND'] = sound
# Chargement son alarme 25%
try:
sound = pygame.mixer.Sound(settings.PERCENT_25_ALERT_FILENAME)
except:
return Error_codes.SOUND_25_LEFT
if not sound:
return Error_codes.SOUND_25_LEFT
self.__sounds['PERCENT_25_ALERT'] = sound
# Chargement son alarme 10%
try:
sound = pygame.mixer.Sound(settings.PERCENT_10_ALERT_FILENAME)
except:
return Error_codes.SOUND_10_LEFT
if not sound:
return Error_codes.SOUND_10_LEFT
self.__sounds['PERCENT_10_ALERT'] = sound
# Renvoi des codes d'erreur
return Error_codes.SUCCES
def get(self, name: str) -> pygame.mixer.Sound or None:
"""
Retourne le son correspondant au nom spécifié (name).
:param name: nom du son
:return: le son si disponible, None sinon
"""
assert self.__sounds
return self.__sounds.get(name, None)
# collection de personnages (singleton du GoF implémenté avec un Global Object Pattern de python)
characters_collection = None
# collection d'icones de personnages (singleton du GoF implémenté avec un Global Object Pattern de python)
characters_icons_collection = None
# collection d'de barres de progression (singleton du GoF implémenté avec un Global Object Pattern de python)
progress_bar_collection = None
# collection de tuiles (singleton du GoF implémenté avec un Global Object Pattern de python)
tiles_collection = None
# collection d'actifs (assets) (singleton du GoF implémenté avec un Global Object Pattern de python)
assets_collection = None
# collection d'images de minuterie et d'icônes pour les incidents
# (singleton du GoF implémenté avec un Global Object Pattern de python)
incidents_collection = None
# collection de sons (singleton du GoF implémenté avec un Global Object Pattern de python)
sounds_collection = None
# Image de fleche
arrow = None
def init() -> Error_codes:
""" Initialise l'ensemble des ressources. """
global characters_collection
if not characters_collection:
characters_collection = __CharactersCollection()
return_code = characters_collection.init()
if return_code != Error_codes.SUCCES:
return return_code
global characters_icons_collection
if not characters_icons_collection:
characters_icons_collection = __CharactersIconCollection()
return_code = characters_icons_collection.init()
if return_code != Error_codes.SUCCES:
return return_code
global progress_bar_collection
if not progress_bar_collection:
progress_bar_collection = __ProgressBarCollection()
return_code = progress_bar_collection.init()
if return_code != Error_codes.SUCCES:
return return_code
global tiles_collection
if not tiles_collection:
tiles_collection = __TilesCollection()
return_code = tiles_collection.init()
if return_code != Error_codes.SUCCES:
return return_code
global assets_collection
if not assets_collection:
assets_collection = __AssetsCollection()
return_code = assets_collection.init()
if return_code != Error_codes.SUCCES:
return return_code
global incidents_collection
if not incidents_collection:
incidents_collection = __IncidentsCollection()
return_code = incidents_collection.init()
if return_code != Error_codes.SUCCES:
return return_code
global sounds_collection
if not sounds_collection:
sounds_collection = __SoundsCollection()
return_code = sounds_collection.init()
if return_code != Error_codes.SUCCES:
return return_code
global arrow
if not arrow:
arrow = __Arrow()
return_code = arrow.init()
if return_code != Error_codes.SUCCES:
return return_code
return Error_codes.SUCCES