Marketクラスリファクタ

This commit is contained in:
kimura 2021-10-27 09:58:07 +09:00
parent 2bb250d135
commit 0ba6b6fc01
1 changed files with 59 additions and 90 deletions

View File

@ -18,6 +18,14 @@ public enum ShopState
public class Market : SingletonMonoBehaviour<Market> public class Market : SingletonMonoBehaviour<Market>
{ {
public static readonly int ShopStockCount = 20; public static readonly int ShopStockCount = 20;
private static readonly (int want, float weight)[] customerWeightTable = {
(1, 70f),
(2, 20f),
(3, 3f),
(4, 1.5f),
(5, .5f),
(0, 5f),
};
[SerializeField] private CustomerFlow customerFlow; [SerializeField] private CustomerFlow customerFlow;
[SerializeField] private GameObject orderPosisionObject; [SerializeField] private GameObject orderPosisionObject;
@ -190,13 +198,6 @@ public class Market : SingletonMonoBehaviour<Market>
// 商品補充 // 商品補充
RefillShopStockData(); RefillShopStockData();
#if UNITY_EDITOR
StockFlavorLog();
Debug.Log($"ShopStock{gameData.ShopStock.Count}, {shuffledOrder.Count + orders.Count}");
Debug.Log($"displayCount:{displayFlavors.Count}\n" +
$"shuffled:{shuffledOrder.Count} {string.Join(",", shuffledOrder)}\n" +
$"orders:{orders.Count} {string.Join(",", orders)}");
#endif
// 表示データ更新 // 表示データ更新
var isReorder = false; var isReorder = false;
var refillList = new List<int>(); var refillList = new List<int>();
@ -261,6 +262,7 @@ public class Market : SingletonMonoBehaviour<Market>
controller.ChangeCustomerState(CustomerState.Wait); controller.ChangeCustomerState(CustomerState.Wait);
}).AddTo(this); }).AddTo(this);
// 客湧き
customerFlow.Flow.Subscribe(isCustomer => customerFlow.Flow.Subscribe(isCustomer =>
{ {
if (IsPause.Value) if (IsPause.Value)
@ -271,57 +273,13 @@ public class Market : SingletonMonoBehaviour<Market>
var (isSpecial, orderCount) = GetCustomerData(isCustomer); var (isSpecial, orderCount) = GetCustomerData(isCustomer);
// 複数パターンある場合ChooseRandom // 複数パターンある場合ChooseRandom
CustomerAnimator prefab; var prefab = isSpecial ? customerData.ChooseSpecialPrefab() : customerData.ChooseNormalPrefab();
if (isSpecial) var customerController = SpawnCustomer();
{
prefab = customerData.ChooseSpecialPrefab();
}
else
{
prefab = customerData.ChooseNormalPrefab();
}
var customerController = Instantiate(customerControllerPrefab, transform);
customerController.Setup(orderPosisionObject.transform.GetComponentsInChildren<Transform>().ToList().Skip(1).ToList()); customerController.Setup(orderPosisionObject.transform.GetComponentsInChildren<Transform>().ToList().Skip(1).ToList());
customerController.OrderCount = orderCount; customerController.OrderCount = orderCount;
customerController.CustomerPrefab = prefab; customerController.CustomerPrefab = prefab;
customerControllerList.Add(customerController); customerControllerList.Add(customerController);
IsPause.Subscribe(x =>
{
customerController.IsPause = x;
}).AddTo(customerController);
customerController.MoveEndObservable
.SkipLatestValueOnSubscribe()
.DistinctUntilChanged()
.Subscribe(prevMovingType =>
{
// Debug.Log($"move end {type} {customerController.GetHashCode()}");
switch (prevMovingType)
{
case CustomerMovingType.WalkSide:
case CustomerMovingType.WalkSideEat:
customerList.Remove(customerController);
customerControllerList.Remove(customerController);
Destroy(customerController.gameObject);
break;
case CustomerMovingType.WalkCenter:
if (customerController.State.Value == CustomerState.WalkShop)
{
customerList.Add(customerController);
}
break;
case CustomerMovingType.WalkBackHalf:
waitCustomerList.Add(customerController);
break;
case CustomerMovingType.StayBackOrder:
requestSubject.OnNext(customerController);
break;
default:
break;
}
}).AddTo(customerController);
if (isCustomer) if (isCustomer)
{ {
@ -362,23 +320,18 @@ public class Market : SingletonMonoBehaviour<Market>
}).AddTo(this); }).AddTo(this);
} }
// お客さん出現パターン確率計算と行動パターン計算
private int GetOrderCount(bool isSpecial) private (bool isSpecial, int orderCount) GetCustomerData (bool isCustomer)
{ {
if (isSpecial) var isSpecial = isCustomer ? Random.value < .01f : Random.value < .03f;
{
return 5;
}
var customerWeightTable = new (int want, float weight)[]{ // セレブは5個購入固定
(1, 70f), return isSpecial ? (true, 5) : (false, GetOrderCount());
(2, 20f), }
(3, 3f),
(4, 1.5f),
(5, .5f),
(0, 5f),
};
// お客さん出現パターン確率計算と行動パターン計算
private int GetOrderCount()
{
var randomPoint = Random.value * customerWeightTable.Sum(x => x.weight); var randomPoint = Random.value * customerWeightTable.Sum(x => x.weight);
foreach (var value in customerWeightTable) foreach (var value in customerWeightTable)
{ {
@ -391,26 +344,42 @@ public class Market : SingletonMonoBehaviour<Market>
return customerWeightTable.Last().want; return customerWeightTable.Last().want;
} }
private (bool isSpecial, int orderCount) GetCustomerData (bool isCustomer) private CustomerController SpawnCustomer()
{ {
var isSpecial = false; var customerController = Instantiate(customerControllerPrefab, transform);
IsPause.Subscribe(x => customerController.IsPause = x).AddTo(customerController);
if (isCustomer) customerController.MoveEndObservable
{ .SkipLatestValueOnSubscribe()
isSpecial = Random.value < .01f; .DistinctUntilChanged()
} .Subscribe(prevMovingType =>
else {
{ // Debug.Log($"move end {type} {customerController.GetHashCode()}");
isSpecial = Random.value < .03f; switch (prevMovingType)
} {
case CustomerMovingType.WalkSide:
// セレブは5個購入固定 case CustomerMovingType.WalkSideEat:
if (isSpecial) customerList.Remove(customerController);
{ customerControllerList.Remove(customerController);
return (true, GetOrderCount(true)); Destroy(customerController.gameObject);
} break;
case CustomerMovingType.WalkCenter:
return (false, GetOrderCount(false)); if (customerController.State.Value == CustomerState.WalkShop)
{
customerList.Add(customerController);
}
break;
case CustomerMovingType.WalkBackHalf:
waitCustomerList.Add(customerController);
break;
case CustomerMovingType.StayBackOrder:
requestSubject.OnNext(customerController);
break;
default:
break;
}
}).AddTo(customerController);
return customerController;
} }
private int SellPopcorn(List<ProductStockData> stockDataList) private int SellPopcorn(List<ProductStockData> stockDataList)