// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created : 2018-2-29
// Description : 游戏逻辑实体管理器,对游戏中所有Entity提供创建,移除和管理
// Last Modified By :
// Last Modified On :
// ***********************************************************************
//
//
// ***********************************************************************
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";
///
/// 框架层提供管理
///
private IEntityManager _entityManager;
///
///
///
private IEntityGroup _defaultGroup;
///
///
///
private IEntityGroup _bossGroup;
///
///
///
private IEntityGroup _minionGroup;
///
///
///
private IEntityGroup _propGroup;
private Transform defaultRoot
{
get => this.transform;
}
///
///
///
public List propPrefabs = new List(4);
///
///
///
public List cartPrefabs = new List(1);
///
///
///
public List dropPrefabs = new List(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();
_bossGroup = this.AddEntityGroup("b");
_minionGroup = this.AddEntityGroup("m");
_propGroup = this.AddEntityGroup();
completed?.Invoke();
}
private void OnInitialized()
{
}
///
///
///
///
///
private IEntityGroup AddEntityGroup(string sub = "") where T : Entity
{
var fullName = Utils.Text.GetFullName(sub);
if (_entityManager.HasGroup(fullName))
return _entityManager.GetGroup(fullName);
else
return _entityManager.AddGroup(fullName);
}
///
///
///
///
public void RemoveEntity(Entity entity)
{
if (entity != null)
{
_entityManager.RemoveEntity(entity.id);
entity.Dispose();
}
}
///
///
///
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();
}
///
///
///
public void RemoveAllProp()
{
foreach (var entity in _propGroup.GetAllEntities())
{
entity.Dispose();
}
_propGroup.RemoveAll();
}
///
///
///
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 响应事件
///
/// 进入场景
///
public void OnEnterScene()
{
AssetProxy.Instance.CreateReleasePool(ASSET_POOL_NAME);
this.CreateMainPlayer();
//EntityMainPlayer.Instance.OnEnterScene();
}
///
/// 离开场景
///
public void OnExitScene()
{
_entityManager.Clear();
AssetProxy.Instance.RemoveReleasePool(ASSET_POOL_NAME);
}
#endregion
#region 创建
///
/// 创建主角
///
public void CreateMainPlayer()
{
}
///
/// 创建Boss
///
public void CreateBoss(EntityInfo info)
{
if (_bossGroup != null)
{
info.guid = GetLocalGuid();
var boss = new GameObject(info.name).AddComponent();
boss.transform.parent = defaultRoot;
boss.Init(info);
_bossGroup.AddEntity(boss);
}
}
///
/// 创建小兵
///
///
public void CreateMinion(EntityInfo info)
{
if (_minionGroup != null)
{
info.guid = GetLocalGuid();
var monster = new GameObject(info.name).AddComponent();
monster.transform.parent = defaultRoot;
monster.Init(info);
_minionGroup.AddEntity(monster);
}
}
///
/// 创建掉落包
///
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();
drop.Init(info);
_defaultGroup.AddEntity(drop);
}
}
///
/// 道具
///
///
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();
prop.Init(info);
_propGroup.AddEntity(prop);
}
}
}
///
///
///
///
public void CreateCart(EntityInfo info)
{
if (_defaultGroup != null && cartPrefabs.Count > 0)
{
info.guid = GetLocalGuid();
var cart = Object.Instantiate(cartPrefabs[0], defaultRoot).GetComponent();
cart.Init(info);
//_defaultGroup.AddEntity(cart);
}
}
#endregion
#region 查找
public Entity FindEntity(int id)
{
return _entityManager.GetEntity(id) as Entity;
}
public IReadOnlyCollection FindAllBossInScene()
{
return _bossGroup.GetAllEntities();
}
public IReadOnlyCollection FindAllMinionInScene()
{
return _minionGroup.GetAllEntities();
}
public IReadOnlyCollection 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 _childEntities = new List(8);
public bool GetChildren(int id, List 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
}
}