#include "BattleScript.h" #include "EnemyScript.h" #include #include #include using namespace std; using namespace crepe; BattleScript::BattleScript() { engine.seed(rd()); } void BattleScript::init() { std::uniform_int_distribution dist(2, 10); int random_enemy_amount = dist(this->engine); // this->create_battle(random_enemy_amount); this->subscribe([this](const BattleStartEvent & e) -> bool { return this->create_battle(e); }); } void BattleScript::fixed_update(duration_t dt) { if (!battle_active) return; bool enemies_alive = false; RefVector enemy_scripts = this->get_components_by_tag("enemy"); for (BehaviorScript & script : enemy_scripts) { if (script.active) { enemies_alive = true; } } if (!enemies_alive) { this->battle_active = false; this->trigger_event(); } } bool BattleScript::create_battle(const BattleStartEvent & e) { this->battle_active = true; RefVector enemy_scripts = this->get_components_by_tag("enemy"); std::uniform_real_distribution dist(10, 30); for (int i = 0; i < e.num_enemies; i++) { BehaviorScript & script = enemy_scripts[i]; script.active = true; this->queue_event( SpawnEnemyEvent { .speed = dist(engine), .column = i, }, script.game_object_id ); } return false; }