CombatDataTransfer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Static data transfer class for passing combat-related data between scenes
  6. /// Handles team data, enemy data, terrain information, and battle context
  7. /// </summary>
  8. public static class CombatDataTransfer
  9. {
  10. [System.Serializable]
  11. public class CombatSessionData
  12. {
  13. [Header("Battle Context")]
  14. public string battleDescription = "";
  15. public Vector2Int battleLocation = Vector2Int.zero;
  16. public TerrainType battleTerrain = TerrainType.Plains;
  17. public FeatureType battleFeature = FeatureType.None;
  18. [Header("Team Data")]
  19. public List<TeamCharacterCombatData> playerTeam = new List<TeamCharacterCombatData>();
  20. [Header("Enemy Data")]
  21. public List<EnemyCombatData> enemies = new List<EnemyCombatData>();
  22. [Header("Environmental Settings")]
  23. public Weather weather = Weather.Clear;
  24. public float timeOfDay = 12f; // 0-24 hours
  25. public CombatSessionData()
  26. {
  27. playerTeam = new List<TeamCharacterCombatData>();
  28. enemies = new List<EnemyCombatData>();
  29. }
  30. }
  31. [System.Serializable]
  32. public class TeamCharacterCombatData
  33. {
  34. public string characterName = "";
  35. public bool isMale = true;
  36. // Core stats
  37. public int strength = 10;
  38. public int dexterity = 10;
  39. public int constitution = 10;
  40. public int wisdom = 10;
  41. public int perception = 10;
  42. // Combat stats
  43. public int maxHealth = 20;
  44. public int currentHealth = 20;
  45. public int armorClass = 10;
  46. // Equipment
  47. public string equippedWeapon = "Sword"; // Keep for backward compatibility
  48. public WeaponItem equippedWeaponItem = null; // NEW: Actual weapon item reference
  49. public string equippedArmor = "";
  50. public List<string> availableWeapons = new List<string>();
  51. public List<string> availableArmor = new List<string>();
  52. public List<string> miscItems = new List<string>(); // Character's inventory items
  53. // Resources
  54. public int gold = 25;
  55. public int silver = 0;
  56. public int copper = 0;
  57. public TeamCharacterCombatData()
  58. {
  59. availableWeapons = new List<string>();
  60. availableArmor = new List<string>();
  61. miscItems = new List<string>();
  62. }
  63. /// <summary>
  64. /// Create combat data from a TeamCharacter
  65. /// </summary>
  66. public static TeamCharacterCombatData FromTeamCharacter(TeamCharacter teamChar)
  67. {
  68. var combatData = new TeamCharacterCombatData();
  69. if (teamChar != null)
  70. {
  71. combatData.characterName = teamChar.name;
  72. combatData.isMale = teamChar.isMale;
  73. combatData.strength = teamChar.strength;
  74. combatData.dexterity = teamChar.dexterity;
  75. combatData.constitution = teamChar.constitution;
  76. combatData.wisdom = teamChar.wisdom;
  77. combatData.perception = teamChar.perception;
  78. combatData.gold = teamChar.gold;
  79. combatData.silver = teamChar.silver;
  80. combatData.copper = teamChar.copper;
  81. // Ensure equipped weapon is not empty, fallback to first available or "Fists"
  82. combatData.equippedWeapon = !string.IsNullOrEmpty(teamChar.equippedWeapon)
  83. ? teamChar.equippedWeapon
  84. : (teamChar.weapons != null && teamChar.weapons.Count > 0)
  85. ? teamChar.weapons[0]
  86. : "Fists";
  87. // Try to find the actual WeaponItem for the equipped weapon
  88. combatData.equippedWeaponItem = FindWeaponItem(combatData.equippedWeapon);
  89. combatData.equippedArmor = teamChar.equippedArmor;
  90. // Calculate derived stats
  91. combatData.maxHealth = 10 + (teamChar.constitution * 2); // HP formula
  92. combatData.currentHealth = combatData.maxHealth; // Start at full health
  93. combatData.armorClass = 10 + (teamChar.dexterity / 2 - 5); // AC formula
  94. // Copy equipment lists
  95. if (teamChar.weapons != null)
  96. combatData.availableWeapons.AddRange(teamChar.weapons);
  97. if (teamChar.armor != null)
  98. combatData.availableArmor.AddRange(teamChar.armor);
  99. if (teamChar.miscItems != null)
  100. combatData.miscItems.AddRange(teamChar.miscItems);
  101. }
  102. return combatData;
  103. }
  104. }
  105. [System.Serializable]
  106. public class EnemyCombatData
  107. {
  108. public string enemyName = "Bandit";
  109. public string enemyType = "Humanoid";
  110. public int maxHealth = 15;
  111. public int currentHealth = 15;
  112. public int armorClass = 12;
  113. public int threatLevel = 2;
  114. public string preferredWeapon = "Sword"; // Keep for backward compatibility
  115. public WeaponItem preferredWeaponItem = null; // NEW: Actual weapon item reference
  116. public EnemyCharacterData sourceData = null; // Reference to original data
  117. /// <summary>
  118. /// Create combat data from EnemyCharacterData
  119. /// </summary>
  120. public static EnemyCombatData FromEnemyData(EnemyCharacterData enemyData, int instanceNumber = 1)
  121. {
  122. var combatData = new EnemyCombatData();
  123. if (enemyData != null)
  124. {
  125. combatData.enemyName = $"{enemyData.enemyName}_{instanceNumber}";
  126. combatData.enemyType = enemyData.enemyName;
  127. combatData.maxHealth = enemyData.maxHealth;
  128. combatData.currentHealth = enemyData.maxHealth;
  129. combatData.armorClass = enemyData.armorClass;
  130. combatData.threatLevel = enemyData.threatLevel;
  131. // Extract weapon type properly from WeaponItem
  132. if (enemyData.preferredWeapon != null)
  133. {
  134. combatData.preferredWeapon = enemyData.preferredWeapon.weaponType.ToString();
  135. combatData.preferredWeaponItem = enemyData.preferredWeapon; // Store actual WeaponItem
  136. }
  137. else
  138. {
  139. combatData.preferredWeapon = "Fists"; // Default fallback
  140. combatData.preferredWeaponItem = null;
  141. }
  142. combatData.sourceData = enemyData;
  143. }
  144. return combatData;
  145. }
  146. }
  147. // Current combat session data
  148. private static CombatSessionData currentSession = null;
  149. /// <summary>
  150. /// Initialize a new combat session with the given data
  151. /// </summary>
  152. public static void InitializeCombatSession(BattleEventData battleData, TravelEventContext context, List<TeamCharacter> teamMembers)
  153. {
  154. currentSession = new CombatSessionData();
  155. // Set battle context
  156. if (context != null)
  157. {
  158. currentSession.battleLocation = context.currentPosition;
  159. currentSession.battleTerrain = context.currentTile?.terrainType ?? TerrainType.Plains;
  160. currentSession.battleFeature = context.currentTile?.featureType ?? FeatureType.None;
  161. currentSession.weather = context.currentWeather;
  162. currentSession.timeOfDay = context.timeOfDay;
  163. }
  164. // Set battle description
  165. if (battleData != null)
  166. {
  167. currentSession.battleDescription = battleData.battleDescription;
  168. }
  169. // Convert team members to combat data
  170. if (teamMembers != null)
  171. {
  172. foreach (var teamMember in teamMembers)
  173. {
  174. if (teamMember != null)
  175. {
  176. currentSession.playerTeam.Add(TeamCharacterCombatData.FromTeamCharacter(teamMember));
  177. }
  178. }
  179. }
  180. // Create enemy combat data
  181. if (battleData != null && battleData.enemyCharacterData != null)
  182. {
  183. for (int i = 0; i < battleData.enemyCount; i++)
  184. {
  185. currentSession.enemies.Add(EnemyCombatData.FromEnemyData(battleData.enemyCharacterData, i + 1));
  186. }
  187. }
  188. }
  189. /// <summary>
  190. /// Get the current combat session data
  191. /// </summary>
  192. public static CombatSessionData GetCurrentSession()
  193. {
  194. return currentSession;
  195. }
  196. /// <summary>
  197. /// Check if there's a valid combat session
  198. /// </summary>
  199. public static bool HasValidSession()
  200. {
  201. return currentSession != null &&
  202. currentSession.playerTeam.Count > 0 &&
  203. currentSession.enemies.Count > 0;
  204. }
  205. /// <summary>
  206. /// Clear the current combat session (call after battle ends)
  207. /// </summary>
  208. public static void ClearSession()
  209. {
  210. currentSession = null;
  211. }
  212. /// <summary>
  213. /// Get terrain-appropriate description for battle setup
  214. /// </summary>
  215. public static string GetTerrainDescription(TerrainType terrain, FeatureType feature)
  216. {
  217. string baseDescription = terrain switch
  218. {
  219. TerrainType.Plains => "open grasslands",
  220. TerrainType.Forest => "dense woodland",
  221. TerrainType.Mountain => "rocky mountain terrain",
  222. TerrainType.River => "along a flowing river",
  223. TerrainType.Lake => "near a peaceful lake",
  224. TerrainType.Ocean => "along the coastline",
  225. TerrainType.Shore => "on sandy shores",
  226. _ => "unknown terrain"
  227. };
  228. string featureDescription = feature switch
  229. {
  230. FeatureType.Road => "on a well-traveled road",
  231. FeatureType.Bridge => "at a stone bridge",
  232. FeatureType.Town => "within town limits",
  233. FeatureType.Village => "near a small village",
  234. FeatureType.Tunnel => "in a dark tunnel",
  235. FeatureType.Ferry => "at a ferry crossing",
  236. FeatureType.Harbour => "at a busy harbor",
  237. _ => ""
  238. };
  239. if (!string.IsNullOrEmpty(featureDescription))
  240. {
  241. return $"{featureDescription} in {baseDescription}";
  242. }
  243. return $"in {baseDescription}";
  244. }
  245. /// <summary>
  246. /// Create legacy BattleSetupData for compatibility with existing battle scene
  247. /// </summary>
  248. public static void PopulateLegacyBattleSetupData()
  249. {
  250. if (!HasValidSession())
  251. {
  252. Debug.LogError("❌ No valid combat session to populate legacy battle setup data");
  253. return;
  254. }
  255. // Clear existing data
  256. BattleSetupData.playerSelections.Clear();
  257. BattleSetupData.enemySelections.Clear();
  258. // Populate player selections
  259. foreach (var player in currentSession.playerTeam)
  260. {
  261. string weaponType = !string.IsNullOrEmpty(player.equippedWeapon) ? player.equippedWeapon : "Fists";
  262. BattleSetupData.playerSelections.Add(new CharacterSelection
  263. {
  264. characterName = player.characterName,
  265. weaponType = weaponType
  266. });
  267. }
  268. // Populate enemy selections
  269. foreach (var enemy in currentSession.enemies)
  270. {
  271. string weaponType = !string.IsNullOrEmpty(enemy.preferredWeapon) ? enemy.preferredWeapon : "Fists";
  272. BattleSetupData.enemySelections.Add(new CharacterSelection
  273. {
  274. characterName = enemy.enemyName,
  275. weaponType = weaponType
  276. });
  277. }
  278. }
  279. /// <summary>
  280. /// Helper method to find a WeaponItem by name or type
  281. /// </summary>
  282. private static WeaponItem FindWeaponItem(string weaponIdentifier)
  283. {
  284. if (string.IsNullOrEmpty(weaponIdentifier) || weaponIdentifier == "Fists")
  285. {
  286. return null;
  287. }
  288. // Load all WeaponItem assets from Resources folders
  289. WeaponItem[] allWeapons = Resources.LoadAll<WeaponItem>("");
  290. // First try exact name match
  291. foreach (var weapon in allWeapons)
  292. {
  293. if (weapon != null && string.Equals(weapon.itemName, weaponIdentifier, System.StringComparison.OrdinalIgnoreCase))
  294. {
  295. return weapon;
  296. }
  297. }
  298. // Then try weapon type match
  299. if (System.Enum.TryParse<WeaponType>(weaponIdentifier, true, out WeaponType weaponType))
  300. {
  301. // Look for a weapon of this type, prefer "Simple" variants
  302. WeaponItem simpleWeapon = null;
  303. WeaponItem anyWeapon = null;
  304. foreach (var weapon in allWeapons)
  305. {
  306. if (weapon != null && weapon.weaponType == weaponType)
  307. {
  308. anyWeapon = weapon; // Keep track of any weapon of this type
  309. // Prefer simple/basic weapons
  310. if (weapon.itemName.ToLower().Contains("simple") || weapon.itemName.ToLower().Contains("basic"))
  311. {
  312. simpleWeapon = weapon;
  313. break; // Found preferred weapon, stop searching
  314. }
  315. }
  316. }
  317. WeaponItem result = simpleWeapon ?? anyWeapon;
  318. return result;
  319. }
  320. return null;
  321. }
  322. internal static void SetCombatSession(CombatSessionData sessionData)
  323. {
  324. if (sessionData == null)
  325. {
  326. return;
  327. }
  328. currentSession = sessionData;
  329. }
  330. }