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.)
Presione el botón Instanciar (representado por el icono de una cadena) y seleccione su escena ``Player.tscn``guardada.
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 enemigosTimer (llamado
ScoreTimer
) - para incrementar el puntaje cada segundoTimer (llamado
StartTimer
) - para crear una retardo antes de comenzarPosition2D (llamado
StartPosition
) - para indicar la posición inicial del jugador
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:
Selecciona el botón del medio ("Añadir Punto") y dibuja el camino haciendo clic para agregar los puntos en las esquinas como se muestra. Para tener los puntos ajustados a la cuadrícula, asegúrate de tener marcado "Ajustar a cuadrícula". Esta opción se encuentra a la izquierda del botón "Bloquear", que aparece como un imán al lado de las líneas de intersección.
Importante
Dibuja la ruta en el sentido de las agujas del reloj ¡o tus enemigos apuntarán hacia afuera en lugar de hacia adentro!
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í:
Script Principal¶
Agrega un script a Main
. Al principio del script usaremos export (PackedScene)
para permitirnos elegir la escena de enemigos que queremos instanciar.
extends Node
export(PackedScene) var mob_scene
var score
public class Main : Node
{
// Don't forget to rebuild the project so the editor knows about the new export variable.
#pragma warning disable 649
// We assign this in the editor, so we don't need the warning about not being assigned.
[Export]
public PackedScene MobScene;
#pragma warning restore 649
public int Score;
}
// Copy `player.gdns` to `main.gdns` and replace `Player` with `Main`.
// Attach the `main.gdns` file to the Main node.
// Create two files `main.cpp` and `main.hpp` next to `entry.cpp` in `src`.
// This code goes in `main.hpp`. We also define the methods we'll be using here.
#ifndef MAIN_H
#define MAIN_H
#include <AudioStreamPlayer.hpp>
#include <CanvasLayer.hpp>
#include <Godot.hpp>
#include <Node.hpp>
#include <PackedScene.hpp>
#include <PathFollow2D.hpp>
#include <RandomNumberGenerator.hpp>
#include <Timer.hpp>
#include "hud.hpp"
#include "player.hpp"
class Main : public godot::Node {
GODOT_CLASS(Main, godot::Node)
int score;
HUD *_hud;
Player *_player;
godot::Node2D *_start_position;
godot::PathFollow2D *_mob_spawn_location;
godot::Timer *_mob_timer;
godot::Timer *_score_timer;
godot::Timer *_start_timer;
godot::AudioStreamPlayer *_music;
godot::AudioStreamPlayer *_death_sound;
godot::Ref<godot::RandomNumberGenerator> _random;
public:
godot::Ref<godot::PackedScene> mob_scene;
void _init() {}
void _ready();
void game_over();
void new_game();
void _on_MobTimer_timeout();
void _on_ScoreTimer_timeout();
void _on_StartTimer_timeout();
static void _register_methods();
};
#endif // MAIN_H
También agregamos una llamada a randomize()
aquí para que el generador de números aleatorios genere diferentes números aleatorios cada vez que se ejecuta el juego:
func _ready():
randomize()
public override void _Ready()
{
GD.Randomize();
}
// This code goes in `main.cpp`.
#include "main.hpp"
#include <SceneTree.hpp>
#include "mob.hpp"
void Main::_ready() {
_hud = get_node<HUD>("HUD");
_player = get_node<Player>("Player");
_start_position = get_node<godot::Node2D>("StartPosition");
_mob_spawn_location = get_node<godot::PathFollow2D>("MobPath/MobSpawnLocation");
_mob_timer = get_node<godot::Timer>("MobTimer");
_score_timer = get_node<godot::Timer>("ScoreTimer");
_start_timer = get_node<godot::Timer>("StartTimer");
// Uncomment these after adding the nodes in the "Sound effects" section of "Finishing up".
//_music = get_node<godot::AudioStreamPlayer>("Music");
//_death_sound = get_node<godot::AudioStreamPlayer>("DeathSound");
_random = (godot::Ref<godot::RandomNumberGenerator>)godot::RandomNumberGenerator::_new();
_random->randomize();
}
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:
Arrastra
Mob.tscn``del panel "Sistema de Archivos" y suéltalo en la propiedad ``Mob
.Haga clic en la flecha hacia abajo junto a "[empty]" y elija "Load". Selecciona
Mob.tscn
.
A continuación selecciona el nodo Player
en el panel de escenas y accede al panel Nodo en la barra lateral. Asegúrate de tener seleccionada la pestaña Señales
.
Deberías ver una lista de señales para el nodo Player
. Busca la señal hit
en la lista y haz doble clic en esta (o clic derecho y selecciona "Conectar..."). Esto abrirá el diálogo de conexión. Haremos una nueva función llamada game_over
, la que ejecutará lo que debe suceder cuando el juego termina. Escribe "game_over" en el campo "Método receptor" en la parte inferior de la ventana y haz clic en "Conectar". Agrega el siguiente código a la nueva función, así como una función new_game
que configurará todo en un juego nuevo:
func game_over():
$ScoreTimer.stop()
$MobTimer.stop()
func new_game():
score = 0
$Player.start($StartPosition.position)
$StartTimer.start()
public void GameOver()
{
GetNode<Timer>("MobTimer").Stop();
GetNode<Timer>("ScoreTimer").Stop();
}
public void NewGame()
{
Score = 0;
var player = GetNode<Player>("Player");
var startPosition = GetNode<Position2D>("StartPosition");
player.Start(startPosition.Position);
GetNode<Timer>("StartTimer").Start();
}
// This code goes in `main.cpp`.
void Main::game_over() {
_score_timer->stop();
_mob_timer->stop();
}
void Main::new_game() {
score = 0;
_player->start(_start_position->get_position());
_start_timer->start();
}
Ahora conecta la señal timeout()
de cada uno de los nodos Timer (StartTimer
, ScoreTimer
y MobTimer
) al script de Main. StartTimer
iniciará los otros dos temporizadores. ScoreTimer
incrementará la puntuación en 1.
func _on_ScoreTimer_timeout():
score += 1
func _on_StartTimer_timeout():
$MobTimer.start()
$ScoreTimer.start()
public void OnScoreTimerTimeout()
{
Score++;
}
public void OnStartTimerTimeout()
{
GetNode<Timer>("MobTimer").Start();
GetNode<Timer>("ScoreTimer").Start();
}
// This code goes in `main.cpp`.
void Main::_on_ScoreTimer_timeout() {
score += 1;
}
void Main::_on_StartTimer_timeout() {
_mob_timer->start();
_score_timer->start();
}
// Also add this to register all methods and the mob scene property.
void Main::_register_methods() {
godot::register_method("_ready", &Main::_ready);
godot::register_method("game_over", &Main::game_over);
godot::register_method("new_game", &Main::new_game);
godot::register_method("_on_MobTimer_timeout", &Main::_on_MobTimer_timeout);
godot::register_method("_on_ScoreTimer_timeout", &Main::_on_ScoreTimer_timeout);
godot::register_method("_on_StartTimer_timeout", &Main::_on_StartTimer_timeout);
godot::register_property("mob_scene", &Main::mob_scene, (godot::Ref<godot::PackedScene>)nullptr);
}
En _on_MobTimer_timeout()
, crearemos una instancia de mob, elegiremos un punto de partida aleatorio a lo largo del Path2D
, y pondremos al mob en movimiento. El nodo PathFollow2D
girará automáticamente a medida que siga la ruta, así que lo utilizaremos para seleccionar la dirección y la posición de la mob. Cuando generemos un mob, elegiremos un valor aleatorio entre 150.0
y 250.0
para la velocidad a la que se moverá cada mob (sería aburrido si todos se movieran a la misma velocidad).
Observa que añadiremos la nueva instancia a la escena usando add_child()
.
func _on_MobTimer_timeout():
# Create a new instance of the Mob scene.
var mob = mob_scene.instance()
# Choose a random location on Path2D.
var mob_spawn_location = get_node("MobPath/MobSpawnLocation")
mob_spawn_location.offset = randi()
# 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 += rand_range(-PI / 4, PI / 4)
mob.rotation = direction
# Choose the velocity for the mob.
var velocity = Vector2(rand_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)
public void OnMobTimerTimeout()
{
// Note: Normally it is best to use explicit types rather than the `var`
// keyword. However, var is acceptable to use here because the types are
// obviously Mob and PathFollow2D, since they appear later on the line.
// Create a new instance of the Mob scene.
var mob = (Mob)MobScene.Instance();
// Choose a random location on Path2D.
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
mobSpawnLocation.Offset = GD.Randi();
// Set the mob's direction perpendicular to the path direction.
float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
// Set the mob's position to a random location.
mob.Position = mobSpawnLocation.Position;
// Add some randomness to the direction.
direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
mob.Rotation = direction;
// Choose the velocity.
var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
mob.LinearVelocity = velocity.Rotated(direction);
// Spawn the mob by adding it to the Main scene.
AddChild(mob);
}
// This code goes in `main.cpp`.
void Main::_on_MobTimer_timeout() {
// Create a new instance of the Mob scene.
godot::Node *mob = mob_scene->instance();
// Choose a random location on Path2D.
_mob_spawn_location->set_offset((real_t)_random->randi());
// Set the mob's direction perpendicular to the path direction.
real_t direction = _mob_spawn_location->get_rotation() + (real_t)Math_PI / 2;
// Set the mob's position to a random location.
mob->set("position", _mob_spawn_location->get_position());
// Add some randomness to the direction.
direction += _random->randf_range((real_t)-Math_PI / 4, (real_t)Math_PI / 4);
mob->set("rotation", direction);
// Choose the velocity for the mob.
godot::Vector2 velocity = godot::Vector2(_random->randf_range(150.0, 250.0), 0.0);
mob->set("linear_velocity", velocity.rotated(direction));
// Spawn the mob by adding it to the Main scene.
add_child(mob);
}
Importante
¿Por qué PI
? En las funciones que requieren ángulos, Godot utiliza radianes, no grados. Pi representa media vuelta en radianes, aproximadamente 3.1415
(también existe TAU
que es igual a 2 * PI
). Si estás más cómodo trabajando con grados, necesitarás usar las funciones deg2rad()
y rad2deg()
para convertir entre los dos.
Probando la escena¶
Vamos a probar la escena para asegurarnos de que todo funciona. Añade esta llamada new_game
a _ready()
:
func _ready():
randomize()
new_game()
public override void _Ready()
{
NewGame();
}
// This code goes in `main.cpp`.
void Main::_ready() {
new_game();
}
También asignemos Principal
como nuestra Escena Principal
, la que se ejecuta automáticamente cuando se lanza el juego. Presiona el botón "Play" y selecciona Main.tscn
cuando se te pida.
Truco
Si ya habías establecido otra escena como "Main Scene", puedes hacer clic derecho en Main.tscn
en el dock del Sistema de Archivos y seleccionar "Establecer como escena principal".
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.