public void ClearEntity(Entity entity) { TraverseMap(MapAction.Remove, entity, entity.X, entity.Y); }
private void TraverseMap(MapAction action, Entity entity, int targetX, int targetY) { CheckBounds(targetX, targetY); CheckBounds(targetX + entity.Width - 1, targetY + entity.Height - 1); List<Entity> collisions = new List<Entity>(); for (var x = targetX; x < targetX + entity.Width; x++) { for (var y = targetY; y < targetY + entity.Height; y++) { switch (action) { case MapAction.Check: var mapEntity = GetEntity(x, y); if (mapEntity != null) { //store all collisions and then decide what to do with them collisions.Add(mapEntity); } break; case MapAction.Add: Rows[y][x] = entity; break; case MapAction.Remove: if (Rows[y][x] == entity) { Rows[y][x] = null; } break; } } } if (collisions.Count > 0) { throw new CollisionException {Entities = collisions, Entity = collisions[0]}; } }
public void AddEntity(Entity entity) { TraverseMap(MapAction.Check, entity, entity.X, entity.Y); TraverseMap(MapAction.Add, entity, entity.X, entity.Y); UpdateManager.AddEntity(entity); }
public void MoveEntity(Entity entity, int x, int y) { TraverseMap(MapAction.Remove, entity, entity.X, entity.Y); try { TraverseMap(MapAction.Check, entity, x, y); TraverseMap(MapAction.Add, entity, x, y); entity.X = x; entity.Y = y; } catch (MoveNotOnMapException) { AddEntity(entity); throw; } catch (CollisionException) { AddEntity(entity); throw; } }
public void RemoveEntity(Entity entity) { UpdateManager.RemoveEntity(entity); entity.Alive = false; TraverseMap(MapAction.Remove, entity, entity.X, entity.Y); }
private void AddEntityToDictionary(Entity entity) { if (!Entities.ContainsKey(entity.PlayerNumber)) { Entities.Add(entity.PlayerNumber, new Dictionary<EntityType, List<Entity>>()); } if (!Entities[entity.PlayerNumber].ContainsKey(entity.Type)) { Entities[entity.PlayerNumber].Add(entity.Type, new List<Entity>()); } Entities[entity.PlayerNumber][entity.Type].Add(entity); }
public void RemoveEntity(Entity entity) { EntitiesKilled.Add(entity); }
public void AddEntity(Entity entity) { if (EntitiesUnclassified.ContainsKey(entity.Id)) return; EntitiesAdded.Add(entity); }
private void RemoveEntityFromDictionary(Dictionary<int, Dictionary<EntityType, List<Entity>>> dictionary, Entity entity) { if (!dictionary.ContainsKey(entity.PlayerNumber)) { return; } if (!dictionary[entity.PlayerNumber].ContainsKey(entity.Type)) { return; } dictionary[entity.PlayerNumber][entity.Type].Remove(entity); }
private void TraverseMap(MapAction action, Entity entity, int targetX, int targetY) { CheckBounds(targetX, targetY); CheckBounds(targetX + entity.Width - 1, targetY + entity.Height - 1); for (var x = targetX; x < targetX + entity.Width; x++) { for (var y = targetY; y < targetY + entity.Height; y++) { switch (action) { case MapAction.Check: var mapEntity = GetEntity(x, y); if (mapEntity != null) { throw new CollisionException {Entity = mapEntity}; } break; case MapAction.Add: Rows[y][x] = entity; break; case MapAction.Remove: Rows[y][x] = null; break; } } } }