mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
更新demo
更新demo
This commit is contained in:
@@ -25,7 +25,7 @@ public class EntityAsteroid : MonoBehaviour
|
||||
if (name.StartsWith("player"))
|
||||
{
|
||||
GameEvent.Send(ActorEventDefine.AsteroidExplosion,this.transform.position, this.transform.rotation);
|
||||
GameModule.Resource.FreeGameObject(this.gameObject);
|
||||
PoolManager.Instance.PushGameObject(this.gameObject);
|
||||
}
|
||||
}
|
||||
void OnTriggerExit(Collider other)
|
||||
@@ -33,7 +33,7 @@ public class EntityAsteroid : MonoBehaviour
|
||||
var name = other.gameObject.name;
|
||||
if (name.StartsWith("Boundary"))
|
||||
{
|
||||
GameModule.Resource.FreeGameObject(this.gameObject);
|
||||
PoolManager.Instance.PushGameObject(this.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,3 +1,4 @@
|
||||
using GameLogic;
|
||||
using TEngine;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -28,7 +29,7 @@ public class EntityBullet : MonoBehaviour
|
||||
{
|
||||
if (name.StartsWith("enemy") == false)
|
||||
{
|
||||
GameModule.Resource.FreeGameObject(this.gameObject);
|
||||
PoolManager.Instance.PushGameObject(this.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +37,7 @@ public class EntityBullet : MonoBehaviour
|
||||
{
|
||||
if (name.StartsWith("player") == false)
|
||||
{
|
||||
GameModule.Resource.FreeGameObject(this.gameObject);
|
||||
PoolManager.Instance.PushGameObject(this.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,7 +46,7 @@ public class EntityBullet : MonoBehaviour
|
||||
var name = other.gameObject.name;
|
||||
if (name.StartsWith("Boundary"))
|
||||
{
|
||||
GameModule.Resource.FreeGameObject(this.gameObject);
|
||||
PoolManager.Instance.PushGameObject(this.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using TEngine;
|
||||
using GameLogic;
|
||||
using TEngine;
|
||||
using UnityEngine;
|
||||
|
||||
public class EntityEffect : MonoBehaviour
|
||||
@@ -11,6 +12,6 @@ public class EntityEffect : MonoBehaviour
|
||||
}
|
||||
private void DelayDestroy()
|
||||
{
|
||||
GameModule.Resource.FreeGameObject(this.gameObject);
|
||||
PoolManager.Instance.PushGameObject(this.gameObject);
|
||||
}
|
||||
}
|
@@ -69,7 +69,7 @@ public class EntityEnemy : MonoBehaviour
|
||||
if (name.StartsWith("player"))
|
||||
{
|
||||
GameEvent.Send(ActorEventDefine.EnemyDead,this.transform.position, this.transform.rotation);
|
||||
GameModule.Resource.FreeGameObject(this.gameObject);
|
||||
PoolManager.Instance.PushGameObject(this.gameObject);
|
||||
}
|
||||
}
|
||||
void OnTriggerExit(Collider other)
|
||||
@@ -77,7 +77,7 @@ public class EntityEnemy : MonoBehaviour
|
||||
var name = other.gameObject.name;
|
||||
if (name.StartsWith("Boundary"))
|
||||
{
|
||||
GameModule.Resource.FreeGameObject(this.gameObject);
|
||||
PoolManager.Instance.PushGameObject(this.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -51,7 +51,7 @@ public class EntityPlayer : MonoBehaviour
|
||||
if (name.StartsWith("enemy") || name.StartsWith("asteroid"))
|
||||
{
|
||||
GameEvent.Send(ActorEventDefine.PlayerDead,transform.position, transform.rotation);
|
||||
GameModule.Resource.FreeGameObject(this.gameObject);
|
||||
PoolManager.Instance.PushGameObject(this.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
@@ -11,239 +11,241 @@ using Random = UnityEngine.Random;
|
||||
/// 战斗房间
|
||||
/// </summary>
|
||||
[Update]
|
||||
public class BattleSystem:BehaviourSingleton<BattleSystem>
|
||||
public class BattleSystem : BehaviourSingleton<BattleSystem>
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
Ready,
|
||||
Spawn,
|
||||
WaitSpawn,
|
||||
WaitWave,
|
||||
GameOver,
|
||||
}
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
Ready,
|
||||
Spawn,
|
||||
WaitSpawn,
|
||||
WaitWave,
|
||||
GameOver,
|
||||
}
|
||||
|
||||
private GameObject _roomRoot;
|
||||
private GameObject _roomRoot;
|
||||
|
||||
// 关卡参数
|
||||
private const int EnemyCount = 10;
|
||||
private const int EnemyScore = 10;
|
||||
private const int AsteroidScore = 1;
|
||||
private readonly Vector3 _spawnValues = new Vector3(6, 0, 20);
|
||||
private readonly string[] _entityLocations = new string[]
|
||||
{
|
||||
"asteroid01", "asteroid02", "asteroid03", "enemy_ship"
|
||||
};
|
||||
// 关卡参数
|
||||
private const int EnemyCount = 10;
|
||||
private const int EnemyScore = 10;
|
||||
private const int AsteroidScore = 1;
|
||||
private readonly Vector3 _spawnValues = new Vector3(6, 0, 20);
|
||||
|
||||
private ESteps _steps = ESteps.None;
|
||||
private int _totalScore = 0;
|
||||
private int _waveSpawnCount = 0;
|
||||
//
|
||||
// private UniTimer _startWaitTimer = UniTimer.CreateOnceTimer(1f);
|
||||
// private UniTimer _spawnWaitTimer = UniTimer.CreateOnceTimer(0.75f);
|
||||
// private UniTimer _waveWaitTimer = UniTimer.CreateOnceTimer(4f);
|
||||
private readonly string[] _entityLocations = new string[]
|
||||
{
|
||||
"asteroid01", "asteroid02", "asteroid03", "enemy_ship"
|
||||
};
|
||||
|
||||
private float _startWaitTimer = 1f;
|
||||
private float _spawnWaitTimer = 0.75f;
|
||||
private float _waveWaitTimer = 4f;
|
||||
private ESteps _steps = ESteps.None;
|
||||
private int _totalScore = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 加载房间
|
||||
/// </summary>
|
||||
public async UniTaskVoid LoadRoom()
|
||||
{
|
||||
_startWaitTimer = 1f;
|
||||
|
||||
await UniTask.Yield();
|
||||
// 创建房间根对象
|
||||
_roomRoot = new GameObject("BattleRoom");
|
||||
private int _waveSpawnCount = 0;
|
||||
//
|
||||
// private UniTimer _startWaitTimer = UniTimer.CreateOnceTimer(1f);
|
||||
// private UniTimer _spawnWaitTimer = UniTimer.CreateOnceTimer(0.75f);
|
||||
// private UniTimer _waveWaitTimer = UniTimer.CreateOnceTimer(4f);
|
||||
|
||||
// 加载背景音乐
|
||||
GameModule.Audio.Play(AudioType.Music, "music_background", true);
|
||||
private float _startWaitTimer = 1f;
|
||||
private float _spawnWaitTimer = 0.75f;
|
||||
private float _waveWaitTimer = 4f;
|
||||
|
||||
// 创建玩家实体对象
|
||||
var handle = GameModule.Resource.LoadAsset<GameObject>("player_ship", _roomRoot.transform);
|
||||
var entity = handle.GetComponent<EntityPlayer>();
|
||||
/// <summary>
|
||||
/// 加载房间
|
||||
/// </summary>
|
||||
public async UniTaskVoid LoadRoom()
|
||||
{
|
||||
_startWaitTimer = 1f;
|
||||
|
||||
// 显示战斗界面
|
||||
GameModule.UI.ShowUIAsync<UIBattleWindow>();
|
||||
await UniTask.Yield();
|
||||
// 创建房间根对象
|
||||
_roomRoot = new GameObject("BattleRoom");
|
||||
|
||||
// 监听游戏事件
|
||||
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);
|
||||
// 加载背景音乐
|
||||
GameModule.Audio.Play(AudioType.Music, "music_background", true);
|
||||
|
||||
_steps = ESteps.Ready;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 销毁房间
|
||||
/// </summary>
|
||||
public void DestroyRoom()
|
||||
{
|
||||
// 加载背景音乐
|
||||
GameModule.Audio.Stop(AudioType.Music, true);
|
||||
// 创建玩家实体对象
|
||||
var handle = PoolManager.Instance.GetGameObject("player_ship",parent: _roomRoot.transform);
|
||||
var entity = handle.GetComponent<EntityPlayer>();
|
||||
|
||||
// if (_entitySpawner != null)
|
||||
// {
|
||||
// _entitySpawner.DestroyAll(true);
|
||||
// }
|
||||
// 显示战斗界面
|
||||
GameModule.UI.ShowUIAsync<UIBattleWindow>();
|
||||
|
||||
if (_roomRoot != null)
|
||||
Object.Destroy(_roomRoot);
|
||||
// 监听游戏事件
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
_steps = ESteps.Ready;
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
UpdateRoom();
|
||||
}
|
||||
/// <summary>
|
||||
/// 销毁房间
|
||||
/// </summary>
|
||||
public void DestroyRoom()
|
||||
{
|
||||
// 加载背景音乐
|
||||
GameModule.Audio.Stop(AudioType.Music, true);
|
||||
|
||||
/// <summary>
|
||||
/// 更新房间
|
||||
/// </summary>
|
||||
public void UpdateRoom()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.GameOver)
|
||||
return;
|
||||
// if (_entitySpawner != null)
|
||||
// {
|
||||
// _entitySpawner.DestroyAll(true);
|
||||
// }
|
||||
|
||||
if (_steps == ESteps.Ready)
|
||||
{
|
||||
_startWaitTimer -= Time.deltaTime;
|
||||
if (_startWaitTimer <= 0)
|
||||
{
|
||||
_steps = ESteps.Spawn;
|
||||
}
|
||||
}
|
||||
if (_roomRoot != null)
|
||||
Object.Destroy(_roomRoot);
|
||||
|
||||
if (_steps == ESteps.Spawn)
|
||||
{
|
||||
var enemyLocation = _entityLocations[Random.Range(0, 4)];
|
||||
Vector3 spawnPosition = new Vector3(Random.Range(-_spawnValues.x, _spawnValues.x), _spawnValues.y, _spawnValues.z);
|
||||
Quaternion spawnRotation = Quaternion.identity;
|
||||
GameModule.UI.CloseUI<UIBattleWindow>();
|
||||
|
||||
if (enemyLocation == "enemy_ship")
|
||||
{
|
||||
// 生成敌人实体
|
||||
var gameObject = GameModule.Resource.LoadAsset<GameObject>(enemyLocation, _roomRoot.transform);
|
||||
gameObject.transform.position = spawnPosition;
|
||||
gameObject.transform.rotation = spawnRotation;
|
||||
var entity = gameObject.GetComponent<EntityEnemy>();
|
||||
entity.InitEntity();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 生成小行星实体
|
||||
var gameObject = GameModule.Resource.LoadAsset<GameObject>(enemyLocation, _roomRoot.transform);
|
||||
gameObject.transform.position = spawnPosition;
|
||||
gameObject.transform.rotation = spawnRotation;
|
||||
var entity = gameObject.GetComponent<EntityAsteroid>();
|
||||
entity.InitEntity();
|
||||
}
|
||||
// 监听游戏事件
|
||||
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);
|
||||
}
|
||||
|
||||
_waveSpawnCount++;
|
||||
if (_waveSpawnCount >= EnemyCount)
|
||||
{
|
||||
_steps = ESteps.WaitWave;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.WaitSpawn;
|
||||
}
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
UpdateRoom();
|
||||
}
|
||||
|
||||
if (_steps == ESteps.WaitSpawn)
|
||||
{
|
||||
_spawnWaitTimer -= Time.deltaTime;
|
||||
if (_spawnWaitTimer <= 0)
|
||||
{
|
||||
_spawnWaitTimer = 0.75f;
|
||||
_steps = ESteps.Spawn;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 更新房间
|
||||
/// </summary>
|
||||
public void UpdateRoom()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.GameOver)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.WaitWave)
|
||||
{
|
||||
_waveWaitTimer -= Time.deltaTime;
|
||||
if (_waveWaitTimer <= 0)
|
||||
{
|
||||
_waveWaitTimer = 4f;
|
||||
_waveSpawnCount = 0;
|
||||
_steps = ESteps.Spawn;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (_steps == ESteps.Ready)
|
||||
{
|
||||
_startWaitTimer -= Time.deltaTime;
|
||||
if (_startWaitTimer <= 0)
|
||||
{
|
||||
_steps = ESteps.Spawn;
|
||||
}
|
||||
}
|
||||
|
||||
if (_steps == ESteps.Spawn)
|
||||
{
|
||||
var enemyLocation = _entityLocations[Random.Range(0, 4)];
|
||||
Vector3 spawnPosition = new Vector3(Random.Range(-_spawnValues.x, _spawnValues.x), _spawnValues.y, _spawnValues.z);
|
||||
Quaternion spawnRotation = Quaternion.identity;
|
||||
|
||||
if (enemyLocation == "enemy_ship")
|
||||
{
|
||||
// 生成敌人实体
|
||||
var gameObject = PoolManager.Instance.GetGameObject(enemyLocation,parent: _roomRoot.transform);
|
||||
gameObject.transform.position = spawnPosition;
|
||||
gameObject.transform.rotation = spawnRotation;
|
||||
var entity = gameObject.GetComponent<EntityEnemy>();
|
||||
entity.InitEntity();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 生成小行星实体
|
||||
var gameObject = PoolManager.Instance.GetGameObject(enemyLocation,parent: _roomRoot.transform);
|
||||
gameObject.transform.position = spawnPosition;
|
||||
gameObject.transform.rotation = spawnRotation;
|
||||
var entity = gameObject.GetComponent<EntityAsteroid>();
|
||||
entity.InitEntity();
|
||||
}
|
||||
|
||||
_waveSpawnCount++;
|
||||
if (_waveSpawnCount >= EnemyCount)
|
||||
{
|
||||
_steps = ESteps.WaitWave;
|
||||
}
|
||||
else
|
||||
{
|
||||
_steps = ESteps.WaitSpawn;
|
||||
}
|
||||
}
|
||||
|
||||
if (_steps == ESteps.WaitSpawn)
|
||||
{
|
||||
_spawnWaitTimer -= Time.deltaTime;
|
||||
if (_spawnWaitTimer <= 0)
|
||||
{
|
||||
_spawnWaitTimer = 0.75f;
|
||||
_steps = ESteps.Spawn;
|
||||
}
|
||||
}
|
||||
|
||||
if (_steps == ESteps.WaitWave)
|
||||
{
|
||||
_waveWaitTimer -= Time.deltaTime;
|
||||
if (_waveWaitTimer <= 0)
|
||||
{
|
||||
_waveWaitTimer = 4f;
|
||||
_waveSpawnCount = 0;
|
||||
_steps = ESteps.Spawn;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region 接收事件
|
||||
#region 接收事件
|
||||
|
||||
private void OnPlayerDead(Vector3 position,Quaternion rotation)
|
||||
{
|
||||
// 创建爆炸效果
|
||||
var gameObject = GameModule.Resource.LoadAsset<GameObject>("explosion_player", _roomRoot.transform);
|
||||
gameObject.transform.position = position;
|
||||
gameObject.transform.rotation = rotation;
|
||||
var entity = gameObject.GetComponent<EntityEffect>();
|
||||
entity.InitEntity();
|
||||
_steps = ESteps.GameOver;
|
||||
GameEvent.Send(ActorEventDefine.GameOver);
|
||||
}
|
||||
private void OnPlayerDead(Vector3 position, Quaternion rotation)
|
||||
{
|
||||
// 创建爆炸效果
|
||||
var gameObject = PoolManager.Instance.GetGameObject("explosion_player",parent: _roomRoot.transform);
|
||||
gameObject.transform.position = position;
|
||||
gameObject.transform.rotation = rotation;
|
||||
var entity = gameObject.GetComponent<EntityEffect>();
|
||||
entity.InitEntity();
|
||||
_steps = ESteps.GameOver;
|
||||
GameEvent.Send(ActorEventDefine.GameOver);
|
||||
}
|
||||
|
||||
private void OnEnemyDead(Vector3 position,Quaternion rotation)
|
||||
{
|
||||
// 创建爆炸效果
|
||||
var gameObject = GameModule.Resource.LoadAsset<GameObject>("explosion_enemy", _roomRoot.transform);
|
||||
gameObject.transform.position = position;
|
||||
gameObject.transform.rotation = rotation;
|
||||
var entity = gameObject.GetComponent<EntityEffect>();
|
||||
entity.InitEntity();
|
||||
private void OnEnemyDead(Vector3 position, Quaternion rotation)
|
||||
{
|
||||
// 创建爆炸效果
|
||||
var gameObject = PoolManager.Instance.GetGameObject("explosion_enemy",parent: _roomRoot.transform);
|
||||
gameObject.transform.position = position;
|
||||
gameObject.transform.rotation = rotation;
|
||||
var entity = gameObject.GetComponent<EntityEffect>();
|
||||
entity.InitEntity();
|
||||
|
||||
_totalScore += EnemyScore;
|
||||
GameEvent.Send(ActorEventDefine.ScoreChange,_totalScore);
|
||||
}
|
||||
_totalScore += EnemyScore;
|
||||
GameEvent.Send(ActorEventDefine.ScoreChange, _totalScore);
|
||||
}
|
||||
|
||||
private void OnAsteroidExplosion(Vector3 position,Quaternion rotation)
|
||||
{
|
||||
// 创建爆炸效果
|
||||
var gameObject = GameModule.Resource.LoadAsset<GameObject>("explosion_asteroid", _roomRoot.transform);
|
||||
gameObject.transform.position = position;
|
||||
gameObject.transform.rotation = rotation;
|
||||
var entity = gameObject.GetComponent<EntityEffect>();
|
||||
entity.InitEntity();
|
||||
private void OnAsteroidExplosion(Vector3 position, Quaternion rotation)
|
||||
{
|
||||
// 创建爆炸效果
|
||||
var gameObject = PoolManager.Instance.GetGameObject("explosion_asteroid",parent: _roomRoot.transform);
|
||||
gameObject.transform.position = position;
|
||||
gameObject.transform.rotation = rotation;
|
||||
var entity = gameObject.GetComponent<EntityEffect>();
|
||||
entity.InitEntity();
|
||||
|
||||
_totalScore += AsteroidScore;
|
||||
GameEvent.Send(ActorEventDefine.ScoreChange,_totalScore);
|
||||
}
|
||||
_totalScore += AsteroidScore;
|
||||
GameEvent.Send(ActorEventDefine.ScoreChange, _totalScore);
|
||||
}
|
||||
|
||||
private void OnPlayerFireBullet(Vector3 position,Quaternion rotation)
|
||||
{
|
||||
// 创建子弹实体
|
||||
var gameObject = GameModule.Resource.LoadAsset<GameObject>("player_bullet", _roomRoot.transform);
|
||||
gameObject.transform.position = position;
|
||||
gameObject.transform.rotation = rotation;
|
||||
var entity = gameObject.GetComponent<EntityBullet>();
|
||||
entity.InitEntity();
|
||||
}
|
||||
|
||||
private void OnEnemyFireBullet(Vector3 position,Quaternion rotation)
|
||||
{
|
||||
// 创建子弹实体
|
||||
var gameObject = GameModule.Resource.LoadAsset<GameObject>("enemy_bullet", _roomRoot.transform);
|
||||
private void OnPlayerFireBullet(Vector3 position, Quaternion rotation)
|
||||
{
|
||||
// 创建子弹实体
|
||||
var gameObject = PoolManager.Instance.GetGameObject("player_bullet",parent: _roomRoot.transform);
|
||||
gameObject.transform.position = position;
|
||||
gameObject.transform.rotation = rotation;
|
||||
var entity = gameObject.GetComponent<EntityBullet>();
|
||||
entity.InitEntity();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
private void OnEnemyFireBullet(Vector3 position, Quaternion rotation)
|
||||
{
|
||||
// 创建子弹实体
|
||||
var gameObject = PoolManager.Instance.GetGameObject("enemy_bullet",parent: _roomRoot.transform);
|
||||
gameObject.transform.position = position;
|
||||
gameObject.transform.rotation = rotation;
|
||||
var entity = gameObject.GetComponent<EntityBullet>();
|
||||
entity.InitEntity();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
@@ -0,0 +1,181 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TEngine;
|
||||
using UnityEngine;
|
||||
|
||||
namespace GameLogic
|
||||
{
|
||||
public class PoolManager : MonoBehaviour
|
||||
{
|
||||
private static PoolManager _instance;
|
||||
|
||||
public static PoolManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = FindObjectOfType<PoolManager>();
|
||||
}
|
||||
|
||||
if (_instance == null)
|
||||
{
|
||||
GameObject gameObject = new GameObject();
|
||||
gameObject.name = nameof(PoolManager);
|
||||
_instance = gameObject.AddComponent<PoolManager>();
|
||||
_instance.poolRootObj = gameObject;
|
||||
DontDestroyOnLoad(_instance);
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
[SerializeField] private GameObject poolRootObj;
|
||||
public Dictionary<string, GameObjectPoolData> gameObjectPoolDic = new Dictionary<string, GameObjectPoolData>();
|
||||
public Dictionary<string, ObjectPoolData> objectPoolDic = new Dictionary<string, ObjectPoolData>();
|
||||
|
||||
public GameObject GetGameObject(string assetName, Transform parent = null)
|
||||
{
|
||||
GameObject obj = null;
|
||||
if (gameObjectPoolDic.TryGetValue(assetName, out var gameObjectPoolData) && gameObjectPoolData.poolQueue.Count > 0)
|
||||
{
|
||||
obj = gameObjectPoolData.GetObj(parent);
|
||||
}
|
||||
|
||||
if (obj == null)
|
||||
{
|
||||
obj = GameModule.Resource.LoadGameObject(assetName, parent: parent);
|
||||
obj.name = assetName;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
public void PushGameObject(GameObject obj)
|
||||
{
|
||||
string objName = obj.name;
|
||||
if (gameObjectPoolDic.TryGetValue(objName, out var gameObjectPoolData))
|
||||
{
|
||||
gameObjectPoolData.PushObj(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
gameObjectPoolDic.Add(objName, new GameObjectPoolData(obj, poolRootObj));
|
||||
}
|
||||
}
|
||||
|
||||
public T GetObject<T>() where T : class, new()
|
||||
{
|
||||
return CheckObjectCache<T>() ? (T)objectPoolDic[typeof(T).FullName].GetObj() : new T();
|
||||
}
|
||||
|
||||
public void PushObject(object obj)
|
||||
{
|
||||
string fullName = obj.GetType().FullName;
|
||||
if (objectPoolDic.ContainsKey(fullName))
|
||||
{
|
||||
objectPoolDic[fullName].PushObj(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
objectPoolDic.Add(fullName, new ObjectPoolData(obj));
|
||||
}
|
||||
}
|
||||
|
||||
private bool CheckObjectCache<T>()
|
||||
{
|
||||
string fullName = typeof(T).FullName;
|
||||
return fullName != null && objectPoolDic.ContainsKey(fullName) && objectPoolDic[fullName].poolQueue.Count > 0;
|
||||
}
|
||||
|
||||
public void Clear(bool clearGameObject = true, bool clearCObject = true)
|
||||
{
|
||||
if (clearGameObject)
|
||||
{
|
||||
for (int index = 0; index < poolRootObj.transform.childCount; ++index)
|
||||
{
|
||||
Destroy(poolRootObj.transform.GetChild(index).gameObject);
|
||||
}
|
||||
gameObjectPoolDic.Clear();
|
||||
}
|
||||
|
||||
if (!clearCObject)
|
||||
{
|
||||
return;
|
||||
}
|
||||
objectPoolDic.Clear();
|
||||
}
|
||||
|
||||
public void ClearAllGameObject() => Clear(clearCObject: false);
|
||||
|
||||
public void ClearGameObject(string prefabName)
|
||||
{
|
||||
GameObject obj = poolRootObj.transform.Find(prefabName).gameObject;
|
||||
if (obj == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Destroy(obj);
|
||||
gameObjectPoolDic.Remove(prefabName);
|
||||
}
|
||||
|
||||
public void ClearGameObject(GameObject prefab) => ClearGameObject(prefab.name);
|
||||
|
||||
public void ClearAllObject() => Clear(false);
|
||||
|
||||
public void ClearObject<T>() => objectPoolDic.Remove(typeof(T).FullName);
|
||||
|
||||
public void ClearObject(Type type) => objectPoolDic.Remove(type.FullName);
|
||||
}
|
||||
|
||||
public class ObjectPoolData
|
||||
{
|
||||
public readonly Queue<object> poolQueue = new Queue<object>();
|
||||
|
||||
public ObjectPoolData(object obj) => PushObj(obj);
|
||||
|
||||
public void PushObj(object obj) => poolQueue.Enqueue(obj);
|
||||
|
||||
public object GetObj() => poolQueue.Dequeue();
|
||||
}
|
||||
|
||||
public class GameObjectPoolData
|
||||
{
|
||||
public readonly GameObject fatherObj;
|
||||
public readonly Queue<GameObject> poolQueue;
|
||||
|
||||
public GameObjectPoolData(GameObject obj, GameObject poolRootObj)
|
||||
{
|
||||
fatherObj = new GameObject(obj.name);
|
||||
fatherObj.transform.SetParent(poolRootObj.transform);
|
||||
poolQueue = new Queue<GameObject>();
|
||||
PushObj(obj);
|
||||
}
|
||||
|
||||
public GameObjectPoolData(GameObject fatherObj)
|
||||
{
|
||||
this.fatherObj = fatherObj;
|
||||
}
|
||||
|
||||
public void PushObj(GameObject obj)
|
||||
{
|
||||
poolQueue.Enqueue(obj);
|
||||
obj.transform.SetParent(fatherObj.transform);
|
||||
obj.SetActive(false);
|
||||
}
|
||||
|
||||
public GameObject GetObj(Transform parent = null)
|
||||
{
|
||||
GameObject go = poolQueue.Dequeue();
|
||||
go.SetActive(true);
|
||||
go.transform.SetParent(parent);
|
||||
if (parent == null)
|
||||
{
|
||||
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(go, UnityEngine.SceneManagement.SceneManager.GetActiveScene());
|
||||
}
|
||||
|
||||
return go;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6793af44177448799777835de5cc4a53
|
||||
timeCreated: 1710746480
|
@@ -15,7 +15,7 @@ namespace GameLogic
|
||||
private Button m_btnRestart;
|
||||
private Button m_btnHome;
|
||||
|
||||
public override void ScriptGenerator()
|
||||
protected override void ScriptGenerator()
|
||||
{
|
||||
m_textScore = FindChildComponent<Text>("ScoreView/m_textScore");
|
||||
m_goOverView = FindChild("m_goOverView").gameObject;
|
||||
@@ -27,13 +27,13 @@ namespace GameLogic
|
||||
|
||||
#endregion
|
||||
|
||||
public override void RegisterEvent()
|
||||
protected override void RegisterEvent()
|
||||
{
|
||||
AddUIEvent<int>(ActorEventDefine.ScoreChange, OnScoreChange);
|
||||
AddUIEvent(ActorEventDefine.GameOver, OnGameOver);
|
||||
}
|
||||
|
||||
public override void OnRefresh()
|
||||
protected override void OnRefresh()
|
||||
{
|
||||
m_textScore.text = "Score : 0";
|
||||
m_goOverView.SetActive(false);
|
||||
|
Reference in New Issue
Block a user