protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        // Handle change of game mode
        if (m_CurrentGameModeName != modeName.Value)
        {
            m_CurrentGameModeName = modeName.Value;

            switch (m_CurrentGameModeName)
            {
            case "deathmatch":
                m_GameMode = new GameModeDeathmatch();
                break;

            case "assault":
                m_GameMode = new GameModeAssault();
                break;

            default:
                m_GameMode = new NullGameMode();
                break;
            }
            m_GameMode.Initialize(m_World, this);
            GameDebug.Log("New gamemode : '" + m_GameMode.GetType().ToString() + "'");
            Restart();
            return(default);
Example #2
0
    protected override void OnUpdate()
    {
        //newGDV.GetCharValue();

        // Handle change of game mode
        if (m_CurrentGameModeName != modeName.Value)
        {
            m_CurrentGameModeName = modeName.Value;

            switch (m_CurrentGameModeName)
            {
            case "deathmatch":
                m_GameMode = new GameModeDeathmatch();
                break;

            case "assault":
                m_GameMode = new GameModeAssault();
                break;

            default:
                m_GameMode = new NullGameMode();
                break;
            }
            m_GameMode.Initialize(m_World, this);
            GameDebug.Log("New gamemode : '" + m_GameMode.GetType().ToString() + "'");
            Restart();
            return;
        }

        // Handle joining players
        var playerStates = m_PlayersComponentGroup.GetComponentArray <PlayerState>();

        for (int i = 0, c = playerStates.Length; i < c; ++i)
        {
            var player = playerStates[i];
            if (!player.gameModeSystemInitialized)
            {
                player.score            = 0;
                player.displayGameScore = true;
                player.goalCompletion   = -1.0f;
                m_GameMode.OnPlayerJoin(player);
                player.gameModeSystemInitialized = true;
            }
        }

        m_GameMode.Update();

        // General rules
        gameModeState.gameTimerSeconds = GetGameTimer();

        var playerEntities          = m_PlayersComponentGroup.GetEntityArray();
        var playerCharacterControls = m_PlayersComponentGroup.GetComponentArray <PlayerCharacterControl>();

        for (int i = 0, c = playerStates.Length; i < c; ++i)
        {
            var player           = playerStates[i];
            var controlledEntity = player.controlledEntity;
            var playerEntity     = playerEntities[i];


            player.actionString = player.enableCharacterSwitch ? "Press H to change character" : "";

            var charControl = playerCharacterControls[i];

            // Spawn contolled entity (character) any missing
            if (controlledEntity == Entity.Null)
            {
                var position = new Vector3(0.0f, 0.2f, 0.0f);
                var rotation = Quaternion.identity;
                GetRandomSpawnTransform(player.teamIndex, ref position, ref rotation);

                m_GameMode.OnPlayerRespawn(player, ref position, ref rotation);

                if (charControl.characterType == -1)
                {
                    charControl.characterType = Game.characterType.IntValue;
                    if (Game.allowCharChange.IntValue == 1)
                    {
                        charControl.characterType = 3;
                        //charControl.characterType = SelectedCharacter;                                                                              //Change spawned character here
                    }
                }

                if (charControl.characterType == 1000)
                {
                    SpectatorCamSpawnRequest.Create(PostUpdateCommands, position, rotation, playerEntity);
                }
                else
                {
                    CharacterSpawnRequest.Create(PostUpdateCommands, charControl.characterType, position, rotation, playerEntity);
                }

                continue;
            }

            // Has new new entity been requested
            if (charControl.requestedCharacterType != -1)
            {
                if (charControl.requestedCharacterType != charControl.characterType)
                {
                    charControl.characterType = charControl.requestedCharacterType;
                    if (player.controlledEntity != Entity.Null)
                    {
                        // Despawn current controlled entity. New entity will be created later
                        if (EntityManager.HasComponent <Character>(controlledEntity))
                        {
                            var predictedState = EntityManager.GetComponentData <CharacterPredictedData>(controlledEntity);
                            var rotation       = predictedState.velocity.magnitude > 0.01f ? Quaternion.LookRotation(predictedState.velocity.normalized) : Quaternion.identity;

                            CharacterDespawnRequest.Create(PostUpdateCommands, controlledEntity);
                            CharacterSpawnRequest.Create(PostUpdateCommands, charControl.characterType, predictedState.position, rotation, playerEntity);
                        }
                        player.controlledEntity = Entity.Null;
                    }
                }
                charControl.requestedCharacterType = -1;
                continue;
            }

            if (EntityManager.HasComponent <HealthStateData>(controlledEntity))
            {
                // Is character dead ?
                var healthState = EntityManager.GetComponentData <HealthStateData>(controlledEntity);
                if (healthState.health == 0)
                {
                    // Send kill msg
                    if (healthState.deathTick == m_World.worldTime.tick)
                    {
                        var         killerEntity = healthState.killedBy;
                        var         killerIndex  = FindPlayerControlling(ref playerStates, killerEntity);
                        PlayerState killerPlayer = null;
                        if (killerIndex != -1)
                        {
                            killerPlayer = playerStates[killerIndex];
                            var format = s_KillMessages[Random.Range(0, s_KillMessages.Length)];
                            var l      = StringFormatter.Write(ref _msgBuf, 0, format, killerPlayer.playerName, player.playerName, m_TeamColors[killerPlayer.teamIndex], m_TeamColors[player.teamIndex]);
                            chatSystem.SendChatAnnouncement(new CharBufView(_msgBuf, l));
                        }
                        else
                        {
                            var format = s_SuicideMessages[Random.Range(0, s_SuicideMessages.Length)];
                            var l      = StringFormatter.Write(ref _msgBuf, 0, format, player.playerName, m_TeamColors[player.teamIndex]);
                            chatSystem.SendChatAnnouncement(new CharBufView(_msgBuf, l));
                        }
                        m_GameMode.OnPlayerKilled(player, killerPlayer);
                    }

                    // Respawn dead players except if in ended mode
                    if (m_EnableRespawning && (m_World.worldTime.tick - healthState.deathTick) *
                        m_World.worldTime.tickInterval > respawnDelay.IntValue)
                    {
                        // Despawn current controlled entity. New entity will be created later
                        if (EntityManager.HasComponent <Character>(controlledEntity))
                        {
                            CharacterDespawnRequest.Create(PostUpdateCommands, controlledEntity);
                        }
                        player.controlledEntity = Entity.Null;
                    }
                }
            }
        }
    }