} //set{this[new Coord(x,z)]=value;} } public T this[Coord c] { get { int i = c.ToInt(); if (grid.ContainsKey(i)) { return(grid[i]); } else { return(null); } } //set { } }
public void Nail(Coord coord) { int hash = coord.ToInt(); //looking if already nailed if (nailedHashes.Contains(hash)) { return; } //finding object in grid T obj = null; if (grid.ContainsKey(hash)) { obj = grid[hash]; } //creating object if it was not found bool isObjNew = obj == null; if (isObjNew) { obj = Construct(); } //saving obj to serialized nailed nailedHashes.Add(hash); if (!grid.ContainsKey(hash)) { grid.Add(coord.ToInt(), obj); } //calling onCreate if obj was just created if (isObjNew) { OnCreate(obj, coord); } }
public bool IsNailed(Coord coord) { int hash = coord.ToInt(); if (nailedHashes.Contains(hash)) { return(true); } else { return(false); } }
public void Unnail(Coord coord, bool remove = true) { int hash = coord.ToInt(); if (!nailedHashes.Contains(hash)) { return; //looking if it was ever nailed } nailedHashes.Remove(hash); if (remove && grid.ContainsKey(hash)) { T obj = grid[hash]; if (obj != null) { OnRemove(obj); } grid.Remove(hash); } }
public virtual void Deploy(CoordRect[] rects, Coord[] centers, bool allowMove = true) { Dictionary <int, T> newGrid = new Dictionary <int, T>(); //adding nailed objs foreach (int hash in nailedHashes) { if (!grid.ContainsKey(hash)) { Debug.Log("Could not find nailed object"); } T obj = grid[hash]; if (obj != null) { newGrid.Add(hash, obj); } grid.Remove(hash); } //adding objects within rect for (int r = 0; r < centers.Length; r++) { CoordRect rect = rects[r]; Coord min = rect.Min; Coord max = rect.Max; for (int x = min.x; x < max.x; x++) { for (int z = min.z; z < max.z; z++) { Coord coord = new Coord(x, z); int hash = coord.ToInt(); if (grid.ContainsKey(hash)) { T obj = grid[hash]; if (obj != null) { newGrid.Add(hash, obj); } grid.Remove(hash); } } } } //creating an unused stack - sorted by distance Stack <T> unused = new Stack <T>(); Stack <int> unusedHashes = new Stack <int>(); if (grid.Count != 0) { foreach (Coord c in Coord.MultiDistanceArea(centers, 20000000)) { int hash = c.ToInt(); if (grid.ContainsKey(hash)) { if (grid[hash] != null) { unused.Push(grid[hash]); unusedHashes.Push(hash); } grid.Remove(hash); if (grid.Count == 0) { break; } } } } //filling empty areas with unused (or new) objects for (int r = 0; r < centers.Length; r++) { CoordRect rect = rects[r]; Coord center = centers[r]; foreach (Coord c in center.DistanceArea(rect)) { int hash = c.ToInt(); if (newGrid.ContainsKey(hash)) { continue; } T obj = null; if (unused.Count != 0 && allowMove) { obj = unused.Pop(); unusedHashes.Pop(); //popping the furtherest unused OnMove(obj, c); } else { obj = Construct(); OnCreate(obj, c); } newGrid.Add(hash, obj); } } //adding other unused to trail or removing them while (unused.Count > 0) { T obj = unused.Pop(); int hash = unusedHashes.Pop(); //if (!allowMove) OnRemove(obj); //else newGrid.Add(hash, obj); } //removing all other objs left //foreach(KeyValuePair<int,T> kvp in grid) OnRemove(kvp.Value); grid = newGrid; }
public virtual void Deploy(CoordRect rect, Coord center, bool allowMove = true) { Dictionary <int, T> newGrid = new Dictionary <int, T>(); //adding nailed objs foreach (int hash in nailedHashes) { if (!grid.ContainsKey(hash)) { Debug.Log("Could not find nailed object"); } T obj = grid[hash]; if (obj != null) { newGrid.Add(hash, obj); } grid.Remove(hash); } //adding objects within stock rect Coord min = rect.Min - stockMargin; Coord max = rect.Max + stockMargin; for (int x = min.x; x < max.x; x++) { for (int z = min.z; z < max.z; z++) { Coord coord = new Coord(x, z); int hash = coord.ToInt(); if (grid.ContainsKey(hash)) { T obj = grid[hash]; if (obj != null) { newGrid.Add(hash, obj); } grid.Remove(hash); } } } //filling empty areas with unused (or new) objects foreach (Coord c in center.DistanceArea(rect)) { int hash = c.ToInt(); if (newGrid.ContainsKey(hash)) { continue; } T obj = null; if (grid.Count != 0 && allowMove) { obj = grid.First().Value; OnMove(obj, c); grid.Remove(grid.First().Key); } else { obj = Construct(); OnCreate(obj, c); } newGrid.Add(hash, obj); } //removing all other objs left foreach (KeyValuePair <int, T> kvp in grid) { OnRemove(kvp.Value); } grid = newGrid; }
public virtual void Deploy(CoordRect[] createRects, CoordRect[] removeRects, Coord[] centers, bool allowMove = true) { Dictionary <int, T> newGrid = new Dictionary <int, T>(); //adding nailed objs foreach (int hash in nailedHashes) { if (!grid.ContainsKey(hash)) { Debug.Log("Could not find nailed object"); } T obj = grid[hash]; if (obj != null && !obj.Equals(null)) { newGrid.Add(hash, obj); //obj!=null will return true for deleted unity object } grid.Remove(hash); } //adding objects within remove rect for (int r = 0; r < centers.Length; r++) { CoordRect rect = removeRects[r]; Coord min = rect.Min; Coord max = rect.Max; for (int x = min.x; x < max.x; x++) { for (int z = min.z; z < max.z; z++) { Coord coord = new Coord(x, z); int hash = coord.ToInt(); if (grid.ContainsKey(hash)) { T obj = grid[hash]; if (obj != null && !obj.Equals(null)) { newGrid.Add(hash, obj); } grid.Remove(hash); } } } } //filling create rect empty areas with unused (or new) objects for (int r = 0; r < centers.Length; r++) { CoordRect rect = createRects[r]; Coord center = centers[r]; foreach (Coord c in center.DistanceArea(rect)) { int hash = c.ToInt(); if (newGrid.ContainsKey(hash)) { continue; } //moving T obj = null; if (grid.Count != 0 && allowMove) { KeyValuePair <int, T> first = grid.First(); obj = first.Value; grid.Remove(first.Key); OnMove(obj, c); } //creating else { obj = Construct(); OnCreate(obj, c); } newGrid.Add(hash, obj); } } //removing all other objs left foreach (KeyValuePair <int, T> kvp in grid) { OnRemove(kvp.Value); } grid = newGrid; }