Monster.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System.Collections.Generic;
  2. using Unity.VisualScripting;
  3. using UnityEngine;
  4. namespace MazeGen
  5. {
  6. /// <summary>
  7. /// Monster size for combat positioning. Determines grid footprint and spatial presence.
  8. /// Small: 4 creatures fit in 1 grid square (fractionally tracked)
  9. /// Medium: 1 creature = 1 grid square
  10. /// Large: 1 creature = 4x4 grid squares
  11. /// </summary>
  12. public enum MonsterSize
  13. {
  14. Small, // 4 per square
  15. Medium, // 1 per square (1x1)
  16. Large // 4x4 squares per creature
  17. }
  18. /// <summary>
  19. /// Defines a loot entry with an item and its min/max drop count.
  20. /// E.g., a Goblin might drop 0-2 Health Potions.
  21. /// </summary>
  22. [System.Serializable]
  23. public class LootEntry
  24. {
  25. public Item Item;
  26. [Tooltip("Minimum number of this item to drop (0 means it might not drop at all)")]
  27. public int MinCount = 0;
  28. [Tooltip("Maximum number of this item to drop")]
  29. public int MaxCount = 1;
  30. public LootEntry() { }
  31. public LootEntry(Item item, int minCount, int maxCount)
  32. {
  33. Item = item;
  34. MinCount = minCount;
  35. MaxCount = maxCount;
  36. }
  37. }
  38. /// <summary>
  39. /// A ScriptableObject that defines a monster template (stats, appearance, loot).
  40. /// Monsters are instantiated from this template when they spawn in a room.
  41. /// </summary>
  42. public class Monster : ScriptableObject
  43. {
  44. [SerializeField] private string _monsterName = "Goblin";
  45. [SerializeField] private Color _color = Color.green;
  46. [SerializeField] private CharacterStats _baseStats = new CharacterStats();
  47. [SerializeField] private MonsterSize _size = MonsterSize.Medium;
  48. // Optional starting equipment
  49. [SerializeField] private Weapon _startingWeapon;
  50. [SerializeField] private Armor _startingArmor;
  51. // Loot table: what items might this monster drop with min/max counts
  52. [SerializeField] private LootEntry[] _lootTable;
  53. // Loot options
  54. [SerializeField] private bool _includeStartingWeaponAsLoot = false;
  55. [SerializeField] private bool _includeStartingArmorAsLoot = false;
  56. [SerializeField] [Range(0f, 1f)] private float _lootDropChance = 1.0f; // Chance to drop any loot at all
  57. public string MonsterName => _monsterName;
  58. public Color Color => _color;
  59. public CharacterStats BaseStats => _baseStats;
  60. public MonsterSize Size => _size;
  61. public Weapon StartingWeapon => _startingWeapon;
  62. public Armor StartingArmor => _startingArmor;
  63. public LootEntry[] LootTable => _lootTable;
  64. public bool IncludeStartingWeaponAsLoot => _includeStartingWeaponAsLoot;
  65. public bool IncludeStartingArmorAsLoot => _includeStartingArmorAsLoot;
  66. public float LootDropChance => _lootDropChance;
  67. /// <summary>Build the final loot pool for this monster instance based on min/max ranges.</summary>
  68. public List<Item> GenerateLootPool()
  69. {
  70. var pool = new List<Item>();
  71. // Determine if this monster drops any loot at all
  72. if (Random.value > _lootDropChance)
  73. return pool; // Return empty pool (no loot)
  74. // Add items from the loot table based on min/max rolls
  75. if (_lootTable != null)
  76. {
  77. foreach (var entry in _lootTable)
  78. {
  79. if (entry?.Item == null) continue;
  80. int count = Random.Range(entry.MinCount, entry.MaxCount + 1);
  81. for (int i = 0; i < count; i++)
  82. {
  83. pool.Add(entry.Item);
  84. }
  85. }
  86. }
  87. // Optionally add starting equipment
  88. if (_includeStartingWeaponAsLoot && _startingWeapon != null)
  89. {
  90. pool.Add(_startingWeapon);
  91. }
  92. if (_includeStartingArmorAsLoot && _startingArmor != null)
  93. {
  94. pool.Add(_startingArmor);
  95. }
  96. return pool;
  97. }
  98. /// <summary>Create a fresh character sheet for a monster instance (with a copy of stats).</summary>
  99. public CharacterSheet CreateCharacterSheet()
  100. {
  101. // Pass null GameObject (Monster is a ScriptableObject, not a MonoBehaviour)
  102. // CharacterSheet will use a default name instead
  103. var sheet = new CharacterSheet(null, _baseStats.Clone());
  104. // Override with the monster template's name
  105. sheet.Name = _monsterName;
  106. if (_startingWeapon != null)
  107. sheet.EquipWeapon(_startingWeapon);
  108. if (_startingArmor != null)
  109. sheet.EquipArmor(_startingArmor);
  110. return sheet;
  111. }
  112. #if UNITY_EDITOR
  113. private void OnValidate()
  114. {
  115. if (_baseStats == null)
  116. _baseStats = new CharacterStats();
  117. }
  118. #endif
  119. }
  120. }