-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpac_animation_builder.py
More file actions
216 lines (164 loc) · 8.18 KB
/
pac_animation_builder.py
File metadata and controls
216 lines (164 loc) · 8.18 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
#!/usr/bin/env python3
"""
Système d'animation pour les Pacs basé sur les spritesheets blue.json et red.json
Ce script analyse les fichiers JSON des spritesheets et définit les animations
pour les différents types de pacs et états (vivant, mort, collision).
"""
import json
import os
import re
from typing import Dict, List, Tuple
class PacAnimationSystem:
"""Système de gestion des animations des Pacs"""
def __init__(self):
self.sprite_data = {}
self.animations = {}
def load_spritesheet(self, json_path: str, color: str):
"""Charge une spritesheet depuis un fichier JSON"""
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
self.sprite_data[color] = data
print(f"Chargé spritesheet {color}: {len(data['frames'])} frames")
def parse_frame_name(self, frame_name: str) -> Tuple[str, int, str, int]:
"""
Parse un nom de frame selon les patterns:
- mort_[color]_[type]_mort000[1-7]
- paku_[color]_[type]_walk000[1-4]
Returns: (color, type, animation_type, frame_number)
"""
# Pattern pour frames de mort: mort_blue_2_mort0001
mort_pattern = r'mort_(blue|red)_(\d+)_mort000(\d+)'
mort_match = re.match(mort_pattern, frame_name)
if mort_match:
color, pac_type, frame_num = mort_match.groups()
return color, int(pac_type), 'mort', int(frame_num)
# Pattern pour frames de marche: paku_blue_1_walk0001
walk_pattern = r'paku_(blue|red)_(\d+)_walk000(\d+)'
walk_match = re.match(walk_pattern, frame_name)
if walk_match:
color, pac_type, frame_num = walk_match.groups()
return color, int(pac_type), 'walk', int(frame_num)
return None, None, None, None
def build_animations(self):
"""Construit les définitions d'animation pour tous les types de pacs"""
for color in ['blue', 'red']:
if color not in self.sprite_data:
continue
frames = self.sprite_data[color]['frames']
# Grouper les frames par type de pac et type d'animation
pac_animations = {}
for frame_name in frames.keys():
color_parsed, pac_type, anim_type, frame_num = self.parse_frame_name(frame_name)
if color_parsed != color:
continue
if pac_type not in pac_animations:
pac_animations[pac_type] = {}
if anim_type not in pac_animations[pac_type]:
pac_animations[pac_type][anim_type] = []
pac_animations[pac_type][anim_type].append((frame_num, frame_name))
# Trier les frames par numéro et créer les animations
for pac_type in pac_animations:
self.animations[f"{color}_{pac_type}"] = {}
# Animation de marche (vivant)
if 'walk' in pac_animations[pac_type]:
walk_frames = sorted(pac_animations[pac_type]['walk'])
walk_frame_names = [frame_name for _, frame_name in walk_frames]
# Animation normale: 8 frames (aller-retour)
normal_animation = walk_frame_names + walk_frame_names[::-1]
self.animations[f"{color}_{pac_type}"]['normal'] = {
'frames': normal_animation,
'duration': 8, # 8 frames pour une animation complète
'loop': True
}
# Animation de collision: avancer et reculer
self.animations[f"{color}_{pac_type}"]['collision'] = {
'frames': walk_frame_names[:2] + walk_frame_names[:2][::-1], # 1-2-2-1
'duration': 4,
'loop': False
}
# Animation de mort
if 'mort' in pac_animations[pac_type]:
mort_frames = sorted(pac_animations[pac_type]['mort'])
mort_frame_names = [frame_name for _, frame_name in mort_frames]
self.animations[f"{color}_{pac_type}"]['mort'] = {
'frames': mort_frame_names,
'duration': 7, # 7 frames pour l'animation de mort
'loop': False
}
def get_animation(self, pac_color: str, pac_type: int, animation_type: str) -> Dict:
"""Récupère une animation spécifique"""
key = f"{pac_color}_{pac_type}"
if key in self.animations and animation_type in self.animations[key]:
return self.animations[key][animation_type]
return None
def generate_javascript_constants(self) -> str:
"""Génère le code JavaScript pour les constantes d'animation"""
js_code = "// Constantes d'animation pour les Pacs\n"
js_code += "// Généré automatiquement par pac_animation_builder.py\n\n"
js_code += "export const PAC_ANIMATIONS = {\n"
for pac_key, animations in self.animations.items():
js_code += f" {pac_key}: {{\n"
for anim_type, anim_data in animations.items():
frames_js = "[" + ", ".join(f'"{frame}"' for frame in anim_data['frames']) + "]"
js_code += f" {anim_type}: {{\n"
js_code += f" frames: {frames_js},\n"
js_code += f" duration: {anim_data['duration']},\n"
js_code += f" loop: {str(anim_data['loop']).lower()}\n"
js_code += " },\n"
js_code += " },\n"
js_code += "};\n\n"
# Ajouter les constantes de types
js_code += "// Types de pacs par ligue\n"
js_code += "export const PAC_TYPES = {\n"
js_code += " WOOD_BRONZE: 1, // Type 1: ligues Wood et Bronze\n"
js_code += " SCISSOR: 2, // Type 2: SCISSOR\n"
js_code += " PAPER: 3, // Type 3: PAPER\n"
js_code += " ROCK: 4 // Type 4: ROCK\n"
js_code += "};\n\n"
# Ajouter les couleurs
js_code += "// Couleurs disponibles\n"
js_code += "export const PAC_COLORS = {\n"
js_code += " BLUE: 'blue',\n"
js_code += " RED: 'red'\n"
js_code += "};\n\n"
# Ajouter les types d'animation
js_code += "// Types d'animation\n"
js_code += "export const ANIMATION_TYPES = {\n"
js_code += " NORMAL: 'normal', // Animation normale de déplacement\n"
js_code += " MORT: 'mort', // Animation de mort\n"
js_code += " COLLISION: 'collision' // Animation de collision (avancer-reculer)\n"
js_code += "};\n"
return js_code
def save_javascript_file(self, output_path: str):
"""Sauvegarde les constantes JavaScript dans un fichier"""
js_code = self.generate_javascript_constants()
with open(output_path, 'w', encoding='utf-8') as f:
f.write(js_code)
print(f"Fichier JavaScript sauvegardé: {output_path}")
def main():
# Créer le système d'animation
system = PacAnimationSystem()
# Charger les spritesheets
script_dir = os.path.dirname(os.path.abspath(__file__))
frontend_dir = os.path.join(script_dir, 'frontend', 'public', 'sprites')
blue_json = os.path.join(frontend_dir, 'blue.json')
red_json = os.path.join(frontend_dir, 'red.json')
if os.path.exists(blue_json):
system.load_spritesheet(blue_json, 'blue')
if os.path.exists(red_json):
system.load_spritesheet(red_json, 'red')
# Construire les animations
system.build_animations()
# Afficher un résumé
print("\n=== RÉSUMÉ DES ANIMATIONS ===\n")
for pac_key, animations in system.animations.items():
print(f"Pac {pac_key}:")
for anim_type, anim_data in animations.items():
print(f" {anim_type}: {len(anim_data['frames'])} frames, durée: {anim_data['duration']}, loop: {anim_data['loop']}")
print()
# Générer le fichier JavaScript
output_js = os.path.join(script_dir, 'frontend', 'src', 'pac_animations.js')
system.save_javascript_file(output_js)
print("Système d'animation des Pacs créé avec succès!")
if __name__ == "__main__":
main()