using System.Collections.Generic; using UnityEngine; public class Farmhouse : MonoBehaviour { [Header("Farmhouse Settings")] public int maxFarmers = 2; // How many farmers can live here public float fieldBuildRange = 10f; // How far from farmhouse fields can be built public List ownedFields = new List(); public List residentFarmers = new List(); [Header("Field Construction")] public GameObject fieldPrefab; // Will be created procedurally if null public FieldSize nextFieldSize = FieldSize.Small; // Size for next field to be built private Building buildingComponent; void Start() { buildingComponent = GetComponent(); Debug.Log($"Farmhouse initialized - Max farmers: {maxFarmers}, Field range: {fieldBuildRange}"); } void Update() { // Check if we need to assign farmers to field construction CheckFieldConstruction(); // Update field management ManageFields(); } public bool CanBuildField(Vector3 position) { // Check if position is within building range float distance = Vector3.Distance(transform.position, position); if (distance > fieldBuildRange) { Debug.Log($"Field position too far from farmhouse ({distance:F1}m > {fieldBuildRange}m)"); return false; } // Check if we have farmers available to build if (GetAvailableFarmer() == null) { Debug.Log("No available farmers to build field"); return false; } // Check for overlapping fields or other obstacles Collider[] overlapping = Physics.OverlapSphere(position, (int)nextFieldSize * 0.6f); foreach (var col in overlapping) { if (col.GetComponent() != null || col.GetComponent() != null) { Debug.Log($"Field position blocked by {col.name}"); return false; } } return true; } public Field BuildField(Vector3 position, FieldSize size) { if (!CanBuildField(position)) return null; // Create field GameObject GameObject fieldObj = CreateFieldObject(position, size); Field field = fieldObj.GetComponent(); // Set up field properties field.fieldSize = size; field.farmhouse = gameObject; field.isUnderConstruction = true; // Assign a farmer to build it Villager farmer = GetAvailableFarmer(); if (farmer != null) { field.assignedFarmer = farmer; farmer.AssignToFieldConstruction(field); } // Add to owned fields ownedFields.Add(field); Debug.Log($"Started building {size} field at {position}. Assigned farmer: {farmer?.villagerName}"); return field; } GameObject CreateFieldObject(Vector3 position, FieldSize size) { GameObject fieldObj; if (fieldPrefab != null) { fieldObj = Instantiate(fieldPrefab, position, Quaternion.identity); } else { // Create basic field fieldObj = GameObject.CreatePrimitive(PrimitiveType.Cube); fieldObj.name = $"Field_{size}"; fieldObj.transform.position = position; fieldObj.transform.localScale = new Vector3((int)size, 0.1f, (int)size); // Make it look like a field Renderer renderer = fieldObj.GetComponent(); renderer.material.color = new Color(0.4f, 0.3f, 0.2f); // Dirt brown initially } // Add Field component if not present if (fieldObj.GetComponent() == null) { fieldObj.AddComponent(); } return fieldObj; } public Villager GetAvailableFarmer() { foreach (var farmer in residentFarmers) { if (farmer != null && farmer.currentJob == JobType.Farmer && farmer.state == VillagerState.Idle) { return farmer; } } return null; } public bool CanHouseFarmer() { return residentFarmers.Count < maxFarmers; } public bool AddFarmer(Villager farmer) { if (!CanHouseFarmer()) return false; if (!residentFarmers.Contains(farmer)) { residentFarmers.Add(farmer); farmer.SetHomeBuilding(gameObject); Debug.Log($"Farmer {farmer.villagerName} now lives at farmhouse"); return true; } return false; } public void RemoveFarmer(Villager farmer) { if (residentFarmers.Remove(farmer)) { farmer.SetHomeBuilding(null); Debug.Log($"Farmer {farmer.villagerName} moved out of farmhouse"); } } void CheckFieldConstruction() { // Check if any fields need farmers for construction foreach (var field in ownedFields) { if (field != null && field.isUnderConstruction && field.assignedFarmer == null) { Villager availableFarmer = GetAvailableFarmer(); if (availableFarmer != null) { field.assignedFarmer = availableFarmer; availableFarmer.AssignToFieldConstruction(field); Debug.Log($"Assigned {availableFarmer.villagerName} to continue field construction"); } } } } void ManageFields() { // Remove destroyed or null fields from the list ownedFields.RemoveAll(field => field == null); // Could add seasonal management, field rotation, etc. here later } public int GetTotalFoodProduction() { int totalFood = 0; foreach (var field in ownedFields) { if (field != null && !field.isUnderConstruction) { totalFood += field.currentFood; } } return totalFood; } public string GetFarmhouseInfo() { int totalFields = ownedFields.Count; int constructedFields = 0; int totalFood = 0; foreach (var field in ownedFields) { if (field != null) { if (!field.isUnderConstruction) { constructedFields++; totalFood += field.currentFood; } } } return $"Farmhouse\nFarmers: {residentFarmers.Count}/{maxFarmers}\nFields: {constructedFields}/{totalFields}\nTotal Food: {totalFood}"; } // Method to be called by UI for building fields public void StartFieldConstruction(FieldSize size) { nextFieldSize = size; // The actual field placement will be handled by the building system // This just prepares the farmhouse for the next field Debug.Log($"Farmhouse ready to build {size} field"); } void OnDrawGizmosSelected() { // Show field building range Gizmos.color = Color.green; Gizmos.DrawWireSphere(transform.position, fieldBuildRange); // Show connections to owned fields Gizmos.color = Color.yellow; foreach (var field in ownedFields) { if (field != null) { Gizmos.DrawLine(transform.position, field.transform.position); } } } }