Update
This commit is contained in:
ALEXTANG
2023-05-13 20:31:11 +08:00
parent 7bf081269c
commit 0397d0f01e
8 changed files with 1846 additions and 65 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: e7f73a9317cd2134fa6613d787208825
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,7 +1,23 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
namespace TEngine
{
[AttributeUsage(AttributeTargets.Class)]
public class UpdateAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Class)]
public class LateUpdateAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Class)]
public class FixedUpdateAttribute : Attribute
{
}
/// <summary>
/// 通过LogicSys来驱动且具备Unity完整生命周期的单例不继承MonoBehaviour
/// </summary>
@@ -53,14 +69,30 @@ namespace TEngine
{
}
/// <summary>
/// 帧更新。
/// <remarks>需要UpdateAttribute。</remarks>
/// </summary>
public virtual void Update()
{
}
/// <summary>
/// 后帧更新。
/// <remarks>需要LateUpdateAttribute。</remarks>
/// </summary>
public virtual void LateUpdate()
{
}
/// <summary>
/// 物理帧更新。
/// <remarks>需要FixedUpdateAttribute。</remarks>
/// </summary>
public virtual void FixedUpdate()
{
}
public virtual void Destroy()
{
}
@@ -84,12 +116,25 @@ namespace TEngine
private readonly List<BaseBehaviourSingleton> _listStart = new List<BaseBehaviourSingleton>();
private readonly List<BaseBehaviourSingleton> _listUpdate = new List<BaseBehaviourSingleton>();
private readonly List<BaseBehaviourSingleton> _listLateUpdate = new List<BaseBehaviourSingleton>();
private readonly List<BaseBehaviourSingleton> _listFixedUpdate = new List<BaseBehaviourSingleton>();
public void RegSingleton(BaseBehaviourSingleton inst)
{
Log.Assert(!_listInst.Contains(inst));
_listInst.Add(inst);
_listStart.Add(inst);
if (HadAttribute<UpdateAttribute>(inst.GetType()))
{
_listUpdate.Add(inst);
}
if (HadAttribute<LateUpdateAttribute>(inst.GetType()))
{
_listLateUpdate.Add(inst);
}
if (HadAttribute<FixedUpdateAttribute>(inst.GetType()))
{
_listFixedUpdate.Add(inst);
}
}
public void UnRegSingleton(BaseBehaviourSingleton inst)
@@ -175,6 +220,20 @@ namespace TEngine
}
}
public override void OnFixedUpdate()
{
var listFixedUpdate = _listFixedUpdate;
var listFixedUpdateCnt = listFixedUpdate.Count;
for (int i = 0; i < listFixedUpdateCnt; i++)
{
var inst = listFixedUpdate[i];
TProfiler.BeginFirstSample(inst.GetType().FullName);
inst.FixedUpdate();
TProfiler.EndFirstSample();
}
}
public override void OnDestroy()
{
for (int i = 0; i < _listInst.Count; i++)
@@ -208,5 +267,12 @@ namespace TEngine
inst.OnDrawGizmos();
}
}
private bool HadAttribute<T>(Type type) where T:Attribute
{
T attribute = Attribute.GetCustomAttribute(type, typeof(T)) as T;
return attribute != null;
}
}
}

View File

