469 lines
13 KiB
C#
469 lines
13 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class GameMgr : SingletonAutoMono<GameMgr>
|
||
{
|
||
// 保存上次检查日期的键
|
||
private string LastDayKey;
|
||
|
||
// 用于保存在线时间的键
|
||
private string OnlineTimeKey;
|
||
|
||
//场景中的萌宠
|
||
public List<PetObject> petList;
|
||
|
||
//等待删除的萌宠
|
||
private List<PetObject> delList;
|
||
|
||
//高亮的萌宠
|
||
private List<PetObject> hightLightList;
|
||
|
||
//分数
|
||
private int score=0;
|
||
|
||
//总在线时间
|
||
public float timeAll = 0;
|
||
//保存在线时间间隔
|
||
private float saveTime = 0;
|
||
|
||
//技能1的分数累计
|
||
public int skillOne = 0;
|
||
//技能1的分数累计
|
||
public int skillTwo = 0;
|
||
|
||
public void init()
|
||
{
|
||
petList = new List<PetObject>();
|
||
delList = new List<PetObject>();
|
||
hightLightList = new List<PetObject>();
|
||
LastDayKey = GameDataMgr.Instance.player.openId;
|
||
OnlineTimeKey = GameDataMgr.Instance.player.openId + "_online";
|
||
GameDataMgr.Instance.commonMsg.openId=GameDataMgr.Instance.player.openId;
|
||
StartCoroutine(MailGet());
|
||
}
|
||
|
||
// 协程:每10秒发送一次请求
|
||
IEnumerator MailGet()
|
||
{
|
||
while (true)
|
||
{
|
||
// 调用发送请求的方法
|
||
StartCoroutine(NetMgr.Instance.MailPost(GameDataMgr.Instance.commonMsg));
|
||
// 等待10秒再发送下一次请求
|
||
yield return new WaitForSeconds(10f);
|
||
|
||
}
|
||
}
|
||
|
||
void Start()
|
||
{
|
||
if (IsNewDay())
|
||
{
|
||
timeAll = 0;
|
||
}
|
||
else
|
||
{
|
||
// 加载之前保存的在线时间
|
||
LoadOnlineTime();
|
||
}
|
||
}
|
||
void Update()
|
||
{
|
||
timeAll += Time.deltaTime;
|
||
saveTime += Time.deltaTime;
|
||
if (saveTime>=5f)
|
||
{
|
||
//保存在线时间
|
||
SaveOnlineTime();
|
||
saveTime = 0f;
|
||
}
|
||
|
||
if (timeAll>=3600f)
|
||
{
|
||
timeAll = 3600f;
|
||
}
|
||
}
|
||
|
||
// 保存在线时间到本地
|
||
private void SaveOnlineTime()
|
||
{
|
||
PlayerPrefs.SetFloat(OnlineTimeKey, timeAll);
|
||
PlayerPrefs.Save();
|
||
}
|
||
|
||
// 从本地加载在线时间
|
||
private void LoadOnlineTime()
|
||
{
|
||
timeAll = PlayerPrefs.GetFloat(OnlineTimeKey, 0f);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断是否是新的一天
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
private bool IsNewDay()
|
||
{
|
||
// 获取当前日期(只获取到年月日)
|
||
string nowDay = DateTime.Now.ToString("yyyy-MM-dd");
|
||
|
||
// 从 PlayerPrefs 加载上次保存的日期
|
||
string lastDay = PlayerPrefs.GetString(LastDayKey, string.Empty);
|
||
|
||
// 如果上次保存的日期为空,说明是第一次运行游戏,或者是新的一天
|
||
if (string.IsNullOrEmpty(lastDay) || lastDay != nowDay)
|
||
{
|
||
// 更新 PlayerPrefs 中的日期
|
||
PlayerPrefs.SetString(LastDayKey, nowDay);
|
||
PlayerPrefs.Save();
|
||
|
||
//第一次运行游戏,清空玩家的在线礼物领取状态,并返还给服务器
|
||
for (int i = 0; i < GameDataMgr.Instance.player.onlineGiftOpen.Count; i++)
|
||
{
|
||
GameDataMgr.Instance.player.onlineGiftOpen[i] = false;
|
||
}
|
||
StartCoroutine(NetMgr.Instance.ChangeDataPost(GameDataMgr.Instance.player));
|
||
return true; // 返回 true,表示是新的一天
|
||
}
|
||
return false; // 返回 false,表示还是同一天
|
||
}
|
||
|
||
/// <summary>
|
||
/// 显示高亮
|
||
/// </summary>
|
||
/// <param name="pet"></param>
|
||
public void IsHightLight(PetObject pet)
|
||
{
|
||
pet.CreatHightLight();
|
||
hightLightList.Add(pet);
|
||
delList.Add(pet);
|
||
|
||
//遍历相同颜色,且距离在一定范围内,还没有添加进集合的物体
|
||
for (int i = 0; i < petList.Count; i++)
|
||
{
|
||
|
||
if (petList[i] != null && petList[i].petSprite.sprite == pet.petSprite.sprite && DistancePet(pet, petList[i]) && !petList[i].isHightLight)
|
||
{
|
||
IsHightLight(petList[i]);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 待删除萌宠按距离排序
|
||
/// </summary>
|
||
public void DelListSort()
|
||
{
|
||
Dictionary<PetObject, float> dic = new Dictionary<PetObject, float>();
|
||
List<KeyValuePair<PetObject, float>> li = new List<KeyValuePair<PetObject, float>>();
|
||
float f;
|
||
for (int i = 0; i < delList.Count; i++)
|
||
{
|
||
f = Vector2.Distance(delList[0].transform.position, delList[i].transform.position);
|
||
dic.Add(delList[i], f);
|
||
}
|
||
|
||
foreach (KeyValuePair<PetObject, float> kvp in dic)
|
||
{
|
||
li.Add(kvp);
|
||
}
|
||
li.Sort((a, b) =>
|
||
{
|
||
return a.Value > b.Value ? 1 : -1;
|
||
});
|
||
delList.Clear();
|
||
for (int i = 0; i < li.Count; i++)
|
||
{
|
||
delList.Add(li[i].Key);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 技能1随机删除萌宠
|
||
/// </summary>
|
||
public void SkillOneDelPet()
|
||
{
|
||
skillOne = 0;
|
||
PetObject pet = petList[UnityEngine.Random.Range(0, petList.Count)];
|
||
pet.CreatHightLight();
|
||
delList.Add(pet);
|
||
List<PetObject> listPet = new List<PetObject>();
|
||
|
||
int index;
|
||
//遍历所有萌宠,且距离在一定范围内,还没有添加进集合的物体
|
||
for (int i = 0; i < petList.Count; i++)
|
||
{
|
||
if (petList[i] != null && DistancePet(pet, petList[i]) && petList[i].isHightLight == false)
|
||
{
|
||
if (!delList.Contains(petList[i]))
|
||
{
|
||
listPet.Add(petList[i]);
|
||
}
|
||
}
|
||
}
|
||
if (listPet.Count > 2)
|
||
{
|
||
for (int j = 0; j < 2; j++)
|
||
{
|
||
index = UnityEngine.Random.Range(0, listPet.Count);
|
||
delList.Add(listPet[index]);
|
||
listPet.Remove(listPet[index]);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
switch (listPet.Count)
|
||
{
|
||
case 0:
|
||
break;
|
||
case 1:
|
||
delList.Add(listPet[0]);
|
||
listPet[0].CreatHightLight();
|
||
break;
|
||
case 2:
|
||
delList.Add(listPet[0]);
|
||
listPet[0].CreatHightLight();
|
||
delList.Add(listPet[1]);
|
||
listPet[0].CreatHightLight();
|
||
break;
|
||
}
|
||
}
|
||
listPet.Clear();
|
||
DelHightLight();
|
||
}
|
||
|
||
List<PetObject> li = new List<PetObject>();
|
||
/// <summary>
|
||
/// 技能2选择三个连续的萌宠删除
|
||
/// </summary>
|
||
public void SkillTwoDelPet(PetObject pet)
|
||
{
|
||
if (delList.Count==0)
|
||
{
|
||
pet.CreatHightLight();
|
||
delList.Add(pet);
|
||
}
|
||
|
||
li.Add(pet);
|
||
|
||
if (delList.Count>0)
|
||
{
|
||
for (int i = 0; i < delList.Count; i++)
|
||
{
|
||
if (DistancePet(delList[i], li[0]))
|
||
{
|
||
if (!delList.Contains(li[0]))
|
||
{
|
||
li[0].CreatHightLight();
|
||
delList.Add(li[0]);
|
||
}
|
||
}
|
||
//技能2待删除的列表 最大为3个
|
||
if (delList.Count == 3)
|
||
{
|
||
//重置技能需要的分数
|
||
skillTwo = 0;
|
||
StartCoroutine(DelPet());
|
||
PlayerMain.Instance.isSkillTwoStart = false;
|
||
li.Clear();
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
li.Clear();
|
||
}
|
||
|
||
//删除高亮物体逻辑
|
||
public void DelHightLight()
|
||
{
|
||
DelListSort();
|
||
StartCoroutine(DelPet());
|
||
}
|
||
|
||
//逐步消除
|
||
private IEnumerator DelPet()
|
||
{
|
||
//取消屏幕点击
|
||
PlayerMain.Instance.isStart = false;
|
||
|
||
for (int i = 0; i < delList.Count; i++)
|
||
{
|
||
petList.Remove(delList[i]);
|
||
yield return null;
|
||
}
|
||
//消除对象
|
||
for (int i = 0; i < delList.Count; i++)
|
||
{
|
||
//图片隐藏
|
||
delList[i].CloseSpirte();
|
||
//播放一次消除声音
|
||
PoolMgr.Instance.GetObj("Music/SoundMusic").GetComponent<SoundMusic>().PlaySoundMusic();
|
||
delList[i].DeleteHightLight();
|
||
//创建删除特效
|
||
delList[i].CreatDelEff();
|
||
if (i<delList.Count-1)
|
||
{
|
||
yield return new WaitForSeconds(0.2f);
|
||
}
|
||
}
|
||
//删除对象
|
||
for (int i = 0; i < delList.Count; i++)
|
||
{
|
||
if (delList[i]!=null)
|
||
{
|
||
Destroy(delList[i].gameObject);
|
||
yield return null;
|
||
}
|
||
}
|
||
//游戏界面分数变化
|
||
UIManager.Instance.GetPanel<GamePanel>().UpdateScore(AddScore());
|
||
//改变按钮颜色
|
||
UIManager.Instance.GetPanel<GamePanel>().UpdateButon(skillOne,skillTwo);
|
||
//清空列表
|
||
ClearDicInfo();
|
||
//通关
|
||
if (petList.Count == 0)
|
||
{
|
||
GamePanel gamePanel = UIManager.Instance.GetPanel<GamePanel>();
|
||
PlayerData player = GameDataMgr.Instance.player;
|
||
|
||
//是最后一关
|
||
if (gamePanel.levInt == GameDataMgr.Instance.levList.Count)
|
||
{
|
||
//显示最后一关的通关面板
|
||
UIManager.Instance.ShowPanel<Pass1Panel>();
|
||
//通关后,关卡分数里面的个数小于 通过的第几关
|
||
if (player.customLev < gamePanel.levInt)
|
||
{
|
||
player.customLev = gamePanel.levInt;
|
||
StartCoroutine(NetMgr.Instance.ChangeDataPost(player));
|
||
}
|
||
}
|
||
|
||
//不是最后一关
|
||
else
|
||
{
|
||
UIManager.Instance.ShowPanel<Pass2Panel>();
|
||
//通关后,关卡分数里面的个数小于 通过的第几关
|
||
if (player.customLev < gamePanel.levInt)
|
||
{
|
||
player.customLev = gamePanel.levInt;
|
||
StartCoroutine(NetMgr.Instance.ChangeDataPost(player));
|
||
}
|
||
|
||
}
|
||
petList.Clear();
|
||
}
|
||
PlayerMain.Instance.isStart = true;
|
||
}
|
||
|
||
//清空list
|
||
public void ClearDicInfo()
|
||
{
|
||
hightLightList.Clear();
|
||
delList.Clear();
|
||
}
|
||
|
||
//给外界判断两个物体之间的距离
|
||
public bool DistancePet(PetObject pet, PetObject pet2)
|
||
{
|
||
if (pet.gameObject == null || pet2.gameObject == null)
|
||
{
|
||
return false;
|
||
}
|
||
else if (Vector2.Distance(pet.transform.position, pet2.transform.position) < 2f)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
//取消高亮显示
|
||
public void CancelHightLight()
|
||
{
|
||
for (int i = 0; i < hightLightList.Count; i++)
|
||
{
|
||
hightLightList[i].DeleteHightLight();
|
||
}
|
||
ClearDicInfo();
|
||
}
|
||
|
||
//分数增加逻辑
|
||
private int AddScore()
|
||
{
|
||
if (delList.Count > 0)
|
||
{
|
||
if (delList.Count >= 5)
|
||
{
|
||
skillOne += 5 * 5;
|
||
if (skillOne>=100)
|
||
{
|
||
skillOne = 100;
|
||
}
|
||
skillTwo += 5 * 5;
|
||
if (skillTwo >= 200)
|
||
{
|
||
skillTwo = 200;
|
||
}
|
||
return score += 5 * 5;
|
||
}
|
||
else
|
||
{
|
||
skillOne += delList.Count * delList.Count;
|
||
if (skillOne >= 100)
|
||
{
|
||
skillOne = 100;
|
||
}
|
||
skillTwo += delList.Count * delList.Count;
|
||
if (skillTwo >= 200)
|
||
{
|
||
skillTwo = 200;
|
||
}
|
||
return score += delList.Count * delList.Count;
|
||
}
|
||
}
|
||
return score;
|
||
}
|
||
|
||
//开启创建萌宠协程函数
|
||
public void CreatePet()
|
||
{
|
||
StartCoroutine(CreatePetObj());
|
||
}
|
||
|
||
GameObject obj;
|
||
private List<GameObject> objs = new List<GameObject>();
|
||
//创建萌宠
|
||
public IEnumerator CreatePetObj()
|
||
{
|
||
List<PetInfo> pets = GameDataMgr.Instance.levData.petInfoList;
|
||
Transform target = GameObject.Find("GamePos").transform;
|
||
if (objs.Count!=0)
|
||
{
|
||
for (int i = 0; i < objs.Count; i++)
|
||
{
|
||
Destroy(objs[i].gameObject);
|
||
}
|
||
objs.Clear();
|
||
petList.Clear();
|
||
}
|
||
for (int i = 0; i < pets.Count; i++)
|
||
{
|
||
obj = Instantiate(Resources.Load<GameObject>(pets[i].res));
|
||
obj.transform.position = new Vector2(pets[i].post_x, pets[i].post_y);
|
||
obj.transform.SetParent(target, false);
|
||
obj.GetComponent<Rigidbody2D>().simulated = false;
|
||
petList.Add(obj.GetComponent<PetObject>());
|
||
objs.Add(obj);
|
||
}
|
||
for (int i = 0; i < objs.Count; i++)
|
||
{
|
||
objs[i].GetComponent<Rigidbody2D>().simulated = true;
|
||
}
|
||
yield return null;
|
||
}
|
||
|
||
}
|