Ejemplo n.º 1
0
    private void SpawnPlayer(PlayerController controller, int spawnIndex)
    {
        var battleModels = ApplicationModels.GetModel <BattleModel>().Players;
        var model        = battleModels.Find(p => p.PlayerModel == controller.Owner);

        SpawnPlayer(controller, model, spawnIndex);
    }
Ejemplo n.º 2
0
    private void ResetBoard()
    {
        _spawnPoints.Shuffle();
        for (int i = 0, count = _players.Count; i < count; ++i)
        {
            var controller = _players[i];
            SpawnPlayer(controller, i);
        }

        if (ApplicationModels.GetModel <GameModel>().SpawnBlocks)
        {
            for (int i = 0; i < NB_COLUMNS; ++i)
            {
                for (int j = 0; j < NB_ROWS; ++j)
                {
                    var tile = _tiles[i, j];
                    if (tile.IsEmpty()) // No ignore flags => tile must be completely empty to receive a block (spawn areas stay empty)
                    {
                        var block    = Instantiate(_destructibleBlockPrefab, _groundMesh.transform);
                        var position = GetTileCenter(tile);
                        position.y += 0.5f * block.BoxCollider.size.y * block.transform.localScale.y;
                        block.transform.position = position;
                        tile.Content.Add(block);
                    }
                }
            }
        }

        GameFacade.RoundStart.Invoke();
    }
Ejemplo n.º 3
0
    private bool OnExplosionOnTile(Bomb bomb, Vector2Int coords, int contentIgnoreFlags)
    {
        var     canPropagate = true;
        var     tile         = _tiles[coords.x, coords.y];
        Vector3 position     = GetTileCenter(tile);
        var     explosion    = Instantiate(ScriptableObjectsDatabase.PlayerSettings.ExplosionPrefab, position, Quaternion.identity, _groundMesh.transform);

        Destroy(explosion, 1);
        //Debug.Log("spawning explosion at : " + position);
        if (!tile.IsEmpty(contentIgnoreFlags))
        {
            if (tile.IsWall)
            {
                canPropagate = false;
            }
            else if (tile.IsDestructibleBlock)
            {
                var block = tile.DestructibleBlock;
                tile.Content.Remove(block);
                Destroy(block.gameObject);
                // TODO: Spawn bonus

                canPropagate = false; // TODO: Depends on the player's bonus (spiked bomb?)
            }
            else
            {
                // TODO: determine if the explosion propagates when it hits a player or a bomb.
                // For the moment, let's say yes
                var tileBomb = tile.Bomb;
                if (tileBomb)
                {
                    tileBomb.Explode();
                }

                // Handle players hit by the bomb
                var players = tile.Players;
                if (players != null)
                {
                    var battleModels = ApplicationModels.GetModel <BattleModel>().Players;
                    for (int i = 0, count = players.Count; i < count; ++i)
                    {
                        var player = players[i];
                        player.transform.position = OFF_SCREEN_POSITION;
                        player.Status             = PlayerController.InputStatus.Blocked;
                        tile.Content.Remove(player);

                        var victim = battleModels.Find(p => p.PlayerModel == player.Owner);
                        victim.IsSpawned = false;

                        var attacker = battleModels.Find(p => p.PlayerModel == bomb.Owner);

                        _gameMode.OnPlayerHit(victim, attacker);
                    }
                }
            }
        }

        return(canPropagate);
    }
Ejemplo n.º 4
0
    public void ShowRankings()
    {
        var players = new List <PlayerBattleModel>(ApplicationModels.GetModel <BattleModel>().Players);

        players.Sort(PlayerBattleModel.CompareScores);
        players.ForEach(AddRankingItem);
        GameFacade.ReadyInputPressed.Connect(OnReadyInputPressed);
    }
Ejemplo n.º 5
0
    public void Setup(PlayerBattleModel player)
    {
        _playerColor.color = player.PlayerModel.Color;
        _playerScore.text  = player.Score.ToString();

        var playerIndex = ApplicationModels.GetModel <BattleModel>().Players.FindIndex(player);

        _playerName.text = string.Format(PlayerNameFormat, playerIndex + 1);

        SetPlayerReady(false);
    }
