138 lines
4.0 KiB
C#
138 lines
4.0 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()
|
|
{
|
|
TileManager.Instance.Init();
|
|
var mapSize = 10;
|
|
float noiseScale = 0.1f;
|
|
|
|
// 设置偏移量
|
|
offsetX = -5; // 示例:地图数据从 (-5, -5) 开始
|
|
offsetY = -5;
|
|
|
|
for (int x = 0; x < mapSize; x++)
|
|
{
|
|
List<int> col = new();
|
|
for (int y = 0; y < mapSize; y++)
|
|
{
|
|
// 计算柏林噪声值
|
|
float noiseValue = Mathf.PerlinNoise((x + offsetX) * noiseScale, (y + offsetY) * noiseScale);
|
|
if (noiseValue < 0.5f) // 小于 0.5 表示 Dirt
|
|
{
|
|
col.Add(TileManager.Instance.tileID.GetValueOrDefault("Dirt"));
|
|
}
|
|
else // 大于等于 0.5 表示 Grass
|
|
{
|
|
col.Add(TileManager.Instance.tileID.GetValueOrDefault("Grass"));
|
|
}
|
|
}
|
|
|
|
mapData.Add(col);
|
|
}
|
|
|
|
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)]);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|