aboutsummaryrefslogtreecommitdiff
path: root/game/missile/SpawnEvent.cpp
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2025-01-08 14:10:27 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2025-01-08 14:10:27 +0100
commitb445a1716a46dc875e0b2180c1a1b6022ec7a6d3 (patch)
tree1395518417ef02ca8adfa961e0734ea7aeb71a6d /game/missile/SpawnEvent.cpp
parentc9c9702edc58ff8f40b13dc6b86b216421f79e9b (diff)
missile/preview/schedular/PreviewScene
Diffstat (limited to 'game/missile/SpawnEvent.cpp')
-rw-r--r--game/missile/SpawnEvent.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/game/missile/SpawnEvent.cpp b/game/missile/SpawnEvent.cpp
new file mode 100644
index 0000000..03a9b8c
--- /dev/null
+++ b/game/missile/SpawnEvent.cpp
@@ -0,0 +1,47 @@
+#include "SpawnEvent.h"
+
+#include <crepe/api/Animator.h>
+#include <crepe/api/AudioSource.h>
+#include <crepe/api/BehaviorScript.h>
+#include <crepe/api/Camera.h>
+#include <crepe/api/Sprite.h>
+#include <crepe/api/Transform.h>
+
+#include <cstdlib>
+#include <random>
+
+using namespace crepe;
+
+void MissileSpawnEventHandler::init() {
+ subscribe<MissileSpawnEvent>([this](const MissileSpawnEvent & ev) -> bool {
+ return this->on_event(ev);
+ });
+}
+
+std::random_device rd;
+std::mt19937 gen(rd());
+
+bool MissileSpawnEventHandler::on_event(const MissileSpawnEvent & event) {
+ auto missile_sprites = this->get_components_by_name<Sprite>("missile");
+ auto missile_transforms = this->get_components_by_name<Transform>("missile");
+ auto missile_behaviorscripts = this->get_components_by_name<BehaviorScript>("missile");
+ auto missile_audiosources = this->get_components_by_name<AudioSource>("missile");
+ auto & camera_transform = this->get_components_by_name<Transform>("camera").front().get();
+
+ for (size_t i = 0; i < missile_behaviorscripts.size(); ++i) {
+ auto & script = missile_behaviorscripts[i].get();
+ if (script.active) continue;
+ script.active = true;
+
+ missile_audiosources[i * 2].get().play();
+
+ auto & transform = missile_transforms[i].get();
+ transform.position.x = camera_transform.position.x + this->MISSILE_OFFSET;
+ std::uniform_int_distribution<> dist(this->MIN_RANGE, this->MAX_RANGE);
+ transform.position.y = dist(gen);
+
+ break;
+ }
+
+ return false;
+}