Ejemplo n.º 6
0
    public BattleModel()
    {
        Players = new List <PlayerBattleModel>();

        var models = ApplicationModels.GetModel <GameModel>().Players;

        for (int i = 0, count = models.Count; i < count; ++i)
        {
            Players.Add(new PlayerBattleModel(models[i]));
        }
    }
Ejemplo n.º 7
0
    private void OnPlayerStarted(int joystickNumber)
    {
        var gameModel   = ApplicationModels.GetModel <GameModel>();
        var startPlayer = gameModel.Players.FindIndex(player => player.JoystickNumber == joystickNumber);

        if (startPlayer >= 0)
        {
            Debug.Log(string.Format("Game started by player {0} (joystick {1})", startPlayer + 1, joystickNumber));
        }

        SceneManager.LoadScene("Scenes/TestLevel", LoadSceneMode.Single);
    }
Ejemplo n.º 8
0
    public void Initialize()
    {
        _scoreItems = new List <PlayerScoreUI>();
        var players = ApplicationModels.GetModel <BattleModel>().Players;

        for (int i = 0, count = players.Count; i < count; ++i)
        {
            var parent = i % 2 == 0 ? _leftColumn : _rightColumn;
            var item   = Instantiate(_scoreUiPrefab, parent.transform);
            item.Initialize(players[i]);
            _scoreItems.Add(item);
        }
    }
Ejemplo n.º 9
0
    public void Show(int winnerIndex)
    {
        bool hasWinner = winnerIndex >= 0;

        _winRoot.SetActive(hasWinner);
        _drawRoot.SetActive(!hasWinner);

        if (hasWinner)
        {
            var playerModel = ApplicationModels.GetModel <GameModel>().Players[winnerIndex];
            _winnerColor.color = playerModel.Color;
            _winText.text      = string.Format(WinTextFormat, winnerIndex + 1);
        }
    }
Ejemplo n.º 10
0
    private void Awake()
    {
        var model          = ApplicationModels.GetModel <GameModel>();
        var joystickOffset = TestData.UseKeyboard ? 0 : 1;

        for (int i = 0; i < TestData.NbPlayers; ++i)
        {
            model.AddPlayer(i + joystickOffset, ScriptableObjectsDatabase.PlayerSettings.Colors[i]);
        }

        model.SelectedMode = _testData.GameMode;
        model.SpawnBlocks  = _testData.SpawnBlocks;

        SceneManager.LoadScene(TestData.ScenePath, LoadSceneMode.Single);
    }
Ejemplo n.º 11
0
    private void OnPlayerJoined(int joystickNumber)
    {
        var gameModel = ApplicationModels.GetModel <GameModel>();

        if (gameModel.Players.Find(player => player.JoystickNumber == joystickNumber) == null)
        {
            int playerOrder = gameModel.Players.Count;
            var color       = ScriptableObjectsDatabase.PlayerSettings.Colors[playerOrder];

            _playersItem[playerOrder].Open(playerOrder, color);
            gameModel.AddPlayer(joystickNumber, color);

            _startMessage.gameObject.SetActive(true);
            _joinMessage.gameObject.SetActive(playerOrder != BlikeGame.MAX_PLAYERS - 1);
        }
    }
Ejemplo n.º 12
0
    public void OnUpdate()
    {
        if (!IsOver)
        {
            ElapsedTime += Time.deltaTime;
            var secondsRemaining = Mathf.CeilToInt(_timeLimit - ElapsedTime);
            GameFacade.TimeRemainingChanged.Invoke(secondsRemaining);

            if (IsOver)
            {
                var players   = ApplicationModels.GetModel <BattleModel>().Players;
                var winners   = new List <int>();
                var bestScore = int.MinValue;
                for (int i = 0, count = players.Count; i < count; ++i)
                {
                    var playerScore = players[i].Score;
                    if (playerScore >= bestScore)
                    {
                        if (playerScore > bestScore)
                        {
                            winners.Clear();
                        }

                        winners.Add(i);
                        bestScore = playerScore;
                    }
                }

                var winnerIndex = winners.Count > 1 ? -1 : winners[0];
                GameFacade.BattleEnd.Invoke(winnerIndex, true);
            }
            else
            {
                // TODO: wait for a few seconds before respawn
                for (int i = 0, count = _playersToRespawn.Count; i < count; ++i)
                {
                    GameFacade.SpawnPlayer.Invoke(_playersToRespawn[i]);
                }

                _playersToRespawn.Clear();
            }
        }
    }
