386 lines
7.7 KiB
C#
386 lines
7.7 KiB
C#
// ***********************************************************************
|
||
// Assembly : Unity
|
||
// Author : Kimch
|
||
// Created : 2018-2-29
|
||
// Description : 游戏逻辑实体管理器,对游戏中所有Entity提供创建,移除和管理
|
||
// Last Modified By :
|
||
// Last Modified On :
|
||
// ***********************************************************************
|
||
// <copyright file= "EntityManager" company=""></copyright>
|
||
// <summary></summary>
|
||
// ***********************************************************************
|
||
namespace G
|
||
{
|
||
using System.Collections.Generic;
|
||
using F;
|
||
using F.Entity;
|
||
using F.ObjectPool;
|
||
using UnityEngine;
|
||
|
||
public partial class EntityManager : MonoBehaviour
|
||
{
|
||
public const string ASSET_POOL_NAME = "entities";
|
||
|
||
/// <summary>
|
||
/// 框架层提供管理
|
||
/// </summary>
|
||
private IEntityManager _entityManager;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
private IEntityGroup<Entity> _defaultGroup;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
private IEntityGroup<EntityEnemy> _bossGroup;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
private IEntityGroup<EntityEnemy> _minionGroup;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
private IEntityGroup<EntityProp> _propGroup;
|
||
|
||
private Transform defaultRoot
|
||
{
|
||
get => this.transform;
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public List<GameObject> propPrefabs = new List<GameObject>(4);
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public List<GameObject> cartPrefabs = new List<GameObject>(1);
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public List<GameObject> dropPrefabs = new List<GameObject>(1);
|
||
|
||
#region Method
|
||
|
||
private static int _LocalGuid = 100000;
|
||
public static int GetLocalGuid()
|
||
{
|
||
return _LocalGuid++;
|
||
}
|
||
|
||
public void Initialize(System.Action completed)
|
||
{
|
||
_entityManager = KFramework.EntityManager;
|
||
|
||
_defaultGroup = this.AddEntityGroup<Entity>();
|
||
_bossGroup = this.AddEntityGroup<EntityEnemy>("b");
|
||
_minionGroup = this.AddEntityGroup<EntityEnemy>("m");
|
||
_propGroup = this.AddEntityGroup<EntityProp>();
|
||
|
||
completed?.Invoke();
|
||
}
|
||
|
||
private void OnInitialized()
|
||
{
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <returns></returns>
|
||
private IEntityGroup<T> AddEntityGroup<T>(string sub = "") where T : Entity
|
||
{
|
||
var fullName = Utils.Text.GetFullName<T>(sub);
|
||
|
||
if (_entityManager.HasGroup(fullName))
|
||
return _entityManager.GetGroup<T>(fullName);
|
||
else
|
||
return _entityManager.AddGroup<T>(fullName);
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="entity"></param>
|
||
public void RemoveEntity(Entity entity)
|
||
{
|
||
if (entity != null)
|
||
{
|
||
_entityManager.RemoveEntity(entity.id);
|
||
entity.Dispose();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public void RemoveAllEntity()
|
||
{
|
||
//if (_mainPlayer != null)
|
||
// _mainPlayer.Dispose();
|
||
|
||
//
|
||
foreach (var entity in _defaultGroup.GetAllEntities())
|
||
{
|
||
entity.Dispose();
|
||
}
|
||
_defaultGroup.RemoveAll();
|
||
|
||
//
|
||
foreach (var entity in _bossGroup.GetAllEntities())
|
||
{
|
||
entity.Dispose();
|
||
}
|
||
_bossGroup.RemoveAll();
|
||
|
||
//
|
||
foreach (var entity in _minionGroup.GetAllEntities())
|
||
{
|
||
entity.Dispose();
|
||
}
|
||
_minionGroup.RemoveAll();
|
||
|
||
foreach (var entity in _propGroup.GetAllEntities())
|
||
{
|
||
entity.Dispose();
|
||
}
|
||
_propGroup.RemoveAll();
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public void RemoveAllProp()
|
||
{
|
||
foreach (var entity in _propGroup.GetAllEntities())
|
||
{
|
||
entity.Dispose();
|
||
}
|
||
_propGroup.RemoveAll();
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public void RemoveAllEnemy()
|
||
{
|
||
foreach (var entity in _bossGroup.GetAllEntities())
|
||
{
|
||
entity.Dispose();
|
||
}
|
||
_bossGroup.RemoveAll();
|
||
|
||
foreach (var entity in _minionGroup.GetAllEntities())
|
||
{
|
||
entity.Dispose();
|
||
}
|
||
_minionGroup.RemoveAll();
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 响应事件
|
||
|
||
/// <summary>
|
||
/// 进入场景
|
||
/// </summary>
|
||
public void OnEnterScene()
|
||
{
|
||
AssetProxy.Instance.CreateReleasePool(ASSET_POOL_NAME);
|
||
|
||
this.CreateMainPlayer();
|
||
//EntityMainPlayer.Instance.OnEnterScene();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 离开场景
|
||
/// </summary>
|
||
public void OnExitScene()
|
||
{
|
||
_entityManager.Clear();
|
||
|
||
AssetProxy.Instance.RemoveReleasePool(ASSET_POOL_NAME);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 创建
|
||
|
||
/// <summary>
|
||
/// 创建主角
|
||
/// </summary>
|
||
public void CreateMainPlayer()
|
||
{
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建Boss
|
||
/// </summary>
|
||
public void CreateBoss(EntityInfo info)
|
||
{
|
||
if (_bossGroup != null)
|
||
{
|
||
info.guid = GetLocalGuid();
|
||
var boss = new GameObject(info.name).AddComponent<EntityBoss>();
|
||
boss.transform.parent = defaultRoot;
|
||
boss.Init(info);
|
||
|
||
_bossGroup.AddEntity(boss);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建小兵
|
||
/// </summary>
|
||
/// <param name="info"></param>
|
||
public void CreateMinion(EntityInfo info)
|
||
{
|
||
if (_minionGroup != null)
|
||
{
|
||
info.guid = GetLocalGuid();
|
||
|
||
var monster = new GameObject(info.name).AddComponent<EntityMinion>();
|
||
monster.transform.parent = defaultRoot;
|
||
monster.Init(info);
|
||
|
||
_minionGroup.AddEntity(monster);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建掉落包
|
||
/// </summary>
|
||
public void CreateDropItem(EntityInfo info)
|
||
{
|
||
if (_defaultGroup != null && dropPrefabs.Count > 0)
|
||
{
|
||
info.guid = GetLocalGuid();
|
||
var dropGO = SpawnPool.Spawn(dropPrefabs[0]);
|
||
dropGO.transform.parent = defaultRoot;
|
||
var drop = dropGO.GetComponent<EntityDrop>();
|
||
drop.Init(info);
|
||
|
||
_defaultGroup.AddEntity(drop);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 道具
|
||
/// </summary>
|
||
/// <param name="info"></param>
|
||
public void CreateProp(EntityInfo info)
|
||
{
|
||
if (_propGroup != null && info.itemId <= propPrefabs.Count)
|
||
{
|
||
var prefab = propPrefabs[info.itemId - 1];
|
||
if (prefab)
|
||
{
|
||
info.guid = GetLocalGuid();
|
||
var propGO = Object.Instantiate(prefab);
|
||
propGO.transform.parent = defaultRoot;
|
||
var prop = propGO.GetComponent<EntityProp>();
|
||
prop.Init(info);
|
||
_propGroup.AddEntity(prop);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="info"></param>
|
||
public void CreateCart(EntityInfo info)
|
||
{
|
||
if (_defaultGroup != null && cartPrefabs.Count > 0)
|
||
{
|
||
info.guid = GetLocalGuid();
|
||
var cart = Object.Instantiate(cartPrefabs[0], defaultRoot).GetComponent<EntityCart>();
|
||
cart.Init(info);
|
||
//_defaultGroup.AddEntity(cart);
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 查找
|
||
|
||
public Entity FindEntity(int id)
|
||
{
|
||
return _entityManager.GetEntity(id) as Entity;
|
||
}
|
||
|
||
public IReadOnlyCollection<EntityEnemy> FindAllBossInScene()
|
||
{
|
||
return _bossGroup.GetAllEntities();
|
||
}
|
||
|
||
public IReadOnlyCollection<EntityEnemy> FindAllMinionInScene()
|
||
{
|
||
return _minionGroup.GetAllEntities();
|
||
}
|
||
|
||
public IReadOnlyCollection<EntityProp> FindAllPropInScene()
|
||
{
|
||
return _propGroup.GetAllEntities();
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 父子关系
|
||
|
||
public Entity GetParent(int id)
|
||
{
|
||
return _entityManager.GetEntityParent(id) as Entity;
|
||
}
|
||
|
||
public void SetParent(int id, int parentId)
|
||
{
|
||
_entityManager.SetParentEntity(id, parentId);
|
||
}
|
||
|
||
readonly List<IEntity> _childEntities = new List<IEntity>(8);
|
||
public bool GetChildren(int id, List<Entity> result)
|
||
{
|
||
if (result != null && _entityManager.GetEntityChildren(id, _childEntities))
|
||
{
|
||
foreach (var item in _childEntities)
|
||
{
|
||
result.Add(item as Entity);
|
||
}
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Unity
|
||
|
||
public static EntityManager Instance => _Instance;
|
||
|
||
private static EntityManager _Instance;
|
||
|
||
private void Awake()
|
||
{
|
||
if (_Instance)
|
||
{
|
||
Destroy(_Instance.gameObject);
|
||
}
|
||
_Instance = this;
|
||
|
||
this.Initialize(this.OnInitialized);
|
||
OnEnterScene();
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
OnExitScene();
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|
||
|