180 lines
4.2 KiB
C#
180 lines
4.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Gameplay;
|
|
using LC.Newtonsoft.Json;
|
|
using PhxhSDK.Phxh;
|
|
using TGS;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
using UnityEditor.SceneManagement;
|
|
#endif
|
|
using UnityEngine;
|
|
|
|
[ExecuteAlways]
|
|
public class MapManager : MonoBehaviour
|
|
{
|
|
//[SerializeField]
|
|
//[HideInInspector]
|
|
private TerrainGridSystem _tgs;
|
|
|
|
[SerializeField]
|
|
[HideInInspector]
|
|
private TextAsset _mapDataJson;
|
|
|
|
//[SerializeField]
|
|
//[HideInInspector]
|
|
private MapData _mapData;
|
|
|
|
private Map _map;
|
|
|
|
public Map Map
|
|
{
|
|
get
|
|
{
|
|
if (_map == null)
|
|
{
|
|
_map = MapUtils.LoadMap(TGS, MapData);
|
|
}
|
|
|
|
return _map;
|
|
}
|
|
}
|
|
|
|
public TerrainGridSystem TGS
|
|
{
|
|
get
|
|
{
|
|
if (_tgs == null)
|
|
{
|
|
var allTgs = FindObjectsOfType<TerrainGridSystem>();
|
|
_tgs = allTgs.FirstOrDefault(t => t.gameObject.scene == gameObject.scene);
|
|
}
|
|
|
|
return _tgs;
|
|
}
|
|
}
|
|
|
|
public TextAsset MapDataJson
|
|
{
|
|
get => _mapDataJson;
|
|
set
|
|
{
|
|
if (_mapDataJson != value && value != null)
|
|
{
|
|
var data = JsonToMapData(value);
|
|
if (data != null)
|
|
{
|
|
_mapDataJson = value;
|
|
Refresh();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Refresh()
|
|
{
|
|
_mapData = null;
|
|
_map = null;
|
|
var mapData = MapData;
|
|
var map = Map;
|
|
TGS.showCells = true;
|
|
TGS.colorizeTerritories = true;
|
|
TGS.TerritoryDestroyAll();
|
|
}
|
|
|
|
public MapData MapData
|
|
{
|
|
get
|
|
{
|
|
if (_mapData == null)
|
|
{
|
|
var data = JsonToMapData(MapDataJson);
|
|
if (data != null)
|
|
{
|
|
_mapData = data;
|
|
var width = _mapData.width;
|
|
var height = _mapData.height;
|
|
if (width != 0 && height != 0)
|
|
{
|
|
CommonUtils.CorrectTGSColumnAndRow(ref width, ref height);
|
|
}
|
|
else
|
|
{
|
|
width = 2;
|
|
height = 2;
|
|
}
|
|
|
|
TGS.columnCount = width;
|
|
TGS.rowCount = height;
|
|
_mapData.width = width;
|
|
_mapData.height = height;
|
|
}
|
|
}
|
|
|
|
return _mapData;
|
|
}
|
|
// set
|
|
// {
|
|
// if (_mapData != value)
|
|
// {
|
|
// _mapData = value;
|
|
// if (value == null)
|
|
// {
|
|
// Map = null;
|
|
// }
|
|
// else
|
|
// {
|
|
// Map = MapUtils.LoadMap(TGS, _mapData);
|
|
// //MapUtils.BindCameraController(_camera, Map);
|
|
// //var cameraController = _camera.gameObject.GetComponent<CameraController>();
|
|
// //cameraController.InitializeCameraPosition(initialCameraPosition);
|
|
// }
|
|
// }
|
|
// }
|
|
}
|
|
|
|
private MapData JsonToMapData(TextAsset jsonText)
|
|
{
|
|
if (jsonText != null)
|
|
{
|
|
var jsonStr = jsonText.text;
|
|
if (string.IsNullOrEmpty(jsonStr))
|
|
{
|
|
var mapData = new MapData();
|
|
mapData.ResetBlocks();
|
|
return mapData;
|
|
}
|
|
else
|
|
{
|
|
return JsonConvert.DeserializeObject<MapData>(jsonStr);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public void SaveMapData()
|
|
{
|
|
#if UNITY_EDITOR
|
|
if (_mapDataJson != null && !Application.isPlaying)
|
|
{
|
|
var path = AssetDatabase.GetAssetPath(_mapDataJson);
|
|
JsonHelper.SaveJson(path, _mapData);
|
|
Debug.Log($"保存地图数据成功!");
|
|
AssetDatabase.Refresh();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
Refresh();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
SaveMapData();
|
|
}
|
|
} |