4. Inimigos
No editor
Seção intitulada “No editor”-
Crie o objeto
Enemy, com o spriteinimigo. Ele não vai na room. -
Crie a camada de Instâncias
Inimigos, abaixo deTiros, e volte aInstancias. -
Crie o objeto
Spawner, sem sprite, e arraste-o para a roomMain.
function onSpawn(self) self.animSpeed = 0.1 self.vspeed = random(0.8, 1.6) self:onCollide("Bullet", function(bullet) bullet:destroy() self:kill() end)end
function onUpdate(self) if self.y > roomHeight() + 16 then self:destroy() endend
function kill(self) if not self:exists() then return end self:destroy()end#pragma once#include <brasa/brasa.hpp>
class Enemy : public brasa::Entity {public: BRASA_VARS(Enemy)
void onSpawn() override; void onUpdate() override;
void kill();};#include "Enemy.hpp"
using namespace brasa;
void Enemy::onSpawn() { animSpeed = 0.1f; vspeed = random(0.8f, 1.6f); onCollide<Bullet>([this](Bullet& bullet) { bullet.destroy(); kill(); });}
void Enemy::onUpdate() { if (y > static_cast<float>(roomHeight()) + 16.0f) destroy();}
void Enemy::kill() { if (isDestroyed()) return; destroy();}
BRASA_ENTITY(Enemy);randomdá a cada inimigo uma velocidade de queda.onCollideroda quando umBulletencosta: o tiro é destruído e o inimigo morre.killé uma função do próprio objeto. A checagem no começo evita morrer duas vezes no mesmo passo.
Spawner
Seção intitulada “Spawner”function onSpawn(self) self.criados = 0 self:agendar()end
function agendar(self) local intervalo = math.max(15, 60 - self.criados * 2) self:after(intervalo, function() self.criados = self.criados + 1 local margem = 16 spawn("Enemy", random(margem, roomWidth() - margem), -16, "Inimigos") self:agendar() end)end#pragma once#include <brasa/brasa.hpp>
class Spawner : public brasa::Entity {public: BRASA_VARS(Spawner)
void onSpawn() override;
private: void scheduleNext(); int spawned_ = 0;};#include "Spawner.hpp"#include <algorithm>
using namespace brasa;
void Spawner::onSpawn() { scheduleNext(); }
void Spawner::scheduleNext() { const int interval = std::max(15, 60 - spawned_ * 2); after(interval, [this] { ++spawned_; const float margin = 16.0f; spawn<Enemy>(random(margin, static_cast<float>(roomWidth()) - margin), -16.0f, "Inimigos"); scheduleNext(); });}
BRASA_ENTITY(Spawner);O intervalo começa em 60 passos e cai 2 a cada inimigo, até o mínimo de 15. A função cria um inimigo acima da tela e se agenda de novo.
Play. Os inimigos caem cada vez mais rápido e somem com os tiros.
Próxima parte: 5. Pontos e vidas
