WXGame/Blacksmith/Assets/Script/GameSystem.cs

303 lines
8.7 KiB
C#
Raw Normal View History

2024-11-13 16:56:37 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
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;
public float maxTime=100;
public float nowTime;
2024-11-21 10:26:47 +08:00
public int goldCoin=0;
public int playingGold=0;
2024-11-13 16:56:37 +08:00
[SerializeField]
2024-11-21 10:26:47 +08:00
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-11-13 16:56:37 +08:00
[SerializeField] private RectTransform seleTransform;
private int id=0;
private int index = -1;
private bool getNpc=false;
2024-12-04 17:26:27 +08:00
#if UNITY_EDITOR
[InspectorButton]
private void ClearPlayerPrefs()
{
PlayerPrefs.DeleteAll();
}
[InspectorButton]
private void AddDicData()
{
var datas=JsonTab.Instance.tables.Item.DataList;
foreach (var item in datas)
{
DataManager.SaveItemUnLock(item.ID,true);
DataManager.AddItem(item.ID,100);
}
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);
DataManager.SetNowLevel(71001);
}
2024-12-04 17:26:27 +08:00
#endif
2024-11-13 16:56:37 +08:00
private void Awake()
{
ins = this;
foreach (var data in List)
{
2024-12-04 17:26:27 +08:00
DataManager.AddItem(data.id,data.num);
2024-11-13 16:56:37 +08:00
}
2024-12-04 17:26:27 +08:00
tab=new JsonTab();
tab.InitData();
2024-11-21 10:26:47 +08:00
goldCoin = 5;
2024-11-13 16:56:37 +08:00
drawingManager=transform.AddComponent<DrawingManager>();
forgePanel.ResetItemData();
forgePanel.ResetSlotItem();
2024-12-04 17:26:27 +08:00
_npcBuyDatas = new List<NPCBuyData>();
2024-11-13 16:56:37 +08:00
getNpc = false;
SaveFirstID();
2024-11-21 10:26:47 +08:00
forgePanel.ResetGold();
2024-11-29 21:37:01 +08:00
abManager.InitAsset(()=>
{
2024-12-04 17:26:27 +08:00
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]));
2024-12-04 17:26:27 +08:00
DataManager.SaveFirst();
}
2024-11-29 21:37:01 +08:00
});
2024-11-21 10:26:47 +08:00
}
/// <summary>
/// 重置商店刷新
/// </summary>
2024-11-29 21:37:01 +08:00
2024-11-21 10:26:47 +08:00
public void ResetItem()
{
forgePanel.ResetItemData();
2024-11-13 16:56:37 +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
{
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
}
nowTime = 0;
SaveFirstID();
}
public void SaveNPCbo(bool bo)
{
getNpc = bo;
}
2024-11-21 10:26:47 +08:00
/// <summary>
/// 默认获得金币
/// </summary>
public void GoldRest()
{
goldCoin += playingGold;
forgePanel.ResetGold();
}
/// <summary>
/// 双倍获得金币
/// </summary>
public void DoubleGoldRest()
{
goldCoin += (playingGold*2);
forgePanel.ResetGold();
}
public void StartGamePanel()
{
forgePanel.OpenStartPanel();
}
/// <summary>
/// 设置购买物品和无物品判断
/// </summary>
2024-11-13 16:56:37 +08:00
private void SaveFirstID()
{
if (_npcBuyDatas.Count>0)
{
drawingManager.itemID = _npcBuyDatas[0].buyID;
forgePanel.ResetCrafting(drawingManager.itemID);
forgePanel.Open();
}
else
{
forgePanel.Close();
}
}
// Start is called before the first frame update
void Start()
{
}
public void Crafting(bool bo)
{
drawingManager.CraftItem(bo,() =>
{
forgePanel.ResetItemData();
forgePanel.ResetSlotItem();
});
}
public void SaveSeledID(int sendID)
{
id = sendID;
index = -1;
seleTransform.GetComponent<SeletItemUI>().SaveSprite(id);
}
public void SaveIndex(int slotIndex)
{
index = slotIndex;
}
private void FixedUpdate()
{
if (_npcBuyDatas.Count>0)
{
nowTime += Time.deltaTime;
if (nowTime>maxTime)
{
_npcBuyDatas = new List<NPCBuyData>();
nowTime = 0;
SaveFirstID();
}
}
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonUp(0))
{
if (id!=0)
{
if (Input.mousePosition.x>640&&Input.mousePosition.y>900)
{
var itemData=JsonTab.Instance.tables.Item.Get(drawingManager.itemID);
2024-11-13 16:56:37 +08:00
if (drawingManager.itemID==id)
{
playingGold += itemData.Itemprice;
2024-11-13 16:56:37 +08:00
DataManager.RemoveItem(id, (bool bo) =>
{
if (bo)
{
_npcBuyDatas.Remove(_npcBuyDatas[0]);
SaveFirstID();
2024-11-21 10:26:47 +08:00
2024-11-13 16:56:37 +08:00
if (_npcBuyDatas.Count>0)
{
forgePanel.SetData(true);
}
2024-11-21 10:26:47 +08:00
else
{
forgePanel.OpenStartPanel();
}
2024-11-13 16:56:37 +08:00
}
});
}
else
{
var itemprice=itemData.Itemprice * 0.01f;
playingGold += (int)itemprice;
2024-11-21 10:26:47 +08:00
DataManager.RemoveItem(id, (bool bo) =>
{
if (bo)
{
_npcBuyDatas.Remove(_npcBuyDatas[0]);
SaveFirstID();
if (_npcBuyDatas.Count>0)
{
forgePanel.SetData(false);
}
else
{
forgePanel.OpenStartPanel();
}
}
});
2024-11-13 16:56:37 +08:00
}
}
else
{
if (index!=-1)
{
drawingManager.SaveIndexID(index,id);
}
}
}
id = 0;
seleTransform.position = new Vector3(-3000, 0, 0);
index = -1;
getNpc = false;
forgePanel.ResetSlotItem();
forgePanel.ResetItemData();
}
if (Input.GetMouseButton(0))
{
if (id!=0)
{
Vector2 mouse = Input.mousePosition;
seleTransform.position = mouse;
}
}
}
}