143 lines
5.1 KiB
C#
143 lines
5.1 KiB
C#
//------------------------------------------------------------
|
||
// Game Framework
|
||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||
// Homepage: https://gameframework.cn/
|
||
// Feedback: mailto:ellan@gameframework.cn
|
||
//------------------------------------------------------------
|
||
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using Definition;
|
||
using Entity.EntityData;
|
||
using UnityGameFramework.Runtime;
|
||
using DataTable;
|
||
using Definition.Enum;
|
||
using Game.Utility;
|
||
|
||
namespace Entity
|
||
{
|
||
public static class EntityExtension
|
||
{
|
||
// 关于 EntityId 的约定:
|
||
// 0 为无效
|
||
// 正值用于和服务器通信的实体(如玩家角色、NPC、怪等,服务器只产生正值)
|
||
// 负值用于本地生成的临时实体(如特效、FakeObject等)
|
||
private static int s_SerialId = -10;
|
||
|
||
private const string EntityNamespace = "Entity.";
|
||
private static 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 ShowPlayer(this EntityComponent entityComponent, PlayerData data)
|
||
{
|
||
entityComponent.ShowEntity(typeof(Player), "Player", Constant.AssetPriority.PlayerAsset, data);
|
||
}
|
||
|
||
public static void ShowEnemy(this EntityComponent entityComponent, EnemyData data)
|
||
{
|
||
string typeName = EntityNamespace + (EnemyType)data.TypeId;
|
||
if (!_typeDict.TryGetValue(typeName, out Type enemyType))
|
||
{
|
||
enemyType = Type.GetType(typeName);
|
||
if (enemyType == null)
|
||
{
|
||
Log.Warning("Can not load entity type '{0}'.", typeName);
|
||
return;
|
||
}
|
||
|
||
_typeDict.Add(typeName, enemyType);
|
||
}
|
||
|
||
entityComponent.ShowEntity(enemyType, "Enemy", Constant.AssetPriority.EnemyAsset, data);
|
||
}
|
||
|
||
public static void ShowWeapon(this EntityComponent entityComponent, WeaponData data)
|
||
{
|
||
string typeName = EntityNamespace + (WeaponType)data.TypeId;
|
||
if (!_typeDict.TryGetValue(typeName, out Type weaponType))
|
||
{
|
||
weaponType = Type.GetType(typeName);
|
||
if (weaponType == null)
|
||
{
|
||
Log.Warning("Can not load entity type '{0}'.", typeName);
|
||
return;
|
||
}
|
||
|
||
_typeDict.Add(typeName, weaponType);
|
||
}
|
||
|
||
entityComponent.ShowEntity(weaponType, "Weapon", Constant.AssetPriority.WeaponAsset, data);
|
||
}
|
||
|
||
public static void ShowBullet(this EntityComponent entityComponent, BulletData data)
|
||
{
|
||
entityComponent.ShowEntity(typeof(Bullet), "Bullet", Constant.AssetPriority.BulletAsset, 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;
|
||
}
|
||
|
||
DREntity drEntity = GameEntry.DataTable.GetDataTableRow<DREntity>(data.TypeId);
|
||
if (drEntity == null)
|
||
{
|
||
Log.Warning("Can not load entity id '{0}' from data table.", data.TypeId.ToString());
|
||
return;
|
||
}
|
||
|
||
entityComponent.ShowEntity(data.Id, logicType, AssetUtility.GetEntityAsset(drEntity.AssetName), entityGroup,
|
||
priority, data);
|
||
}
|
||
|
||
public static int GenerateSerialId(this EntityComponent entityComponent)
|
||
{
|
||
return --s_SerialId;
|
||
}
|
||
}
|
||
} |