2023-08-22 19:08:45 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class MapData
|
|
|
|
|
{
|
|
|
|
|
public enum EnvironmentType
|
|
|
|
|
{
|
|
|
|
|
[LabelText("平原")]
|
|
|
|
|
Plain = 0,
|
|
|
|
|
[LabelText("山地")]
|
|
|
|
|
Mountain,
|
|
|
|
|
[LabelText("草原")]
|
|
|
|
|
Grassland,
|
|
|
|
|
[LabelText("林地")]
|
|
|
|
|
Forest,
|
|
|
|
|
[LabelText("雪地")]
|
|
|
|
|
Snow,
|
|
|
|
|
[LabelText("泥地")]
|
|
|
|
|
MuddyLand,
|
|
|
|
|
[LabelText("戈壁")]
|
|
|
|
|
Gobi,
|
|
|
|
|
[LabelText("沙漠")]
|
|
|
|
|
Desert,
|
|
|
|
|
[LabelText("高原")]
|
2023-10-19 15:00:19 +08:00
|
|
|
Plateau
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
public class Region
|
|
|
|
|
{
|
|
|
|
|
public List<Vector2Int> positions = new List<Vector2Int>(16);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ReadOnly]
|
|
|
|
|
public int id;
|
|
|
|
|
public string name;
|
|
|
|
|
[MinValue(1)]
|
|
|
|
|
public int width;
|
|
|
|
|
[MinValue(1)]
|
|
|
|
|
public int height;
|
|
|
|
|
public EnvironmentType environmentType;
|
|
|
|
|
public List<MapBlockData> BlocksData = null;
|
|
|
|
|
[JsonProperty]
|
|
|
|
|
private Dictionary<int, Region> _regions = new Dictionary<int, Region>(8);
|
|
|
|
|
|
|
|
|
|
[HideInInspector]
|
|
|
|
|
public Vector2Int initialCameraPosition;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public MapData(int id, string name, int width, int height, EnvironmentType envType = EnvironmentType.Plain)
|
|
|
|
|
{
|
|
|
|
|
this.id = id;
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.width = width;
|
|
|
|
|
this.height = height;
|
2023-10-19 15:00:19 +08:00
|
|
|
environmentType = envType;
|
2023-08-22 19:08:45 +08:00
|
|
|
ResetBlocks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetBlocks()
|
|
|
|
|
{
|
|
|
|
|
var count = width * height;
|
|
|
|
|
BlocksData = new List<MapBlockData>(count);
|
2023-10-19 15:00:19 +08:00
|
|
|
for (var i = 0; i < count; i++)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
|
|
|
|
BlocksData.Add(new MapBlockData());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetBlockData(Vector2Int position, MapBlockData blockData)
|
|
|
|
|
{
|
|
|
|
|
if (CheckPosition(position))
|
|
|
|
|
{
|
|
|
|
|
BlocksData[position.x * width + position.y] = blockData;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MapBlockData GetBlockData(Vector2Int position)
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
return CheckPosition(position) ? BlocksData[Position2BlockIndex(position)] : null;
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CheckPosition(Vector2Int position)
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
return position.x >= 0 && position.y >= 0;
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Position2BlockIndex(Vector2Int position)
|
|
|
|
|
{
|
|
|
|
|
return position.x * width + position.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector2Int BlockIndex2Position(int blockIndex)
|
|
|
|
|
{
|
|
|
|
|
return new Vector2Int(blockIndex / width, blockIndex % width);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetBlockTerrainType(int blockIndex, int terrainTypeIndex)
|
|
|
|
|
{
|
|
|
|
|
if (blockIndex >= 0 && blockIndex < BlocksData.Count)
|
|
|
|
|
{
|
|
|
|
|
BlocksData[blockIndex].TerrainTypeIndex = terrainTypeIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
public void AddRegion(Region area, int newRegionId = -1)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (!CheckRegion(area))
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
DebugUtil.LogError("{0} AddPreparingArea failed, area:{1} is invalid!", GetType().Name, area);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
2023-10-19 15:00:19 +08:00
|
|
|
var regionId = GenerateRegionId();
|
|
|
|
|
if (_regions.ContainsKey(newRegionId) && newRegionId >= 0)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
_regions[newRegionId] = area;
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
_regions.Add(regionId, area);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
public Region GetRegion(int regionId)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
if (_regions.TryGetValue(regionId, out var region))
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
return region;
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
DebugUtil.LogError("{0} GetPreparingArea failed, id:{1} not exists!", GetType().Name, regionId);
|
2023-08-22 19:08:45 +08:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Dictionary<int, Region> GetAllRegions()
|
|
|
|
|
{
|
|
|
|
|
return _regions;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
public void RemoveRegion(int regionId)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
if (_regions.ContainsKey(regionId))
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
_regions.Remove(regionId);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
DebugUtil.LogError("{0} RemovePreparingArea failed, id:{1} not exists!",GetType().Name, regionId);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveAllRegion()
|
|
|
|
|
{
|
|
|
|
|
_regions.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GenerateRegionId()
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
var max = _regions.Count;
|
|
|
|
|
for (var i = 0; i < max; i++)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
|
|
|
|
if (!_regions.ContainsKey(i))
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return max;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CheckRegion(Region area)
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
for (var i = 0; i < area.positions.Count; i++)
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
|
|
|
|
if (!CheckPosition(area.positions[i]))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|