aboutsummaryrefslogtreecommitdiff
path: root/game
diff options
context:
space:
mode:
Diffstat (limited to 'game')
-rw-r--r--game/GameScene.cpp33
-rw-r--r--game/Random.cpp4
-rw-r--r--game/Random.h1
-rw-r--r--game/prefab/ZapperObject.cpp2
-rw-r--r--game/prefab/ZapperPoolScript.cpp12
-rw-r--r--game/prefab/ZapperPoolScript.h3
-rw-r--r--game/prefab/ZapperPoolSubScene.cpp2
7 files changed, 18 insertions, 39 deletions
diff --git a/game/GameScene.cpp b/game/GameScene.cpp
index 8bd6c74..6c21843 100644
--- a/game/GameScene.cpp
+++ b/game/GameScene.cpp
@@ -103,39 +103,6 @@ void GameScene::load_scene() {
Asset boom_audio_asset {"asset/sfx/window_smash.ogg"};
boom_audio.add_component<AudioSource>(boom_audio_asset);
- GameObject laser = new_object("laser", "laser", vec2(2000, 0));
- Asset laser_asset {"asset/obstacles/laser/laserPower.png"};
- Sprite & laser_sprite = laser.add_component<Sprite>(
- laser_asset,
- Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_OBSTACLES,
- .order_in_layer = 0,
- .size = vec2(100, 100),
- }
- );
- laser.add_component<Rigidbody>(Rigidbody::Data {
- .body_type = Rigidbody::BodyType::KINEMATIC,
- .kinematic_collision = false,
- .collision_layer = COLL_LAY_LASER,
- });
- laser.add_component<BoxCollider>(vec2(100, 100));
- GameObject missile = new_object("missile", "missile", vec2(4000, 0));
- Asset missile_asset {"asset/obstacles/missile/missile.png"};
- Sprite & missile_sprite = missile.add_component<Sprite>(
- missile_asset,
- Sprite::Data {
- .sorting_in_layer = SORT_IN_LAY_OBSTACLES,
- .order_in_layer = 0,
- .size = vec2(100, 100),
- }
- );
- missile.add_component<Rigidbody>(Rigidbody::Data {
- .body_type = Rigidbody::BodyType::KINEMATIC,
- .kinematic_collision = false,
- .collision_layer = COLL_LAY_MISSILE,
- });
- missile.add_component<BoxCollider>(vec2(100, 100));
-
EndGameSubScene endgamewindow;
endgamewindow.create(*this);
}
diff --git a/game/Random.cpp b/game/Random.cpp
index 59be3c5..821ddc8 100644
--- a/game/Random.cpp
+++ b/game/Random.cpp
@@ -26,3 +26,7 @@ unsigned Random::u(unsigned upper, unsigned lower) {
return x + lower;
}
+bool Random::b() {
+ return rand() % 2;
+}
+
diff --git a/game/Random.h b/game/Random.h
index cf05e87..8db616c 100644
--- a/game/Random.h
+++ b/game/Random.h
@@ -6,6 +6,7 @@ public:
static double d(double upper = 1.0, double lower = 0.0);
static int i(int upper, int lower = 0);
static unsigned u(unsigned upper, unsigned lower = 0);
+ static bool b();
};
diff --git a/game/prefab/ZapperObject.cpp b/game/prefab/ZapperObject.cpp
index 712eca9..0a290e0 100644
--- a/game/prefab/ZapperObject.cpp
+++ b/game/prefab/ZapperObject.cpp
@@ -100,8 +100,6 @@ void ZapperObject::place(const crepe::vec2 & position, float rotation, float len
this->sprite.beam.data.size.x = length;
this->collider.dimensions = offset.rotate(rotation) * 2 + vec2(30, 30) * SCALE;
-
- this->set_active(true);
}
void ZapperObject::set_active(bool active) {
diff --git a/game/prefab/ZapperPoolScript.cpp b/game/prefab/ZapperPoolScript.cpp
index 812024b..e201149 100644
--- a/game/prefab/ZapperPoolScript.cpp
+++ b/game/prefab/ZapperPoolScript.cpp
@@ -23,10 +23,11 @@ void ZapperPoolScript::init() {
}
void ZapperPoolScript::fixed_update(crepe::duration_t) {
+ float threshold = camera_transform->position.x - camera_camera->viewport_size.x / 2 - OFFSCREEN_MARGIN;
for (ZapperObject & zapper : this->pool) {
if (!zapper.active) continue;
- if (zapper.transform.position.x < camera_transform->position.x)
+ if (zapper.transform.position.x < threshold)
zapper.set_active(false);
}
@@ -40,11 +41,16 @@ void ZapperPoolScript::spawn_random() {
if (!zapper) return; // pool exhausted
vec2 pos = {
- .x = camera_transform->position.x + camera_camera->viewport_size.x / 2,
+ .x = camera_transform->position.x + camera_camera->viewport_size.x / 2 + OFFSCREEN_MARGIN,
.y = Random::f(0.5, -0.5) * HALLWAY_HEIGHT,
};
- zapper->place(pos, 0, Random::f(400, 200));
+ bool horizontal = Random::b();
+ float rotation = 90.0 * horizontal;
+ float length = horizontal ? Random::f(400, 200) : Random::f(200, 50);
+
+ zapper->place(pos, rotation, length);
+ zapper->set_active(true);
}
OptionalRef<ZapperObject> ZapperPoolScript::get_next_zapper() {
diff --git a/game/prefab/ZapperPoolScript.h b/game/prefab/ZapperPoolScript.h
index 5daba79..6aee8b2 100644
--- a/game/prefab/ZapperPoolScript.h
+++ b/game/prefab/ZapperPoolScript.h
@@ -26,5 +26,8 @@ private:
private:
void spawn_random();
+
+private:
+ static constexpr float OFFSCREEN_MARGIN = 40;
};
diff --git a/game/prefab/ZapperPoolSubScene.cpp b/game/prefab/ZapperPoolSubScene.cpp
index 923ed57..e341090 100644
--- a/game/prefab/ZapperPoolSubScene.cpp
+++ b/game/prefab/ZapperPoolSubScene.cpp
@@ -12,7 +12,7 @@ ZapperPoolSubScene::ZapperPoolSubScene(Scene & scene)
Log::logf(Log::DEBUG, "Building zapper pool...");
vector<ZapperObject> pool;
for (size_t i = 0; i < this->POOL_SIZE; i++)
- pool.emplace_back(scene.new_object("zapper"));
+ pool.emplace_back(scene.new_object("zapper", "zapper"));
BehaviorScript & behavior = this->controller.add_component<BehaviorScript>();
behavior.set_script<ZapperPoolScript>(std::move(pool));
}