@@ -11,7 +11,8 @@ using Random = UnityEngine.Random;
/// <summary>
/// 战斗房间
/// </summary>
public class BattleRoom
[Update]
public class BattleSystem:BehaviourSingleton<BattleSystem>
{
private enum ESteps
{
@@ -44,15 +45,50 @@ public class BattleRoom
private UniTimer _spawnWaitTimer = UniTimer.CreateOnceTimer(0.75f);
private UniTimer _waveWaitTimer = UniTimer.CreateOnceTimer(4f);
public BattleRoom()
{
Utility.Unity.AddUpdateListener(UpdateRoom);
}
~BattleRoom()
/// <summary>
/// 加载房间
/// </summary>
public IEnumerator LoadRoom()
{
Utility.Unity.RemoveUpdateListener(UpdateRoom);
DestroyRoom();
// 创建房间根对象
_roomRoot = new GameObject("BattleRoom");
// 加载背景音乐
GameModule.Audio.Play(AudioType.Music, "music_background", true);
// 创建游戏对象发生器
_entitySpawner = UniPooling.CreateSpawner("DefaultPackage");
// 创建游戏对象池
yield return _entitySpawner.CreateGameObjectPoolAsync("player_ship");
yield return _entitySpawner.CreateGameObjectPoolAsync("player_bullet");
yield return _entitySpawner.CreateGameObjectPoolAsync("enemy_ship");
yield return _entitySpawner.CreateGameObjectPoolAsync("enemy_bullet");
yield return _entitySpawner.CreateGameObjectPoolAsync("asteroid01");
yield return _entitySpawner.CreateGameObjectPoolAsync("asteroid02");
yield return _entitySpawner.CreateGameObjectPoolAsync("asteroid03");
yield return _entitySpawner.CreateGameObjectPoolAsync("explosion_asteroid");
yield return _entitySpawner.CreateGameObjectPoolAsync("explosion_enemy");
yield return _entitySpawner.CreateGameObjectPoolAsync("explosion_player");
// 创建玩家实体对象
var handle = _entitySpawner.SpawnSync("player_ship", _roomRoot.transform);
var entity = handle.GameObj.GetComponent<EntityPlayer>();
entity.InitEntity(handle);
// 显示战斗界面
yield return GameModule.UI.ShowUIAsync<UIBattleWindow>();
// 监听游戏事件
GameEvent.AddEventListener<Vector3,Quaternion>(ActorEventDefine.PlayerDead,OnPlayerDead);
GameEvent.AddEventListener<Vector3,Quaternion>(ActorEventDefine.EnemyDead,OnEnemyDead);
GameEvent.AddEventListener<Vector3,Quaternion>(ActorEventDefine.AsteroidExplosion,OnAsteroidExplosion);
GameEvent.AddEventListener<Vector3,Quaternion>(ActorEventDefine.PlayerFireBullet,OnPlayerFireBullet);
GameEvent.AddEventListener<Vector3,Quaternion>(ActorEventDefine.EnemyFireBullet,OnEnemyFireBullet);
_startWaitTimer.Reset();
_steps = ESteps.Ready;
}
/// <summary>
@@ -70,6 +106,18 @@ public class BattleRoom
Object.Destroy(_roomRoot);
GameModule.UI.CloseWindow<UIBattleWindow>();
// 监听游戏事件
GameEvent.RemoveEventListener<Vector3,Quaternion>(ActorEventDefine.PlayerDead,OnPlayerDead);
GameEvent.RemoveEventListener<Vector3,Quaternion>(ActorEventDefine.EnemyDead,OnEnemyDead);
GameEvent.RemoveEventListener<Vector3,Quaternion>(ActorEventDefine.AsteroidExplosion,OnAsteroidExplosion);
GameEvent.RemoveEventListener<Vector3,Quaternion>(ActorEventDefine.PlayerFireBullet,OnPlayerFireBullet);
GameEvent.RemoveEventListener<Vector3,Quaternion>(ActorEventDefine.EnemyFireBullet,OnEnemyFireBullet);
}
public override void Update()
{
UpdateRoom();
}
/// <summary>
@@ -140,49 +188,6 @@ public class BattleRoom
}
}
/// <summary>
/// 加载房间
/// </summary>
public IEnumerator LoadRoom()
{
// 创建房间根对象
_roomRoot = new GameObject("BattleRoom");
// 加载背景音乐
GameModule.Audio.Play(AudioType.Music, "music_background", true);
// 创建游戏对象发生器
_entitySpawner = UniPooling.CreateSpawner("DefaultPackage");
// 创建游戏对象池
yield return _entitySpawner.CreateGameObjectPoolAsync("player_ship");
yield return _entitySpawner.CreateGameObjectPoolAsync("player_bullet");
yield return _entitySpawner.CreateGameObjectPoolAsync("enemy_ship");
yield return _entitySpawner.CreateGameObjectPoolAsync("enemy_bullet");
yield return _entitySpawner.CreateGameObjectPoolAsync("asteroid01");
yield return _entitySpawner.CreateGameObjectPoolAsync("asteroid02");
yield return _entitySpawner.CreateGameObjectPoolAsync("asteroid03");
yield return _entitySpawner.CreateGameObjectPoolAsync("explosion_asteroid");
yield return _entitySpawner.CreateGameObjectPoolAsync("explosion_enemy");
yield return _entitySpawner.CreateGameObjectPoolAsync("explosion_player");
// 创建玩家实体对象
var handle = _entitySpawner.SpawnSync("player_ship", _roomRoot.transform);
var entity = handle.GameObj.GetComponent<EntityPlayer>();
entity.InitEntity(handle);
// 显示战斗界面
yield return GameModule.UI.ShowUIAsync<UIBattleWindow>();
// 监听游戏事件
GameEvent.AddEventListener<Vector3,Quaternion>(ActorEventDefine.PlayerDead,OnPlayerDead);
GameEvent.AddEventListener<Vector3,Quaternion>(ActorEventDefine.EnemyDead,OnEnemyDead);
GameEvent.AddEventListener<Vector3,Quaternion>(ActorEventDefine.AsteroidExplosion,OnAsteroidExplosion);
GameEvent.AddEventListener<Vector3,Quaternion>(ActorEventDefine.PlayerFireBullet,OnPlayerFireBullet);
GameEvent.AddEventListener<Vector3,Quaternion>(ActorEventDefine.EnemyFireBullet,OnEnemyFireBullet);
_steps = ESteps.Ready;
}
#region

