NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Map/MapData.cs

208 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using Newtonsoft.Json;
using UnityEngine.Serialization;
[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("高原")]
Plateau
}
[Serializable]
public class Region
{
public List<int> positions = new ();
}
[ReadOnly]
public int width;
[ReadOnly]
public int height;
public EnvironmentType environmentType;
[HideInInspector]
public List<MapBlockData> BlocksData = new();
[HideInInspector]
public List<Region> regions = new();
[HideInInspector]
public Vector2Int initialCameraPosition;
public MapData()
{
}
public MapData(int id,string name, int width, int height, EnvironmentType envType = EnvironmentType.Plain)
{
this.width = width;
this.height = height;
environmentType = envType;
ResetBlocks();
}
public void ResetBlocks()
{
var count = width * height;
BlocksData.Clear();
for (var i = 0; i < count; i++)
{
BlocksData.Add(new MapBlockData());
}
}
public void SetBlockData(Vector2Int position, MapBlockData blockData)
{
if (CheckPosition(position))
{
BlocksData[position.x * width + position.y] = blockData;
}
}
public MapBlockData GetBlockData(int cellIndex)
{
if (cellIndex < 0 || cellIndex >= BlocksData.Count)
{
DebugUtil.LogError("GetBlockData failed, position:{0} is invalid!", cellIndex);
return null;
}
return BlocksData[cellIndex];
}
public bool CheckPosition(Vector2Int position)
{
return position.x >= 0 && position.y >= 0;
}
//为了和TGS中坐标定义一致格子的坐标采用X朝上Y朝右
public int Position2BlockIndex(Vector2Int position)
{
return position.x * width + position.y;
}
//为了和TGS中坐标定义一致格子的坐标采用X朝上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;
}
}
// public void AddRegion(Region area, int newRegionId = -1)
// {
//
// if (!CheckRegion(area))
// {
// DebugUtil.LogError("{0} AddPreparingArea failed, area:{1} is invalid!", GetType().Name, area);
// }
// var regionId = GenerateRegionId();
// if (_regions.ContainsKey(newRegionId) && newRegionId >= 0)
// {
// _regions[newRegionId] = area;
// }
// else
// {
// _regions.Add(regionId, area);
// }
//
// }
public Region GetRegion(int regionId)
{
if (IsRegionExist(regionId))
return regions[regionId];
return null;
// if (_regions.TryGetValue(regionId, out var region))
// {
// return region;
// }
// else
// {
// DebugUtil.LogError("{0} GetPreparingArea failed, id:{1} not exists!", GetType().Name, regionId);
// return null;
// }
}
// public List<Region> GetAllRegions()
// {
// return _regions;
// }
public void RemoveRegion(int regionId)
{
if (!IsRegionExist(regionId))
regions.RemoveAt(regionId);
// if (_regions.ContainsKey(regionId))
// {
// _regions.Remove(regionId);
// }
// else
// {
// DebugUtil.LogError("{0} RemovePreparingArea failed, id:{1} not exists!",GetType().Name, regionId);
// }
}
public void RemoveAllRegion()
{
regions.Clear();
}
// private int GenerateRegionId()
// {
// var max = _regions.Count;
// for (var i = 0; i < max; i++)
// {
// if (!_regions.ContainsKey(i))
// {
// return i;
// }
// }
// return max;
// }
// private bool CheckRegion(Region area)
// {
// for (var i = 0; i < area.positions.Count; i++)
// {
// if (!CheckPosition(area.positions[i]))
// {
// return false;
// }
// }
// return true;
// }
public bool IsRegionExist(int regionIndex)
{
return regionIndex >= 0 && regionIndex < regions.Count;
}
}