update yooasset2.0

This commit is contained in:
hevinci
2023-09-20 16:10:37 +08:00
parent 01cf9ccd54
commit 6687d75090
253 changed files with 1859 additions and 9299 deletions
@@ -2,8 +2,6 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Window;
using UniFramework.Pooling;
using UniFramework.Event;
using UniFramework.Utility;
using YooAsset;
@@ -24,16 +22,14 @@ public class BattleRoom
{
None,
Ready,
Spawn,
SpawnEnemy,
WaitSpawn,
WaitWave,
GameOver,
}
private EventGroup _eventGroup = new EventGroup();
private Spawner _entitySpawner;
private readonly EventGroup _eventGroup = new EventGroup();
private GameObject _roomRoot;
private AssetOperationHandle _musicHandle;
// 关卡参数
private const int EnemyCount = 10;
@@ -49,29 +45,46 @@ public class BattleRoom
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 UniTimer _startWaitTimer = UniTimer.CreateOnceTimer(1f);
private readonly UniTimer _spawnWaitTimer = UniTimer.CreateOnceTimer(0.75f);
private readonly UniTimer _waveWaitTimer = UniTimer.CreateOnceTimer(4f);
private readonly List<AssetHandle> _handles = new List<AssetHandle>(1000);
/// <summary>
/// 初始化房间
/// </summary>
public void IntRoom()
{
// 创建房间根对象
_roomRoot = new GameObject("BattleRoom");
// 监听游戏事件
_eventGroup.AddListener<BattleEventDefine.PlayerDead>(OnHandleEventMessage);
_eventGroup.AddListener<BattleEventDefine.EnemyDead>(OnHandleEventMessage);
_eventGroup.AddListener<BattleEventDefine.AsteroidExplosion>(OnHandleEventMessage);
_eventGroup.AddListener<BattleEventDefine.PlayerFireBullet>(OnHandleEventMessage);
_eventGroup.AddListener<BattleEventDefine.EnemyFireBullet>(OnHandleEventMessage);
_steps = ESteps.Ready;
}
/// <summary>
/// 销毁房间
/// </summary>
public void DestroyRoom()
{
if (_musicHandle != null)
_musicHandle.Release();
if (_eventGroup != null)
_eventGroup.RemoveAllListener();
if (_entitySpawner != null)
_entitySpawner.DestroyAll(true);
if (_roomRoot != null)
GameObject.Destroy(_roomRoot);
UniWindow.CloseWindow<UIBattleWindow>();
foreach(var handle in _handles)
{
handle.Release();
}
_handles.Clear();
}
/// <summary>
@@ -86,30 +99,30 @@ public class BattleRoom
{
if (_startWaitTimer.Update(Time.deltaTime))
{
_steps = ESteps.Spawn;
// 生成实体
var handle = YooAssets.LoadAssetAsync<GameObject>("player_ship");
handle.Completed += (AssetHandle handle) =>
{
handle.InstantiateSync(_roomRoot.transform);
};
_handles.Add(handle);
_steps = ESteps.SpawnEnemy;
}
}
if (_steps == ESteps.Spawn)
if (_steps == ESteps.SpawnEnemy)
{
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 handle = YooAssets.LoadAssetAsync<GameObject>(enemyLocation);
handle.Completed += (AssetHandle handle) =>
{
// 生成敌人实体
var handle = _entitySpawner.SpawnSync(enemyLocation, _roomRoot.transform, spawnPosition, spawnRotation);
var entity = handle.GameObj.GetComponent<EntityEnemy>();
entity.InitEntity(handle);
}
else
{
// 生成小行星实体
var handle = _entitySpawner.SpawnSync(enemyLocation, _roomRoot.transform, spawnPosition, spawnRotation);
var entity = handle.GameObj.GetComponent<EntityAsteroid>();
entity.InitEntity(handle);
}
handle.InstantiateSync(spawnPosition, spawnRotation, _roomRoot.transform);
};
_handles.Add(handle);
_waveSpawnCount++;
if (_waveSpawnCount >= EnemyCount)
@@ -127,7 +140,7 @@ public class BattleRoom
if (_spawnWaitTimer.Update(Time.deltaTime))
{
_spawnWaitTimer.Reset();
_steps = ESteps.Spawn;
_steps = ESteps.SpawnEnemy;
}
}
@@ -137,62 +150,11 @@ public class BattleRoom
{
_waveWaitTimer.Reset();
_waveSpawnCount = 0;
_steps = ESteps.Spawn;
_steps = ESteps.SpawnEnemy;
}
}
}
/// <summary>
/// 加载房间
/// </summary>
public IEnumerator LoadRoom()
{
// 创建房间根对象
_roomRoot = new GameObject("BattleRoom");
// 加载背景音乐
_musicHandle = YooAssets.LoadAssetAsync<AudioClip>("music_background");
yield return _musicHandle;
// 播放背景音乐
var audioSource = _roomRoot.AddComponent<AudioSource>();
audioSource.loop = true;
audioSource.clip = _musicHandle.AssetObject as AudioClip;
audioSource.Play();
// 创建游戏对象发生器
_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 UniWindow.OpenWindowAsync<UIBattleWindow>("UIBattle");
// 监听游戏事件
_eventGroup.AddListener<BattleEventDefine.PlayerDead>(OnHandleEventMessage);
_eventGroup.AddListener<BattleEventDefine.EnemyDead>(OnHandleEventMessage);
_eventGroup.AddListener<BattleEventDefine.AsteroidExplosion>(OnHandleEventMessage);
_eventGroup.AddListener<BattleEventDefine.PlayerFireBullet>(OnHandleEventMessage);
_eventGroup.AddListener<BattleEventDefine.EnemyFireBullet>(OnHandleEventMessage);
_steps = ESteps.Ready;
}
/// <summary>
/// 接收事件
/// </summary>
@@ -204,9 +166,12 @@ public class BattleRoom
var msg = message as BattleEventDefine.PlayerDead;
// 创建爆炸效果
var handle = _entitySpawner.SpawnSync("explosion_player", _roomRoot.transform, msg.Position, msg.Rotation);
var entity = handle.GameObj.GetComponent<EntityEffect>();
entity.InitEntity(handle);
var handle = YooAssets.LoadAssetAsync<GameObject>("explosion_player");
handle.Completed += (AssetHandle handle) =>
{
handle.InstantiateSync(msg.Position, msg.Rotation, _roomRoot.transform);
};
_handles.Add(handle);
_steps = ESteps.GameOver;
BattleEventDefine.GameOver.SendEventMessage();
@@ -216,9 +181,12 @@ public class BattleRoom
var msg = message as BattleEventDefine.EnemyDead;
// 创建爆炸效果
var handle = _entitySpawner.SpawnSync("explosion_enemy", _roomRoot.transform, msg.Position, msg.Rotation);
var entity = handle.GameObj.GetComponent<EntityEffect>();
entity.InitEntity(handle);
var handle = YooAssets.LoadAssetAsync<GameObject>("explosion_enemy");
handle.Completed += (AssetHandle handle) =>
{
handle.InstantiateSync(msg.Position, msg.Rotation, _roomRoot.transform);
};
_handles.Add(handle);
_totalScore += EnemyScore;
BattleEventDefine.ScoreChange.SendEventMessage(_totalScore);
@@ -228,9 +196,12 @@ public class BattleRoom
var msg = message as BattleEventDefine.AsteroidExplosion;
// 创建爆炸效果
var handle = _entitySpawner.SpawnSync("explosion_asteroid", _roomRoot.transform, msg.Position, msg.Rotation);
var entity = handle.GameObj.GetComponent<EntityEffect>();
entity.InitEntity(handle);
var handle = YooAssets.LoadAssetAsync<GameObject>("explosion_asteroid");
handle.Completed += (AssetHandle handle) =>
{
handle.InstantiateSync(msg.Position, msg.Rotation, _roomRoot.transform);
};
_handles.Add(handle);
_totalScore += AsteroidScore;
BattleEventDefine.ScoreChange.SendEventMessage(_totalScore);
@@ -240,18 +211,24 @@ public class BattleRoom
var msg = message as BattleEventDefine.PlayerFireBullet;
// 创建子弹实体
var handle = _entitySpawner.SpawnSync("player_bullet", _roomRoot.transform, msg.Position, msg.Rotation);
var entity = handle.GameObj.GetComponent<EntityBullet>();
entity.InitEntity(handle);
var handle = YooAssets.LoadAssetAsync<GameObject>("player_bullet");
handle.Completed += (AssetHandle handle) =>
{
handle.InstantiateSync(msg.Position, msg.Rotation, _roomRoot.transform);
};
_handles.Add(handle);
}
else if (message is BattleEventDefine.EnemyFireBullet)
{
var msg = message as BattleEventDefine.EnemyFireBullet;
// 创建子弹实体
var handle = _entitySpawner.SpawnSync("enemy_bullet", _roomRoot.transform, msg.Position, msg.Rotation);
var entity = handle.GameObj.GetComponent<EntityBullet>();
entity.InitEntity(handle);
var handle = YooAssets.LoadAssetAsync<GameObject>("enemy_bullet");
handle.Completed += (AssetHandle handle) =>
{
handle.InstantiateSync(msg.Position, msg.Rotation, _roomRoot.transform);
};
_handles.Add(handle);
}
}
}
@@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 2673676235c2ebf4790768011d015d1d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,27 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Pooling;
public class EntityAsteroid : MonoBehaviour
{
public float MoveSpeed = -5f;
public float Tumble = 5f;
private SpawnHandle _handle;
private Rigidbody _rigidbody;
public void InitEntity(SpawnHandle handle)
{
_handle = handle;
_rigidbody.velocity = this.transform.forward * MoveSpeed;
_rigidbody.angularVelocity = Random.insideUnitSphere * Tumble;
}
void Awake()
{
_rigidbody = this.transform.GetComponent<Rigidbody>();
_rigidbody.velocity = this.transform.forward * MoveSpeed;
_rigidbody.angularVelocity = Random.insideUnitSphere * Tumble;
}
void OnTriggerEnter(Collider other)
{
@@ -29,8 +21,7 @@ public class EntityAsteroid : MonoBehaviour
if (name.StartsWith("player"))
{
BattleEventDefine.AsteroidExplosion.SendEventMessage(this.transform.position, this.transform.rotation);
_handle.Restore();
_handle = null;
GameObject.Destroy(this.gameObject);
}
}
void OnTriggerExit(Collider other)
@@ -38,8 +29,7 @@ public class EntityAsteroid : MonoBehaviour
var name = other.gameObject.name;
if (name.StartsWith("Boundary"))
{
_handle.Restore();
_handle = null;
GameObject.Destroy(this.gameObject);
}
}
}
@@ -1,25 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Pooling;
public class EntityBullet : MonoBehaviour
{
public float MoveSpeed = 20f;
public float DelayDestroyTime = 5f;
private SpawnHandle _handle;
private Rigidbody _rigidbody;
public void InitEntity(SpawnHandle handle)
{
_handle = handle;
_rigidbody.velocity = this.transform.forward * MoveSpeed;
}
void Awake()
{
_rigidbody = this.transform.GetComponent<Rigidbody>();
_rigidbody.velocity = this.transform.forward * MoveSpeed;
}
void OnTriggerEnter(Collider other)
{
@@ -32,8 +25,7 @@ public class EntityBullet : MonoBehaviour
{
if (name.StartsWith("enemy") == false)
{
_handle.Restore();
_handle = null;
GameObject.Destroy(this.gameObject);
}
}
@@ -41,8 +33,7 @@ public class EntityBullet : MonoBehaviour
{
if (name.StartsWith("player") == false)
{
_handle.Restore();
_handle = null;
GameObject.Destroy(this.gameObject);
}
}
}
@@ -51,8 +42,7 @@ public class EntityBullet : MonoBehaviour
var name = other.gameObject.name;
if (name.StartsWith("Boundary"))
{
_handle.Restore();
_handle = null;
GameObject.Destroy(this.gameObject);
}
}
}
@@ -1,23 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Pooling;
public class EntityEffect : MonoBehaviour
{
public float DelayDestroyTime = 1f;
private SpawnHandle _handle;
public void InitEntity(SpawnHandle handle)
private void Awake()
{
_handle = handle;
Invoke(nameof(DelayDestroy), DelayDestroyTime);
}
private void DelayDestroy()
{
_handle.Restore();
_handle = null;
GameObject.Destroy(this.gameObject);
}
}
@@ -1,7 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Pooling;
using Random = UnityEngine.Random;
public class EntityEnemy : MonoBehaviour
@@ -17,7 +16,6 @@ public class EntityEnemy : MonoBehaviour
public Vector2 ManeuverTime = new Vector2(1, 2);
public Vector2 ManeuverWait = new Vector2(1, 2);
private SpawnHandle _handle;
private Transform _shotSpawn;
private Rigidbody _rigidbody;
private AudioSource _audioSource;
@@ -26,22 +24,18 @@ public class EntityEnemy : MonoBehaviour
private float _targetManeuver;
public void InitEntity(SpawnHandle handle)
{
_handle = handle;
_rigidbody.velocity = this.transform.forward * -5f;
_lastFireTime = Time.time;
_currentSpeed = _rigidbody.velocity.z;
_targetManeuver = 0f;
StartCoroutine(Evade());
}
void Awake()
{
_rigidbody = this.gameObject.GetComponent<Rigidbody>();
_audioSource = this.gameObject.GetComponent<AudioSource>();
_shotSpawn = this.transform.Find("shot_spawn");
_rigidbody.velocity = this.transform.forward * -5f;
_lastFireTime = Time.time;
_currentSpeed = _rigidbody.velocity.z;
_targetManeuver = 0f;
StartCoroutine(Evade());
}
void Update()
{
@@ -72,8 +66,7 @@ public class EntityEnemy : MonoBehaviour
if (name.StartsWith("player"))
{
BattleEventDefine.EnemyDead.SendEventMessage(this.transform.position, this.transform.rotation);
_handle.Restore();
_handle = null;
GameObject.Destroy(this.gameObject);
}
}
void OnTriggerExit(Collider other)
@@ -81,8 +74,7 @@ public class EntityEnemy : MonoBehaviour
var name = other.gameObject.name;
if (name.StartsWith("Boundary"))
{
_handle.Restore();
_handle = null;
GameObject.Destroy(this.gameObject);
}
}
@@ -1,7 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UniFramework.Pooling;
public class EntityPlayer : MonoBehaviour
{
@@ -9,17 +8,11 @@ public class EntityPlayer : MonoBehaviour
public float MoveSpeed = 10f;
public float FireRate = 0.25f;
private SpawnHandle _handle;
private float _nextFireTime = 0f;
private Transform _shotSpawn;
private Rigidbody _rigidbody;
private AudioSource _audioSource;
public void InitEntity(SpawnHandle handle)
{
_handle = handle;
}
void Awake()
{
_rigidbody = this.gameObject.GetComponent<Rigidbody>();
@@ -58,8 +51,7 @@ public class EntityPlayer : MonoBehaviour
if (name.StartsWith("enemy") || name.StartsWith("asteroid"))
{
BattleEventDefine.PlayerDead.SendEventMessage(this.transform.position, this.transform.rotation);
_handle.Restore();
_handle = null;
GameObject.Destroy(this.gameObject);
}
}
}