327 lines
8.8 KiB
C#
327 lines
8.8 KiB
C#
using System;
|
|
using Thousandto.Core.Support;
|
|
using FLogger = UnityEngine.Gonbest.MagicCube.FLogger;
|
|
|
|
namespace Thousandto.Code.Logic
|
|
{
|
|
/// <summary>
|
|
/// 游戏数据状态的基础类
|
|
/// </summary>
|
|
public class GameStateBase : IGameState
|
|
{
|
|
#region //私有变量
|
|
GameStateId _stateId = 0;
|
|
//游戏状态参数
|
|
object _arg = null;
|
|
//游戏数据状态的状态
|
|
GameStateStatus status = GameStateStatus.None;
|
|
#endregion
|
|
|
|
#region//属性
|
|
public object Arg
|
|
{
|
|
get
|
|
{
|
|
return _arg;
|
|
}
|
|
set
|
|
{
|
|
_arg = value;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取状态的ID
|
|
/// </summary>
|
|
public GameStateId GameStateID
|
|
{
|
|
get
|
|
{
|
|
return OnGetStateID();
|
|
}
|
|
}
|
|
|
|
//获取当前是否位激活状态
|
|
public bool IsActived
|
|
{
|
|
get {
|
|
return (status == GameStateStatus.Running || status == GameStateStatus.Entered);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region//构造函数
|
|
public GameStateBase(GameStateId id)
|
|
{
|
|
_stateId = id;
|
|
}
|
|
#endregion
|
|
|
|
#region//公共函数
|
|
public int GetStateId()
|
|
{
|
|
return (int)_stateId;
|
|
}
|
|
/// <summary>
|
|
/// 初始化状态
|
|
/// </summary>
|
|
public void Load()
|
|
{
|
|
if (status == GameStateStatus.None)
|
|
{
|
|
status = GameStateStatus.Initialized;
|
|
//FLogger.Log(string.Format("GameState Load: {0}", GameStateID));
|
|
try
|
|
{
|
|
OnLoad();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 释放状态
|
|
/// </summary>
|
|
public void Release()
|
|
{
|
|
if (status != GameStateStatus.Destroied)
|
|
{
|
|
status = GameStateStatus.Destroied;
|
|
//FLogger.Log(string.Format("GameState Release: {0}", GameStateID));
|
|
try
|
|
{
|
|
OnRelease();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 检查切换状态
|
|
/// </summary>
|
|
/// <param name="state"></param>
|
|
public bool Check(IGameState state)
|
|
{
|
|
bool ret = false;
|
|
if (status == GameStateStatus.Initialized || status == GameStateStatus.Left)
|
|
{
|
|
try
|
|
{
|
|
ret = OnCheck(state);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
public void Enter()
|
|
{
|
|
status = GameStateStatus.Entered;
|
|
//FLogger.Log(string.Format("GameState Enter: {0}", GameStateID));
|
|
try
|
|
{
|
|
OnEnter();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
}
|
|
public void Leave()
|
|
{
|
|
if (status == GameStateStatus.Running || status == GameStateStatus.Paused)
|
|
{
|
|
status = GameStateStatus.Left;
|
|
//FLogger.Log(string.Format("GameState Leave: {0}", GameStateID));
|
|
try
|
|
{
|
|
OnLeave();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 挂起状态
|
|
/// </summary>
|
|
/// <param name="cue"></param>
|
|
public void Suspend()
|
|
{
|
|
if (status == GameStateStatus.Running || status == GameStateStatus.Entered)
|
|
{
|
|
status = GameStateStatus.Paused;
|
|
//FLogger.Log(string.Format("GameState Suspend: {0}", GameStateID));
|
|
try
|
|
{
|
|
OnSuspend();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 唤醒状态
|
|
/// </summary>
|
|
/// <param name="cue"></param>
|
|
public void Resume()
|
|
{
|
|
if (status == GameStateStatus.Paused)
|
|
{
|
|
status = GameStateStatus.Running;
|
|
//FLogger.Log(string.Format("GameState Resume: {0}", GameStateID));
|
|
try
|
|
{
|
|
OnResume();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 状态的心跳处理
|
|
/// </summary>
|
|
/// <param name="deltaTime"></param>
|
|
/// <returns></returns>
|
|
public bool Update(float deltaTime)
|
|
{
|
|
if (status == GameStateStatus.Running || status == GameStateStatus.Entered)
|
|
{
|
|
status = GameStateStatus.Running;
|
|
try
|
|
{
|
|
return OnUpdate(deltaTime);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
/// <summary>
|
|
/// 状态的渲染处理
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool Render()
|
|
{
|
|
try
|
|
{
|
|
return OnRender();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
return false;
|
|
}
|
|
/// <summary>
|
|
/// 处理转进
|
|
/// </summary>
|
|
/// <param name="dt"></param>
|
|
/// <returns></returns>
|
|
public bool ProcessTransitionIn(float dt)
|
|
{
|
|
try
|
|
{
|
|
return OnProcessTransitionIn(dt);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
return false;
|
|
}
|
|
/// <summary>
|
|
/// 处理转出
|
|
/// </summary>
|
|
/// <param name="dt"></param>
|
|
/// <returns></returns>
|
|
public bool ProcessTransitionOut(float dt)
|
|
{
|
|
try
|
|
{
|
|
return OnProcessTransitionOut(dt);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
return false;
|
|
}
|
|
/// <summary>
|
|
/// 渲染转进
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool RenderTransitionIn()
|
|
{
|
|
try
|
|
{
|
|
return OnRenderTransitionIn();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
return false;
|
|
}
|
|
/// <summary>
|
|
/// 渲染转出
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool RenderTransitionOut()
|
|
{
|
|
try
|
|
{
|
|
return OnRenderTransitionOut();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
return false;
|
|
}
|
|
public void HandlerMessage(object msg)
|
|
{
|
|
try
|
|
{
|
|
OnHandlerMessage(msg);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FLogger.LogException(e);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region//需要由子类重写的保护虚函数
|
|
protected virtual GameStateId OnGetStateID(){return GameStateId.None;}
|
|
protected virtual void OnLoad() { }
|
|
protected virtual void OnRelease() { }
|
|
protected virtual bool OnCheck(IGameState state) { return true; }
|
|
protected virtual void OnEnter() { }
|
|
protected virtual void OnLeave() { }
|
|
protected virtual void OnSuspend() { }
|
|
protected virtual void OnResume() { }
|
|
protected virtual void OnHandlerMessage(object msg) { }
|
|
protected virtual bool OnUpdate(float deltaTime) { return true; }
|
|
protected virtual bool OnRender() { return true; }
|
|
protected virtual bool OnRenderTransitionIn() { return true; }
|
|
protected virtual bool OnRenderTransitionOut() { return true; }
|
|
protected virtual bool OnProcessTransitionIn(float dt) { return true; }
|
|
protected virtual bool OnProcessTransitionOut(float dt) { return true; }
|
|
#endregion
|
|
|
|
}
|
|
}
|