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