/// <summary> /// Inserts an entity at a spot on the terrain. Used by Add and Move. /// </summary> /// <param name="toInsert">The entity to insert</param> /// <param name="location">The location to insert at</param> /// <param name="removeOldInstance">True if Insert should delete an existing occurrence of toInsert in the terrain</param> void Insert(SingleEntity toInsert, Vector location, bool removeOldInstance) { try { if (location.X < 0 || location.Y < 0 || location.X > Size.X || location.Y > Size.Y) { return; } TerrainSpot oldSpot; TerrainSpot newSpot; if (Field.TryGetValue(location, out newSpot)) { bool allowed = true; foreach (var o in newSpot.Occupants) { var callee = o.CollideWith(new CollisionEventArgs(toInsert, location)); var caller = toInsert.CollideWith(new CollisionEventArgs(o, location)); allowed = !callee && !caller; if (!allowed) { break; } } if (allowed) { if (removeOldInstance) { Field.TryGetValue(toInsert.Position, out oldSpot); oldSpot.Occupants.Remove(toInsert); if (oldSpot.Occupants.Count == 0) { Field.Remove(toInsert.Position); } } toInsert.Position = location; newSpot.Occupants.Add(toInsert); } } else { if (removeOldInstance) { Field.TryGetValue(toInsert.Position, out oldSpot); oldSpot.Occupants.Remove(toInsert); if (oldSpot.Occupants.Count == 0) { Field.Remove(toInsert.Position); } } toInsert.Position = location; Field.Add(location, new TerrainSpot(toInsert)); } toInsert.ParentScene = ParentScene; } catch (Exception) { } }
public TerrainSpot(SingleEntity first) { Occupants = new List <SingleEntity>(); Occupants.Add(first); }