View File

@@ -35,6 +35,7 @@ namespace GameLogic
public override void OnRefresh()
{
m_textScore.text = "Score : 0";
m_goOverView.SetActive(false);
}
@@ -43,14 +44,10 @@ namespace GameLogic
private async UniTaskVoid OnClickRestartBtn()
{
await UniTask.Yield();
// yield return YooAssets.LoadSceneAsync("scene_battle");
//
// _battleRoom = new BattleRoom();
// yield return _battleRoom.LoadRoom();
//
// // 释放资源
// var package = YooAssets.GetPackage("DefaultPackage");
// package.UnloadUnusedAssets();
await GameModule.Resource.LoadSceneAsync("scene_battle").ToUniTask(Utility.Unity.Behaviour);
BattleSystem.Instance.DestroyRoom();
await BattleSystem.Instance.LoadRoom().ToUniTask(Utility.Unity.Behaviour);
}
private async UniTaskVoid OnClickHomeBtn()

View File

@@ -36,9 +36,7 @@ public partial class GameApp:Singleton<GameApp>
private async UniTaskVoid StartBattleRoom()
{
await GameModule.Resource.LoadSceneAsync("scene_battle").ToUniTask(Utility.Unity.Behaviour);
BattleRoom battleRoom = new BattleRoom();
await battleRoom.LoadRoom().ToUniTask(Utility.Unity.Behaviour);
await BattleSystem.Instance.LoadRoom().ToUniTask(Utility.Unity.Behaviour);
}
/// <summary>

View File

@@ -20,6 +20,7 @@ public partial class GameApp
{
// 初始化对象池系统
UniPooling.Initalize();
AddLogicSys(BehaviourSingleSystem.Instance);
}
/// <summary>

View File

@@ -16,6 +16,7 @@ namespace TEngine
{
private static GameObject _entity;
private static MainBehaviour _behaviour;
public static MainBehaviour Behaviour => _behaviour;
#region Coroutine
@@ -259,7 +260,7 @@ namespace TEngine
_behaviour = _entity.AddComponent<MainBehaviour>();
}
private class MainBehaviour : MonoBehaviour
public class MainBehaviour : MonoBehaviour
{
private event UnityAction updateEvent;
private event UnityAction fixedUpdateEvent;