Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

La escena principal del juego

Ahora es el momento de reunir todo lo que hicimos en una escena de juego jugable.

Crea una nueva escena y añade un Nodo llamado Main. (La razón por la que estamos usando Node en lugar de Node2D es porque este nodo será un contenedor para manejar la lógica del juego. No requiere funcionalidad 2D en sí mismo.)

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

../../_images/instance_scene.webp

Ahora agrega los siguientes nodos como hijos de Main, y nómbralos como se muestra (los valores son en segundos):

  • Timer (llamado MobTimer) - para controlar cuán seguido aparecerán los enemigos

  • Timer (llamado ScoreTimer) - para incrementar el puntaje cada segundo

  • Timer (llamado StartTimer) - para crear una retardo antes de comenzar

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

Ajustar la propiedad Wait Time (tiempo de espera) de cada nodo Timer como se indica:

  • MobTimer: 0.5

  • ScoreTimer: 1

  • StartTimer: 2

Además, marca la propiedad One Shot (una vez) del nodo StartTimer como "Activado" y ajusta la propiedad Position del nodo StartPosition en (240,450).

Agregando enemigos

El nodo Main generará los nuevos enemigos, y queremos que aparezcan en una ubicación al azar en el borde de la pantalla. Agregue un nodo Path2D llamado MobPath como hijo de Main. Cuando seleccione Path2D, verá que aparecen nuevos botones en la parte de arriba del 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

Dibuja la ruta en el sentido de las agujas del reloj ¡o tus enemigos apuntarán hacia afuera en lugar de hacia adentro!

../../_images/draw_path2d.gif

Después de colocar el punto 4, haz clic en el botón "Cerrar Curva" y la curva se completará.

Ahora que el camino está definido, agrega un nodo PathFollow2D como hijo de MobPath y nómbralo MobSpawnLocation. Este nodo rotará automáticamente y seguirá el camino mientras se mueve, así podemos usarlo para seleccionar una posición al azar y una dirección a lo largo del camino.

Tu escena debería verse así:

../../_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

Haz clic en el nodo Main y verás la propiedad Mob en el Inspector bajo "Script Variables".

Puedes asignar el valor de esta propiedad de dos maneras:

  • 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()

Now connect the timeout() signal of each of the Timer nodes (StartTimer, ScoreTimer, and MobTimer) to the main script. StartTimer will start the other two timers. 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).

Observa que añadiremos la nueva instancia a la escena usando add_child().

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 = get_node("MobPath/MobSpawnLocation")
    mob_spawn_location.progress_ratio = randf()

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

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

    # 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.

Probando la escena

Vamos a probar la escena para asegurarnos de que todo funciona. Añade esta llamada new_game a _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.

Truco

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".

Deberías poder mover al jugador alrededor, ver a los mobs creándose, y ver al jugador desaparecer cuando es golpeado por un mob.

Cuando estés seguro de que todo funciona, quita la llamada a new_game() de ready().

¿Qué le falta a nuestro juego? Alguna interfaz de usuario. En la siguiente lección, agregaremos una pantalla de título y mostraremos la puntuación del jugador.