aboutsummaryrefslogtreecommitdiff
path: root/game/prefab
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2025-01-07 14:33:07 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2025-01-07 14:33:07 +0100
commit5d798c30af7026099344a068e91e1684018b4386 (patch)
tree923b54a2745c338478246b8707c6ce8361822fc7 /game/prefab
parent6d69c8ef6b663bd6716b441cc7d01164c7e33dfc (diff)
parent42cbef630ccaf3e841459d364edade1a3c72a525 (diff)
merge + more WIP
Diffstat (limited to 'game/prefab')
-rw-r--r--game/prefab/CMakeLists.txt1
-rw-r--r--game/prefab/ZapperObject.cpp4
-rw-r--r--game/prefab/ZapperObject.h2
-rw-r--r--game/prefab/ZapperPoolScript.cpp33
-rw-r--r--game/prefab/ZapperPoolScript.h14
-rw-r--r--game/prefab/ZapperPoolSubScene.cpp18
-rw-r--r--game/prefab/ZapperPoolSubScene.h8
7 files changed, 75 insertions, 5 deletions
diff --git a/game/prefab/CMakeLists.txt b/game/prefab/CMakeLists.txt
index d77b9b1..6c36ef2 100644
--- a/game/prefab/CMakeLists.txt
+++ b/game/prefab/CMakeLists.txt
@@ -1,5 +1,6 @@
target_sources(main PUBLIC
ZapperObject.cpp
ZapperPoolSubScene.cpp
+ ZapperPoolScript.cpp
)
diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp
index 3c70d04..61681ae 100644
--- a/game/prefab/ZapperObject.cpp
+++ b/game/prefab/ZapperObject.cpp
@@ -80,8 +80,8 @@ ZapperObject::ZapperObject(crepe::GameObject && base)
.collision_layer = COLL_LAY_ZAPPER,
})},
collider {add_component<BoxCollider>(vec2(0, 0))} {
- this->place(this->transform.position, 0, 300);
this->set_active(false);
+ Log::logf(Log::DEBUG, "creating zapper");
}
void ZapperObject::place(const crepe::vec2 & position, float rotation, float length) {
@@ -114,5 +114,7 @@ void ZapperObject::set_active(bool active) {
this->body.active = active;
this->collider.active = active;
+
+ this->active = active;
}
diff --git a/game/prefab/ZapperObject.h b/game/prefab/ZapperObject.h
index 9f4e4a1..42edc51 100644
--- a/game/prefab/ZapperObject.h
+++ b/game/prefab/ZapperObject.h
@@ -11,6 +11,8 @@ public:
ZapperObject(crepe::GameObject &&);
public:
+ bool active = true;
+
struct {
crepe::Sprite & orb_start;
crepe::Sprite & orb_end;
diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp
new file mode 100644
index 0000000..00dd213
--- /dev/null
+++ b/game/prefab/ZapperPoolScript.cpp
@@ -0,0 +1,33 @@
+#include <crepe/api/Camera.h>
+
+#include "../Config.h"
+
+#include "ZapperPoolScript.h"
+#include "ZapperPoolSubScene.h"
+
+using namespace crepe;
+using namespace std;
+
+ZapperPoolScript::ZapperPoolScript(ZapperPoolSubScene & pool) : pool(pool) {}
+
+void ZapperPoolScript::init() {
+ subscribe<CreateZapperEvent>([this](const CreateZapperEvent &) {
+ this->spawn_random();
+ return true;
+ });
+}
+
+void ZapperPoolScript::spawn_random() {
+ vec2 pos = this->get_camera_pos();
+ logf(Log::DEBUG, "Spawning random zappers at {}", pos);
+
+}
+
+vec2 ZapperPoolScript::get_camera_pos() {
+ Transform & transform = get_components_by_name<Transform>(CAMERA_NAME).back();
+ Camera & camera = get_components_by_name<Camera>(CAMERA_NAME).back();
+
+ // right middle edge position
+ return transform.position + vec2(camera.viewport_size.x / 2, 0);
+}
+
diff --git a/game/prefab/ZapperPoolScript.h b/game/prefab/ZapperPoolScript.h
index 79f9a89..b545fbb 100644
--- a/game/prefab/ZapperPoolScript.h
+++ b/game/prefab/ZapperPoolScript.h
@@ -2,7 +2,21 @@
#include <crepe/api/Script.h>
+class ZapperPoolSubScene;
+
class ZapperPoolScript : public crepe::Script {
+public:
+ ZapperPoolScript(ZapperPoolSubScene & pool);
+
+ void init();
+
+private:
+ ZapperPoolSubScene & pool;
+
+private:
+ crepe::vec2 get_camera_pos();
+private:
+ void spawn_random();
};
diff --git a/game/prefab/ZapperPoolSubScene.cpp b/game/prefab/ZapperPoolSubScene.cpp
index 578688a..d7d30ea 100644
--- a/game/prefab/ZapperPoolSubScene.cpp
+++ b/game/prefab/ZapperPoolSubScene.cpp
@@ -1,13 +1,25 @@
+#include <crepe/api/BehaviorScript.h>
+
#include "ZapperPoolSubScene.h"
+#include "ZapperPoolScript.h"
using namespace crepe;
using namespace std;
ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene)
: controller { scene.new_object("controller") } {
- for (size_t i = 0; i < this->POOL_SIZE; i++)
- zappers.emplace_back(scene.new_object("zapper"));
+ this->controller.add_component<BehaviorScript>().set_script<ZapperPoolScript>(*this);
+
+ Log::logf(Log::DEBUG, "Building zapper pool...");
+ for (size_t i = 0; i < this->POOL_SIZE; i++)
+ zappers.emplace_back(scene.new_object("zapper"));
+}
-
+OptionalRef<ZapperObject> ZapperPoolSubScene::get_next_zapper() {
+ for (ZapperObject & zapper : this->zappers) {
+ if (!zapper.active) continue;
+ return zapper;
+ }
+ return {};
}
diff --git a/game/prefab/ZapperPoolSubScene.h b/game/prefab/ZapperPoolSubScene.h
index 79598ce..25328ee 100644
--- a/game/prefab/ZapperPoolSubScene.h
+++ b/game/prefab/ZapperPoolSubScene.h
@@ -4,18 +4,24 @@
#include <crepe/api/Scene.h>
#include <crepe/api/GameObject.h>
+#include <crepe/api/Event.h>
+#include <crepe/util/OptionalRef.h>
#include "ZapperObject.h"
+class CreateZapperEvent : public crepe::Event {};
+
class ZapperPoolSubScene {
public:
ZapperPoolSubScene(crepe::Scene & scene);
-public:
+private:
crepe::GameObject controller;
std::vector<ZapperObject> zappers;
private:
static constexpr size_t POOL_SIZE = 4;
+public:
+ crepe::OptionalRef<ZapperObject> get_next_zapper();
};