104 lines
3.7 KiB
C#
104 lines
3.7 KiB
C#
//------------------------------------------------------------
|
|
// Game Framework
|
|
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
|
// Homepage: https://gameframework.cn/
|
|
// Feedback: mailto:ellan@gameframework.cn
|
|
//------------------------------------------------------------
|
|
|
|
using GameFramework.DataTable;
|
|
using System;
|
|
using GeometryTD.CustomUtility;
|
|
using GeometryTD.DataTable;
|
|
using GeometryTD.Entity;
|
|
using GeometryTD.Entity.EntityData;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD
|
|
{
|
|
public static class EntityExtension
|
|
{
|
|
private static int s_SerialId = 0;
|
|
|
|
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 ShowEnemy(this EntityComponent entityComponent, EnemyData data)
|
|
{
|
|
entityComponent.ShowEntity(typeof(EnemyEntity), "Enemy", Constant.AssetPriority.EnemyAsset, data);
|
|
}
|
|
|
|
public static void ShowDefenseTower(this EntityComponent entityComponent, TowerData data)
|
|
{
|
|
entityComponent.ShowEntity(typeof(TowerEntity), "Tower", Constant.AssetPriority.EnemyAsset, data);
|
|
}
|
|
|
|
public static void ShowBullet(this EntityComponent entityComponent, BulletData data)
|
|
{
|
|
entityComponent.ShowEntity(typeof(BulletEntity), "Bullet", Constant.AssetPriority.BulletAsset, data);
|
|
}
|
|
|
|
public static void ShowMap(this EntityComponent entityComponent, MapData data)
|
|
{
|
|
ShowMap(entityComponent, data, null);
|
|
}
|
|
|
|
public static void ShowMap(this EntityComponent entityComponent, MapData data, string mapAssetName)
|
|
{
|
|
if (data == null)
|
|
{
|
|
Log.Warning("Map data is invalid.");
|
|
return;
|
|
}
|
|
|
|
string resolvedMapAssetName = string.IsNullOrEmpty(mapAssetName) ? data.LevelId.ToString() : mapAssetName;
|
|
string mapAssetPath = AssetUtility.GetLevelMapAsset(resolvedMapAssetName);
|
|
entityComponent.ShowEntity(data.Id, typeof(MapEntity), mapAssetPath, "Map", Constant.AssetPriority.MapAsset, 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;
|
|
}
|
|
|
|
IDataTable<DREntity> dtEntity = GameEntry.DataTable.GetDataTable<DREntity>();
|
|
DREntity drEntity = dtEntity.GetDataRow(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;
|
|
}
|
|
}
|
|
}
|