private int CalculateCellItemLeftToGenerate(CellItemTypeInBoard type) { var cellItemCountInBoard = _contexts.game.GetEntities(GameMatcher.AllOf(GameMatcher.CellItem)) .Count(e => Equals(e.cellItemType.Value, type.CellItemType)); var cellItemReservations = _contexts.game.GetEntities(GameMatcher.CellItemReservation) .Count(e => Equals(e.cellItemReservation.CellItemType, type)); var cellItemCountLeftInGoal = CalculateItemCountLeftInGoal(type.GoalType); return(cellItemCountLeftInGoal - (cellItemCountInBoard + cellItemReservations)); }
private int CalculateGenerationAmountForCellItem(CellItemTypeInBoard type, int defaultAmount) { var cellItemCountInBoard = _contexts.game.GetEntities(GameMatcher.AllOf(GameMatcher.GoalType)) .Count(e => Equals(e.goalType.Value, type.GoalType)); var cellItemReservations = _contexts.game.GetEntities(GameMatcher.CellItemReservation) .Count(e => Equals(e.cellItemReservation.CellItemType, type)); var itemCountLeftInGoal = CalculateItemCountLeftInGoal(type.GoalType); var generationAmount = Mathf.Min(defaultAmount, itemCountLeftInGoal - (cellItemCountInBoard + cellItemReservations)); return(Mathf.Max(generationAmount, 0)); }
private void GenerateCellItem(GameEntity generator, int defaultAmount, CellItemTypeInBoard type, GeneratorRadius generatorRadius) { var generationAmount = generator.goalEffectType.Type == GoalEffectType.Decrease ? CalculateGenerationAmountForCellItem(type, defaultAmount) : defaultAmount; var positionsToCheck = GetPositionsToCheck(generator.gridPosition.value, generatorRadius); var cellItemIds = SelectSuitableCellItems(positionsToCheck).Shuffle().Take(generationAmount).ToList(); var reservations = new List <int>(); foreach (var id in cellItemIds) { var reservationId = IdHelper.GetNewReservationId(); var cell = _contexts.game.GetEntityWithCellId(id); cell.AddCellItemReservation(reservationId, type); reservations.Add(reservationId); WaitHelper.Increase(WaitType.CriticalAnimation); } generator.AddReservedCells(reservations); if (generator.goalEffectType.Type == GoalEffectType.Decrease) { _contexts.game.goalForGenerators.Goals[type.GoalType] -= generationAmount; if (CalculateCellItemLeftToGenerate(type) == 0) { CloseGenerators(generator); } } else { GoalHelper.AddToGoal(type.GoalType, reservations.Count); } }
public static Level ParseLevel(int levelNo) { //Get Map var levelStr = levelNo.ToString().PadLeft(5, '0'); var map = new TmxMap($"{Application.streamingAssetsPath}/Levels/level_{levelStr}.tmx"); //Get Level Info From Map var mapProperties = map.Properties.ToArray(); var width = map.Width; var height = map.Height; var itemLayer = map.TileLayers.FirstOrDefault(layer => layer.Name == ItemLayer); var cellItemLayer = map.TileLayers.FirstOrDefault(layer => layer.Name == CellItemLayer); var groupLayers = new TmxObjectGroup[10]; var groups = new Group[10]; foreach (var temp in map.ObjectGroups) { if (!GroupNames.ContainsKey(temp.Name)) { continue; } groupLayers[GroupNames[temp.Name]] = temp; } //goals var goals = new Dictionary <GoalType, int>(4); var goalCount = 0; foreach (var pair in mapProperties) { if (goalCount >= 4) { break; } if (!Enum.TryParse(pair.Key, true, out GoalType goalType)) { continue; } if (!int.TryParse(pair.Value, out var goalAmount)) { continue; } goals.Add(goalType, goalAmount); goalCount++; } if (goalCount == 0) { throw new ApplicationException("No goal in level"); } //move count var moveCountPair = mapProperties.FirstOrDefault(p => p.Key == MoveCount); if (!int.TryParse(moveCountPair.Value, out var moveCount)) { throw new ApplicationException("No move count"); } //items if (itemLayer == null) { throw new ApplicationException("Item layer is null"); } var itemList = itemLayer.Tiles.Select(i => FakeItemIds.Contains(i.Gid - 1) ? ItemTypeInBoard.Empty : Enumeration.FromValue <ItemTypeInBoard>(i.Gid - 1)) .ToArray(); var itemGrid = itemList.ToArray().Make2DArray(height, width); //cell items CellItemTypeInBoard[,] cellItemGrid; if (cellItemLayer == null) { cellItemGrid = new CellItemTypeInBoard[height, width].Populate(CellItemTypeInBoard.Empty); } else { cellItemGrid = cellItemLayer.Tiles.Select(i => Enumeration.FromValue <CellItemTypeInBoard>(i.Gid - 1)) .ToArray().Make2DArray(height, width); } //groups if (groupLayers.All(g => g == null)) { var items = new List <ItemTypeInBoard> { ItemTypeInBoard.BlueCube, ItemTypeInBoard.GreenCube, ItemTypeInBoard.RedCube, ItemTypeInBoard.YellowCube, }; groups[0] = new Group { Items = items, UseForDrop = true }; Debug.LogError("Could not find any Group. Created Temp group for drop"); } else { for (var i = 0; i < groupLayers.Length; i++) { if (groupLayers[i] == null) { continue; } //group items if (groupLayers[i].Objects.Count <= 0) { throw new ApplicationException( $"There were no items in {groupLayers[i].Name}. Added temp items to group"); } var items = groupLayers[i].Objects.ToList() .Select(o => Enumeration.FromValue <ItemTypeInBoard>(o.Tile.Gid - 1)).ToList(); //use for drop bool useForDrop; if (!groupLayers[i].Properties.ContainsKey(UseForDrop)) { useForDrop = false; } else { bool.TryParse(groupLayers[i].Properties[UseForDrop], out useForDrop); } groups[i] = new Group { Items = items, UseForDrop = useForDrop, }; } if (groups.All(g => g == null || !g.UseForDrop)) { throw new ApplicationException($"Could not find any group for drop. Please add it"); } } var level = new Level(width, height, itemGrid, cellItemGrid, goals, moveCount, groups); return(level); }
private void CreateCellItem(CellItemTypeInBoard type, int x, int y) { var entity = _contexts.game.CreateEntity(); entity.AddWillSpawnCellItem(type, x, y); }