using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using DragonLi.Core;
using UnityEngine;
using System.Runtime.Serialization;
namespace Valheim
{
public class EditMap : MonoBehaviour
{
public Place place;
public GameObject Areas;
public GameObject Pets;
public GameObject Enemys;
public GameObject Cages;
public GameObject Revives;
public ObjDoor Door;
public ObjBoss Boss;
///
/// 编辑器模式的文本存储
///
/// 路径,类似于Assets/Resources
/// 名字,需要带后缀
/// 存储的文本
void WindowsSave(string _path, string _name, string _str)
{
#if UNITY_EDITOR
System.IO.Directory.CreateDirectory(_path);
using (System.IO.StreamWriter writer = System.IO.File.CreateText(_path + "/" + _name))
writer.Write(_str);
UnityEditor.AssetDatabase.Refresh();
#endif
}
public void Start()
{
MonoSingleton.Instance.WaitSecondTodo(() =>
{
Debug.Log("开始生成地图");
// // 修改怪物区域id
// for (int i = 0; i < Areas.transform.childCount; i++)
// {
// for (int j = 0; j < Enemys.transform.childCount; j++)
// {
// Transform area = Areas.transform.GetChild(i);
// Transform obj = Enemys.transform.GetChild(j);
// ObjEnemy objEnemy = obj.GetComponent();
// Bounds bounds = area.GetComponent().bounds;
// // 判断是否在区域内
// if (bounds.Contains(new Vector3(obj.transform.position.x, 0, obj.transform.position.z)))
// {
// if (objEnemy != null)
// {
// // objEnemy.areaId = area.GetComponent().AreaId;
// }
// }
// }
// }
// // 修改宠物笼子id
// for (int i = 0; i < Cages.transform.childCount; i++)
// {
// for (int j = 0; j < Pets.transform.childCount; j++)
// {
// Transform cage = Cages.transform.GetChild(i);
// Transform obj = Pets.transform.GetChild(j);
// ObjPet objPet = obj.GetComponent();
// Bounds bounds = cage.GetComponent().bounds;
// // 判断是否在笼子内
// if (bounds.Contains(new Vector3(obj.transform.position.x, 0, obj.transform.position.z)))
// {
// if (objPet != null)
// {
// objPet.cageId = cage.GetComponent().CageId;
// }
// }
// }
// }
string jsonstr = "{\"Data\":[";
int index = 0;
// 区域
for (int i = 0; i < Areas.transform.childCount; i++)
{
Transform obj = Areas.transform.GetChild(i);
ObjArea objArea = obj.GetComponent();
if (objArea != null)
{
if (i != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(objArea.PreparData());
}
}
// 笼子
for (int i = 0; i < Cages.transform.childCount; i++)
{
Transform obj = Cages.transform.GetChild(i);
ObjCage objCage = obj.GetComponent();
if (objCage != null)
{
if (index != 0 || i != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(objCage.PreparData());
}
}
// 怪物
for (int i = 0; i < Enemys.transform.childCount; i++)
{
Transform obj = Enemys.transform.GetChild(i);
ObjEnemy objEnemy = obj.GetComponent();
if (objEnemy != null)
{
if (index != 0 || i != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(objEnemy.PreparData());
}
}
// 宠物
for (int i = 0; i < Pets.transform.childCount; i++)
{
Transform obj = Pets.transform.GetChild(i);
ObjPet objPet = obj.GetComponent();
if (objPet != null)
{
if (index != 0 || i != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(objPet.PreparData());
}
}
// 复活点
for (int i = 0; i < Revives.transform.childCount; i++)
{
Transform obj = Revives.transform.GetChild(i);
ObjRevive objRevive = obj.GetComponent();
if (objRevive != null)
{
if (index != 0 || i != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(objRevive.PreparData());
}
}
// 门
if (Door != null)
{
if (index != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(Door.PreparData());
}
// boss
if (Boss != null)
{
if (index != 0)
{
jsonstr += ",";
}
index++;
jsonstr += JsonUtility.ToJson(Boss.PreparData());
}
jsonstr += "]}";
Debug.Log("数据准备就绪");
WindowsSave("Assets/_Valheim/Resources/Maps", place + ".json", jsonstr);
Debug.Log("地图已保存!");
}, 1);
}
}
}