aboutsummaryrefslogtreecommitdiff
path: root/game/enemy/BattleScript.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'game/enemy/BattleScript.cpp')
-rw-r--r--game/enemy/BattleScript.cpp36
1 files changed, 23 insertions, 13 deletions
diff --git a/game/enemy/BattleScript.cpp b/game/enemy/BattleScript.cpp
index 6d96ef6..798cbb9 100644
--- a/game/enemy/BattleScript.cpp
+++ b/game/enemy/BattleScript.cpp
@@ -1,5 +1,7 @@
#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>
@@ -18,11 +20,10 @@ void BattleScript::init() {
void BattleScript::fixed_update(duration_t dt) {
if (!battle_active) return;
bool enemies_alive = false;
- RefVector<BehaviorScript> enemy_scripts
- = this->get_components_by_tag<BehaviorScript>("enemy");
+ RefVector<AI> enemy_ai = this->get_components_by_tag<AI>("enemy");
- for (BehaviorScript & script : enemy_scripts) {
- if (script.active) {
+ for (AI & ai : enemy_ai) {
+ if (ai.active) {
enemies_alive = true;
}
}
@@ -32,20 +33,29 @@ void BattleScript::fixed_update(duration_t dt) {
}
}
bool BattleScript::create_battle(const BattleStartEvent & e) {
- this->battle_active = true;
- RefVector<BehaviorScript> enemy_scripts
- = this->get_components_by_tag<BehaviorScript>("enemy");
- std::uniform_real_distribution<float> dist(10, 30);
- for (int i = 0; i < e.num_enemies; i++) {
- BehaviorScript & script = enemy_scripts[i];
- script.active = true;
+ 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,
},
- script.game_object_id
+ ai.game_object_id
);
+ spawned++;
+ if (spawned >= amount) {
+ return;
+ }
}
- return false;
}