157 lines
5.3 KiB
C#
157 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using SepCore.Definition;
|
|
using UnityGameFramework.Runtime;
|
|
using SepCore.CustomUtility;
|
|
|
|
namespace SepCore.Entity
|
|
{
|
|
public static class EntityExtension
|
|
{
|
|
private static int s_SerialId = -10;
|
|
private static int _serialId = 0;
|
|
|
|
private const string EntityNamespace = "SepCore.Entity.";
|
|
private static readonly Dictionary<string, Type> _typeDict;
|
|
|
|
static EntityExtension()
|
|
{
|
|
_typeDict = new Dictionary<string, Type>();
|
|
}
|
|
|
|
public static EntityBase GetGameEntity(this EntityComponent entityComponent, int entityId)
|
|
{
|
|
UnityGameFramework.Runtime.Entity entity = entityComponent.GetEntity(entityId);
|
|
if (entity == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return (EntityBase)entity.Logic;
|
|
}
|
|
|
|
public static void HideEntity(this EntityComponent entityComponent, EntityBase entity)
|
|
{
|
|
entityComponent.HideEntity(entity.Entity);
|
|
}
|
|
|
|
public static void AttachEntity(this EntityComponent entityComponent, EntityBase entityBase, int ownerId,
|
|
string parentTransformPath = null, object userData = null)
|
|
{
|
|
entityComponent.AttachEntity(entityBase.Entity, ownerId, parentTransformPath, userData);
|
|
}
|
|
|
|
public static void ShowEntity<T>(this EntityComponent entityComponent, EntityDataBase data, string group,
|
|
int priority) where T : EntityBase
|
|
{
|
|
if (data == null)
|
|
{
|
|
Log.Warning("Data is invalid.");
|
|
return;
|
|
}
|
|
|
|
EntityConfig entityConfig = GameEntry.Luban.Get<EntityConfig>(data.AssetName);
|
|
if (entityConfig == null)
|
|
{
|
|
Log.Warning("Can not load entity assetName '{0}' from data table.", data.AssetName);
|
|
return;
|
|
}
|
|
|
|
entityComponent.ShowEntity(data.Id, typeof(T), AssetUtility.GetEntityAsset(entityConfig.PrefabPath), group,
|
|
priority, data);
|
|
}
|
|
|
|
public static void ShowPlayer(this EntityComponent entityComponent, PlayerData data)
|
|
{
|
|
entityComponent.ShowEntity(typeof(Player), "Player", Constant.AssetPriority.PlayerAsset, data);
|
|
}
|
|
|
|
public static void ShowEnemy(this EntityComponent entityComponent, EnemyData data)
|
|
{
|
|
if (data == null)
|
|
{
|
|
Log.Warning("Enemy data is invalid.");
|
|
return;
|
|
}
|
|
|
|
var enemyType = TryGetType(data.EnemyType.ToString());
|
|
entityComponent.ShowEntity(enemyType, "Enemy", Constant.AssetPriority.EnemyAsset, data);
|
|
}
|
|
|
|
public static void ShowWeapon(this EntityComponent entityComponent, WeaponData data)
|
|
{
|
|
if (data == null)
|
|
{
|
|
Log.Warning("Weapon data is invalid.");
|
|
return;
|
|
}
|
|
|
|
var weaponType = TryGetType("Weapon." + data.WeaponType);
|
|
entityComponent.ShowEntity(weaponType, "Weapon", Constant.AssetPriority.WeaponAsset, data);
|
|
}
|
|
|
|
public static void ShowEffect(this EntityComponent entityComponent, EffectData data)
|
|
{
|
|
entityComponent.ShowEntity(typeof(Effect), "Effect", Constant.AssetPriority.EffectAsset, data);
|
|
}
|
|
|
|
public static void ShowCoin(this EntityComponent entityComponent, CoinData data)
|
|
{
|
|
entityComponent.ShowEntity(typeof(CoinEntity), "Drop", Constant.AssetPriority.EffectAsset, data);
|
|
}
|
|
|
|
public static void ShowExp(this EntityComponent entityComponent, ExpData data)
|
|
{
|
|
entityComponent.ShowEntity(typeof(ExpEntity), "Drop", Constant.AssetPriority.EffectAsset, data);
|
|
}
|
|
|
|
private static void ShowEntity(this EntityComponent entityComponent, Type logicType, string entityGroup,
|
|
int priority, EntityDataBase data)
|
|
{
|
|
if (data == null)
|
|
{
|
|
Log.Warning("Data is invalid.");
|
|
return;
|
|
}
|
|
|
|
EntityConfig entityConfig = GameEntry.Luban.Get<EntityConfig>(data.AssetName);
|
|
if (entityConfig == null)
|
|
{
|
|
Log.Warning("Can not load entity assetName '{0}' from data table.", data.AssetName);
|
|
return;
|
|
}
|
|
|
|
entityComponent.ShowEntity(data.Id, logicType, AssetUtility.GetEntityAsset(entityConfig.PrefabPath), entityGroup,
|
|
priority, data);
|
|
}
|
|
|
|
private static Type TryGetType(string rawTypeName)
|
|
{
|
|
string typeName = EntityNamespace + rawTypeName;
|
|
if (!_typeDict.TryGetValue(typeName, out Type type))
|
|
{
|
|
type = Type.GetType(typeName);
|
|
if (type == null)
|
|
{
|
|
Log.Warning("Can not load entity type '{0}'.", typeName);
|
|
return null;
|
|
}
|
|
|
|
_typeDict.Add(typeName, type);
|
|
}
|
|
|
|
return type;
|
|
}
|
|
|
|
public static int NextId(this EntityComponent entityComponent)
|
|
{
|
|
return --s_SerialId;
|
|
}
|
|
|
|
public static int SerialId(this EntityComponent entityComponent)
|
|
{
|
|
return ++_serialId;
|
|
}
|
|
}
|
|
}
|