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 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 GameObject orderPosisionObject;
@ -190,13 +198,6 @@ public class Market : SingletonMonoBehaviour<Market>
// 商品補充
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 refillList = new List<int>();
@ -261,6 +262,7 @@ public class Market : SingletonMonoBehaviour<Market>
controller.ChangeCustomerState(CustomerState.Wait);
}).AddTo(this);
// 客湧き
customerFlow.Flow.Subscribe(isCustomer =>
{
if (IsPause.Value)
@ -271,57 +273,13 @@ public class Market : SingletonMonoBehaviour<Market>
var (isSpecial, orderCount) = GetCustomerData(isCustomer);
// 複数パターンある場合ChooseRandom
CustomerAnimator prefab;
if (isSpecial)
{
prefab = customerData.ChooseSpecialPrefab();
}
else
{
prefab = customerData.ChooseNormalPrefab();
}
var customerController = Instantiate(customerControllerPrefab, transform);
var prefab = isSpecial ? customerData.ChooseSpecialPrefab() : customerData.ChooseNormalPrefab();
var customerController = SpawnCustomer();
customerController.Setup(orderPosisionObject.transform.GetComponentsInChildren<Transform>().ToList().Skip(1).ToList());
customerController.OrderCount = orderCount;
customerController.CustomerPrefab = prefab;
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)
{
@ -362,23 +320,18 @@ public class Market : SingletonMonoBehaviour<Market>
}).AddTo(this);
}
// お客さん出現パターン確率計算と行動パターン計算
private int GetOrderCount(bool isSpecial)
private (bool isSpecial, int orderCount) GetCustomerData (bool isCustomer)
{
if (isSpecial)
{
return 5;
}
var isSpecial = isCustomer ? Random.value < .01f : Random.value < .03f;
var customerWeightTable = new (int want, float weight)[]{
(1, 70f),
(2, 20f),
(3, 3f),
(4, 1.5f),
(5, .5f),
(0, 5f),
};
// セレブは5個購入固定
return isSpecial ? (true, 5) : (false, GetOrderCount());
}
// お客さん出現パターン確率計算と行動パターン計算
private int GetOrderCount()
{
var randomPoint = Random.value * customerWeightTable.Sum(x => x.weight);
foreach (var value in customerWeightTable)
{
@ -391,26 +344,42 @@ public class Market : SingletonMonoBehaviour<Market>
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)
{
isSpecial = Random.value < .01f;
}
else
{
isSpecial = Random.value < .03f;
}
// セレブは5個購入固定
if (isSpecial)
{
return (true, GetOrderCount(true));
}
return (false, GetOrderCount(false));
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);
return customerController;
}
private int SellPopcorn(List<ProductStockData> stockDataList)