(client) feat:实现摄像机跟踪与移动,实现任意位置生成实体,实现更安全的资源加载方式(指定unity内部加载资源) (#42)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Data;
|
||||
using Managers;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
@ -9,36 +10,18 @@ namespace Map
|
||||
public class DoubleMap : MonoBehaviour
|
||||
{
|
||||
public List<List<int>> mapData = new();
|
||||
// public Tilemap dataLevel;
|
||||
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;
|
||||
|
||||
for (int x = 0; x < mapSize; x++)
|
||||
{
|
||||
List<int> col = new();
|
||||
for (int y = 0; y < mapSize; y++)
|
||||
{
|
||||
// 计算柏林噪声值
|
||||
float noiseValue = Mathf.PerlinNoise(x * noiseScale, y * 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();
|
||||
}
|
||||
|
||||
UpdateTexture();
|
||||
}
|
||||
|
||||
public void UpdateTexture()
|
||||
@ -54,33 +37,46 @@ namespace Map
|
||||
|
||||
public int GetTile(int x, int y)
|
||||
{
|
||||
if (x < 0 || x >= mapData.Count)
|
||||
// 转换为相对于 mapData 的索引
|
||||
int relativeX = x - offsetX;
|
||||
int relativeY = y - offsetY;
|
||||
|
||||
if (relativeX < 0 || relativeX >= mapData.Count)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var col = mapData[x];
|
||||
if (y < 0 || y >= mapData.Count)
|
||||
var col = mapData[relativeX];
|
||||
if (relativeY < 0 || relativeY >= mapData.Count)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return col[y];
|
||||
return col[relativeY];
|
||||
}
|
||||
|
||||
public void SetTile(int x, int y, string tileName)
|
||||
{
|
||||
SetTile(x,y,TileManager.Instance.tileID.GetValueOrDefault(tileName));
|
||||
SetTile(x, y, TileManager.Instance.tileID.GetValueOrDefault(tileName));
|
||||
}
|
||||
|
||||
public void SetTile(int x, int y, int id)
|
||||
{
|
||||
mapData[x][y] = id;
|
||||
UpdateTexture(x, y);
|
||||
UpdateTexture(x, y-1);
|
||||
UpdateTexture(x-1, y);
|
||||
UpdateTexture(x-1, y-1);
|
||||
// 转换为相对于 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>
|
||||
@ -89,94 +85,27 @@ namespace Map
|
||||
/// <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(x,y),TileManager.Instance.tileToTileBaseMapping[(lt, rt, lb, rb)]);
|
||||
textureLevel.SetTile(new Vector3Int(x, y, 0),
|
||||
TileManager.Instance.tileToTileBaseMapping[(lt, rt, lb, rb)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class TileManager:Utils.Singleton<TileManager>
|
||||
{
|
||||
public Dictionary<string,TileBase> tileBaseMapping = new();
|
||||
public Dictionary<(int, int, int, int), TileBase> tileToTileBaseMapping = new();
|
||||
public Dictionary<string, int> tileID = new();
|
||||
public void Init()
|
||||
{
|
||||
if (tileToTileBaseMapping.Count > 0)
|
||||
return;
|
||||
Managers.PackagesImageManager.Instance.Init();
|
||||
var imagePack = Managers.PackagesImageManager.Instance;
|
||||
var tileType = Managers.DefineManager.Instance.QueryDefinesByType<TileDef>();
|
||||
for (var i = 0; i < tileType.Length; i++)
|
||||
{
|
||||
tileID.Add(tileType[i].name, i);
|
||||
}
|
||||
|
||||
var tileTextureMappingDef=Managers.DefineManager.Instance.QueryDefinesByType<TileMappingTableDef>();
|
||||
foreach (var mappingTableDef in tileTextureMappingDef)
|
||||
{
|
||||
foreach (var keyVal in mappingTableDef.tileDict)
|
||||
{
|
||||
var key = keyVal.Key;
|
||||
var val = keyVal.Value;
|
||||
var parts = key.Split('_');
|
||||
if (parts.Length != 4)
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{key}内容不合法!\n应该为[瓦片名称_瓦片名称_瓦片名称_瓦片名称]的格式");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(tileID.TryGetValue(parts[0], out var k1) &&
|
||||
tileID.TryGetValue(parts[1], out var k2) &&
|
||||
tileID.TryGetValue(parts[2], out var k3) &&
|
||||
tileID.TryGetValue(parts[3], out var k4)))
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{key}中存在未定义的瓦片名称");
|
||||
continue;
|
||||
}
|
||||
|
||||
var sprite = imagePack.GetSprite(mappingTableDef.packID,val);
|
||||
if (sprite == null)
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{val}中存在未定义的图片名称");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tileToTileBaseMapping.ContainsKey((k1, k2, k3, k4)))
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
Debug.LogWarning($"来自{packName}定义的TileMappingTableDef键值{(k1, k2, k3, k4)}存在重复索引,将忽略重复项");
|
||||
continue;
|
||||
}
|
||||
|
||||
var tile = LoadTile(sprite);
|
||||
tileToTileBaseMapping[(k1, k2, k3, k4)] = tile;
|
||||
tileBaseMapping[val] = tile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Reload()
|
||||
{
|
||||
tileToTileBaseMapping.Clear();
|
||||
Init();
|
||||
}
|
||||
|
||||
public TileBase LoadTile(Sprite sprite)
|
||||
{
|
||||
var newTile = ScriptableObject.CreateInstance<Tile>();
|
||||
newTile.sprite = sprite;
|
||||
newTile.color = Color.white;
|
||||
newTile.colliderType = Tile.ColliderType.Sprite;
|
||||
return newTile;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
30
Client/Assets/Scripts/Map/MapGenerator.cs
Normal file
30
Client/Assets/Scripts/Map/MapGenerator.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
namespace Map
|
||||
{
|
||||
public class MapGenerator:MonoBehaviour
|
||||
{
|
||||
public DoubleMap baseLevel;
|
||||
public Tilemap buildLevel;
|
||||
public Tilemap plantLevel;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
var perline= Utils.PerlinNoise.Instance;
|
||||
int size = 100;
|
||||
for (int i = -size; i <= size; i++)
|
||||
{
|
||||
for (int j = -size; j <= size; j++)
|
||||
{
|
||||
var val = perline.Noise(i, j);
|
||||
if (val < 0)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Map/MapGenerator.cs.meta
Normal file
3
Client/Assets/Scripts/Map/MapGenerator.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 81339b242c794b8d9d28d7111a46bfbf
|
||||
timeCreated: 1754370253
|
Reference in New Issue
Block a user