139 lines
4.7 KiB
C#
139 lines
4.7 KiB
C#
using HxGame;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Runtime.Remoting.Metadata.W3cXsd2001;
|
||
using System.Xml;
|
||
using System.Xml.Linq;
|
||
using UnityEditor;
|
||
using UnityEngine;
|
||
using UnityEngine.Networking;
|
||
using UnityEngine.Rendering;
|
||
using UnityEngine.UI;
|
||
using static MapManager;
|
||
|
||
public partial class MapManager : MonoBehaviour
|
||
{
|
||
public bool LoadObsXml()
|
||
{
|
||
if (_curOpenMapId < 0)
|
||
{
|
||
UIWindow.Instance.ShowMessage("请先打开地图");
|
||
return false;
|
||
}
|
||
|
||
string path = string.Empty;
|
||
|
||
path = PathUtil.GetXmlPath(_curOpenMapId, "Obs");
|
||
|
||
if (!File.Exists(path))
|
||
{
|
||
UIWindow.Instance.ShowMessage("没有找到网格文件, 将重新计算格子");
|
||
return false;
|
||
}
|
||
|
||
XmlDocument xmlDocument = new XmlDocument();
|
||
xmlDocument.Load(path);
|
||
|
||
XmlNode xmlRoot = xmlDocument.SelectSingleNode("Item");
|
||
|
||
string ID = xmlRoot.Attributes.GetNamedItem("ID").Value;
|
||
mapWidth = Convert.ToInt32(xmlRoot.Attributes.GetNamedItem("MapWidth").Value);
|
||
mapHeight = Convert.ToInt32(xmlRoot.Attributes.GetNamedItem("MapHeight").Value);
|
||
|
||
int cellCount = 0;
|
||
|
||
_cellWidth = Convert.ToInt32(xmlRoot.Attributes.GetNamedItem("CellWidth").Value);
|
||
_cellHeight = Convert.ToInt32(xmlRoot.Attributes.GetNamedItem("CellHeight").Value);
|
||
_cellRows = mapHeight / _cellHeight;
|
||
_cellCols = mapWidth / _cellWidth;
|
||
cellCount = _cellRows * _cellCols;
|
||
|
||
|
||
string strData = xmlRoot.Attributes.GetNamedItem("Value").Value;
|
||
|
||
//0,0,1,0;0,1,1,1;0,2,1,2;
|
||
string[] values = strData.Split(';');
|
||
if (values.Length != cellCount)
|
||
{
|
||
UIWindow.Instance.ShowMessage("网格数据错误");
|
||
return false;
|
||
}
|
||
|
||
cellsNode = new CellNode[cellCount];
|
||
for (int i = 0; i < cellCount; i++)
|
||
{
|
||
string[] strCell = values[i].Split(',');
|
||
|
||
if (strCell.Length != 3)
|
||
continue;
|
||
|
||
int y = Convert.ToInt32(strCell[0]);
|
||
int x = Convert.ToInt32(strCell[1]);
|
||
int type = Convert.ToInt32(strCell[2]);
|
||
|
||
//有些格子之前有点问题
|
||
CellType celltype = (CellType)type;
|
||
if ((celltype & CellType.HadRole) > 0)
|
||
celltype ^= CellType.HadRole;
|
||
|
||
cellsNode[i] = new CellNode(x, y, i, celltype);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
public void LoadMapSprite()
|
||
{
|
||
//map父对象;
|
||
GameObject map = new GameObject("map");
|
||
int mapRownum = 11;
|
||
int mapColumn = 14;
|
||
//平铺;
|
||
float jpgscenew = 512f / 100;
|
||
|
||
for (int row = 0; row < mapRownum; row++)
|
||
{
|
||
for (int col = 0; col < mapColumn; col++)
|
||
{
|
||
// 构造文件名,v1000_r1_c1.jpg
|
||
string filename = $"v{1000}_r{row + 1}_c{col + 1}";
|
||
string spPath = PathUtil.GetMapTexure(1000, filename, "jpg");
|
||
GameObject obj = new GameObject(filename);
|
||
obj.transform.SetParent(map.transform);
|
||
SpriteRenderer sr = obj.AddComponent<SpriteRenderer>();
|
||
float x = col * jpgscenew;
|
||
float y = (mapRownum - row - 1) * jpgscenew;
|
||
obj.transform.position = new Vector2(x, y);
|
||
StartCoroutine(LoadSpriteT(spPath, obj,sr));
|
||
// 直接使用原始纹理创建 Sprite
|
||
//sr.sprite = Sprite.Create(asset, new Rect(0, 0, asset.width, asset.height), Vector2.zero, 100f);
|
||
//float x = col * jpgscenew;
|
||
//float y = (mapRownum - row - 1) * jpgscenew;
|
||
//obj.transform.position = new Vector2(x, y);
|
||
//Debug.Log($"Tile {filename} placed at ({x}, {y})");
|
||
}
|
||
}
|
||
MapManager.Instance.ReseCamera(jpgscenew * mapColumn, jpgscenew * mapRownum);
|
||
MapManager.Instance.GenerateMap(jpgscenew * mapColumn, jpgscenew * mapRownum, 0.48f, 0.32f);
|
||
}
|
||
IEnumerator LoadSpriteT(string spName, GameObject go, SpriteRenderer sprite)
|
||
{
|
||
using (UnityWebRequest req = UnityWebRequestTexture.GetTexture(spName))
|
||
{
|
||
yield return req.SendWebRequest();
|
||
if (req.result == UnityWebRequest.Result.Success)
|
||
{
|
||
Texture2D tt = DownloadHandlerTexture.GetContent(req);
|
||
if (tt == null)
|
||
yield break;
|
||
sprite.sprite = Sprite.Create(tt, new Rect(0, 0, tt.width, tt.height), Vector2.zero, 100f);
|
||
}
|
||
else
|
||
{
|
||
UIWindow.Instance.ShowMessage(req.error);
|
||
}
|
||
}
|
||
}
|
||
}
|