有点乱,个人觉得更好的实现
using System;
using System.Collections.Generic;
using Game;
using UnityEngine;
namespace Architecture
{
public class Farm : MonoBehaviour
{
public int productId = -1;
public int productPeriod = 7;
public int count = 5;
public int cost = 5;
public int growPeriod = -1;
public int stage2 = 2;
public int stage3 = 3;
private int _currentStage = 1;
private int _showingStage = 1;
private bool _isGrown;
private bool _isNeedGrow;
private int _passingDays;
private readonly Dictionary<int, GameObject> _stages = new Dictionary<int, GameObject>();
private void Awake()
{
WorldTimer.Instance.DoThisDay += NewDay;
for (var i = 1; i <= 3; i++)
{
_stages[i] = transform.Find(i.ToString()).gameObject;
if (i >= 2)
{
_stages[i].SetActive(false);
}
}
_isNeedGrow = growPeriod > 0;
}
private void Update()
{
if (_showingStage == _currentStage) return;
_stages[_showingStage].SetActive(false);
_stages[_currentStage].SetActive(true);
_showingStage = _currentStage;
}
private void NewDay()
{
_passingDays += 1;
if (growPeriod > 0 && !_isGrown)
{
if (_passingDays <= growPeriod) return;
_isGrown = true;
_passingDays = 0;
}
else
{
_currentStage = _passingDays < stage2 && !_isNeedGrow ? 1 : _passingDays < stage3 ? 2 : 3;
if (_passingDays < productPeriod) return;
_passingDays = 0;
Knapsack.Instance.AddGoods(productId, count);
}
}
private void OnDestroy()
{
WorldTimer.Instance.DoThisDay -= NewDay;
}
}
}