Up to date
This page is up to date for Godot 4.2
.
If you still find outdated information, please open an issue.
Criando o inimigo¶
Agora é hora de criarmos os inimigos de que nosso jogador terá que se desviar. Seu comportamento não será tão complexo: inimigos irão nascer aleatoriamente nos cantos da tela, escolher uma direção aleatória e irão se mover em linha reta.
Nós iremos construir uma cena Inimigo
, que poderemos então instanciar para criar uma quantidade de inimigos independentes no jogo.
Configuração de nós¶
Click Scene -> New Scene from the top menu and add the following nodes:
RigidBody2D (chamado
Turba
)
Não se esqueça de configurar os filhos para que não possam ser selecionados, assim como você fez na cena Jogador.
Select the Mob
node and set it's Gravity Scale
property in the RigidBody2D
section of the inspector to 0
.
This will prevent the mob from falling downwards.
In addition, under the CollisionObject2D
section just beneath the RigidBody2D section,
expand the Collision group and
uncheck the 1
inside the Mask
property.
This will ensure the mobs do not collide with each other.
Set up the AnimatedSprite2D like you did for the
player. This time, we have 3 animations: fly
, swim
, and walk
. There
are two images for each animation in the art folder.
The Animation Speed
property has to be set for each individual animation. Adjust it to 3
for all 3 animations.
You can use the "Play Animation" buttons on the right of the Animation Speed
input field to preview your animations.
Vamos selecionar uma das animações aleatoriamente para que os inimigos tenham alguma variedade.
Like the player images, these mob images need to be scaled down. Set the
AnimatedSprite2D
's Scale
property to (0.75, 0.75)
.
As in the Player
scene, add a CapsuleShape2D
for the collision. To align
the shape with the image, you'll need to set the Rotation
property
to 90
(under "Transform" in the Inspector).
Salve a cena.
Script do inimigo¶
Adicione um script à ``Turba``assim:
extends RigidBody2D
using Godot;
public partial class Mob : RigidBody2D
{
// Don't forget to rebuild the project.
}
Agora, vamos ver o resto do script. Em ``_ready () ``, rodamos a animação e escolhemos aleatoriamente um dos três tipos de animação:
func _ready():
var mob_types = $AnimatedSprite2D.sprite_frames.get_animation_names()
$AnimatedSprite2D.play(mob_types[randi() % mob_types.size()])
public override void _Ready()
{
var animatedSprite2D = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
string[] mobTypes = animatedSprite2D.SpriteFrames.GetAnimationNames();
animatedSprite2D.Play(mobTypes[GD.Randi() % mobTypes.Length]);
}
First, we get the list of animation names from the AnimatedSprite2D's sprite_frames
property. This returns an Array containing all three animation names: ["walk",
"swim", "fly"]
.
Nós precisamos, então, de um número aleatório entre 0
e 2
para selecionar um dos nomes da lista (os índices de um vetor começam do 0
). randi() % n``seleciona aleatoriamente um inteiro entre ``0
e n-1
.
The last piece is to make the mobs delete themselves when they leave the screen.
Connect the screen_exited()
signal of the VisibleOnScreenNotifier2D
node
to the Mob
and add this code:
func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free()
private void OnVisibleOnScreenNotifier2DScreenExited()
{
QueueFree();
}
Isto completa a cena Turba.
Com o jogador e os inimigos prontos, na próxima parte os juntaremos em uma nova cena. Faremos inimigos aparecerem aleatoriamente na área do jogo, moverem-se adiante, tornando nosso projeto um jogo de fato.