NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Map/MapManager.cs

194 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Framework;
using Gameplay;
using LC.Newtonsoft.Json;
using PhxhSDK;
using PhxhSDK.AOT;
using PhxhSDK.Phxh;
using TGS;
#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.Windows;
#endif
using UnityEngine;
using File = System.IO.File;
[ExecuteAlways]
public class MapManager : MonoBehaviour
{
private TerrainGridSystem _tgs;
[SerializeField]
[HideInInspector]
private TextAsset _mapDataJson;
private MapData _mapData;
private Map _map;
private CameraController _cameraController;
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)
{
var data = JsonToMapData(value);
_mapDataJson = value;
if (data != null)
{
Refresh();
}
else
{
_tgs.TerritoryDestroyAll();
}
}
}
}
public MapData MapData
{
get
{
if (_mapData == null)
{
var data = JsonToMapData(MapDataJson);
if (data != null)
{
_mapData = data;
TGS.columnCount = _mapData.Width;
TGS.rowCount = _mapData.Height;
}
}
return _mapData;
}
}
public Map Map
{
get
{
if (_map == null && MapData != null)
{
_map = MapUtils.LoadMap(TGS, MapData);
if (_map != null && CameraController)
MapUtils.BindCameraController(CameraController.GetComponent<Camera>(), _map);
}
return _map;
}
}
public CameraController CameraController
{
get
{
if (_cameraController == null)
{
var cameras = FindObjectsOfType<Camera>();
foreach (var cam in cameras)
{
if (cam.gameObject.scene == gameObject.scene)
{
var cameraController = cam.GetComponent<CameraController>();
if (cameraController)
{
_cameraController = cameraController;
break;
}
}
}
}
return _cameraController;
}
}
public void Refresh()
{
_mapData = null;
_map = null;
if (_mapDataJson == null)
return;
var mapData = MapData;
var map = Map;
TGS.showCells = true;
TGS.colorizeTerritories = true;
//TGS.TerritoryDestroyAll();
}
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
{
//newtowsoft的Json序列化JsonIgnore不起作用先用Unity的
//return JsonConvert.DeserializeObject<MapData>(jsonStr);
return JsonUtility.FromJson<MapData>(jsonStr);
}
}
return null;
}
public void SaveMapData()
{
#if UNITY_EDITOR
if (_mapDataJson != null && _mapData != null)
{
var path = AssetDatabase.GetAssetPath(_mapDataJson);
//newtowsoft的Json序列化JsonIgnore不起作用先用Unity的
//JsonHelper.SaveJson(path, _mapData);
var jsonStr = JsonUtility.ToJson(_mapData, true);
File.WriteAllText(path, jsonStr);
Debug.Log($"保存地图数据成功!");
AssetDatabase.Refresh();
}
#endif
}
private void OnEnable()
{
if (!CommonUtils.IsInGame())
Refresh();
}
private void OnDestroy()
{
if (YooAssetBuildStatus.isInBuild)
return;
if (!Application.isPlaying)
SaveMapData();
}
}