에러
ArgumentException: An item with the same key has already been added. Key: 0
System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <dc753a1061284f8e971ee88ee4826eee>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <dc753a1061284f8e971ee88ee4826eee>:0)
Fy.World.Map.SetRegions () (at Assets/Scripts/Map/Map.cs:70)
Fy.World.Map..ctor (System.Int32 width, System.Int32 height) (at Assets/Scripts/Map/Map.cs:44)
Fy.GameManager.Start () (at Assets/Scripts/Game/GameManager.cs:28)
map 스크립트
using System.Collections.Generic;
using UnityEngine;
using Fy.Definitions;
using Fy.Entity;
using Fy.Helpers;
namespace Fy.World
{
public class Map
{
public const int REGION_SIZE = 5;
//Size
public Vector2Int size { get; private set; }
//Rect
public RectI mapRect;
//public access to region
public MapRegion[] regions { get { return this._regions; } }
//Tiles
private Tile[] _tiles; // indice = x+y etc
//Region our map
private MapRegion[] _regions;
//Region foreach position
private Dictionary<int, int> _regionByPosition;
public Map(int width, int height)
{
this.size = new Vector2Int(width, height);
this._tiles = new Tile[width * height];
this.mapRect = new RectI(new Vector2Int(0, 0), width, height);
foreach (Vector2Int v in this.mapRect)
{
this._tiles[v.x + v.y * this.size.x] = new Tile(v, this);
}
this.SetRegions();
}
//SetRegions
public void SetRegions()
{
int _regionLength = (
Mathf.CeilToInt(this.size.x / Map.REGION_SIZE) *
Mathf.CeilToInt(this.size.y / Map.REGION_SIZE)
);
this._regions = new MapRegion[_regionLength];
this._regionByPosition = new Dictionary<int, int>();
int i = 0;
for (int x = 0; x < this.size.x; x += Map.REGION_SIZE)
{
for (int y = 0; y < this.size.y; y += Map.REGION_SIZE)
{
RectI sectionRect = new RectI(
new Vector2Int(x, y), Map.REGION_SIZE, Map.REGION_SIZE
);
sectionRect.Clip(this.mapRect);
this._regions[i] = new MapRegion(i, sectionRect, this);
foreach (Vector2Int v in sectionRect)
{
this._regionByPosition.Add(v.x + v.y * this.size.x, i); //여기 확인해보면 됨 에러
}
i++;
}
}
}
public void TempEverythingIsDirt()
{
foreach (Tile tile in this)
{
tile.AddTilable(
new Ground(tile.position, Defs.grounds["dirt"])
);
}
}
/*public void TempEverythingIsDirt()
{
foreach (Tile tile in this)
{
if (
tile.position.x == 0 || tile.position.y == 0 ||
tile.position.x == this.size.x - 1 || tile.position.y == this.size.y - 1
)
{
tile.AddTilable(
new Ground(tile.position, Defs.grounds["durt"])
);
}
}
}*/
// map[x,y] get tiltle at x,y
public Tile this[int x, int y]
{
get
{
if (x >= 0 && y >= 0 && x < this.size.x && y < this.size.y)
{
return this._tiles[x + y * this.size.x];
}
return null;
}
}
//map[Vector2Int.zero] get tile at 0,0
public Tile this[Vector2Int v]
{
get
{
return this[v.x, v.y];
}
}
// foreach (Tile t in map) {}
public IEnumerator<Tile> GetEnumerator()
{
foreach (Vector2Int v in this.mapRect)
{
yield return this[v];
}
}
public override string ToString()
{
return "Map(size=" + this.size + ")";
}
}
}
gamemanager 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Fy.Definitions;
using Fy.World;
using Fy.Entity;
using Fy.Helpers;
namespace Fy
{
public class GameManager : MonoBehaviour
{
public Map map;
public bool DrawGizmosTiles = false;
public bool DrawGizmosRegions = false;
private bool _ready;
private void Awake()
{
this._ready = false;
Defs.LoadGroundFromCode(); // Loading our ground definitions;
}
private void Start()
{
this.map = new Map(100, 100);
this.map.TempEverythingIsDirt();
Debug.Log(this.map);
this._ready = true;
}
private void OnDrawGizmos()
{
if (this._ready)
{
if (this.DrawGizmosTiles)
{
foreach (Tile t in this.map)
{
Ground g = (Ground)t.GetTilable(Layer.Ground);
if (g != null)
{
Gizmos.DrawWireCube(
new Vector3(g.position.x + .5f, g.position.y + .5f),
Vector3.one
);
}
}
}
if (this.DrawGizmosRegions)
{
foreach (MapRegion region in this.map.regions)
{
Gizmos.color = new Color(0, 0, 1, .5f);
Gizmos.DrawCube(
new Vector3(
region.regionRect.max.x - (region.regionRect.width / 2f),
region.regionRect.max.y - (region.regionRect.height / 2f)
),
new Vector3(region.regionRect.width - .5f, region.regionRect.height - .5f, 1f)
);
}
}
}
}
}
}
gamemanger start에 newmap(5,5)는 잘 됨 근데 그 이상의 숫자(6,6) or (100,100)를 입력하면 저 에러가뜸. 왜이러는거임?
댓글 0