SimpleFarmhouse.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public class SimpleFarmhouse : MonoBehaviour
  4. {
  5. [SerializeField] private int maxResidents = 2;
  6. [SerializeField] private int currentFood = 0;
  7. [SerializeField] private int maxFood = 20;
  8. [SerializeField] private float foodProductionRate = 2f; // food per second when farmers are working
  9. private List<Villager> residents = new List<Villager>();
  10. private float lastProductionTime;
  11. public bool HasSpace => residents.Count < maxResidents;
  12. public int ResidentCount => residents.Count;
  13. public int AvailableFarmers => residents.Count;
  14. void Start()
  15. {
  16. lastProductionTime = Time.time;
  17. Debug.Log("Simple Farmhouse created");
  18. }
  19. void Update()
  20. {
  21. ProduceFood();
  22. }
  23. void ProduceFood()
  24. {
  25. if (residents.Count > 0 && currentFood < maxFood)
  26. {
  27. float timeSinceLastProduction = Time.time - lastProductionTime;
  28. if (timeSinceLastProduction >= 1f) // Produce every second
  29. {
  30. int foodToAdd = Mathf.FloorToInt(foodProductionRate * residents.Count);
  31. currentFood = Mathf.Min(currentFood + foodToAdd, maxFood);
  32. if (currentFood >= 10) // Transfer food to global storage when we have enough
  33. {
  34. GameManager.Instance.resourceManager.AddResource(ResourceType.Food, currentFood);
  35. Debug.Log($"Farmhouse produced {currentFood} food");
  36. currentFood = 0;
  37. }
  38. lastProductionTime = Time.time;
  39. }
  40. }
  41. }
  42. public bool AddResident(Villager villager)
  43. {
  44. if (HasSpace)
  45. {
  46. residents.Add(villager);
  47. Debug.Log($"Farmer moved into farmhouse. Residents: {residents.Count}/{maxResidents}");
  48. return true;
  49. }
  50. return false;
  51. }
  52. public void RemoveResident(Villager villager)
  53. {
  54. if (residents.Contains(villager))
  55. {
  56. residents.Remove(villager);
  57. Debug.Log($"Farmer left farmhouse. Residents: {residents.Count}/{maxResidents}");
  58. }
  59. }
  60. public string GetBuildingInfo()
  61. {
  62. return $"Farmhouse\nResidents: {residents.Count}/{maxResidents}\nFood stored: {currentFood}/{maxFood}";
  63. }
  64. public void OnBuildingPlaced()
  65. {
  66. // Try to assign nearby homeless farmers
  67. AssignNearbyFarmers();
  68. }
  69. void AssignNearbyFarmers()
  70. {
  71. if (!HasSpace) return;
  72. // Find homeless villagers within range
  73. Collider[] nearbyObjects = Physics.OverlapSphere(transform.position, 10f);
  74. foreach (var obj in nearbyObjects)
  75. {
  76. if (!HasSpace) break;
  77. Villager villager = obj.GetComponent<Villager>();
  78. if (villager != null)
  79. {
  80. AddResident(villager);
  81. }
  82. }
  83. }
  84. void OnDrawGizmos()
  85. {
  86. // Show farmhouse range
  87. Gizmos.color = Color.green;
  88. Gizmos.DrawWireSphere(transform.position, 10f);
  89. // Show food production status
  90. if (residents.Count > 0)
  91. {
  92. Gizmos.color = Color.yellow;
  93. Gizmos.DrawWireCube(transform.position + Vector3.up * 2, Vector3.one * 0.5f);
  94. }
  95. }
  96. }