ShopData読み込み/購入処理追加
This commit is contained in:
parent
508164b087
commit
79a71df4f1
|
|
@ -405,7 +405,7 @@ MonoBehaviour:
|
||||||
m_Script: {fileID: 11500000, guid: 4f77652346b24cc9b0d5da516979f736, type: 3}
|
m_Script: {fileID: 11500000, guid: 4f77652346b24cc9b0d5da516979f736, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
scrollRect: {fileID: 0}
|
scrollRect: {fileID: 2912042418150409388}
|
||||||
itemViewPrefab: {fileID: 8023342812979047025, guid: 907730abf7bed70469027e0e8aba6fd9,
|
itemViewPrefab: {fileID: 8023342812979047025, guid: 907730abf7bed70469027e0e8aba6fd9,
|
||||||
type: 3}
|
type: 3}
|
||||||
headerView: {fileID: 2912042418274684488}
|
headerView: {fileID: 2912042418274684488}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,102 @@
|
||||||
using System.Collections;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using UniRx;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class Shopping : MonoBehaviour
|
public class Shopping : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
[SerializeField] private ShopListView listView;
|
||||||
// Start is called before the first frame update
|
// Start is called before the first frame update
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
var gameData = GameDataManager.GameData;
|
var gameData = GameDataManager.GameData;
|
||||||
CoinManager.Instance.ChangeCoin(gameData.coin);
|
CoinManager.Instance.ChangeCoin(gameData.coin);
|
||||||
|
HeartMeter.Instance.SetHeart(gameData.Heart);
|
||||||
|
var changeCoinSubject = new Subject<int>().AddTo(this);
|
||||||
|
|
||||||
|
// 一覧データ
|
||||||
|
var shopDataList = SpreadsheetDataManager.Instance.GetBaseDataList<ShopData>(Const.ShopDataSheet);
|
||||||
|
foreach (var shopData in shopDataList)
|
||||||
|
{
|
||||||
|
var itemView = listView.AddItemView(shopData);
|
||||||
|
itemView.SetStockCount(GetItemAmount(shopData));
|
||||||
|
// コインを監視して購入可能状態を切り替え
|
||||||
|
changeCoinSubject.Subscribe(ownCoin =>
|
||||||
|
{
|
||||||
|
if (shopData.ConsumeType == ShopItemType.NonConsumable && GetItemAmount(shopData) > 0)
|
||||||
|
{
|
||||||
|
itemView.SetItemActive(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
itemView.SetItemActive(shopData.price < ownCoin);
|
||||||
|
}).AddTo(this);
|
||||||
|
// アイテム購入クリック購読
|
||||||
|
itemView.PurchaseButtonObservable.Subscribe(_ =>
|
||||||
|
{
|
||||||
|
// アイテムを増やす
|
||||||
|
AddItem(shopData);
|
||||||
|
itemView.SetStockCount(GetItemAmount(shopData));
|
||||||
|
CoinManager.Instance.SubCoin(shopData.price);
|
||||||
|
gameData.coin = CoinManager.Instance.OwnCoin;
|
||||||
|
changeCoinSubject.OnNext(gameData.coin);
|
||||||
|
GameDataManager.SaveGameData();
|
||||||
|
}).AddTo(itemView);
|
||||||
|
}
|
||||||
|
changeCoinSubject.OnNext(CoinManager.Instance.OwnCoin);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int GetItemAmount(ShopData shopData)
|
||||||
|
{
|
||||||
|
var gameData = GameDataManager.GameData;
|
||||||
|
switch (shopData.Category)
|
||||||
|
{
|
||||||
|
case ItemCategory.Material:
|
||||||
|
return gameData.Material.Find(data => data.Id == shopData.itemId)?.Amount ?? 0;
|
||||||
|
case ItemCategory.Recipe:
|
||||||
|
return gameData.MyRecipes.Contains(shopData.itemId) ? 1 : 0;
|
||||||
|
case ItemCategory.Pan:
|
||||||
|
return gameData.Pans.Contains(shopData.itemId) ? 1 : 0;
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddItem(ShopData shopData)
|
||||||
|
{
|
||||||
|
var gameData = GameDataManager.GameData;
|
||||||
|
switch (shopData.Category)
|
||||||
|
{
|
||||||
|
case ItemCategory.Material:
|
||||||
|
var index = gameData.Material.FindIndex(x => x.Id == shopData.itemId);
|
||||||
|
if (index == -1)
|
||||||
|
{
|
||||||
|
gameData.Material.Add(new MaterialData
|
||||||
|
{
|
||||||
|
Id = shopData.itemId,
|
||||||
|
Amount = 1
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gameData.Material[index].Amount++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ItemCategory.Recipe:
|
||||||
|
if (!gameData.MyRecipes.Contains(shopData.itemId))
|
||||||
|
{
|
||||||
|
gameData.MyRecipes = new List<int>(gameData.MyRecipes){shopData.itemId}.ToArray();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ItemCategory.Pan:
|
||||||
|
if (!gameData.Pans.Contains(shopData.itemId))
|
||||||
|
{
|
||||||
|
gameData.Pans = new List<int>(gameData.Pans){shopData.itemId}.ToArray();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,4 +67,13 @@ public class CoinManager : SingletonMonoBehaviour<CoinManager>
|
||||||
callback.Invoke();
|
callback.Invoke();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SubCoin(int coin){
|
||||||
|
ownCoin -= coin;
|
||||||
|
if(coinCountText.gameObject.activeInHierarchy){
|
||||||
|
coinCountText.CountUpAnimation(coinTextFormat, ownCoin, duration);
|
||||||
|
}else{
|
||||||
|
coinCountText.ChangeValue(coinTextFormat, ownCoin);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue