Exemplo n.º 1
0
        public override void OnTick(GameLevel gameLevel, int currentTick, out int outTick)
        {
            base.OnTick(gameLevel, currentTick, out outTick);

            int secondsLeft;

            if (EndTick == -1)
            {
                secondsLeft = 0;                 //Trigger a initial refresh
            }
            else
            {
                secondsLeft = (EndTick - currentTick) / 2;
            }

            if (secondsLeft > MaxVoteTime / 2)
            {
                return;                 //Ignore until the ticker has finished
            }
            if (secondsLeft == 0)
            {
                //Can't execute first run
                if (_currentVotingPlayer != null)
                {
                    //Tally up votes

                    int currentVoteCount = 0;

                    gameLevel.DoForAllPlayers(player =>
                    {
                        switch (player.Inventory.InHandSlot)
                        {
                        case 2:
                            currentVoteCount += 1;
                            break;

                        case 3:
                            currentVoteCount += 2;
                            break;

                        case 4:
                            currentVoteCount += 3;
                            break;

                        case 5:
                            currentVoteCount += 4;
                            break;

                        case 6:
                            currentVoteCount += 5;
                            break;

                        default:
                            if (player.Inventory.InHandSlot < 2)
                            {
                                currentVoteCount += 1;                                         //2
                            }
                            else if (player.Inventory.InHandSlot > 6)
                            {
                                currentVoteCount += 5;
                            }
                            break;
                        }
                    });

                    _voteTally.TryAdd(_currentVotingPlayer, currentVoteCount);
                }
                else
                {
                    //Reset gamemode during voting phase
                    gameLevel.DoForAllPlayers(player =>
                    {
                        //player.UseCreativeInventory = false;
                        player.UpdateGameMode(GameMode.Adventure, true);
                    });
                }

                //Pick another player, or end the phase if all voting has finished
                BuildBattleLevel buildLevel = (BuildBattleLevel)gameLevel;

                SkyPlayer nextVotePlayer = null;
                do
                {
                    var gameTeam = buildLevel.BuildTeams[++_currentVotingTeam];

                    List <SkyPlayer> teamPlayer = buildLevel.GetPlayersInTeam(gameTeam);
                    if (teamPlayer.Count > 0)
                    {
                        nextVotePlayer = teamPlayer[0];
                    }
                } while (nextVotePlayer == null && _currentVotingTeam < buildLevel.BuildTeams.Count - 1);

                if (nextVotePlayer == null)
                {
                    gameLevel.UpdateGameState(GetNextGameState(gameLevel));
                    return;
                }

                _currentVotingPlayer = nextVotePlayer;

                EndTick = gameLevel.Tick + MaxVoteTime;

                List <PlayerLocation> voteLocations = ((BuildBattleLevel)gameLevel).GetVoteLocations((BuildBattleTeam)_currentVotingPlayer.GameTeam);

                int i = -1;
                gameLevel.DoForAllPlayers(player =>
                {
                    player.SetAllowFly(true);
                    player.IsFlying = true;
                    player.SendAdventureSettings();

                    if (player == _currentVotingPlayer)
                    {
                        player.Inventory.Clear();                         //Remove voting possibility

                        PlayerLocation teleportLocation = (PlayerLocation)((BuildBattleTeam)player.GameTeam).SpawnLocation.Clone();
                        teleportLocation.Y += 10;

                        player.Teleport(teleportLocation);
                    }
                    else
                    {
                        i++;

                        for (int j = 1; j < 6; j++)
                        {
                            player.Inventory.SetInventorySlot(1 + j, new ItemVote(j, _currentVotingPlayer.Username));
                        }

                        player.Inventory.SetHeldItemSlot(4);                         //Default to middle rating if AFK

                        PlayerLocation voteSpawnLocation      = voteLocations[i];
                        BlockCoordinates voteSpawnCoordinates = voteSpawnLocation.GetCoordinates3D();
                        while (!player.Level.IsAir(voteSpawnCoordinates))
                        {
                            voteSpawnCoordinates.Y++;
                        }

                        voteSpawnLocation = new PlayerLocation(voteSpawnCoordinates.X, voteSpawnCoordinates.Y, voteSpawnCoordinates.Z,
                                                               voteSpawnLocation.HeadYaw, voteSpawnLocation.Yaw, voteSpawnLocation.Pitch);

                        player.Teleport(voteSpawnLocation);
                    }

                    TitleUtil.SendCenteredSubtitle(player, $"§d§lVoting for:\n§7{nextVotePlayer.Username}");
                });
            }

            gameLevel.DoForAllPlayers(player =>
            {
                SendTickableMessage(gameLevel, player, GetTickableInformation(_currentVotingPlayer));
            });
        }