internal void UpdateNPCCell(NPC npc, Vec3f pos) { if (npc == null) { throw new ArgumentNullException("NPC is null!"); } Vec2i coords = NPCCell.GetCoords(pos); if (coords.X != npc.NpcCell.X || coords.Y != npc.NpcCell.Y) { npc.NpcCell.RemoveNPC(npc); CheckCellRemove(npc.NpcCell); int coord = NPCCell.GetCoordinate(coords.X, coords.Y); NPCCell cell; if (!npcCells.TryGetValue(coord, out cell)) { cell = new NPCCell(this, coords.X, coords.Y); npcCells.Add(coord, cell); } cell.AddNPC(npc); npc.NpcCell = cell; } }
internal void AddToNPCCells(NPC npc) { // find the cell for this npc Vec2i coords = NPCCell.GetCoords(npc.Position); int coord = NPCCell.GetCoordinate(coords.X, coords.Y); NPCCell cell; if (!npcCells.TryGetValue(coord, out cell)) { cell = new NPCCell(this, coords.X, coords.Y); npcCells.Add(coord, cell); } cell.AddNPC(npc); npc.NpcCell = cell; }
/// <summary> /// 1000 ingame units accuracy /// </summary> public void ForEachNPCRoughPredicate(Vec3f pos, float radius, Predicate <NPC> predicate) { if (predicate == null) { throw new ArgumentNullException("Predicate is null!"); } Vec2i min = NPCCell.GetCoords(new Vec3f(pos.X - radius, pos.Y, pos.Z - radius)); Vec2i max = NPCCell.GetCoords(new Vec3f(pos.X + radius, pos.Y, pos.Z + radius)); for (int x = min.X; x <= max.X; x++) { for (int y = min.Y; y <= max.Y; y++) { if (npcCells.TryGetValue(NPCCell.GetCoordinate(x, y), out NPCCell cell)) { cell.ForEachNPCPredicate(predicate); } } } }
/// <summary> /// 1000 ingame units accuracy /// </summary> public void ForEachNPCRough(Vec3f pos, float radius, Action <NPC> action) { if (action == null) { throw new ArgumentNullException("Action is null!"); } Vec2i min = NPCCell.GetCoords(new Vec3f(pos.X - radius, pos.Y, pos.Z - radius)); Vec2i max = NPCCell.GetCoords(new Vec3f(pos.X + radius, pos.Y, pos.Z + radius)); for (int x = min.X; x <= max.X; x++) { for (int y = min.Y; y <= max.Y; y++) { NPCCell cell; if (npcCells.TryGetValue(NPCCell.GetCoordinate(x, y), out cell)) { cell.ForEachNPC(action); } } } }