| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using UnityEngine;
- using System.Collections.Generic;
- public class SimpleFarmhouse : MonoBehaviour
- {
- [SerializeField] private int maxResidents = 2;
- [SerializeField] private int currentFood = 0;
- [SerializeField] private int maxFood = 20;
- [SerializeField] private float foodProductionRate = 2f; // food per second when farmers are working
- private List<Villager> residents = new List<Villager>();
- private float lastProductionTime;
- public bool HasSpace => residents.Count < maxResidents;
- public int ResidentCount => residents.Count;
- public int AvailableFarmers => residents.Count;
- void Start()
- {
- lastProductionTime = Time.time;
- Debug.Log("Simple Farmhouse created");
- }
- void Update()
- {
- ProduceFood();
- }
- void ProduceFood()
- {
- if (residents.Count > 0 && currentFood < maxFood)
- {
- float timeSinceLastProduction = Time.time - lastProductionTime;
- if (timeSinceLastProduction >= 1f) // Produce every second
- {
- int foodToAdd = Mathf.FloorToInt(foodProductionRate * residents.Count);
- currentFood = Mathf.Min(currentFood + foodToAdd, maxFood);
- if (currentFood >= 10) // Transfer food to global storage when we have enough
- {
- GameManager.Instance.resourceManager.AddResource(ResourceType.Food, currentFood);
- Debug.Log($"Farmhouse produced {currentFood} food");
- currentFood = 0;
- }
- lastProductionTime = Time.time;
- }
- }
- }
- public bool AddResident(Villager villager)
- {
- if (HasSpace)
- {
- residents.Add(villager);
- Debug.Log($"Farmer moved into farmhouse. Residents: {residents.Count}/{maxResidents}");
- return true;
- }
- return false;
- }
- public void RemoveResident(Villager villager)
- {
- if (residents.Contains(villager))
- {
- residents.Remove(villager);
- Debug.Log($"Farmer left farmhouse. Residents: {residents.Count}/{maxResidents}");
- }
- }
- public string GetBuildingInfo()
- {
- return $"Farmhouse\nResidents: {residents.Count}/{maxResidents}\nFood stored: {currentFood}/{maxFood}";
- }
- public void OnBuildingPlaced()
- {
- // Try to assign nearby homeless farmers
- AssignNearbyFarmers();
- }
- void AssignNearbyFarmers()
- {
- if (!HasSpace) return;
- // Find homeless villagers within range
- Collider[] nearbyObjects = Physics.OverlapSphere(transform.position, 10f);
- foreach (var obj in nearbyObjects)
- {
- if (!HasSpace) break;
- Villager villager = obj.GetComponent<Villager>();
- if (villager != null)
- {
- AddResident(villager);
- }
- }
- }
- void OnDrawGizmos()
- {
- // Show farmhouse range
- Gizmos.color = Color.green;
- Gizmos.DrawWireSphere(transform.position, 10f);
- // Show food production status
- if (residents.Count > 0)
- {
- Gizmos.color = Color.yellow;
- Gizmos.DrawWireCube(transform.position + Vector3.up * 2, Vector3.one * 0.5f);
- }
- }
- }
|