WXGame/Blacksmith/Assets/Script/GameSystem.cs

368 lines
12 KiB
C#
Raw Normal View History

2024-11-13 16:56:37 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-12-18 11:12:52 +08:00
using TTSDK;
2024-11-13 16:56:37 +08:00
using Unity.VisualScripting;
using UnityEngine;
using Random = UnityEngine.Random;
2024-11-13 16:56:37 +08:00
public class GameSystem : MonoBehaviour
{
public static GameSystem ins;
2024-12-04 17:26:27 +08:00
public JsonTab tab;
2024-11-13 16:56:37 +08:00
public DrawingManager drawingManager;
2024-11-29 21:37:01 +08:00
public AssetBundleManager abManager;
public List<ItemDataNum> List;
2024-11-13 16:56:37 +08:00
public List<NPCBuyData> saveStart;
public ItemDataAsset DataAsset;
2024-12-11 13:58:55 +08:00
public float maxTime = 100;
2024-11-13 16:56:37 +08:00
public float nowTime;
2024-12-11 13:58:55 +08:00
public int goldCoin = 0;
public int playingGold = 0;
[SerializeField] public ForgePanel forgePanel;
2024-11-13 16:56:37 +08:00
2024-11-21 10:26:47 +08:00
[SerializeField] public List<NPCBuyData> _npcBuyDatas;
2024-12-11 13:58:55 +08:00
2024-11-13 16:56:37 +08:00
[SerializeField] private RectTransform seleTransform;
2024-12-11 13:58:55 +08:00
private int id = 0;
2024-11-13 16:56:37 +08:00
private int index = -1;
2024-12-11 13:58:55 +08:00
private bool getNpc = false;
2024-12-04 17:26:27 +08:00
#if UNITY_EDITOR
[InspectorButton]
2024-12-11 13:58:55 +08:00
private void ClearPlayerPrefs()
{
PlayerPrefs.DeleteAll();
}
[InspectorButton]
2024-12-11 23:46:58 +08:00
#endif
private void AddDicData()
{
2024-12-21 14:05:40 +08:00
DataManager.SetLevelLock(int.Parse(JsonTab.Instance.tables.Main.Get(20).ConstantValue),true);
DataManager.SaveItemUnLock(int.Parse(JsonTab.Instance.tables.Main.Get(21).ConstantValue), true);
DataManager.SaveItemUnLock(int.Parse(JsonTab.Instance.tables.Main.Get(22).ConstantValue), true);
DataManager.SaveItemUnLock(int.Parse(JsonTab.Instance.tables.Main.Get(23).ConstantValue), true);
DataManager.SaveItemUnLock(int.Parse(JsonTab.Instance.tables.Main.Get(24).ConstantValue), true);
DataManager.SaveItemUnLock(int.Parse(JsonTab.Instance.tables.Main.Get(25).ConstantValue), true);
DataManager.SaveItemUnLock(int.Parse(JsonTab.Instance.tables.Main.Get(26).ConstantValue), true);
DataManager.SaveItemUnLock(int.Parse(JsonTab.Instance.tables.Main.Get(27).ConstantValue), true);
DataManager.SaveItemUnLock(1, true);
DataManager.SaveItemUnLock(2, true);
DataManager.SaveItemUnLock(3, true);
DataManager.SaveItem(1,int.Parse(JsonTab.Instance.tables.Main.Get(1).ConstantValue));
DataManager.SaveItem(2,int.Parse(JsonTab.Instance.tables.Main.Get(18).ConstantValue));
DataManager.SaveItem(3,int.Parse(JsonTab.Instance.tables.Main.Get(19).ConstantValue));
DataManager.SaveDrawLevel(302011, 1);
DataManager.SaveDrawLevel(302101, 1);
DataManager.SaveDrawLevel(302201, 1);
DataManager.SaveDrawLevel(302301, 1);
DataManager.SaveDrawLevel(302401, 1);
DataManager.SaveDrawLevel(302501, 1);
DataManager.SaveDrawLevel(302601, 1);
}
2024-11-13 16:56:37 +08:00
private void Awake()
{
ins = this;
2024-12-18 11:12:52 +08:00
TT.InitSDK((code, env) =>
2024-11-13 16:56:37 +08:00
{
2024-12-18 11:12:52 +08:00
foreach (var data in List)
{
DataManager.AddItem(data.id, data.num);
}
2024-12-11 13:58:55 +08:00
2024-12-18 11:12:52 +08:00
tab = new JsonTab();
tab.InitData();
abManager.InitAsset((() => {}));
2024-11-21 10:26:47 +08:00
2024-12-18 11:12:52 +08:00
if (!DataManager.GetFirst())
{
AddDicData();
//JsonTab.Instance.tables.Main.
// var data = JsonTab.Instance.tables.Main.Get(18);
// var idStr = data.ConstantValue;
// var idList = idStr.Split(',');
// foreach (var id in idList)
// {
// var intId = int.Parse(id);
// DataManager.SaveItemUnLock(intId,true);
// var pData = JsonTab.Instance.tables.CraftingRecipes.Get(intId);
// if (pData.Levelprops1!=0)
// {
// DataManager.SaveItemUnLock(pData.Levelprops1,true);
// }
// if (pData.Levelprops2!=0)
// {
// DataManager.SaveItemUnLock(pData.Levelprops2,true);
// }
// if (pData.Levelprops3!=0)
// {
// DataManager.SaveItemUnLock(pData.Levelprops3,true);
// }
// if (pData.Levelprops4!=0)
// {
// DataManager.SaveItemUnLock(pData.Levelprops4,true);
// }
// }
// var dataItem=JsonTab.Instance.tables.Main.Get(19).ConstantValue;
// var dataItemList = dataItem.Split(',');
// DataManager.SaveItem(int.Parse(dataItemList[0]), int.Parse(dataItemList[1]));
DataManager.SaveFirst();
}
AssetBundleManager.ins.LoadAtlas();
2024-12-18 11:12:52 +08:00
goldCoin = 5;
drawingManager = transform.AddComponent<DrawingManager>();
forgePanel.ResetItemData();
forgePanel.ResetSlotItem();
_npcBuyDatas = new List<NPCBuyData>();
getNpc = false;
SaveFirstID();
UIMgr.ins.OpenForgePanel();
2024-12-18 11:12:52 +08:00
forgePanel.ResetGold();
});
2024-12-11 20:35:20 +08:00
// abManager.InitAsset(() =>
// {
// if (!DataManager.GetFirst())
// {
// //JsonTab.Instance.tables.Main.
// // var data = JsonTab.Instance.tables.Main.Get(18);
// // var idStr = data.ConstantValue;
// // var idList = idStr.Split(',');
// // foreach (var id in idList)
// // {
// // var intId = int.Parse(id);
// // DataManager.SaveItemUnLock(intId,true);
// // var pData = JsonTab.Instance.tables.CraftingRecipes.Get(intId);
// // if (pData.Levelprops1!=0)
// // {
// // DataManager.SaveItemUnLock(pData.Levelprops1,true);
// // }
// // if (pData.Levelprops2!=0)
// // {
// // DataManager.SaveItemUnLock(pData.Levelprops2,true);
// // }
// // if (pData.Levelprops3!=0)
// // {
// // DataManager.SaveItemUnLock(pData.Levelprops3,true);
// // }
// // if (pData.Levelprops4!=0)
// // {
// // DataManager.SaveItemUnLock(pData.Levelprops4,true);
// // }
// // }
// // var dataItem=JsonTab.Instance.tables.Main.Get(19).ConstantValue;
// // var dataItemList = dataItem.Split(',');
// // DataManager.SaveItem(int.Parse(dataItemList[0]), int.Parse(dataItemList[1]));
// DataManager.SaveFirst();
// }
//
// goldCoin = 5;
// drawingManager = transform.AddComponent<DrawingManager>();
// forgePanel.ResetItemData();
// forgePanel.ResetSlotItem();
// _npcBuyDatas = new List<NPCBuyData>();
// getNpc = false;
// SaveFirstID();
// forgePanel.ResetGold();
// });
2024-11-21 10:26:47 +08:00
}
2024-11-29 21:37:01 +08:00
2024-12-11 13:58:55 +08:00
/// <summary>
/// 重置商店刷新
/// </summary>
2024-11-21 10:26:47 +08:00
public void ResetItem()
{
forgePanel.ResetItemData();
2024-11-13 16:56:37 +08:00
}
2024-12-11 13:58:55 +08:00
/// <summary>
/// 开始添加npc
/// </summary>
2024-11-13 16:56:37 +08:00
public void StartNpcData()
{
foreach (var data in JsonTab.Instance.tables.Item.DataList)
2024-11-13 16:56:37 +08:00
{
2024-12-11 13:58:55 +08:00
if (DataManager.GetItemUnLock(data.ID) && data.NPC.Count != 0)
{
NPCBuyData npcBuyData = new NPCBuyData();
npcBuyData.buyID = data.ID;
npcBuyData.npcID = data.NPC[Random.Range(0, data.NPC.Count)];
_npcBuyDatas.Add(npcBuyData);
}
2024-11-13 16:56:37 +08:00
}
2024-12-11 13:58:55 +08:00
2024-11-13 16:56:37 +08:00
nowTime = 0;
SaveFirstID();
}
public void SaveNPCbo(bool bo)
{
getNpc = bo;
}
2024-12-11 13:58:55 +08:00
/// <summary>
/// 默认获得金币
/// </summary>
2024-11-21 10:26:47 +08:00
public void GoldRest()
{
goldCoin += playingGold;
2024-12-20 15:56:02 +08:00
DataManager.AddItem(2,playingGold);
2024-11-21 10:26:47 +08:00
forgePanel.ResetGold();
}
2024-12-11 13:58:55 +08:00
/// <summary>
/// 双倍获得金币
/// </summary>
2024-11-21 10:26:47 +08:00
public void DoubleGoldRest()
{
2024-12-11 13:58:55 +08:00
goldCoin += (playingGold * 2);
2024-12-20 15:56:02 +08:00
DataManager.AddItem(2,playingGold * 2);
2024-11-21 10:26:47 +08:00
forgePanel.ResetGold();
}
2024-12-11 13:58:55 +08:00
2024-11-21 10:26:47 +08:00
public void StartGamePanel()
{
forgePanel.OpenStartPanel();
}
2024-12-11 13:58:55 +08:00
/// <summary>
/// 设置购买物品和无物品判断
/// </summary>
2024-11-13 16:56:37 +08:00
private void SaveFirstID()
{
2024-12-11 13:58:55 +08:00
if (_npcBuyDatas.Count > 0)
2024-11-13 16:56:37 +08:00
{
drawingManager.itemID = _npcBuyDatas[0].buyID;
2024-12-16 17:25:10 +08:00
forgePanel.ResetCrafting(drawingManager.itemID,_npcBuyDatas[0].npcID);
2024-11-13 16:56:37 +08:00
forgePanel.Open();
}
else
{
forgePanel.Close();
}
}
2024-12-11 13:58:55 +08:00
2024-11-13 16:56:37 +08:00
// Start is called before the first frame update
void Start()
{
}
public void Crafting(bool bo)
{
2024-12-11 13:58:55 +08:00
drawingManager.CraftItem(bo, () =>
2024-11-13 16:56:37 +08:00
{
forgePanel.ResetItemData();
forgePanel.ResetSlotItem();
});
}
2024-12-11 13:58:55 +08:00
2024-11-13 16:56:37 +08:00
public void SaveSeledID(int sendID)
{
id = sendID;
index = -1;
seleTransform.GetComponent<SeletItemUI>().SaveSprite(id);
}
public void SaveIndex(int slotIndex)
{
index = slotIndex;
}
private void FixedUpdate()
{
2024-12-11 13:58:55 +08:00
if (_npcBuyDatas.Count > 0)
2024-11-13 16:56:37 +08:00
{
nowTime += Time.deltaTime;
2024-12-11 13:58:55 +08:00
if (nowTime > maxTime)
2024-11-13 16:56:37 +08:00
{
_npcBuyDatas = new List<NPCBuyData>();
nowTime = 0;
SaveFirstID();
}
}
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonUp(0))
{
2024-12-11 13:58:55 +08:00
if (id != 0)
2024-11-13 16:56:37 +08:00
{
2024-12-22 20:35:00 +08:00
if ( Input.mousePosition.y > Screen.height*0.6)
2024-11-13 16:56:37 +08:00
{
2024-12-11 13:58:55 +08:00
var itemData = JsonTab.Instance.tables.Item.Get(drawingManager.itemID);
if (drawingManager.itemID == id)
2024-11-13 16:56:37 +08:00
{
playingGold += itemData.Itemprice;
2024-11-13 16:56:37 +08:00
DataManager.RemoveItem(id, (bool bo) =>
{
if (bo)
{
2024-12-16 17:25:10 +08:00
var npcData = _npcBuyDatas[0];
2024-11-13 16:56:37 +08:00
_npcBuyDatas.Remove(_npcBuyDatas[0]);
SaveFirstID();
2024-12-11 13:58:55 +08:00
if (_npcBuyDatas.Count > 0)
2024-11-13 16:56:37 +08:00
{
2024-12-16 17:25:10 +08:00
forgePanel.SetData(true,npcData);
2024-11-13 16:56:37 +08:00
}
2024-11-21 10:26:47 +08:00
else
{
forgePanel.OpenStartPanel();
}
2024-11-13 16:56:37 +08:00
}
});
}
else
{
2024-12-11 13:58:55 +08:00
var itemprice = itemData.Itemprice * 0.01f;
playingGold += (int)itemprice;
2024-11-21 10:26:47 +08:00
DataManager.RemoveItem(id, (bool bo) =>
{
if (bo)
{
2024-12-16 17:25:10 +08:00
var npcData = _npcBuyDatas[0];
2024-11-21 10:26:47 +08:00
_npcBuyDatas.Remove(_npcBuyDatas[0]);
SaveFirstID();
2024-12-11 13:58:55 +08:00
if (_npcBuyDatas.Count > 0)
2024-11-21 10:26:47 +08:00
{
2024-12-16 17:25:10 +08:00
forgePanel.SetData(false,npcData);
2024-11-21 10:26:47 +08:00
}
else
{
forgePanel.OpenStartPanel();
}
}
});
2024-11-13 16:56:37 +08:00
}
}
else
{
2024-12-11 13:58:55 +08:00
if (index != -1)
2024-11-13 16:56:37 +08:00
{
2024-12-11 13:58:55 +08:00
drawingManager.SaveIndexID(index, id);
2024-11-13 16:56:37 +08:00
}
}
2024-12-13 20:38:15 +08:00
id = 0;
seleTransform.position = new Vector3(-3000, 0, 0);
index = -1;
getNpc = false;
forgePanel.ResetSlotItem();
forgePanel.ResetItemData();
2024-11-13 16:56:37 +08:00
}
}
2024-12-11 13:58:55 +08:00
2024-11-13 16:56:37 +08:00
if (Input.GetMouseButton(0))
{
2024-12-11 13:58:55 +08:00
if (id != 0)
2024-11-13 16:56:37 +08:00
{
Vector2 mouse = Input.mousePosition;
seleTransform.position = mouse;
}
}
}
2024-12-11 13:58:55 +08:00
}