Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
112 lines
3.1 KiB
C#
112 lines
3.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Data;
|
|
using Managers;
|
|
using UnityEngine;
|
|
using UnityEngine.Tilemaps;
|
|
|
|
namespace Map
|
|
{
|
|
public class DoubleMap : MonoBehaviour
|
|
{
|
|
public List<List<int>> mapData = new();
|
|
public Tilemap textureLevel;
|
|
|
|
public Dictionary<string, TileBase> tileDict = new();
|
|
|
|
private int offsetX = 0; // 地图数据的 X 偏移量
|
|
private int offsetY = 0; // 地图数据的 Y 偏移量
|
|
|
|
void Start()
|
|
{
|
|
|
|
|
|
UpdateTexture();
|
|
}
|
|
|
|
public void UpdateTexture()
|
|
{
|
|
for (int i = 0; i < mapData.Count; i++)
|
|
{
|
|
for (int j = 0; j < mapData[i].Count; j++)
|
|
{
|
|
UpdateTexture(i, j);
|
|
}
|
|
}
|
|
}
|
|
|
|
public int GetTile(int x, int y)
|
|
{
|
|
// 转换为相对于 mapData 的索引
|
|
int relativeX = x - offsetX;
|
|
int relativeY = y - offsetY;
|
|
|
|
if (relativeX < 0 || relativeX >= mapData.Count)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
var col = mapData[relativeX];
|
|
if (relativeY < 0 || relativeY >= mapData.Count)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return col[relativeY];
|
|
}
|
|
|
|
public void SetTile(int x, int y, string tileName)
|
|
{
|
|
SetTile(x, y, TileManager.Instance.tileID.GetValueOrDefault(tileName));
|
|
}
|
|
|
|
public void SetTile(int x, int y, int id)
|
|
{
|
|
// 转换为相对于 mapData 的索引
|
|
int relativeX = x - offsetX;
|
|
int relativeY = y - offsetY;
|
|
|
|
if (relativeX >= 0 && relativeX < mapData.Count &&
|
|
relativeY >= 0 && relativeY < mapData[relativeX].Count)
|
|
{
|
|
mapData[relativeX][relativeY] = id;
|
|
UpdateTexture(x, y);
|
|
UpdateTexture(x, y - 1);
|
|
UpdateTexture(x - 1, y);
|
|
UpdateTexture(x - 1, y - 1);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新对应坐标的贴图
|
|
/// </summary>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
/// <returns></returns>
|
|
public void UpdateTexture(int x, int y)
|
|
{
|
|
// 转换为相对于 mapData 的索引
|
|
int relativeX = x - offsetX;
|
|
int relativeY = y - offsetY;
|
|
|
|
if (relativeX < 0 || relativeX >= mapData.Count ||
|
|
relativeY < 0 || relativeY >= mapData[relativeX].Count)
|
|
{
|
|
return; // 如果超出范围,直接返回
|
|
}
|
|
|
|
var lt = GetTile(x, y + 1);
|
|
var rt = GetTile(x + 1, y + 1);
|
|
var lb = GetTile(x, y);
|
|
var rb = GetTile(x + 1, y);
|
|
|
|
if (TileManager.Instance.tileToTileBaseMapping.ContainsKey((lt, rt, lb, rb)))
|
|
{
|
|
textureLevel.SetTile(new Vector3Int(x, y, 0),
|
|
TileManager.Instance.tileToTileBaseMapping[(lt, rt, lb, rb)]);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|