2023-12-15 18:32:21 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2023-12-20 15:55:42 +08:00
|
|
|
|
using Framework;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
using Gameplay;
|
|
|
|
|
|
using LC.Newtonsoft.Json;
|
2023-12-21 15:47:46 +08:00
|
|
|
|
using PhxhSDK;
|
2024-02-21 13:06:50 +08:00
|
|
|
|
using PhxhSDK.AOT;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
using PhxhSDK.Phxh;
|
|
|
|
|
|
using TGS;
|
|
|
|
|
|
#if UNITY_EDITOR
|
2023-12-18 19:42:46 +08:00
|
|
|
|
using System.IO;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
|
using UnityEditor.SceneManagement;
|
2023-12-18 19:42:46 +08:00
|
|
|
|
using UnityEngine.Windows;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
using UnityEngine;
|
2023-12-18 19:42:46 +08:00
|
|
|
|
using File = System.IO.File;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
|
|
|
|
|
|
[ExecuteAlways]
|
|
|
|
|
|
public class MapManager : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
private TerrainGridSystem _tgs;
|
|
|
|
|
|
|
2024-06-04 14:45:36 +08:00
|
|
|
|
[SerializeField] [HideInInspector] private TextAsset _mapDataJson;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
|
|
|
|
|
|
private MapData _mapData;
|
|
|
|
|
|
|
|
|
|
|
|
private Map _map;
|
|
|
|
|
|
|
2023-12-21 15:05:08 +08:00
|
|
|
|
private CameraController _cameraController;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
2023-12-18 19:42:46 +08:00
|
|
|
|
if (_mapDataJson != value)
|
2023-12-15 18:32:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
var data = JsonToMapData(value);
|
2023-12-18 19:42:46 +08:00
|
|
|
|
_mapDataJson = value;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
if (data != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Refresh();
|
|
|
|
|
|
}
|
2023-12-20 12:47:57 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_tgs.TerritoryDestroyAll();
|
|
|
|
|
|
}
|
2023-12-15 18:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public MapData MapData
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_mapData == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var data = JsonToMapData(MapDataJson);
|
|
|
|
|
|
if (data != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_mapData = data;
|
2023-12-20 12:47:57 +08:00
|
|
|
|
TGS.columnCount = _mapData.Width;
|
|
|
|
|
|
TGS.rowCount = _mapData.Height;
|
2023-12-15 18:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _mapData;
|
|
|
|
|
|
}
|
2023-12-21 15:05:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
2023-12-15 18:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private MapData JsonToMapData(TextAsset jsonText)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (jsonText != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var jsonStr = jsonText.text;
|
|
|
|
|
|
if (string.IsNullOrEmpty(jsonStr))
|
|
|
|
|
|
{
|
|
|
|
|
|
var mapData = new MapData();
|
2023-12-18 19:42:46 +08:00
|
|
|
|
//mapData.ResetBlocks();
|
2023-12-15 18:32:21 +08:00
|
|
|
|
return mapData;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-12-18 19:42:46 +08:00
|
|
|
|
//newtowsoft的Json序列化JsonIgnore不起作用,先用Unity的
|
|
|
|
|
|
//return JsonConvert.DeserializeObject<MapData>(jsonStr);
|
|
|
|
|
|
return JsonUtility.FromJson<MapData>(jsonStr);
|
2023-12-15 18:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SaveMapData()
|
|
|
|
|
|
{
|
|
|
|
|
|
#if UNITY_EDITOR
|
2023-12-20 12:47:57 +08:00
|
|
|
|
if (_mapDataJson != null && _mapData != null)
|
2023-12-15 18:32:21 +08:00
|
|
|
|
{
|
2024-06-04 14:45:36 +08:00
|
|
|
|
SaveDate();
|
2023-12-15 18:32:21 +08:00
|
|
|
|
var path = AssetDatabase.GetAssetPath(_mapDataJson);
|
2023-12-18 19:42:46 +08:00
|
|
|
|
//newtowsoft的Json序列化JsonIgnore不起作用,先用Unity的
|
|
|
|
|
|
//JsonHelper.SaveJson(path, _mapData);
|
2023-12-20 12:47:57 +08:00
|
|
|
|
var jsonStr = JsonUtility.ToJson(_mapData, true);
|
2023-12-18 19:42:46 +08:00
|
|
|
|
File.WriteAllText(path, jsonStr);
|
2023-12-15 18:32:21 +08:00
|
|
|
|
Debug.Log($"保存地图数据成功!");
|
|
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
2024-06-04 14:45:36 +08:00
|
|
|
|
|
|
|
|
|
|
private void SaveDate()
|
|
|
|
|
|
{
|
2024-06-04 17:21:01 +08:00
|
|
|
|
Map.MapData.capturePoints = new List<int>();
|
|
|
|
|
|
Map.MapData.flagStartPoints = new List<int>();
|
2024-06-04 14:45:36 +08:00
|
|
|
|
var mapBlocksData = Map.MapData.BlocksData;
|
|
|
|
|
|
for (var i = 0; i < mapBlocksData.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var blockData = mapBlocksData[i];
|
|
|
|
|
|
if (!blockData.IsDefaultTerrainType())
|
|
|
|
|
|
{
|
|
|
|
|
|
var propType = blockData.GetTerrainTypeProperty();
|
|
|
|
|
|
if (propType.capture && !Map.MapData.capturePoints.Contains(i))
|
|
|
|
|
|
{
|
|
|
|
|
|
Map.MapData.capturePoints.Add(i);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (propType.flag && !Map.MapData.flagStartPoints.Contains(i))
|
|
|
|
|
|
{
|
|
|
|
|
|
Map.MapData.flagStartPoints.Add(i);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-20 12:47:57 +08:00
|
|
|
|
private void OnEnable()
|
2023-12-15 18:32:21 +08:00
|
|
|
|
{
|
2023-12-21 15:47:46 +08:00
|
|
|
|
if (!CommonUtils.IsInGame())
|
2023-12-20 12:47:57 +08:00
|
|
|
|
Refresh();
|
2023-12-15 18:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
|
|
{
|
2024-02-21 13:06:50 +08:00
|
|
|
|
if (YooAssetBuildStatus.isInBuild)
|
|
|
|
|
|
return;
|
2023-12-20 12:47:57 +08:00
|
|
|
|
if (!Application.isPlaying)
|
|
|
|
|
|
SaveMapData();
|
2023-12-15 18:32:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|