unity_art_puzzle_playable_luna/APPlayableLuna/Assets/Scripts/GameManager.cs

436 lines
13 KiB
C#
Raw Permalink Normal View History

2023-12-21 08:33:05 +00:00
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
namespace GuruClient
{
public class GameManager : MonoBehaviour
{
private static GameManager _instance;
public static GameManager Instance
{
get
{
return _instance;
}
}
[HideInInspector]
[LunaPlaygroundField("Amount of lives1", 1, "Game Settings")]
public int maxLives1 = 3;
[HideInInspector]
[LunaPlaygroundField("Amount of lives2", 1, "Game Settings")]
public int maxLives2 = 3;
[HideInInspector]
[LunaPlaygroundField("Amount of lives3", 1, "Game Settings")]
public int maxLives3 = 3;
public Camera UICamera;
public GameObjectPool PatchPool { set; get; }
private bool _poolInited = false;
private GameModel _model;
public UIGameView uiGameView;
public GameObject uiGameWin;
public GameObject patchTemplate;
//本关需要填充的补丁
private List<APatch> _stageAllPatch = new List<APatch>();
private List<APatch> _stageAllLine = new List<APatch>();
private List<APatch> _stageCompletePatch = new List<APatch>();
private List<APatch> _needAutoPatch = new List<APatch>();
private List<APatch> _finishAutoPatch = new List<APatch>();
//继续游戏时,需要恢复的拼图数据
private List<APatch> _recoverPatch = new List<APatch>();
[HideInInspector]
public bool _canDragging = false;
//是否正在跨越关卡
private bool _isStageCross = false;
//是否已完成
private bool _isGameWin = false;
[HideInInspector]
public StartMode curStartMode;
//当前局的bPlay次数
private int _curBPlay;
//操作人员
public string Operator {get; set;}
//从LevelStart时间开始记录截止到LevelEnd事件的关卡时长
private int _levelDuration;
//从LevelStart时间开始记录截止到LevelEnd事件的拼图数量
private int _levelDurationStep;
public bool isOrientationPortrait = true;
// Start is called before the first frame update
void Start()
{
_instance = this;
if(Screen.width > Screen.height)
{
Debug.Log("这是横屏");
isOrientationPortrait = true;
}
else
{
Debug.Log("这是竖屏");
isOrientationPortrait = false;
}
GameManager.Instance.InitPatchPool();
_model = new GameModel();
bool isSuccess = _model.InitializeLevel("395", StartMode.Continue, LevelType.MainLine);
if (isSuccess)
{
Debug.Log("关卡初始化成功");
_model.LoadAllPatches();
Debug.Log("关卡拼图加载成功");
uiGameView.StartGame();
}
}
public void InitPatchPool()
{
if (_poolInited)
{
return;
}
PatchPool = GameObjectPool.CreatePool(patchTemplate, 50, false);
_poolInited = true;
}
public GameModel GetModel()
{
return _model;
}
/// <summary>
/// 开始关卡的Stage
/// </summary>
public void StartStage()
{
//根据当前数据恢复关卡
if (_model.GetCurrentStage() > 1 || _model.GetCurrentStep() > 0 || _stageCompletePatch.Count > 0)
{
ToNextLevel(true);
}else
{
ToNextLevel(false);
}
}
/// <summary>
/// 前往下一关
/// </summary>
private void ToNextLevel()
{
ToNextLevel(false);
}
/// <summary>
/// 前往下一关
/// </summary>
/// <param name="isRecover">是否从进度文件恢复</param>
public void ToNextLevel(bool isRecover)
{
if (!isRecover)
{
_model.SetCurrentStage(_model.GetCurrentStage() + 1);
_model.SetCurrentStep(0);
_stageCompletePatch.Clear();
}else
{
uiGameView.LoadAutoPatches(_recoverPatch, false, true);
}
_stageAllPatch.Clear();
_stageAllLine.Clear();
_needAutoPatch.Clear();
_finishAutoPatch.Clear();
int curStage = _model.GetCurrentStage();
List<APatch> tmp = _model.GetStageAllPatch(curStage);
if (tmp != null)
{
_stageAllPatch = tmp;
}
tmp = _model.GetLineAllPatch(curStage);
if (tmp != null)
{
_stageAllLine = tmp;
}
tmp = _model.GetAutoAllPatch(curStage);
if (tmp != null)
{
_needAutoPatch = tmp;
}
_finishAutoPatch.Add(_model.GetFinishPatch());
if (_stageAllPatch.Count > 0)
{
//加载关卡
LoadLevel(isRecover, null);
}else
{
CompletePage(null);
}
_isStageCross = false;
}
/// <summary>
/// 关卡完成
/// </summary>
public void CompletePage(Action callback)
{
//自动补图
if (uiGameView != null)
{
try
{
uiGameView.LoadRole(true, false, callback);
Timer.Instance.SetTimeoutAsync(2.0f, () =>
{
GameWin();
});
}
catch (Exception e)
{
Debug.Log(e);
}
}
}
public void GameWin()
{
if (!_isGameWin)
{
//显示GameWin
_isGameWin = true;
uiGameWin.SetActive(true);
uiGameWin.GetComponent<UIGameWin>().OnBeforStart(_model.GetCurrentLevel());
}
}
/// <summary>
/// 加载关卡
/// </summary>
/// <param name="isRecover"></param>
public void LoadLevel(bool isRecover, Action callback)
{
_canDragging = false;
if (uiGameView != null)
{
if (!isRecover)
{
Timer.Instance.SetTimeoutAsync(0.5f, callback);
}
if (uiGameView != null)
{
if (_model.GetCurrentStage() == 1 || isRecover)
{
uiGameView.LoadBg(callback);
}
LoadLevelInner(isRecover, callback);
}
}
}
private void LoadLevelInner(bool isRecover, Action callback)
{
uiGameView.LoadRole(isRecover, false, callback);
LoadLineTemplate(isRecover,null);
}
/// <summary>
/// 加载线稿
/// </summary>
/// <param name="isRecover"></param>
private void LoadLineTemplate(bool isRecover, Action callback)
{
if(uiGameView != null)
{
uiGameView.LoadLineTemplate(callback);
InitPatches(isRecover);
}
}
/// <summary>
/// 是否可拖拽(拼图)
/// </summary>
/// <returns></returns>
public bool CanDragging()
{
return _canDragging;
}
/// <summary>
/// 初始化拼图
/// </summary>
/// <param name="isRecover">是否为恢复关卡</param>
private void InitPatches(bool isRecover)
{
bool isShowCurrentPatche = _model.IsShowCurrentPatche();
if(_model.GetCurrentStage() == 1 || isRecover || isShowCurrentPatche)
{
if(uiGameView != null)
{
uiGameView.InitPatches(isShowCurrentPatche ? _model.GetCurrentStage() : -1, () => {
//第一阶段和恢复的保证加载完成后,才可拼图
_canDragging = true;
uiGameView.ShowTips(true);
});
}
}else
{
//第二阶段,不需要加载拼图,直接可拼图
_canDragging = true;
}
if(isRecover)
{
CheckNextLevel();
}
EventManager.Dispatch("START_NEXT_LEVEL");
}
/// <summary>
/// 匹配后增加score分数
/// </summary>
/// <param name="aPatch">拼图数据</param>
/// <returns></returns>
public int AddScore(APatch aPatch)
{
LevelDurationStepUpdate();
_model.SetCurrentStep(_model.GetCurrentStep() + 1);
int step = _model.AddTotalStep();
_model.AddCompletePatchCount(1);
Luna.Unity.Analytics.LogEvent($"CompleteStep_{step}", 0);
_stageCompletePatch.Add(aPatch);
return _stageCompletePatch.Count;
}
public bool IsCompleteStage()
{
return _stageAllPatch.Count == _stageCompletePatch.Count && _stageAllPatch.Count != 0;
}
//关卡步数+1步
public void LevelDurationStepUpdate()
{
_levelDurationStep ++;
}
//获取一个未完成拼图
public APatch GetOneUnCompletePatch()
{
IEnumerable<APatch> collection = _stageAllPatch.Except(_stageCompletePatch);
foreach (APatch item in collection)
{
return item;
}
return new APatch();
}
/// <summary>
/// 检测某个拼图数据是否已完成
/// </summary>
/// <param name="aPatch"></param>
/// <returns></returns>
public bool IsCompletedPatch(APatch aPatch)
{
return _stageCompletePatch.Contains(aPatch);
}
/// <summary>
/// 游戏是否已结束
/// </summary>
/// <returns></returns>
public bool IsGameWin()
{
return _isGameWin;
}
/// <summary>
/// 获取当前阶段需要拼接的数据
/// </summary>
/// <returns></returns>
public List<APatch> GetCurrentStageData()
{
return _stageAllPatch;
}
/// <summary>
/// 获取当前阶段的线稿数据
/// </summary>
/// <returns></returns>
public List<APatch> GetCurrentStageLineData()
{
return _stageAllLine;
}
/// <summary>
/// 检测是否应该进入下一关
/// </summary>
/// <param name="step"></param>
public void CheckNextLevel(int step = -1)
{
if (!_isStageCross && (step == -1 ? _model.GetCurrentStep() : step) >= _stageAllPatch.Count)
{
_isStageCross = true;
if (uiGameView != null)
{
uiGameView.RemoveLineTemplate(null);
}
if (_needAutoPatch.Count > 0)
{
//自动补图
if (uiGameView != null)
{
if(uiGameView != null)
{
int curStage = _model.GetCurrentStage();
List<APatch> tmp = _model.GetStageAllPatch(curStage + 1);
if(tmp == null)
{
uiGameView.LoadAutoPatches(_needAutoPatch, false, false, () => {
ToNextLevel();
});
}else
{
uiGameView.LoadAutoPatches(_needAutoPatch);
ToNextLevel();
}
}
}
}else
{
ToNextLevel();
}
}
}
void OnApplicationQuit()
{
Luna.Unity.LifeCycle.GameEnded();
}
}
}