update_excel_from_json.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 根据JSON配置文件更新Excel表格
  5. """
  6. import json
  7. import pandas as pd
  8. import os
  9. from pathlib import Path
  10. def load_json_file(file_path):
  11. """加载JSON文件"""
  12. with open(file_path, 'r', encoding='utf-8') as f:
  13. return json.load(f)
  14. def create_weapons_excel():
  15. """根据weapons.json创建武器配置Excel表格"""
  16. # 加载武器配置
  17. weapons_data = load_json_file('d:/CocosGame/Pong/assets/resources/data/weapons.json')
  18. # 创建武器基础信息表
  19. weapons_basic = []
  20. for weapon in weapons_data['weapons']:
  21. weapons_basic.append({
  22. 'ID': weapon['id'],
  23. '名称': weapon['name'],
  24. '类型': weapon['type'],
  25. '稀有度': weapon['rarity'],
  26. '权重': weapon['weight'],
  27. '伤害': weapon['stats']['damage'],
  28. '射速': weapon['stats']['fireRate'],
  29. '射程': weapon['stats']['range'],
  30. '子弹速度': weapon['stats']['bulletSpeed']
  31. })
  32. # 创建子弹配置表
  33. bullet_configs = []
  34. for weapon in weapons_data['weapons']:
  35. bullet_config = weapon['bulletConfig']
  36. bullet_configs.append({
  37. '武器ID': weapon['id'],
  38. '武器名称': weapon['name'],
  39. '子弹数量类型': bullet_config['count']['type'],
  40. '子弹数量': bullet_config['count']['amount'],
  41. '散射角度': bullet_config['count']['spreadAngle'],
  42. '连发数量': bullet_config['count']['burstCount'],
  43. '连发延迟': bullet_config['count']['burstDelay'],
  44. '轨迹类型': bullet_config['trajectory']['type'],
  45. '轨迹速度': bullet_config['trajectory']['speed'],
  46. '重力': bullet_config['trajectory']['gravity'],
  47. '生命周期类型': bullet_config['lifecycle']['type'],
  48. '最大生存时间': bullet_config['lifecycle']['maxLifetime'],
  49. '穿透次数': bullet_config['lifecycle']['penetration'],
  50. '反弹次数': bullet_config['lifecycle']['ricochetCount'],
  51. '是否返回原点': bullet_config['lifecycle']['returnToOrigin']
  52. })
  53. # 创建方块形状配置表
  54. block_shapes = []
  55. for block in weapons_data['blockSizes']:
  56. # 将4x4矩阵转换为字符串表示
  57. shape_str = ''
  58. for row in block['shape']:
  59. shape_str += ''.join(map(str, row)) + '\n'
  60. block_shapes.append({
  61. 'ID': block['id'],
  62. '名称': block['name'],
  63. '形状矩阵': shape_str.strip()
  64. })
  65. # 创建稀有度权重表
  66. rarity_weights = []
  67. for rarity, weight in weapons_data['rarityWeights'].items():
  68. rarity_weights.append({
  69. '稀有度': rarity,
  70. '权重': weight
  71. })
  72. # 创建Excel文件
  73. with pd.ExcelWriter('d:/CocosGame/Pong/assets/resources/data/excel/方块武器配置/方块武器配置表_更新_v2.xlsx', engine='openpyxl') as writer:
  74. pd.DataFrame(weapons_basic).to_excel(writer, sheet_name='武器基础配置', index=False)
  75. pd.DataFrame(bullet_configs).to_excel(writer, sheet_name='子弹配置', index=False)
  76. pd.DataFrame(block_shapes).to_excel(writer, sheet_name='方块形状配置', index=False)
  77. pd.DataFrame(rarity_weights).to_excel(writer, sheet_name='稀有度权重', index=False)
  78. print("武器配置Excel表格已更新完成!")
  79. def create_levels_excel():
  80. """根据levels目录下的JSON文件创建关卡配置Excel表格"""
  81. levels_dir = Path('d:/CocosGame/Pong/assets/resources/data/levels')
  82. # 关卡基础信息表
  83. levels_basic = []
  84. # 关卡波次配置表
  85. waves_config = []
  86. # 敌人配置表
  87. enemies_config = []
  88. # 敌人名称映射表
  89. enemy_name_mapping = []
  90. # 加载敌人配置以获取名称映射
  91. try:
  92. enemies_data = load_json_file('d:/CocosGame/Pong/assets/resources/data/enemies.json')
  93. if 'nameToIdMapping' in enemies_data:
  94. for chinese_name, enemy_id in enemies_data['nameToIdMapping'].items():
  95. enemy_name_mapping.append({
  96. '中文名称': chinese_name,
  97. '英文ID': enemy_id,
  98. '说明': '关卡配置中使用的敌人名称映射'
  99. })
  100. except Exception as e:
  101. print(f"加载敌人名称映射时出错: {e}")
  102. # 遍历所有关卡文件
  103. for level_file in levels_dir.glob('Level*.json'):
  104. try:
  105. level_data = load_json_file(level_file)
  106. level_id = level_file.stem
  107. # 关卡基础信息
  108. levels_basic.append({
  109. '关卡ID': level_id,
  110. '关卡名称': level_data['name'],
  111. '场景': level_data['scene'],
  112. '描述': level_data['description'],
  113. '可用武器': ', '.join(level_data['weapons'])
  114. })
  115. # 波次和敌人配置
  116. for wave in level_data['waves']:
  117. wave_id = wave['waveId']
  118. # 波次基础信息
  119. waves_config.append({
  120. '关卡ID': level_id,
  121. '波次ID': wave_id,
  122. '敌人种类数': len(wave['enemies'])
  123. })
  124. # 每个敌人的详细配置
  125. for enemy in wave['enemies']:
  126. enemies_config.append({
  127. '关卡ID': level_id,
  128. '波次ID': wave_id,
  129. '敌人类型': enemy['enemyType'],
  130. '数量': enemy['count'],
  131. '生成间隔': enemy['spawnInterval'],
  132. '生成延迟': enemy['spawnDelay'],
  133. '特性': ', '.join(enemy['characteristics'])
  134. })
  135. except Exception as e:
  136. print(f"处理关卡文件 {level_file} 时出错: {e}")
  137. continue
  138. # 创建Excel文件
  139. try:
  140. with pd.ExcelWriter('d:/CocosGame/Pong/assets/resources/data/excel/关卡配置/关卡配置表_完整版_更新_v2.xlsx', engine='openpyxl') as writer:
  141. pd.DataFrame(levels_basic).to_excel(writer, sheet_name='关卡基础配置', index=False)
  142. pd.DataFrame(waves_config).to_excel(writer, sheet_name='波次配置', index=False)
  143. pd.DataFrame(enemies_config).to_excel(writer, sheet_name='敌人配置', index=False)
  144. pd.DataFrame(enemy_name_mapping).to_excel(writer, sheet_name='敌人名称映射', index=False)
  145. print("关卡配置Excel表格已更新完成!")
  146. print(f"生成的工作表包括: 关卡基础配置, 波次配置, 敌人配置, 敌人名称映射")
  147. except Exception as e:
  148. print(f"创建关卡配置Excel文件时出错: {e}")
  149. def main():
  150. """主函数"""
  151. print("开始更新Excel配置表格...")
  152. try:
  153. # 更新武器配置表
  154. create_weapons_excel()
  155. # 更新关卡配置表
  156. create_levels_excel()
  157. print("\n所有Excel表格更新完成!")
  158. print("生成的文件:")
  159. print("- d:/CocosGame/Pong/assets/resources/data/excel/方块武器配置/方块武器配置表_更新_v2.xlsx")
  160. print("- d:/CocosGame/Pong/assets/resources/data/excel/关卡配置/关卡配置表_完整版_更新_v2.xlsx")
  161. except Exception as e:
  162. print(f"更新过程中出现错误: {e}")
  163. import traceback
  164. traceback.print_exc()
  165. if __name__ == '__main__':
  166. main()