using System;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using UnityEditor;
using UnityEngine;
using XGame.Framework.Map;
using XGame.Framework.ObjectCollection;
namespace XGame.Editor.Map
{
internal class EntityViewCodeGen : CodeGenerator
{
///
/// 生成代码
///
///
/// 0->Entity 1->MapUI
///
public bool Gen(EntityBehaviour behaviour, int entityType)
{
if (!VerifyPrefab(behaviour.gameObject))
{
return false;
}
try
{
var isMapUI = entityType == 1;
var codesFolder = isMapUI ? "Map/UI" : "Map/Entities";
InitPath(codesFolder);
var entityName = ToTitleCase(behaviour.gameObject.name);
if (!GenCommonCode(entityName, entityName, entityName + "View", isMapUI ? "MapUIViewTemplate" : "EntityViewTemplate"))
{
return false;
}
if (!GenAIComponent(behaviour, isMapUI, entityName))
{
return false;
}
if (!GenViewModel(behaviour, isMapUI, entityName))
{
return false;
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
catch (Exception ex)
{
Debug.LogException(ex);
return false;
}
return true;
}
private bool GenAIComponent(EntityBehaviour behaviour, bool isMapUI, string entityName)
{
if (isMapUI)
{ //MapUI 不生成AI脚本
return true;
}
var lastCodesPath = codesRootPath;
codesRootPath = codesRootPath.Replace("Map/Entities", "Battle/Components/AI");
var result = GenCommonCode(entityName, entityName, entityName + "AI", "AIComponentTemplate");
codesRootPath = lastCodesPath;
return result;
}
private bool GenViewModel(EntityBehaviour behaviour, bool isMapUI, string entityName)
{
var result = GenViewModelCode(behaviour.Collector as ObjectCollector, entityName, entityName, entityName + "VM", "EntityVMTemplate", codeText =>
{
return codeText.Replace("<#Namespace>", isMapUI ? $"{productName}.Map.UI" : $"{productName}.Map");
});
return result;
}
}
}