販売周りのロジックを修正
This commit is contained in:
parent
ee7abea2bb
commit
0f58cfe47b
|
|
@ -4,14 +4,15 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using UniRx;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
public class Market : MonoBehaviour
|
||||
{
|
||||
public static readonly int ShopStockCount = 21;
|
||||
public static readonly int ShopStockCount = 20;
|
||||
|
||||
[SerializeField] private ShopStockView stockView;
|
||||
|
||||
[SerializeField] private MarketCartView cartView;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
|
@ -25,71 +26,112 @@ public class Market : MonoBehaviour
|
|||
|
||||
StockFlavorLog();
|
||||
|
||||
cartView.SetStock(GameDataManager.GameData.ShopStock);
|
||||
// 陳列
|
||||
var displayFlavors = gameData.ShopStock.Select(x => x).ToList();
|
||||
cartView.SetStock(displayFlavors);
|
||||
|
||||
// 売り順決定
|
||||
var shuffledOrder = ShuffleOrder(displayFlavors.Count);
|
||||
|
||||
// お客さんの出現タイミング(10秒間に1回)
|
||||
Observable.Timer(TimeSpan.FromSeconds(2f), TimeSpan.FromSeconds(10f))
|
||||
.Subscribe(_ =>
|
||||
{
|
||||
// 品切れ
|
||||
if (GameDataManager.GameData.ShopStock.Count == 0)
|
||||
if (gameData.ShopStock.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// 店頭の先頭から売る
|
||||
var id = GameDataManager.GameData.ShopStock[0];
|
||||
GameDataManager.GameData.ShopStock.RemoveAt(0);
|
||||
// shuffledOrder順に販売
|
||||
var orderNum = shuffledOrder[0];
|
||||
shuffledOrder.RemoveAt(0);
|
||||
var targetFlavorId = displayFlavors[orderNum];
|
||||
Debug.Log($"sell:{orderNum} flavor:{targetFlavorId} {shuffledOrder.Count}");
|
||||
|
||||
cartView.SellStock(orderNum);
|
||||
var targetIndex = gameData.ShopStock.FindIndex(x => x == targetFlavorId);
|
||||
var stockData = gameData.ShopStock[targetIndex];
|
||||
gameData.ShopStock.RemoveAt(targetIndex);
|
||||
|
||||
// コイン獲得
|
||||
var bonusRate = 0;
|
||||
var flavor = allRecipe.First(x => x.RecipeId == id);
|
||||
var flavor = allRecipe.First(x => x.RecipeId == stockData);
|
||||
CoinManager.Instance.AddCoinWithEffect(flavor.Price * (1 + bonusRate / 100), () => { });
|
||||
// 売上反映
|
||||
GameDataManager.GameData.coin = CoinManager.Instance.OwnCoin;
|
||||
gameData.coin = CoinManager.Instance.OwnCoin;
|
||||
GameDataManager.SaveGameData();
|
||||
cartView.SellStock();
|
||||
|
||||
// 自動補充 refill
|
||||
RefillProduct();
|
||||
stockView.SetStock(GameDataManager.GameData.StorageTanks);
|
||||
cartView.SetStock(GameDataManager.GameData.ShopStock);
|
||||
RefillOneProduct();
|
||||
var setStockFlag = false;
|
||||
// 補充された場合フレーバー再設定
|
||||
if (gameData.ShopStock.Count == ShopStockCount)
|
||||
{
|
||||
shuffledOrder.Add(orderNum);
|
||||
displayFlavors[orderNum] = gameData.ShopStock.Last();
|
||||
}
|
||||
else if (gameData.ShopStock.Count <= 13 && shuffledOrder.Exists(x => x > 13))
|
||||
{
|
||||
displayFlavors = gameData.ShopStock.Select(x => x).ToList();
|
||||
shuffledOrder = ShuffleOrder(displayFlavors.Count);
|
||||
setStockFlag = true;
|
||||
}
|
||||
else if (gameData.ShopStock.Count <= 7 && shuffledOrder.Exists(x => x > 7))
|
||||
{
|
||||
displayFlavors = gameData.ShopStock.Select(x => x).ToList();
|
||||
shuffledOrder = ShuffleOrder(displayFlavors.Count);
|
||||
setStockFlag = true;
|
||||
}
|
||||
|
||||
// 表示更新
|
||||
this.CallWaitForSeconds(1f, () =>
|
||||
{
|
||||
stockView.SetStock(gameData.StorageTanks);
|
||||
if (gameData.ShopStock.Count == ShopStockCount)
|
||||
{
|
||||
// 補充したフレーバーのスキンを設定
|
||||
cartView.Refill(orderNum, displayFlavors[orderNum]);
|
||||
}
|
||||
else if (setStockFlag)
|
||||
{
|
||||
cartView.SetStock(displayFlavors);
|
||||
}
|
||||
});
|
||||
|
||||
StockFlavorLog();
|
||||
}).AddTo(this);
|
||||
}
|
||||
|
||||
private void RefillProduct()
|
||||
private List<int> ShuffleOrder(int length)
|
||||
{
|
||||
// 店頭に補充
|
||||
// 手前のタンクから出し多分stockをへらす
|
||||
// へらした分を店頭リストに追加する
|
||||
// 店頭とタンクのフレーバーの扱いが違うのに注意
|
||||
// 店頭にはフレーバーごとに並び順があるのでリストに1つづつ入れる必要がある
|
||||
var gameData = GameDataManager.GameData;
|
||||
var shopSpace = ShopStockCount - gameData.ShopStock.Count;
|
||||
var index = 0;
|
||||
var tankCount = gameData.StorageTanks.Count;
|
||||
while (shopSpace > 0)
|
||||
return Enumerable.Range(0, length)
|
||||
.OrderBy(_ => Random.value).ToList();
|
||||
}
|
||||
|
||||
private void RefillOneProduct()
|
||||
{
|
||||
foreach (var tank in GameDataManager.GameData.StorageTanks)
|
||||
{
|
||||
if (index > tankCount - 1)
|
||||
if (tank.Stock == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (GameDataManager.GameData.ShopStock.Count == ShopStockCount)
|
||||
{
|
||||
break;
|
||||
}
|
||||
var tank = gameData.StorageTanks[index];
|
||||
var stockCount = Mathf.Min(shopSpace, tank.Stock);
|
||||
gameData.ShopStock.AddRange(Enumerable.Repeat(tank.FlavorId, stockCount));
|
||||
shopSpace -= stockCount;
|
||||
tank.Stock -= stockCount;
|
||||
gameData.StorageTanks[index] = tank;
|
||||
index++;
|
||||
GameDataManager.GameData.ShopStock.Add(tank.FlavorId);
|
||||
tank.Stock--;
|
||||
}
|
||||
GameDataManager.SaveGameData();
|
||||
}
|
||||
|
||||
public static void StockFlavorLog()
|
||||
{
|
||||
var gameData = GameDataManager.GameData;
|
||||
var shopStockString = "";
|
||||
foreach (var data in RecipeData.GetAllRecipe())
|
||||
{
|
||||
var shopStockCount = gameData.ShopStock.FindAll(x => x == data.RecipeId).Count;
|
||||
var tank = gameData.StorageTanks.FindAll(x => x.FlavorId == data.RecipeId).Sum(x => x.Stock);
|
||||
var shopStockCount = GameDataManager.GameData.ShopStock.FindAll(x => x == data.RecipeId).Count;
|
||||
var tank = GameDataManager.GameData.StorageTanks.FindAll(x => x.FlavorId == data.RecipeId).Sum(x => x.Stock);
|
||||
if (shopStockCount + tank == 0)
|
||||
{
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -29,8 +29,14 @@ public class MarketCartView : MonoBehaviour
|
|||
}
|
||||
}
|
||||
|
||||
public void SellStock()
|
||||
public void Refill(int index, int flavor)
|
||||
{
|
||||
popcornPositions[0].gameObject.SetActive(false);
|
||||
popcornPositions[index].ChangeSkin(flavor);
|
||||
popcornPositions[index].gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public void SellStock(int index)
|
||||
{
|
||||
popcornPositions[index].gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue