A cena principal do jogo

Agora é hora de juntar tudo que fizemos em uma cena jogável.

Create a new scene and add a Node named Main. (The reason we are using Node instead of Node2D is because this node will be a container for handling game logic. It does not require 2D functionality itself.)

Click the Instance button (represented by a chain link icon) and select your saved player.tscn.

../../_images/instance_scene.webp

Now, add the following nodes as children of Main, and name them as shown:

  • Timer (nomeado MobTimer) - para controlar a frequência com que a turba é gerada

  • Timer (nomeado ScoreTimer) - para incrementar a pontuação a cada segundo

  • Timer (nomeado StartTimer) - para dar um atraso antes de começar

  • Marker2D (named StartPosition) - to indicate the player's start position

Set the Wait Time property of each of the Timer nodes as follows (values are in seconds):

  • MobTimer: 0.5

  • ScoreTimer: 1

  • StartTimer: 2

Além disso, configure a propriedade One Shot ("uma só vez") de StartTimer para "Ativo" e Position do nó StartPosition para (240, 450).

Gerando monstros

The Main node will be spawning new mobs, and we want them to appear at a random location on the edge of the screen. Click the Main node in the Scene dock, then add a child Path2D node named MobPath. When you select Path2D, you will see some new buttons at the top of the editor:

../../_images/path2d_buttons.webp

Select the middle one ("Add Point") and draw the path by clicking to add the points at the corners shown. To have the points snap to the grid, make sure "Use Grid Snap" and "Use Smart Snap" are both selected. These options can be found to the left of the "Lock" button, appearing as a magnet next to some dots and intersecting lines, respectively.

../../_images/grid_snap_button.webp

Importante

Desenhe o caminho em sentido horário, ou sua turba vai surgir apontando para fora em vez de para dentro!

../../_images/draw_path2d.gif

Depois de colocar o ponto 4 na imagem, clique no botão "Fechar Curva", e sua curva estará completa.

Agora que o caminho está definido, adicione um nó PathFollow2D como filho de CaminhoTurba e dê o nome de LocalGeraçãoTurba. Esse nó vai rotacionar automaticamente e seguir o caminho conforme ele se move, para que possamos usá-lo para selecionar uma posição e uma direção aleatória ao longo do caminho.

Sua cena deve se parecer com isso:

../../_images/main_scene_nodes.webp

Script principal

Add a script to Main. At the top of the script, we use @export var mob_scene: PackedScene to allow us to choose the Mob scene we want to instance.

extends Node

@export var mob_scene: PackedScene
var score

Click the Main node and you will see the Mob Scene property in the Inspector under "Main.gd".

Você pode atribuir o valor dessa propriedade de duas formas:

  • Drag mob.tscn from the "FileSystem" dock and drop it in the Mob Scene property.

  • Click the down arrow next to "[empty]" and choose "Load". Select mob.tscn.

Next, select the instance of the Player scene under Main node in the Scene dock, and access the Node dock on the sidebar. Make sure to have the Signals tab selected in the Node dock.

You should see a list of the signals for the Player node. Find and double-click the hit signal in the list (or right-click it and select "Connect..."). This will open the signal connection dialog. We want to make a new function named game_over, which will handle what needs to happen when a game ends. Type "game_over" in the "Receiver Method" box at the bottom of the signal connection dialog and click "Connect". You are aiming to have the hit signal emitted from Player and handled in the Main script. Add the following code to the new function, as well as a new_game function that will set everything up for a new game:

func game_over():
    $ScoreTimer.stop()
    $MobTimer.stop()

func new_game():
    score = 0
    $Player.start($StartPosition.position)
    $StartTimer.start()

Agora vamos conectar o sinal timeout() de cada nó Timer (StartTimer, ScoreTimer e MobTimer) ao script principal. Para cada um dos três timers, selecione o timer no painel Cena, abra a aba Sinais no painel Nó e clique duas vezes no sinal timeout() na lista. Isso abrirá uma nova janela de conexão de sinal. As configurações padrão dessa janela devem ser adequadas, então selecione Conectar para criar uma nova conexão de sinal.

Depois que os três timers estiverem configurados, você deverá ver cada timer com uma conexão de Sinal para seu respectivo sinal timeout(), exibida em verde, dentro de suas respectivas abas de Sinais:

  • (Para o MobTimer): _on_mob_timer_timeout()

  • (Para o ScoreTimer): _on_score_timer_timeout()

  • (Para o StartTimer): _on_start_timer_timeout()

Now we define how each of these timers operate by adding the code below. Notice that StartTimer will start the other two timers, and that ScoreTimer will increment the score by 1.

func _on_score_timer_timeout():
    score += 1

func _on_start_timer_timeout():
    $MobTimer.start()
    $ScoreTimer.start()

In _on_mob_timer_timeout(), we will create a mob instance, pick a random starting location along the Path2D, and set the mob in motion. The PathFollow2D node will automatically rotate as it follows the path, so we will use that to select the mob's direction as well as its position. When we spawn a mob, we'll pick a random value between 150.0 and 250.0 for how fast each mob will move (it would be boring if they were all moving at the same speed).

Note que uma nova instância deve ser adicionada à cena usando add_child() (adicionar filho).

func _on_mob_timer_timeout():
    # Create a new instance of the Mob scene.
    var mob = mob_scene.instantiate()

    # Choose a random location on Path2D.
    var mob_spawn_location = $MobPath/MobSpawnLocation
    mob_spawn_location.progress_ratio = randf()

    # Set the mob's position to the random location.
    mob.position = mob_spawn_location.position

    # Set the mob's direction perpendicular to the path direction.
    var direction = mob_spawn_location.rotation + PI / 2

    # Add some randomness to the direction.
    direction += randf_range(-PI / 4, PI / 4)
    mob.rotation = direction

    # Choose the velocity for the mob.
    var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
    mob.linear_velocity = velocity.rotated(direction)

    # Spawn the mob by adding it to the Main scene.
    add_child(mob)

Importante

Why PI? In functions requiring angles, Godot uses radians, not degrees. Pi represents a half turn in radians, about 3.1415 (there is also TAU which is equal to 2 * PI). If you're more comfortable working with degrees, you'll need to use the deg_to_rad() and rad_to_deg() functions to convert between the two.

Testando a cena

Vamos testar a cena para garantir que tudo esta funcionando. Adicione essa chamada new_game{ ao _ready():

func _ready():
    new_game()

Let's also assign Main as our "Main Scene" - the one that runs automatically when the game launches. Press the "Play" button and select main.tscn when prompted.

Dica

If you had already set another scene as the "Main Scene", you can right click main.tscn in the FileSystem dock and select "Set As Main Scene".

Você deve ser capaz de mover o jogador, ver os inimigos nascendo, e ver o jogador desaparecer quando atingido por um inimigo.

When you're sure everything is working, remove the call to new_game() from _ready() and replace it with pass.

O que nos falta? Uma interface de usuário! Na próxima lição, nós adicionaremos uma tela de título e mostraremos a pontuação do jogador.