sábado, 28 de outubro de 2023

UEFN Verse: Versão final do round_based_device

Este é o último artigo da Parte II. Vamos adicionar o código Verse que controla os dispositivos Guard Spawner.

As referências dos dispositivos Guard Spawner serão armazenadas em um array. Adicione a declaração do array depois das variáveis RoundDevice. 

    @editable
    GuardSpawners : []guard_spawner_device = array{}


A habilitação dos dispositivos Guard Spawner conforme o round atual é feita de uma forma semelhante ao dos dispositivos Capture Area. Adicione a função EnableGuardSpawners abaixo que recebe como parâmetro o número de Guard Spawners que devem ser habilitados.

    EnableGuardSpawners(NumberOfSpawners:int):void=        

        MaxIndex := NumberOfSpawners - 1

        for (Index := 0..MaxIndex):
            if(GuardSpawner := GuardSpawners[Index]):
                GuardSpawner.Enable()                


A função EnableGuardSpawners será chamada durante o setup dos rounds. Ela recebe a variável CurrentRound como parâmetro. O código abaixo mostra como vai ficar a função Round1Setup. Faça o mesmo para as outras funções de setup dos rounds.

    Round1Setup():void=
        set CurrentRound = 1
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)


Com estes ajustes completamos o código Verse do round_based_device. Eu decidi fazer um jogo simples para manter o foco nas funções de setup dos rounds que estão associadas aos dispositivos Round Settings. Dentro dessas funções você tem inúmeras possibilidades para a criação de rounds diferenciados.

O código Verse da versão final do round_based_device fica assim:

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

round_based_device := class(creative_device):

    @editable
    EndGameDevice : end_game_device = end_game_device{}

    @editable 
    CaptureAreas: []capture_area_device = array{}

    var NumberOfAreasCaptured : int = 0

    var CurrentRound : int = 0

    @editable
    Round1Device : round_settings_device = round_settings_device{}

    @editable
    Round2Device : round_settings_device = round_settings_device{}

    @editable
    Round3Device : round_settings_device = round_settings_device{}

    @editable
    Round4Device : round_settings_device = round_settings_device{}

    @editable
    Round5Device : round_settings_device = round_settings_device{}

    @editable
    GuardSpawners : []guard_spawner_device = array{}

    OnBegin<override>()<suspends>:void=
        Round1Device.RoundBeginEvent.Subscribe(Round1Setup)
        Round2Device.RoundBeginEvent.Subscribe(Round2Setup)
        Round3Device.RoundBeginEvent.Subscribe(Round3Setup)
        Round4Device.RoundBeginEvent.Subscribe(Round4Setup)
        Round5Device.RoundBeginEvent.Subscribe(Round5Setup)

    AreaCaptured(Agent:agent):void=
        set NumberOfAreasCaptured += 1

        if( NumberOfAreasCaptured = CurrentRound):
            EndGameDevice.Activate(Agent)

    EnableCaptureAreas(NumberOfAreas:int):void=        
        MaxIndex := NumberOfAreas - 1

        for (Index := 0..MaxIndex):
            if(Area := CaptureAreas[Index]):
                Area.Enable()
                Area.AreaIsScoredEvent.Subscribe(AreaCaptured)

    EnableGuardSpawners(NumberOfSpawners:int):void=        

        MaxIndex := NumberOfSpawners - 1

        for (Index := 0..MaxIndex):
            if(GuardSpawner := GuardSpawners[Index]):
                GuardSpawner.Enable()                
            
    Round1Setup():void=
        set CurrentRound = 1
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)
                            
    Round2Setup():void=
        set CurrentRound = 2
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)

    Round3Setup():void=
        set CurrentRound = 3
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)
     
    Round4Setup():void=
        set CurrentRound = 4
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)

    Round5Setup():void=
        set CurrentRound = 5
        
        EnableCaptureAreas(CurrentRound)
        EnableGuardSpawners(CurrentRound)
    


Salve o arquivo e compile o código Verse usando a opção Verse > Build Verse Code do menu do UEFN.

Selecione o round_based_device no nível. Na aba Detailsadicione 5 elementos no array GuardSpawnersselecione as referências dos dispositivos Guard Spawners:


Salve o nível e inicie a sessão. A cada round será habilitado mais um Guard Spawner que irá gerar 2 guardas.



Vai ficar difícil concluir os últimos rounds. Vou dar algumas dicas:

  • Use o dispositivo Verse que está no nível para se esconder e se proteger dos tiros.
  • Quando estiver capturando uma área, fique agachado e se movendo para evitar os tiros dos guardas.
  • Tente eliminar um dos guardas e corra para pegar o rifle que ele deixa cair.


Espero que este exemplo ajude vocês no aprendizado e prática da linguagem Verse. Ainda temos um longo caminho pela frente. 


Sumário Verse