public void AddHeroSpawner(EntityType team, Vector2 position) { Server.Entities.EntityBase entity = new EntityHeroSpawner() { Position = position, Type = EntityType.HeroSpawner | team, }; CurrentMap.Entities.Add(entity.ID, entity); }
/// <summary> /// Crée une nouvelle map depuis un fichier. /// </summary> /// <param name="path"></param> /// <returns></returns> public static MapFile FromFile(string path) { string[] words = File.ReadAllText(path).Split(new char[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); Point size = new Point(10, 10); bool[,] pass = new bool[10, 10]; EntityCollection newEntities = new EntityCollection(); Dictionary <EventId, GameEvent> newEvents = new Dictionary <EventId, GameEvent>(); for (int i = 0; i < words.Length; i++) { if (i == 152) { i = 152; } string word = words[i]; if (word == "size") { int sX = int.Parse(words[i + 1]); int sY = int.Parse(words[i + 2]); size = new Point(sX, sY); pass = new bool[sX, sY]; i += 2; } else if (word == "map") { i++; for (int y = 0; y < size.Y; y++) { for (int x = 0; x < size.X; x++) { pass[x, y] = words[i][x] == '1' ? true : false; } i++; } i--; } else { #region Entities try { if (char.IsDigit(word[0])) { throw new InvalidOperationException(); } // Entitié EntityType type = (EntityType)Enum.Parse(typeof(EntityType), word); float sX = float.Parse(words[i + 1]); float sY = float.Parse(words[i + 2]); EntityBase newEntity = null; switch (type & (EntityType.AllSaved)) { case EntityType.Tower: newEntity = new EntityTower() { Position = new Vector2(sX, sY), Type = type }; break; case EntityType.Spawner: newEntity = new EntitySpawner() { Position = new Vector2(sX, sY), SpawnPosition = new Vector2(sX, sY), Type = type }; break; case EntityType.HeroSpawner: newEntity = new EntityHeroSpawner() { Position = new Vector2(sX, sY), Type = type }; break; case EntityType.WardPlacement: newEntity = new EntityWardPlacement() { Position = new Vector2(sX, sY), Type = type, }; break; case EntityType.Checkpoint: int row = int.Parse(words[i + 3]); int id = int.Parse(words[i + 4]); i += 2; newEntity = new EntityCheckpoint() { Position = new Vector2(sX, sY), Type = type, CheckpointRow = row, CheckpointID = id }; break; case EntityType.Datacenter: newEntity = new EntityDatacenter() { Type = type, Position = new Vector2(sX, sY) }; break; case EntityType.Shop: newEntity = new EntityShop() { Type = type, Position = new Vector2(sX, sY) }; break; case EntityType.MiningFarm: throw new NotImplementedException(); break; } newEntities.Add(newEntity.ID, newEntity); i += 2; } catch (System.ArgumentException) { } #endregion #region Events try { if (char.IsDigit(word[0])) { throw new InvalidOperationException(); } // Entitié EventId type = (EventId)Enum.Parse(typeof(EventId), word); float sX = float.Parse(words[i + 1]); float sY = float.Parse(words[i + 2]); GameEvent newEvent = null; switch (type) { case EventId.Camp8: case EventId.Camp7: case EventId.Camp6: case EventId.Camp5: case EventId.Camp4: case EventId.Camp3: case EventId.Camp2: case EventId.Camp1: newEvent = new EventMonsterCamp() { Position = new Vector2(sX, sY) }; break; case EventId.Router1: case EventId.Router2: newEvent = new EventRouter() { Position = new Vector2(sX, sY) }; break; case EventId.MiningFarm: newEvent = new EventMiningFarm() { Position = new Vector2(sX, sY) }; break; } newEvents.Add(type, newEvent); i += 2; } catch (System.ArgumentException) { } #endregion } } MapFile map = new MapFile() { Entities = newEntities, Passability = pass, Events = newEvents }; return(map); }