Files
HX_MapEditor/Assets/Scripts/Map/MapManager.cs

220 lines
6.3 KiB
C#
Raw Normal View History

2025-06-14 13:46:24 +08:00
using HxGame;
using HxGame.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
using UnityEngine.UI;
2025-06-22 18:23:47 +08:00
using UnityEngine.UIElements;
2025-06-14 13:46:24 +08:00
public partial class MapManager : MonoBehaviour
{
//<2F><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public const int CELLSCALE = 100;
2025-06-16 23:59:28 +08:00
//<2F><><EFBFBD>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD>
public const int widthPixel = 48;
//<2F><><EFBFBD>Ӹ<EFBFBD><D3B8><EFBFBD><EFBFBD><EFBFBD>
public const int heightPixel = 32;
//<2F><><EFBFBD>ŵ<EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>
public const int PicMapPixel = 512;
2025-06-14 13:46:24 +08:00
public delegate void LoadFinishedCallback();
public LoadFinishedCallback onLoadFinishedCallback;
//public delegate void CloseMapCallback();
//public CloseMapCallback onCloseMapCallback;
2025-06-15 20:14:45 +08:00
2025-06-14 13:46:24 +08:00
private static MapManager _mapManager = null;
public static MapManager Instance
{
get
{
if (_mapManager == null)
_mapManager = FindObjectOfType<MapManager>();
return _mapManager;
}
}
[HideInInspector]
public int mapWidth = 0;
[HideInInspector]
public int mapHeight = 0;
private int _maxWidth; //<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private int _maxHeight; //<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><DFB6><EFBFBD><EFBFBD><EFBFBD>
private int _cellWidth;
private int _cellHeight;
private int _cellRows;
private int _cellCols;
//public CellNode[,] cellNodes;
2025-06-16 00:15:41 +08:00
public int _curOpenMapId;
2025-06-14 13:46:24 +08:00
private Material _cellMoveMat;
private Material _cellObsMat;
private Material _cellHideMat;
private Material _cellDefaultMat;
private Material _cellReturnMat; //<2F>سǵ<D8B3>
private Material _cellReliveMat; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private Material _cellTeleportMat; //<2F><><EFBFBD>͵<EFBFBD>
private Material _cellNpcMat; //npcλ<63><CEBB>
private Material _cellPathNodeMat; //pathNode
private Material _cellTriggerMat; //trigger
private bool _StartEditor;
private float _brushRadius;
private CellType _brushCellType;
public int CellRows { get { return _cellRows; } }
public int CellCols { get { return _cellCols; } }
public int CellWidth { get { return _cellWidth; } }
public int CellHeight { get { return _cellHeight; } }
public int MaxWidth { get { return _maxWidth; } }
public int MaxHeight { get { return _maxHeight; } }
private GameObject _curPathObj = null;
private void Start()
{
_cellMoveMat = Resources.Load<Material>("Materials/cellMoveMat");
_cellObsMat = Resources.Load<Material>("Materials/cellObsMat");
_cellHideMat = Resources.Load<Material>("Materials/cellHideMat");
_cellDefaultMat = Resources.Load<Material>("Materials/cellDefaultMat");
_cellReturnMat = Resources.Load<Material>("Materials/cellReturnMat");
_cellReliveMat = Resources.Load<Material>("Materials/cellReliveMat");
_cellTriggerMat = Resources.Load<Material>("Materials/cellTriggerMat");
_cellTeleportMat = Resources.Load<Material>("Materials/cellTeleportMat");
_cellNpcMat = Resources.Load<Material>("Materials/cellNpcMat");
_cellPathNodeMat = Resources.Load<Material>("Materials/cellPathNodeMat");
Cleanup();
}
private void OnCreatedMap(int mapId, int mapWidth, int mapHeight)
{
this.mapWidth = mapWidth;
this.mapHeight = mapHeight;
_curOpenMapId = mapId;
}
public void StartEditor()
{
if (_curOpenMapId < 0)
{
UIWindow.Instance.ShowMessage("<22><><EFBFBD>ȴ򿪵<C8B4>ͼ");
return;
}
_StartEditor = true;
}
public void CloseEditor()
{
_StartEditor = false;
}
public void SetBrush(float radius, CellType type)
{
_brushRadius = radius;
_brushCellType = type;
}
List<MapConfig.AreaConfig> tempSafeAreasConfig = new List<MapConfig.AreaConfig>();
public void CloseMap()
{
if (_curOpenMapId <= 0)
return;
2025-06-22 18:23:47 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><D0B3><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
foreach (Transform child in mapMonsterArea)
{
Destroy(child.gameObject);
}
foreach (Transform child in mapNPCArea)
{
Destroy(child.gameObject);
}
foreach (Transform child in mapTeleportArea)
{
Destroy(child.gameObject);
}
foreach (Transform child in mapReliveArea)
{
Destroy(child.gameObject);
}
2025-06-14 13:46:24 +08:00
UIWindow.Instance.uiCellInfo.CloseMap();
UIWindow.Instance.uiMonstersPanel.RemoveAll();
2025-06-16 00:15:41 +08:00
ClearMapRegions();
2025-06-14 13:46:24 +08:00
Cleanup();
}
private void Cleanup()
{
mapWidth = 0;
mapHeight = 0;
_maxWidth = 0;
_maxHeight = 0;
_cellWidth = -1;
_cellHeight = -1;
_cellRows = -1;
_cellCols = -1;
_curOpenMapId = -1;
_StartEditor = false;
_brushRadius = 0;
_brushCellType = CellType.None;
}
private void Update()
{
bool bHoverUI = EventSystem.current != null && EventSystem.current.IsPointerOverGameObject();
if (bHoverUI)
return;
2025-06-15 20:14:45 +08:00
UpdateCamera();
2025-06-22 15:21:25 +08:00
UpdateCellPos();
2025-06-14 13:46:24 +08:00
}
public void LoadSprite(string spName, GameObject go, int regionWidth=0, int regionHeight=0)
{
StartCoroutine(LoadSpriteCor(spName, go, regionWidth, regionHeight));
}
IEnumerator LoadSpriteCor(string spName, GameObject go, int regionWidth, int regionHeight)
{
string spPath = PathUtil.GetTexture(_curOpenMapId, spName, "jpg");
using (UnityWebRequest req = UnityWebRequestTexture.GetTexture(spPath))
{
yield return req.SendWebRequest();
if (req.result == UnityWebRequest.Result.ConnectionError)
{
UIWindow.Instance.ShowMessage(req.error);
}
else
{
Texture2D tt = DownloadHandlerTexture.GetContent(req);
if (tt == null)
yield break;
//<2F><><EFBFBD>ݻ<EFBFBD>ȡ<EFBFBD><C8A1>Texture<72><65><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>sprite
//Sprite sprite = Sprite.Create((Texture2D)tt, new Rect(0, 0, tt.width, tt.height), new Vector2(0, 0));
//sprite.name = spName;
//go.GetComponent<Image>().sprite = sprite;
tt.name = spName;
go.GetComponent<RawImage>().texture = tt;
go.GetComponent<RectTransform>().sizeDelta = new Vector2(regionWidth, regionHeight);
}
}
}
}