Ejemplo n.º 13
0
    public void OnBombExploded()
    {
        var  players         = ApplicationModels.GetModel <BattleModel>().Players;
        var  activePlayers   = players.FindAll(player => player.IsSpawned);
        var  nbActivePlayers = activePlayers.Count;
        bool isRoundOver     = nbActivePlayers <= 1;

        if (isRoundOver)
        {
            int  winnerIndex = -1;
            bool isGameOver  = false;

            if (nbActivePlayers > 0)
            {
                var winner = activePlayers[0];
                winnerIndex = players.FindIndex(winner);
                isGameOver  = ++winner.Score == NbRoundsToWin;
            }
            GameFacade.BattleEnd.Invoke(winnerIndex, isGameOver);
        }
    }
Ejemplo n.º 14
0
    public void Awake()
    {
        ApplicationModels.RegisterModel <BattleModel>(new BattleModel());

        var gameModel = ApplicationModels.GetModel <GameModel>();
        var mode      = gameModel.SelectedMode;

        _gameMode = GameModeFactory.Create(mode);

        _view = Instantiate(ScriptableObjectsDatabase.GameViews[mode]);
        _view.Initialize();

        for (int i = 0; i < NB_COLUMNS; ++i)
        {
            for (int j = 0; j < NB_ROWS; ++j)
            {
                _tiles[i, j] = new Tile(i, j);
            }
        }

        var playerModels = gameModel.Players;
        int playersCount = playerModels.Count;

        UpdateTilesWithContent <Wall>(_terrain, null);

        var nbSpawns = Math.Min(_spawnPointPositions.Length, playersCount);

        _spawnPoints = new SpawnPoint[nbSpawns];
        for (int i = 0; i < nbSpawns; ++i)
        {
            var tile = GetTile(_spawnPointPositions[i].position);
            _spawnPoints[i].Tile     = tile;
            _spawnPoints[i].Position = GetTileCenter(tile);

            // Flag all nearby tiles as SpawnArea to avoid spawning blocks on them
            var coords = tile.Coords;
            for (int offsetX = -1; offsetX <= 1; ++offsetX)
            {
                for (int offsetY = -1; offsetY <= 1; ++offsetY)
                {
                    var x = coords.x + offsetX;
                    var y = coords.y + offsetY;
                    if (x >= 0 && x < NB_COLUMNS && y >= 0 && y < NB_ROWS)
                    {
                        var areaTile = _tiles[x, y];
                        if (!areaTile.IsWall)
                        {
                            areaTile.Content.Add(SpawnArea.Instance);
                        }
                    }
                }
            }
        }

        _players = new List <PlayerController>(playerModels.Count);
        for (int i = 0; i < playersCount; ++i)
        {
            var model      = playerModels[i];
            var controller = Instantiate(ScriptableObjectsDatabase.PlayerSettings.PlayerPrefab, _groundMesh.transform);
            controller.Initialize(model);

            var playerView = controller.GetComponent <PlayerView>();
            playerView.Initialize(model.Color);

            _players.Add(controller);
        }

        ResetBoard();

        GameFacade.SpawnPlayer.Connect(SpawnPlayer);
        GameFacade.PlayerMoved.Connect(OnPlayerMoved);
        GameFacade.BombExploded.Connect(OnBombExploded);
        GameFacade.BombInputPressed.Connect(OnBombInputPressed);
        GameFacade.BattleEnd.Connect(OnBattleEnd);
        GameFacade.AllPlayersReadyForNextGame.Connect(OnLeave);
    }