geometry-tower-defense/Assets/GameMain/Scripts/Entity/EntityExtension.cs

108 lines
3.9 KiB
C#

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 _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)
{
ShowMap(entityComponent, new MapEntityLoadContext(data, null, null), mapAssetName);
}
public static void ShowMap(this EntityComponent entityComponent, MapEntityLoadContext loadContext)
{
ShowMap(entityComponent, loadContext, null);
}
public static void ShowMap(this EntityComponent entityComponent, MapEntityLoadContext loadContext, string mapAssetName)
{
MapData data = loadContext?.InitialMapData;
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, loadContext);
}
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 --_serialId;
}
}
}