COPY_PASTE_EXAMPLES.csEXAMPLE 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // This file contains copy-paste examples for setting up the combat system.
  2. // Replace YOUR_NAMESPACE with your actual namespace.
  3. using UnityEngine;
  4. using MazeGen;
  5. namespace MazeGen
  6. {
  7. // EXAMPLE 1: Creating a weapon in the Unity Editor
  8. // HOW TO USE:
  9. // 1. Right-click in the Project window (Assets folder or Assets/Resources/Items/Weapons/)
  10. // 2. Select "Create > Weapon"
  11. // 3. Name your weapon asset (e.g., "IronSword.asset")
  12. // 4. Select the created asset and edit its properties in the Inspector:
  13. // - Item Name: "Iron Sword"
  14. // - Description: "A sturdy iron sword"
  15. // - Icon: (drag a sprite)
  16. // - Rarity: 1 (common) to 5 (legendary)
  17. // - Attack Bonus: 5.0
  18. // - Magic Bonus: 0.0
  19. // - Defense Bonus: 1.0
  20. // - Speed Bonus: 0.0
  21. //
  22. // The asset is automatically saved to Resources/Items/Weapons/ for easy runtime loading.
  23. [System.Obsolete("See instructions above - create via Editor instead")]
  24. public class WeaponCreationExample : MonoBehaviour
  25. {
  26. // Example of loading weapons at runtime
  27. private void LoadWeaponsExample()
  28. {
  29. Weapon[] allWeapons = Resources.LoadAll<Weapon>("Items/Weapons/");
  30. foreach (var weapon in allWeapons)
  31. {
  32. Debug.Log($"Loaded weapon: {weapon.ItemName}");
  33. }
  34. }
  35. }
  36. // EXAMPLE 2: Creating a monster programmatically
  37. public class MonsterCreationExample : MonoBehaviour
  38. {
  39. public Monster CreateGoblin()
  40. {
  41. var goblin = ScriptableObject.CreateInstance<Monster>();
  42. goblin.name = "Goblin";
  43. // Create stats
  44. var stats = new CharacterStats();
  45. // stats fields are private - you'd need public setters or reflection
  46. // Best practice: create via editor!
  47. return goblin;
  48. }
  49. }
  50. // EXAMPLE 3: Setting up agents with character sheets in MazeSceneBuilder
  51. public class AgentSetupExample : MonoBehaviour
  52. {
  53. private Weapon[] _startingWeapons;
  54. private Armor[] _startingArmor;
  55. private void LoadResources()
  56. {
  57. // Load weapon templates
  58. _startingWeapons = Resources.LoadAll<Weapon>("Items/Weapons/");
  59. if (_startingWeapons.Length == 0)
  60. Debug.LogWarning("No weapons found in Resources/Items/Weapons/");
  61. // Load armor templates
  62. _startingArmor = Resources.LoadAll<Armor>("Items/Armor/");
  63. if (_startingArmor.Length == 0)
  64. Debug.LogWarning("No armor found in Resources/Items/Armor/");
  65. }
  66. private MazeAgent CreateAgentWithEquipment(string name, Room startRoom)
  67. {
  68. // Create base stats
  69. var stats = new CharacterStats();
  70. var sheet = new CharacterSheet(stats);
  71. // Equip weapon
  72. if (_startingWeapons.Length > 0)
  73. {
  74. var weapon = _startingWeapons[Random.Range(0, _startingWeapons.Length)];
  75. sheet.EquipWeapon(weapon);
  76. }
  77. // Equip armor
  78. if (_startingArmor.Length > 0)
  79. {
  80. var armor = _startingArmor[Random.Range(0, _startingArmor.Length)];
  81. sheet.EquipArmor(armor);
  82. }
  83. // Create agent with character sheet
  84. var agent = new MazeAgent(name, startRoom, sheet);
  85. return agent;
  86. }
  87. }
  88. // EXAMPLE 4: Spawning encounters when generating rooms
  89. public class EncounterSpawningExample : MonoBehaviour
  90. {
  91. private Monster[] _monsterPool;
  92. private float _encounterChance = 0.7f;
  93. private int _minMonstersPerRoom = 1;
  94. private int _maxMonstersPerRoom = 3;
  95. private void LoadMonsters()
  96. {
  97. _monsterPool = Resources.LoadAll<Monster>("Monsters/");
  98. if (_monsterPool.Length == 0)
  99. Debug.LogWarning("No monsters found in Resources/Monsters/");
  100. }
  101. private void SpawnEncounterInRoom(Room room)
  102. {
  103. // Don't spawn in start or exit rooms
  104. if (room.Kind == RoomKind.Start || room.Kind == RoomKind.Exit)
  105. return;
  106. // Random chance to spawn
  107. if (Random.value > _encounterChance)
  108. return;
  109. if (_monsterPool.Length == 0)
  110. return;
  111. // Create and populate encounter
  112. var encounter = new MonsterEncounter();
  113. encounter.SpawnMonsters(
  114. _monsterPool,
  115. _minMonstersPerRoom,
  116. _maxMonstersPerRoom
  117. );
  118. room.Encounter = encounter;
  119. Debug.Log($"Spawned encounter in {room.Id} with {encounter.AliveMonsterCount} monsters");
  120. }
  121. }
  122. // EXAMPLE 5: Triggering combat when agent enters a room
  123. public class CombatTriggerExample : MonoBehaviour
  124. {
  125. private MazeAgent _agent;
  126. private void UpdateAgentCombat()
  127. {
  128. // Check if agent entered a room with monsters
  129. if (_agent.CurrentRoom.Encounter != null &&
  130. _agent.CurrentRoom.Encounter.HasLivingMonsters &&
  131. !_agent.IsDead)
  132. {
  133. ResolveCombat();
  134. }
  135. }
  136. private void ResolveCombat()
  137. {
  138. var outcome = CombatResolver.ResolveCombat(_agent, _agent.CurrentRoom.Encounter);
  139. switch (outcome)
  140. {
  141. case CombatResolver.CombatOutcome.AgentVictory:
  142. Debug.Log($"{_agent.Name} won!");
  143. // TODO: Award loot
  144. // TODO: Update UI
  145. break;
  146. case CombatResolver.CombatOutcome.AgentDefeated:
  147. Debug.Log($"{_agent.Name} was defeated!");
  148. // Agent is now dead - they stop automatically
  149. // TODO: Update UI (show death)
  150. break;
  151. case CombatResolver.CombatOutcome.Fleeing:
  152. Debug.Log($"{_agent.Name} fled!");
  153. // TODO: Force retreat back through entrance
  154. break;
  155. }
  156. }
  157. }
  158. // EXAMPLE 6: Peeking at a room's threat level
  159. public class PeekingExample : MonoBehaviour
  160. {
  161. private MazeAgent _agent;
  162. private AutonomousAgentBrain _brain;
  163. private void CheckThreatOfAdjacentRooms()
  164. {
  165. foreach (var exit in _agent.CurrentRoom.Exits)
  166. {
  167. float threatLevel = _brain.PeekThroughExit(exit, _agent);
  168. string threatDescription = threatLevel < 0.5f ? "Safe" :
  169. threatLevel < 1.5f ? "Moderate" :
  170. threatLevel < 3.0f ? "Dangerous" : "Deadly";
  171. Debug.Log($"Exit leads to {threatDescription} room (CR: {threatLevel:F2})");
  172. }
  173. }
  174. }
  175. // EXAMPLE 7: Setting up weapon with specific bonuses (for reference)
  176. [System.Obsolete("Example only - create Weapons via editor")]
  177. public class WeaponBonusExample : MonoBehaviour
  178. {
  179. private void ExampleWeaponBonuses()
  180. {
  181. // When you create a Weapon, these are the fields:
  182. // - itemName: "Iron Sword"
  183. // - description: "A sturdy iron sword"
  184. // - icon: (sprite reference)
  185. // - rarity: 1 (common)
  186. // - attackBonus: 5.0f
  187. // - magicBonus: 0.0f (no magic effect)
  188. // - defenseBonus: 1.0f (slight parry bonus)
  189. // - speedBonus: 0.0f (no speed effect)
  190. // When equipped, applies:
  191. // - +5 attack
  192. // - +1 defense
  193. // Modifiers stack with other equipment
  194. }
  195. }
  196. // EXAMPLE 8: Armor with penalties
  197. [System.Obsolete("Example only - create Armor via editor")]
  198. public class ArmorExample : MonoBehaviour
  199. {
  200. private void ExampleArmorStats()
  201. {
  202. // Heavy Armor example:
  203. // - defenseBonus: 10.0f (good protection)
  204. // - healthBonus: 5.0f (extra durability)
  205. // - speedPenalty: 2.0f (slow movement)
  206. // - magicResistance: 0.0f (no special resistances)
  207. // Light Armor example:
  208. // - defenseBonus: 3.0f (basic protection)
  209. // - healthBonus: 0.0f
  210. // - speedPenalty: 0.0f (no penalty)
  211. // - magicResistance: 2.0f (magic resistance)
  212. }
  213. }
  214. // EXAMPLE 9: Initializing AutonomousAgentBrain with retreat settings
  215. public class BrainInitializationExample : MonoBehaviour
  216. {
  217. private void InitBrainBrave(AutonomousAgentBrain brain, MazeAgentController controller, MazeGenerator gen)
  218. {
  219. // Brave agent - will enter dangerous rooms
  220. brain.Initialize(
  221. controller,
  222. gen,
  223. initialDelay: 0.5f,
  224. decisionInterval: 0.5f,
  225. exploreChance: 0.8f,
  226. retreatThreshold: 3.0f, // Very high threshold - rarely retreats
  227. isLevelEnded: () => false
  228. );
  229. }
  230. private void InitBrainCautious(AutonomousAgentBrain brain, MazeAgentController controller, MazeGenerator gen)
  231. {
  232. // Cautious agent - avoids danger
  233. brain.Initialize(
  234. controller,
  235. gen,
  236. initialDelay: 0.5f,
  237. decisionInterval: 0.5f,
  238. exploreChance: 0.5f,
  239. retreatThreshold: 0.8f, // Low threshold - retreats often
  240. isLevelEnded: () => false
  241. );
  242. }
  243. private void InitBrainNormal(AutonomousAgentBrain brain, MazeAgentController controller, MazeGenerator gen)
  244. {
  245. // Balanced agent
  246. brain.Initialize(
  247. controller,
  248. gen,
  249. initialDelay: 0.5f,
  250. decisionInterval: 0.5f,
  251. exploreChance: 0.7f,
  252. retreatThreshold: 1.5f, // Medium threshold
  253. isLevelEnded: () => false
  254. );
  255. }
  256. }
  257. // EXAMPLE 10: Monster loot table setup (in editor)
  258. [System.Obsolete("Example only - create Monsters via editor")]
  259. public class MonsterLootExample : MonoBehaviour
  260. {
  261. private void ExampleLootTables()
  262. {
  263. // Goblin's loot table might include:
  264. // [0] Common Dagger
  265. // [1] Leather Armor
  266. // [2] Health Potion
  267. // [3] (empty for "no drop" chance)
  268. // Boss monster's loot table might include:
  269. // [0] Rare Sword
  270. // [1] Legendary Armor
  271. // [2] Magic Scroll
  272. // (no empty slots - always drops something)
  273. }
  274. }
  275. }