WXGame/box1/Assets/Script/GameManager.cs
DESKTOP-DDTRVOR\asus 8883663e13 修改差异
2024-12-28 18:48:02 +08:00

409 lines
13 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using cfg;
using TTSDK;
using TTSDK.UNBridgeLib.LitJson;
// using Unity.VisualScripting;
using UnityEngine;
using Random = UnityEngine.Random;
public class GameManager : MonoBehaviour
{
public static GameManager ins;
public TaskSystem taskSystem;
public SoundSystem soundSystem;
private JsonTab _tab;
public bool start;
public Camera mainCamera;
public int ChapterId => chapterId;
private int chapterId;
public int level => levelID;
private int levelID;
public float NowLenght => nowLenght;
private float nowLenght=0;
public List<ItemData> itemDatas;
public List<ItemData> itemDatas1;
public List<ItemData> itemDatas2;
public List<RandomItemData> list;
public float NowTime => nowTime;
private float nowTime=0;
public bool firstPlay = true;
[SerializeField] public AssetData _data;
#if UNITY_EDITOR
[InspectorButton]
#endif
private void PlayerPrefsClear()
{
PlayerPrefs.DeleteAll();
}
public ItemObj seletObj
{
get { return seletItem; }
set
{
seletItem = value;
if (seletUnlock!=null)
{
seletUnlock.transform.position = MapManager.ins.lockParent.position;
seletUnlock = null;
}
}
}
private ItemObj seletItem;
public UnLockGrid seletUnlock
{
get { return seletLockObj; }
set
{
seletLockObj = value;
if (seletObj != null)
{
ItemSystem.ins.GetGridsRest(seletObj.startGrid, seletObj);
seletObj = null;
}
}
}
private UnLockGrid seletLockObj;
public Grid stopGrid;
private Vector3 oldvec;
public int index = 2;
[SerializeField] private float scale=0.2f;
private void Awake()
{
TT.InitSDK((code, env) =>
{
ins = this;
_tab = new JsonTab();
_tab.InitData();
_data.InitData();
firstPlay = true;
TT.ReportAnalytics<string>("a10101");
DotData.ins.SendEvent("a10101","");
MainPanel.ins.ClosePanelEvent();
});
TT.SendToTAQ(new JsonData
{
["event_type"] = "active"
});
TT.SendToTAQ(new JsonData
{
["event_type"] = "next_day_open"
});
}
private void Start()
{
}
public void SaveNowLenght(float lenght)
{
nowLenght = lenght;
}
public void ResetPack(List<RandomItemData> random,Action action)
{
start = true;
index = 2;
// var indexID = Random.Range(0, 4);
// switch (indexID)
// {
// case 0:
// ItemSystem.ins.InitItem(itemDatas);
// break;
// case 1:
// ItemSystem.ins.InitItem(itemDatas1);
// break;
// case 2:
// ItemSystem.ins.InitItem(itemDatas2);
// break;
// default:
// ItemSystem.ins.InitItem(itemDatas);
// break;
// }
StartCoroutine(ItemSystem.ins.RandomItem(random, () =>
{
action.Invoke();
MapManager.ins.PlayBoxOpen();
MapManager.ins.ResetLockGrid();
ItemSystem.ins.ResetItemOrder();
}));
}
public void ResetLevel(int id,Action action)
{
StartCoroutine(ResetIE(id, action));
}
IEnumerator ResetIE(int id,Action action)
{
BoxDebug.Log("开始关卡当前关卡id:" +id);
var data = JsonTab.Instance.tables.Level.Get(id);
BoxDebug.Log("关卡数据" + data);
levelID = id;
var itemData = JsonTab.Instance.tables.RandomProp.Get(data.ItemList);
BoxDebug.Log("关卡随机道具表数据" +itemData);
var maxTypeweight = 0;
foreach (var weight in itemData.Typeweight)
{
maxTypeweight += weight;
}
yield return null;
BoxDebug.Log("道具总权重"+maxTypeweight);
var maxLevelWeight=0;
foreach (var weight in itemData.Levelweight)
{
maxLevelWeight += weight;
}
yield return null;
BoxDebug.Log("等级总权重"+maxLevelWeight);
int[] propNum = new int[itemData.Typeweight.Count];
for (int i = 0; i < itemData.Typeweight.Count; i++)
{
float a = (float)itemData.Typeweight[i] / (float)maxTypeweight;
propNum[i] = (int)(itemData.Grids * a);
BoxDebug.Log("道具类型"+itemData.Propid[i]+"的数量为:"+propNum);
}
yield return null;
List<RandomItemData> randomItemlist = new List<RandomItemData>();
for (int i = 0; i < propNum.Length; i++)
{
var num = propNum[i];
for (int j = 0; j < itemData.Levelweight.Count; j++)
{
float a = (float)itemData.Levelweight[j] / (float)maxLevelWeight;
var levelNum = (int)(num*a);
RandomItemData randomItemData = new RandomItemData();
randomItemData.num = levelNum;
randomItemData.level = j+1;
var itemID = (from bProp in JsonTab.Instance.tables.Prop.DataList where bProp.PropType == itemData.Propid[i] && bProp.Proplevel == j + 1 select bProp.ID).FirstOrDefault();
randomItemData.itemID = itemID;
var propData = JsonTab.Instance.tables.Prop.DataList.FirstOrDefault(bProp => bProp.PropType == itemData.Propid[i]);
randomItemData.item = ItemSystem.ins.itemObj(propData.Preform);
randomItemlist.Add(randomItemData);
BoxDebug.Log("道具id"+randomItemData.itemID+",数量"+randomItemData.num+",等级"+randomItemData.level);
}
}
list = randomItemlist;
ResetPack(list,action);
MapManager.ins.InitLockData();
MapManager.ins.RandomUnlock();
videoBo = true;
yield return null;
nowTime = 0;
}
public bool VideoBo => videoBo;
private bool videoBo = false;
public void OpenVideoLock()
{
videoBo = true;
MainPanel.ins.ResetPackLockButtonRest();
}
public void PackEvent()
{
index--;
if (index<=0)
{
bool win = true;
foreach (var item in ItemSystem.ins.itemObjs)
{
if (item.type==GridType.wait)
{
win = false;
}
}
start = false;
MapManager.ins.PlayBoxClose();
MainPanel.ins.StartTourPanel();
nowTime = 0;
return;
}
else
{
MapManager.ins.PlayBoxClose(()=>
{
MapManager.ins.PlayBoxOpen();
});
}
ItemSystem.ins.PackItem();
}
/// <summary>
/// 刷新背包获得道具数据
/// </summary>
public void ResetPropattributes()
{
ItemSystem.ins.ResetPropattributes();
MainPanel.ins.ResetPropattributes();
}
private void Update()
{
if (start)
{
nowTime += Time.deltaTime;
if (nowTime>=300)
{
index=0;
PackEvent();
}
}
if (Input.GetMouseButtonDown(0))
{
oldvec = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
if (seletUnlock!=null )
{
if (stopGrid!=null&& MapManager.ins.UnLockOpen(stopGrid,seletUnlock.openData))
{
MapManager.ins.LockRayRest(stopGrid, seletUnlock.openData,out var bo);
soundSystem.PlaceAudio();
MapManager.ins.ResetRayLockGrid();
MapManager.ins.LockAddData();
MapManager.ins.CloseUnlock();
videoBo = false;
MainPanel.ins.ResetPackLockButtonRest();
}
else
{
MapManager.ins.ResetRayLockGrid();
MapManager.ins.ResetUnlock();
}
seletUnlock = null;
}
if (seletObj!= null)
{
BoxDebug.Log(stopGrid);
BoxDebug.Log(stopGrid.GetItemObj());
if (stopGrid!= null && stopGrid.GetItemObj()!= null&& stopGrid.GetItemObj()!=seletObj)
{
ItemSystem.ins.ItemLevelUp(stopGrid.GetItemObj(), seletObj);
ItemSystem.ins.ResetWaitGrid();
}
else
{
if (MapManager.ins.openSave(stopGrid,seletObj))
{
foreach (var grid in seletObj._grids)
{
switch (grid.Type)
{
case GridType.box:
grid.item = null;
break;
case GridType.wait:
grid.Remove(seletObj);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
seletObj.startGrid = stopGrid;
soundSystem.PlaceAudio();
ItemSystem.ins.GetGridsRest(seletObj.startGrid, seletObj);
ItemSystem.ins.ResetWaitGrid();
}
}
if (seletObj!=null)
{
ItemSystem.ins.SetPosition(seletObj, seletObj.startGrid);
}
ItemSystem.ins.ResetItemOrder();
stopGrid = null;
seletObj = null;
// if (ItemSystem.ins.WaitGridIsNull())
// {
// StartCoroutine(ItemSystem.ins.AddNewItem());
// }
MapManager.ins.ResetAnimation();
ResetPropattributes();
}
}
if (seletObj!=null)
{
Ray rays = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hits;
if (Physics.Raycast(rays, out hits))
{
//射线检测第三个参数可根据需求设置或不设置具体看api;
Physics.Raycast(rays, out hits, 500000);
Vector3 mousePos = hits.point+seletObj.mouseDev;
if (MapManager.ins.GetRegionGridType(mousePos,out var type))
{
MapManager.ins.RegionGrid(mousePos, type, out var grid);
if (stopGrid!=grid)
{
stopGrid = grid;
MapManager.ins.RegionGridBack(stopGrid, seletObj.itemSize.x+stopGrid.X, seletObj.itemSize.y+stopGrid.Y);
}
}
}
}
if (seletUnlock!=null)
{
Ray rays = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hits;
if (Physics.Raycast(rays, out hits))
{
//射线检测第三个参数可根据需求设置或不设置具体看api;
Physics.Raycast(rays, out hits, 500000);
Vector3 mousePos = hits.point + seletUnlock.mouseDev;
if (MapManager.ins.GetRegionGridType(mousePos,out var type))
{
MapManager.ins.RegionGrid(mousePos, type, out var grid);
BoxDebug.Log(stopGrid+" "+ grid);
if (stopGrid!=grid)
{
stopGrid = grid;
MapManager.ins.RayToutchGrid(stopGrid, seletUnlock.openData);
}
}
}
}
if (Input.GetMouseButton(0) && seletObj!=null&& start)
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//射线检测第三个参数可根据需求设置或不设置具体看api;
Physics.Raycast(ray, out hit, 500000);
Vector3 mousePos = hit.point;
seletObj.transform.position = mousePos;
}
if (Input.GetMouseButton(0) && seletUnlock!= null && start)
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//射线检测第三个参数可根据需求设置或不设置具体看api;
Physics.Raycast(ray, out hit, 500000);
Vector3 mousePos = hit.point;
seletUnlock.transform.position = mousePos;
}
}
}