diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-11 21:32:30 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-11 21:32:30 +0100 |
commit | a6803980f1e74ecf1abb007b7c77f00d2cd92c43 (patch) | |
tree | 425ca961b27117d6e5d5fa0ae5cfca93351e0b33 /game/enemy/BattleScript.cpp | |
parent | 6bc0025e4c24ed6659d993f3469c10615fb0e273 (diff) | |
parent | 525636bb2158ecea68ebb9d6b8d2dc722524c5e5 (diff) |
merge master into loek/doxygen
Diffstat (limited to 'game/enemy/BattleScript.cpp')
-rw-r--r-- | game/enemy/BattleScript.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/game/enemy/BattleScript.cpp b/game/enemy/BattleScript.cpp new file mode 100644 index 0000000..798cbb9 --- /dev/null +++ b/game/enemy/BattleScript.cpp @@ -0,0 +1,61 @@ +#include "BattleScript.h" +#include "EnemyConfig.h" +#include "EnemyScript.h" +#include "api/Transform.h" +#include <crepe/api/AI.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/Metadata.h> +using namespace std; +using namespace crepe; + +BattleScript::BattleScript() { engine.seed(rd()); } +void BattleScript::init() { + std::uniform_int_distribution<int> dist(2, 10); + int random_enemy_amount = dist(this->engine); + // this->create_battle(random_enemy_amount); + this->subscribe<BattleStartEvent>([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<AI> enemy_ai = this->get_components_by_tag<AI>("enemy"); + + for (AI & ai : enemy_ai) { + if (ai.active) { + enemies_alive = true; + } + } + if (!enemies_alive) { + this->battle_active = false; + this->trigger_event<BattleWonEvent>(); + } +} +bool BattleScript::create_battle(const BattleStartEvent & e) { + this->battle_active = e.battle; + this->spawn_enemies(e.num_enemies); + return false; +} +void BattleScript::spawn_enemies(int amount) { + RefVector<AI> enemy_ai = this->get_components_by_tag<AI>("enemy"); + std::uniform_real_distribution<float> dist(70, 150); + int spawned = 0; + for (int i = 0; i < ENEMY_POOL_MAX; i++) { + AI & ai = enemy_ai[i]; + Transform & enemy_transform + = this->get_components_by_id<Transform>(ai.game_object_id).front(); + if (ai.active == true || enemy_transform.position != ENEMY_POOL_LOCATION) continue; + this->queue_event<SpawnEnemyEvent>( + SpawnEnemyEvent { + .speed = dist(engine), + .column = i, + }, + ai.game_object_id + ); + spawned++; + if (spawned >= amount) { + return; + } + } +} |