| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using UnityEngine;
- using UnityEngine.InputSystem;
- public enum FieldSize
- {
- Small = 1, // 1x1, fast to work, low yield
- Medium = 2, // 2x2, medium time, medium yield
- Large = 3 // 3x3, slow to work, high yield
- }
- public class Field : MonoBehaviour, ISelectable
- {
- [Header("Field Properties")]
- public FieldSize fieldSize = FieldSize.Small;
- public int maxFood = 30;
- public int currentFood = 0;
- public int totalHarvests = 0; // Track how many times it's been harvested
- public int maxHarvests = 5; // Field depletes after this many harvests
- public float growthTime = 20f; // Base time to fully grow
- public bool isFullyGrown = false;
- [Header("Construction")]
- public bool isUnderConstruction = true;
- public float constructionProgress = 0f;
- public float constructionTime = 8f; // Time for farmer to build the field
- [Header("References")]
- public GameObject farmhouse; // The farmhouse that owns this field
- public Villager assignedFarmer; // The farmer working this field
- private float growthTimer = 0f;
- private Renderer fieldRenderer;
- void Start()
- {
- fieldRenderer = GetComponent<Renderer>();
- SetupFieldProperties();
- UpdateVisualState();
- }
- void Update()
- {
- if (isUnderConstruction)
- {
- // Construction is handled by the farmer - this just updates visuals
- UpdateVisualState();
- }
- else if (!isFullyGrown && currentFood < maxFood)
- {
- // Grow food over time
- growthTimer += Time.deltaTime;
- if (growthTimer >= growthTime)
- {
- GrowFood();
- growthTimer = 0f;
- }
- UpdateVisualState();
- }
- }
- void SetupFieldProperties()
- {
- // Adjust properties based on field size
- int sizeMultiplier = (int)fieldSize;
- maxFood = 20 * sizeMultiplier * sizeMultiplier; // Small=20, Medium=80, Large=180
- constructionTime = 5f + (sizeMultiplier * 3f); // Small=8s, Medium=11s, Large=14s
- growthTime = 15f + (sizeMultiplier * 5f); // Small=20s, Medium=25s, Large=30s
- // Scale the visual size
- transform.localScale = new Vector3(sizeMultiplier, 0.1f, sizeMultiplier);
- Debug.Log($"Field created - Size: {fieldSize}, MaxFood: {maxFood}, ConstructionTime: {constructionTime}s");
- }
- public void AdvanceConstruction(float deltaTime)
- {
- if (!isUnderConstruction) return;
- constructionProgress += deltaTime;
- if (constructionProgress >= constructionTime)
- {
- CompleteConstruction();
- }
- UpdateVisualState();
- }
- public void CompleteConstruction()
- {
- isUnderConstruction = false;
- constructionProgress = constructionTime;
- currentFood = maxFood / 4; // Start with some food
- Debug.Log($"Field construction completed! Starting with {currentFood}/{maxFood} food");
- UpdateVisualState();
- }
- void GrowFood()
- {
- int growthAmount = Random.Range(5, 15);
- currentFood = Mathf.Min(currentFood + growthAmount, maxFood);
- if (currentFood >= maxFood)
- {
- isFullyGrown = true;
- }
- Debug.Log($"Field grew {growthAmount} food. Now has {currentFood}/{maxFood}");
- }
- public int HarvestFood(int requestedAmount)
- {
- if (isUnderConstruction || currentFood <= 0) return 0;
- int harvestedAmount = Mathf.Min(requestedAmount, currentFood);
- currentFood -= harvestedAmount;
- if (currentFood < maxFood)
- {
- isFullyGrown = false;
- growthTimer = 0f; // Reset growth timer when harvested
- totalHarvests++;
- }
- // Check if field is depleted
- if (totalHarvests >= maxHarvests)
- {
- Debug.Log($"🌾 Field depleted after {totalHarvests} harvests. Field will be removed.");
- if (assignedFarmer != null)
- {
- assignedFarmer.targetWorkplace = null;
- }
- Destroy(gameObject, 1f); // Destroy after a short delay
- return harvestedAmount;
- }
- Debug.Log($"Harvested {harvestedAmount} food from field. Remaining: {currentFood}/{maxFood}. Uses: {totalHarvests}/{maxHarvests}");
- UpdateVisualState();
- return harvestedAmount;
- }
- void UpdateVisualState()
- {
- if (fieldRenderer == null) return;
- if (isUnderConstruction)
- {
- // Brown/dirt color during construction, with progress indication
- float progress = constructionProgress / constructionTime;
- Color constructionColor = Color.Lerp(new Color(0.4f, 0.3f, 0.2f), new Color(0.6f, 0.4f, 0.2f), progress);
- fieldRenderer.material.color = constructionColor;
- }
- else
- {
- // Green shades based on food content
- float foodPercent = (float)currentFood / maxFood;
- if (foodPercent > 0.8f)
- {
- fieldRenderer.material.color = new Color(0.2f, 0.8f, 0.2f); // Bright green - ready to harvest
- }
- else if (foodPercent > 0.4f)
- {
- fieldRenderer.material.color = new Color(0.4f, 0.7f, 0.3f); // Medium green - growing
- }
- else
- {
- fieldRenderer.material.color = new Color(0.6f, 0.5f, 0.3f); // Brownish - recently harvested
- }
- }
- }
- public string GetFieldInfo()
- {
- if (isUnderConstruction)
- {
- float progressPercent = (constructionProgress / constructionTime) * 100f;
- return $"🌾 Field ({fieldSize})\\nUnder Construction: {progressPercent:F0}%\\nBuilder: {(assignedFarmer != null ? assignedFarmer.villagerName : "Needed")}";
- }
- else
- {
- float depletionPercent = ((float)totalHarvests / maxHarvests) * 100f;
- return $"🌾 Field ({fieldSize})\\nFood: {currentFood}/{maxFood}\\nGrown: {(isFullyGrown ? "Yes" : "Growing...")}\\nUses: {totalHarvests}/{maxHarvests} ({depletionPercent:F0}% depleted)";
- }
- }
- // ISelectable implementation
- public void OnSelected()
- {
- // Visual indication could be added here
- }
- public void OnDeselected()
- {
- // Clear visual indication
- }
- void OnMouseDown()
- {
- if (Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame)
- {
- if (GameManager.Instance?.selectionManager != null)
- {
- GameManager.Instance.selectionManager.SelectObject(gameObject);
- }
- }
- }
- public float GetWorkTime()
- {
- // Return how long it takes to work this field (affects farmer efficiency)
- return 2f + ((int)fieldSize * 1.5f); // Small=3.5s, Medium=5s, Large=6.5s
- }
- }
|