/// <summary> /// Builds new Ship from preset, user, and cellTypes /// </summary> /// <param name="owner">User, who owns the Ship</param> /// <param name="preset">Ship preset</param> /// <param name="cellTypes">cellTypes[i] - type of room with id = i</param> /// <returns>Built Ship</returns> public Ship BuildShip(User owner, ShipPreset preset, IEnumerable<int> cellTypes) { Ship ship = new Ship(owner, preset.RoomIds.GetLength(1), preset.RoomIds.GetLength(0)); ship.preset = preset; currentWeaponId = 0; assignedWeaponIds = new Dictionary<int, int>(); for (int i = 0; i < ship.cells.Length; i++) { for (int j = 0; j < ship.cells[i].Length; j++) { int roomId = preset.RoomIds[i, j]; ship.cells[i][j] = buildCell(ship, cellTypes.ToList()[roomId], roomId); if (currentWeaponId >= preset.weapons.Length) currentWeaponId = 0; } } assignesStatuses = new Dictionary<int, CellStatus>(); InitCellStatuses(ship, preset); if (preset.afterShipBuilt != null) preset.afterShipBuilt.Invoke(ship); return ship; }
/// <summary> /// Gets Ship preset from it's index /// </summary> /// <param name="presetId"></param> /// <returns></returns> public ShipPreset GetShip(int presetId) { ShipPreset preset = new ShipPreset(); preset.ImageFilename = "/content/img/shipBackground" + presetId + ".png"; preset.LayoutFilename = "/content/img/shipLayout" + presetId + ".png"; switch (presetId) { case 1: preset.LayoutOffset = new double[] { 28, 29 }; preset.weapons = new Weapon[] { new Weapon() { position = new double[] { 19, 49 }, weaponType = 0, reversed = true }, new Weapon() { position = new double[] { 284, 49 }, weaponType = 0 } }; preset.RoomIds = new int[,]{ {1,1,0,0,0,5,5}, {0,2,2,3,4,4,0}, {0,0,0,3,0,0,0} }; preset.DefaultLayout = new int[] { 0, 2, 4, 3, 1, 2 }; break; case 2: preset.LayoutOffset = new double[] { 51, 66 }; preset.weapons = new Weapon[] { new Weapon() { position = new double[] { 52, 53 }, weaponType = 0, reversed = true }, }; preset.RoomIds = new int[,]{ {1,2}, {3,3} }; preset.DefaultLayout = new int[] { 0, 2, 1, 3}; break; } /*preset.afterShipBuilt = (ship => ((CellWeapon)ship.cells[0][5]).weaponId = ((CellWeapon)ship.cells[0][6]).weaponId = 1);*/ return preset; }
/// <summary> /// Sets status field for every Cell in ship.cells /// Room with the same roomId should have same staus /// </summary> /// <param name="ship"></param> /// <param name="preset"></param> private void InitCellStatuses(Ship ship, ShipPreset preset) { for (int i = 0; i < ship.cells.Length; i++) { for (int j = 0; j < ship.cells[i].Length; j++) { int roomId = preset.RoomIds[i, j]; if (!assignesStatuses.ContainsKey(roomId)) assignesStatuses[roomId] = new CellStatus() { energy = 0, health = 10 }; assignesStatuses[roomId].cellsInRoomCount++; ship.cells[i][j].status = ship.status[i][j] = assignesStatuses[roomId]; } } }