private void CreateAndSpawnPlayers(GameModePvEConfig gameMode, LevelStageConfig levelStageConfig, GameLevelGridModel levelGridModel) { var playerConfigs = gameMode.PlayerConfigs; var spawnCorners = levelStageConfig.PlayersSpawnCorners; Assert.IsTrue(playerConfigs.Length <= spawnCorners.Length, "players count greater than the level spawn corners"); var zip = spawnCorners.Zip(playerConfigs, (spawnCorner, playerConfig) => (spawnCorner, playerConfig)); foreach (var(spawnCorner, playerConfig) in zip) { var playerInput = _inputService.RegisterPlayerInput(playerConfig); var player = _gameFactory.CreatePlayer(playerConfig, playerInput); Assert.IsNotNull(player); var position = levelGridModel.GetCornerWorldPosition(spawnCorner); var go = _gameFactory.SpawnEntity(playerConfig.HeroConfig, position); Assert.IsNotNull(go); var hero = (IHero)go.GetComponent <HeroController>(); player.AttachHero(hero, playerInput); Game.LevelManager.AddPlayer(playerConfig.PlayerTagConfig, player); } }
private void CreateAndSpawnEnemies(LevelStageConfig levelStageConfig) { var enemySpawnElements = levelStageConfig.Enemies; foreach (var enemySpawnElement in enemySpawnElements) { var enemy = _gameFactory.CreateEnemy(enemySpawnElement.EnemyConfig); Assert.IsNotNull(enemy); Game.LevelManager.AddEnemy(enemySpawnElement.EnemyConfig, enemy); } }
public GameLevelGridModel(LevelStageConfig levelStageConfig) { _size = math.int2(levelStageConfig.ColumnsNumber, levelStageConfig.RowsNumber); Assert.IsTrue(math.all(_size % 2 != int2.zero)); var playersSpawnCorners = levelStageConfig.PlayersSpawnCorners; var softBlocksCoverage = levelStageConfig.SoftBlocksCoverage; var reservedCellsIndices = new HashSet<int>( playersSpawnCorners .SelectMany( corner => { var coordinate = corner * (_size - 1); var offset = math.select(-1, 1, corner == int2.zero); return new[] { GetFlattenCellCoordinate(coordinate), GetFlattenCellCoordinate(math.mad(math.int2(1, 0), offset, coordinate)), GetFlattenCellCoordinate(math.mad(math.int2(0, 1), offset, coordinate)) }; } ) ); var reservedCellsNumber = reservedCellsIndices.Count; var totalCellsNumber = _size.x * _size.y - reservedCellsNumber; var hardBlocksNumber = (_size.x - 1) * (_size.y - 1) / 4; var softBlocksNumber = (int) math.round((totalCellsNumber - hardBlocksNumber) * softBlocksCoverage / 100.0f); var floorCellsNumber = totalCellsNumber - softBlocksNumber - hardBlocksNumber; var cellTypeNumbers = math.int2(floorCellsNumber, softBlocksNumber); var powerUpItems = levelStageConfig.PowerUpItems; var softBlocksPerPowerUpItem = softBlocksNumber / powerUpItems.Length; Random.InitState(levelStageConfig.RandomSeed); var powerItemsIndices = new HashSet<int>( Enumerable .Range(0, powerUpItems.Length) .Select(i => Random.Range(i * softBlocksPerPowerUpItem, (i + 1) * softBlocksPerPowerUpItem)) ); _grid = Enumerable .Range(0, totalCellsNumber + reservedCellsNumber) .Select( index => { if (reservedCellsIndices.Contains(index)) return GridTileType.FloorTile; var coordinate = math.int2(index % _size.x, index / _size.x); if (math.all(coordinate % 2 == 1)) return GridTileType.HardBlock; var range = (int2) (cellTypeNumbers == int2.zero); var softBlockOdds = (int) (cellTypeNumbers.y * 100.0f / (cellTypeNumbers.x + cellTypeNumbers.y)); var typeIndex = Convert.ToInt32(Random.Range(0, 100) < softBlockOdds); typeIndex = math.clamp(typeIndex, range.x, 2 - range.y); var tileType = typeIndex == 0 ? GridTileType.FloorTile : GridTileType.SoftBlock; if (tileType == GridTileType.SoftBlock && powerItemsIndices.Contains(cellTypeNumbers[typeIndex])) tileType |= GridTileType.PowerUpItem; --cellTypeNumbers[typeIndex]; return tileType; } ) .ToArray(); // Random.InitState(new System.Random().Next()); }