/// <summary> /// Adds the given unit to this map. /// Returns its new ID. /// </summary> public ulong AddUnit(Unit u) { u.Pos.OnChanged += Callback_UnitMoved; if (!u.IsIDRegistered) { u.RegisterID(nextID); nextID += 1; } //Add the unit's info to various cached data structures. if (!unitsByType.ContainsKey(u.MyType)) { unitsByType.Add(u.MyType, new HashSet <Unit>()); } unitsByType[u.MyType].Add(u); if (!unitsByPos.ContainsAt(u.Pos, u)) { unitsByPos.AddValue(u, u.Pos); } idToUnit.Add(u.ID, u); units.Add(u); u.MyGroup.UnitsByID.Add(u.ID); if (OnUnitAdded != null) { OnUnitAdded(this, u); } return(u.ID); }
private void CommonInit() { Groups = new Group.GroupSet(this); IsPaused = new Stat <bool, Map>(this, false); pathingGraph = new Graph(this); pathing = new Pathfinding.PathFinder <Vector2i>(pathingGraph, null); unitsByPos = new GridSet <Unit>(Tiles.Dimensions); //When the tile grid changes size, reset "unitsByPos". Tiles.OnTileGridReset += (grid, oldSize, newSize) => { unitsByPos = new GridSet <Unit>(newSize); foreach (Unit u in units) { unitsByPos.AddValue(u, u.Pos); } }; }