From 7739a80176cea889ce240d18d354c5174825b25a Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 9 Jan 2025 14:46:30 +0100 Subject: workers during normal sequence working --- game/enemy/BattleScript.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'game/enemy/BattleScript.cpp') diff --git a/game/enemy/BattleScript.cpp b/game/enemy/BattleScript.cpp index 6d96ef6..3eda89e 100644 --- a/game/enemy/BattleScript.cpp +++ b/game/enemy/BattleScript.cpp @@ -1,6 +1,7 @@ #include "BattleScript.h" #include "EnemyScript.h" #include +#include #include #include using namespace std; @@ -28,16 +29,23 @@ void BattleScript::fixed_update(duration_t dt) { } if (!enemies_alive) { this->battle_active = false; + cout << "battle won" << endl; this->trigger_event(); } } bool BattleScript::create_battle(const BattleStartEvent & e) { - this->battle_active = true; + this->battle_active = e.battle; + this->spawn_enemies(e.num_enemies); + return false; +} +void BattleScript::spawn_enemies(int amount) { RefVector enemy_scripts = this->get_components_by_tag("enemy"); - std::uniform_real_distribution dist(10, 30); - for (int i = 0; i < e.num_enemies; i++) { + std::uniform_real_distribution dist(70, 150); + + for (int i = 0; i < amount; i++) { BehaviorScript & script = enemy_scripts[i]; + if(script.active == true) continue; script.active = true; this->queue_event( SpawnEnemyEvent { @@ -47,5 +55,4 @@ bool BattleScript::create_battle(const BattleStartEvent & e) { script.game_object_id ); } - return false; } -- cgit v1.2.3 From c1f7b409bd6c883f7e9ff0f81afd3bee47469bdd Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 9 Jan 2025 14:50:28 +0100 Subject: removed iostream --- game/enemy/BattleScript.cpp | 2 -- game/enemy/EnemyScript.cpp | 6 +----- 2 files changed, 1 insertion(+), 7 deletions(-) (limited to 'game/enemy/BattleScript.cpp') diff --git a/game/enemy/BattleScript.cpp b/game/enemy/BattleScript.cpp index 3eda89e..2d1e143 100644 --- a/game/enemy/BattleScript.cpp +++ b/game/enemy/BattleScript.cpp @@ -1,7 +1,6 @@ #include "BattleScript.h" #include "EnemyScript.h" #include -#include #include #include using namespace std; @@ -29,7 +28,6 @@ void BattleScript::fixed_update(duration_t dt) { } if (!enemies_alive) { this->battle_active = false; - cout << "battle won" << endl; this->trigger_event(); } } diff --git a/game/enemy/EnemyScript.cpp b/game/enemy/EnemyScript.cpp index 04d577a..2382847 100644 --- a/game/enemy/EnemyScript.cpp +++ b/game/enemy/EnemyScript.cpp @@ -2,7 +2,6 @@ #include "../Config.h" #include "../Random.h" #include "EnemyConfig.h" -#include #include #include #include @@ -42,17 +41,14 @@ void EnemyScript::fixed_update(duration_t dt) { Rigidbody & enemy_body = this->get_component(); AI & ai_component = this->get_component(); - //transform.position += enemy_body.data.linear_velocity * dt.count(); float direction_to_player_y = player_transform.position.y - transform.position.y; float distance_to_player_y = std::abs(direction_to_player_y); float adjustment_speed = speed * (distance_to_player_y / MAX_DISTANCE); - //cout << "before clamp speed: " << adjustment_speed << endl; adjustment_speed = std::clamp(adjustment_speed, MIN_SPEED, MAX_SPEED); - // Move the path nodes on the Y-axis - //cout << "adjusted_speed: " << adjustment_speed << endl; Rigidbody& player_body = this->get_components_by_tag("player").front(); + // move path nodes for (vec2 & path_node : ai_component.path) { path_node.y += (direction_to_player_y > 0 ? 1 : -1) * adjustment_speed * dt.count(); path_node.x += player_body.data.linear_velocity.x * dt.count(); -- cgit v1.2.3 From b091cb7910fa82e9fb00abdb163d277ba574f804 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 9 Jan 2025 14:54:51 +0100 Subject: make format --- game/enemy/BattleScript.cpp | 4 ++-- game/enemy/EnemyBulletSubScene.cpp | 2 +- game/enemy/EnemyScript.cpp | 28 ++++++++++++---------------- game/enemy/EnemyScript.h | 1 + game/enemy/EnemySubScene.cpp | 4 ++-- game/main.cpp | 2 +- game/scheduler/ObjectsScheduler.cpp | 20 ++++++++++---------- 7 files changed, 29 insertions(+), 32 deletions(-) (limited to 'game/enemy/BattleScript.cpp') diff --git a/game/enemy/BattleScript.cpp b/game/enemy/BattleScript.cpp index 2d1e143..cfffcb3 100644 --- a/game/enemy/BattleScript.cpp +++ b/game/enemy/BattleScript.cpp @@ -40,10 +40,10 @@ void BattleScript::spawn_enemies(int amount) { RefVector enemy_scripts = this->get_components_by_tag("enemy"); std::uniform_real_distribution dist(70, 150); - + for (int i = 0; i < amount; i++) { BehaviorScript & script = enemy_scripts[i]; - if(script.active == true) continue; + if (script.active == true) continue; script.active = true; this->queue_event( SpawnEnemyEvent { diff --git a/game/enemy/EnemyBulletSubScene.cpp b/game/enemy/EnemyBulletSubScene.cpp index eb43f0a..c0e94e0 100644 --- a/game/enemy/EnemyBulletSubScene.cpp +++ b/game/enemy/EnemyBulletSubScene.cpp @@ -29,7 +29,7 @@ int EnemyBulletSubScene::create(Scene & scn, int counter) { .body_type = Rigidbody::BodyType::KINEMATIC, .linear_velocity = vec2 {-400, 0}, .kinematic_collision = false, - .collision_layers = {COLL_LAY_BOT_TOP,COLL_LAY_MISSILE, COLL_LAY_ZAPPER}, + .collision_layers = {COLL_LAY_BOT_TOP, COLL_LAY_MISSILE, COLL_LAY_ZAPPER}, .collision_layer = COLL_LAY_BULLET }); bullet_body.active = false; diff --git a/game/enemy/EnemyScript.cpp b/game/enemy/EnemyScript.cpp index 2382847..8804f50 100644 --- a/game/enemy/EnemyScript.cpp +++ b/game/enemy/EnemyScript.cpp @@ -2,18 +2,17 @@ #include "../Config.h" #include "../Random.h" #include "EnemyConfig.h" +#include "api/Color.h" +#include "api/Sprite.h" #include #include #include #include #include #include -#include #include +#include #include -#include "../Random.h" -#include "api/Color.h" -#include "api/Sprite.h" #include using namespace crepe; using namespace std; @@ -43,11 +42,10 @@ void EnemyScript::fixed_update(duration_t dt) { float direction_to_player_y = player_transform.position.y - transform.position.y; float distance_to_player_y = std::abs(direction_to_player_y); - float adjustment_speed = speed * (distance_to_player_y / MAX_DISTANCE); adjustment_speed = std::clamp(adjustment_speed, MIN_SPEED, MAX_SPEED); - Rigidbody& player_body = this->get_components_by_tag("player").front(); + Rigidbody & player_body = this->get_components_by_tag("player").front(); // move path nodes for (vec2 & path_node : ai_component.path) { path_node.y += (direction_to_player_y > 0 ? 1 : -1) * adjustment_speed * dt.count(); @@ -63,7 +61,7 @@ void EnemyScript::fixed_update(duration_t dt) { } std::chrono::duration elapsed_hit = now - last_hit; //hit blink timer - if(elapsed_hit > blink_time){ + if (elapsed_hit > blink_time) { set_hit_blink(false); } } @@ -92,10 +90,10 @@ bool EnemyScript::spawn_enemy(const SpawnEnemyEvent & e) { return false; } -void EnemyScript::set_hit_blink(bool status){ +void EnemyScript::set_hit_blink(bool status) { RefVector sprites = this->get_components(); - for(Sprite& sprite : sprites){ - if(status){ + for (Sprite & sprite : sprites) { + if (status) { sprite.data.color = Color::RED; continue; } @@ -109,13 +107,12 @@ bool EnemyScript::on_collide(const CollisionEvent & e) { last_hit = std::chrono::steady_clock::now(); //Sprite& sprite; set_hit_blink(true); - } - if(health <= 0){ + if (health <= 0) { this->despawn_enemy(); } //body_animator.play(); - + return false; } @@ -153,9 +150,8 @@ void EnemyScript::shoot(const vec2 & location) { } } -void EnemyScript::create_tank(){ +void EnemyScript::create_tank() { RefVector sprites = this->get_components(); - Sprite& tank_body = sprites[2]; + Sprite & tank_body = sprites[2]; tank_body.active = true; - } diff --git a/game/enemy/EnemyScript.h b/game/enemy/EnemyScript.h index 24799a5..2bd9742 100644 --- a/game/enemy/EnemyScript.h +++ b/game/enemy/EnemyScript.h @@ -20,6 +20,7 @@ public: void create_tank(); void create_soldier(); void set_hit_blink(bool status); + private: std::random_device rd; std::default_random_engine engine; diff --git a/game/enemy/EnemySubScene.cpp b/game/enemy/EnemySubScene.cpp index edc537f..fe9fd63 100644 --- a/game/enemy/EnemySubScene.cpp +++ b/game/enemy/EnemySubScene.cpp @@ -74,7 +74,7 @@ int EnemySubScene::create(Scene & scn, int enemy_counter) { .looping = true, } ); - // tanky body + // tanky body Asset tank_body_asset {"asset/workers/workerFatBody.png"}; enemy.add_component(vec2(50, 50)); Sprite & tank_body_sprite = enemy.add_component( @@ -121,7 +121,7 @@ int EnemySubScene::create(Scene & scn, int enemy_counter) { } ); //gun - Asset enemy_pistol_asset{"asset/workers/gun.png"}; + Asset enemy_pistol_asset {"asset/workers/gun.png"}; Sprite & enemy_pistol_sprite = enemy.add_component( enemy_pistol_asset, Sprite::Data { diff --git a/game/main.cpp b/game/main.cpp index a34121e..751cbe5 100644 --- a/game/main.cpp +++ b/game/main.cpp @@ -17,7 +17,7 @@ int main() { Config::get_instance() = ENGINE_CONFIG; Engine gameloop; - + gameloop.add_scene(); gameloop.add_scene(); gameloop.add_scene(); diff --git a/game/scheduler/ObjectsScheduler.cpp b/game/scheduler/ObjectsScheduler.cpp index a802806..8415ef8 100644 --- a/game/scheduler/ObjectsScheduler.cpp +++ b/game/scheduler/ObjectsScheduler.cpp @@ -4,8 +4,8 @@ #include "../Config.h" #include "../Random.h" -#include "../missile/SpawnEvent.h" #include "../enemy/EnemyScript.h" +#include "../missile/SpawnEvent.h" #include "api/Rigidbody.h" #include "api/Transform.h" #include "enemy/BattleScript.h" @@ -16,23 +16,23 @@ void ObjectsScheduler::preset_0() { trigger_event(MissileSpawnEvent {}); trigger_event(MissileSpawnEvent {}); this->trigger_event(BattleStartEvent { - .num_enemies = Random::i(3,1), + .num_enemies = Random::i(3, 1), .battle = false, }); } -void ObjectsScheduler::preset_1() { - trigger_event(MissileSpawnEvent {}); +void ObjectsScheduler::preset_1() { + trigger_event(MissileSpawnEvent {}); this->trigger_event(BattleStartEvent { - .num_enemies = Random::i(4,1), + .num_enemies = Random::i(4, 1), .battle = false, - }); + }); } -void ObjectsScheduler::preset_2() { +void ObjectsScheduler::preset_2() { trigger_event(CreateZapperEvent {}); this->trigger_event(BattleStartEvent { - .num_enemies = Random::i(2,1), + .num_enemies = Random::i(2, 1), .battle = false, - }); + }); } void ObjectsScheduler::preset_3() { trigger_event(CreateZapperEvent {}); } void ObjectsScheduler::preset_4() {} @@ -42,7 +42,7 @@ void ObjectsScheduler::boss_fight_1() { this->trigger_event(BattleStartEvent { .num_enemies = 7, .battle = true, - }); + }); RefVector rb_back_forest = this->get_components_by_tag("forest_background"); -- cgit v1.2.3 From dc8140877c1ae638285d812035473591a1814ce7 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 9 Jan 2025 16:06:45 +0100 Subject: enemy death working --- game/enemy/BattleScript.cpp | 30 +++++++++------ game/enemy/EnemyPool.h | 2 +- game/enemy/EnemyScript.cpp | 88 ++++++++++++++++++++++++++++++++++---------- game/enemy/EnemyScript.h | 2 + game/enemy/EnemySubScene.cpp | 29 ++------------- 5 files changed, 93 insertions(+), 58 deletions(-) (limited to 'game/enemy/BattleScript.cpp') diff --git a/game/enemy/BattleScript.cpp b/game/enemy/BattleScript.cpp index cfffcb3..f482d5a 100644 --- a/game/enemy/BattleScript.cpp +++ b/game/enemy/BattleScript.cpp @@ -1,5 +1,7 @@ #include "BattleScript.h" #include "EnemyScript.h" +#include "../enemy/EnemyConfig.h" +#include "api/Transform.h" #include #include #include @@ -18,11 +20,11 @@ void BattleScript::init() { void BattleScript::fixed_update(duration_t dt) { if (!battle_active) return; bool enemies_alive = false; - RefVector enemy_scripts - = this->get_components_by_tag("enemy"); + RefVector enemy_ai + = this->get_components_by_tag("enemy"); - for (BehaviorScript & script : enemy_scripts) { - if (script.active) { + for (AI & ai : enemy_ai) { + if (ai.active) { enemies_alive = true; } } @@ -37,20 +39,24 @@ bool BattleScript::create_battle(const BattleStartEvent & e) { return false; } void BattleScript::spawn_enemies(int amount) { - RefVector enemy_scripts - = this->get_components_by_tag("enemy"); + RefVector enemy_ai + = this->get_components_by_tag("enemy"); std::uniform_real_distribution dist(70, 150); - - for (int i = 0; i < amount; i++) { - BehaviorScript & script = enemy_scripts[i]; - if (script.active == true) continue; - script.active = true; + int spawned = 0; + for (int i = 0; i < 7; i++) { + AI& ai = enemy_ai[i]; + Transform& enemy_transform = this->get_components_by_id(ai.game_object_id).front(); + if (ai.active == true || enemy_transform.position != ENEMY_POOL_LOCATION) continue; this->queue_event( SpawnEnemyEvent { .speed = dist(engine), .column = i, }, - script.game_object_id + ai.game_object_id ); + spawned++; + if(spawned >= amount){ + return; + } } } diff --git a/game/enemy/EnemyPool.h b/game/enemy/EnemyPool.h index f4d6765..ec96ac4 100644 --- a/game/enemy/EnemyPool.h +++ b/game/enemy/EnemyPool.h @@ -7,5 +7,5 @@ public: void create_enemies(crepe::Scene & scn); private: - static constexpr int MAXIMUM_AMOUNT = 10; + static constexpr int MAXIMUM_AMOUNT = 7; }; diff --git a/game/enemy/EnemyScript.cpp b/game/enemy/EnemyScript.cpp index 8804f50..c9603ce 100644 --- a/game/enemy/EnemyScript.cpp +++ b/game/enemy/EnemyScript.cpp @@ -1,7 +1,7 @@ #include "EnemyScript.h" #include "../Config.h" #include "../Random.h" -#include "EnemyConfig.h" +#include "../enemy/EnemyConfig.h" #include "api/Color.h" #include "api/Sprite.h" #include @@ -32,10 +32,25 @@ void EnemyScript::init() { }); }; void EnemyScript::fixed_update(duration_t dt) { - if (this->alive) { - return; + if(!spawned) return; + auto now = std::chrono::steady_clock::now(); + std::chrono::duration elapsed_hit = now - last_hit; + //hit blink timer + if (elapsed_hit > blink_time) { + set_hit_blink(false); } Transform & transform = this->get_component(); + if (!this->alive) { + Camera & camera = this->get_components_by_name("camera").front(); + Transform & cam_transform = this->get_components_by_name("camera").front(); + vec2 half_screen = camera.viewport_size / 2; + float x_value = cam_transform.position.x - half_screen.x - 100; + if(transform.position.x < x_value){ + this->despawn_enemy(); + } + return; + } + Transform & player_transform = this->get_components_by_name("player").front(); Rigidbody & enemy_body = this->get_component(); AI & ai_component = this->get_component(); @@ -52,29 +67,41 @@ void EnemyScript::fixed_update(duration_t dt) { path_node.x += player_body.data.linear_velocity.x * dt.count(); } //bullet fire logic: - auto now = std::chrono::steady_clock::now(); + std::chrono::duration elapsed = now - last_fired; if (elapsed > shot_delay) { this->shoot(transform.position); last_fired = now; this->shot_delay = std::chrono::duration(Random::f(4, 1)); } - std::chrono::duration elapsed_hit = now - last_hit; - //hit blink timer - if (elapsed_hit > blink_time) { - set_hit_blink(false); - } + } bool EnemyScript::spawn_enemy(const SpawnEnemyEvent & e) { + this->speed = e.speed; + this->alive = true; + this->spawned = true; + RefVector animators = this->get_components(); + for (Animator & anim : animators) { + anim.active = false; + anim.set_anim(0); + } + RefVector sprites = this->get_components(); + for (Sprite & sprite : sprites) { + sprite.data.position_offset.x = 0; + } + Sprite& jetpack = sprites[2]; + jetpack.data.position_offset.x = 20; + Sprite& gun = sprites[3]; + gun.data.position_offset.x = -20; AI & ai_component = this->get_component(); Transform & transform = this->get_component(); Camera & camera = this->get_components_by_name("camera").front(); Transform & cam_transform = this->get_components_by_name("camera").front(); vec2 half_screen = camera.viewport_size / 2; - float x_value = cam_transform.position.x + half_screen.x - 70 * (1 + e.column); + float x_value = cam_transform.position.x + half_screen.x - 60 * (1 + e.column); uniform_real_distribution dist( cam_transform.position.y - half_screen.y + 100, cam_transform.position.y + half_screen.y - 100 @@ -87,6 +114,7 @@ bool EnemyScript::spawn_enemy(const SpawnEnemyEvent & e) { ai_component.make_oval_path(10, 30, vec2 {x_value, random_height}, 1.5708, true); ai_component.active = true; this->last_fired = std::chrono::steady_clock::now(); + return false; } @@ -102,6 +130,7 @@ void EnemyScript::set_hit_blink(bool status) { } bool EnemyScript::on_collide(const CollisionEvent & e) { + if(!this->alive)return false; if (e.info.other.metadata.tag == "player_bullet") { this->health--; last_hit = std::chrono::steady_clock::now(); @@ -109,23 +138,44 @@ bool EnemyScript::on_collide(const CollisionEvent & e) { set_hit_blink(true); } if (health <= 0) { - this->despawn_enemy(); + this->death(); } //body_animator.play(); return false; } - +void EnemyScript::death(){ + + Rigidbody& rb = this->get_component(); + Transform& tr = this->get_component(); + RefVector animators = this->get_components(); + for (Animator & anim : animators) { + anim.active = false; + anim.set_anim(3); + } + RefVector sprites = this->get_components(); + for (Sprite & sprite : sprites) { + sprite.data.position_offset.x = 15; + } + rb.data.linear_velocity_coefficient = {0.5, 1}; + //rb.add_force_linear(vec2{0,20}); + rb.data.gravity_scale = 20; + tr.rotation = 90; + AI& ai = this->get_component(); + ai.active = false; + this->alive = false; + AI& ai_component = this->get_component(); + ai_component.active = false; +} void EnemyScript::despawn_enemy() { Transform & transform = this->get_component(); - BehaviorScript & enemy_script = this->get_component(); - enemy_script.active = false; - Animator & body_animator = this->get_components().front(); - body_animator.data.col = 2; + Rigidbody& rb = this->get_component(); + rb.data.gravity_scale = 0; + rb.data.linear_velocity = {0,0}; + transform.rotation = 0; transform.position = ENEMY_POOL_LOCATION; - AI & ai_component = this->get_component(); - // Rigidbody& enemy_body - ai_component.active = false; + + this->spawned = false; } void EnemyScript::shoot(const vec2 & location) { diff --git a/game/enemy/EnemyScript.h b/game/enemy/EnemyScript.h index 2bd9742..03343f6 100644 --- a/game/enemy/EnemyScript.h +++ b/game/enemy/EnemyScript.h @@ -17,6 +17,7 @@ public: bool on_collide(const crepe::CollisionEvent & collisionData); void despawn_enemy(); bool spawn_enemy(const SpawnEnemyEvent & e); + void death(); void create_tank(); void create_soldier(); void set_hit_blink(bool status); @@ -25,6 +26,7 @@ private: std::random_device rd; std::default_random_engine engine; bool alive = false; + bool spawned = false; float speed = 50; int health = 2; const float MIN_SPEED = 20; diff --git a/game/enemy/EnemySubScene.cpp b/game/enemy/EnemySubScene.cpp index fe9fd63..8224e2f 100644 --- a/game/enemy/EnemySubScene.cpp +++ b/game/enemy/EnemySubScene.cpp @@ -52,7 +52,7 @@ int EnemySubScene::create(Scene & scn, int enemy_counter) { .looping = false, } ); - enemy_body_sprite.active = false; + enemy_body_sprite.active = true; body_animator.pause(); body_animator.active = true; enemy.add_component(vec2(40, 60), vec2(-20, 0)); @@ -74,30 +74,6 @@ int EnemySubScene::create(Scene & scn, int enemy_counter) { .looping = true, } ); - // tanky body - Asset tank_body_asset {"asset/workers/workerFatBody.png"}; - enemy.add_component(vec2(50, 50)); - Sprite & tank_body_sprite = enemy.add_component( - tank_body_asset, - Sprite::Data { - .flip = {true, false}, - .sorting_in_layer = SORT_IN_LAY_WORKERS_FRONT, - .order_in_layer = 0, - .size = vec2(0, 50), - } - ); - tank_body_sprite.active = true; - Animator & tank_animator = enemy.add_component( - tank_body_sprite, ivec2(32, 32), uvec2(4, 8), - Animator::Data { - .fps = 5, - .col = 1, - .row = 0, - .looping = false, - } - ); - tank_animator.pause(); - tank_animator.active = true; //jetpack enemy.add_component(25, vec2(0, -20)); @@ -135,8 +111,9 @@ int EnemySubScene::create(Scene & scn, int enemy_counter) { enemy.add_component(Asset("asset/sfx/bike_gun_2.ogg")).volume = 0.1; AI & ai_component = enemy.add_component(3000); ai_component.path_follow_on(); + ai_component.active = false; BehaviorScript & enemy_script = enemy.add_component().set_script(); - enemy_script.active = false; + //enemy_script.active = false; return enemy_counter; } -- cgit v1.2.3 From e09e5d3b22f551ef462ba028530dddb6bc578257 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 9 Jan 2025 16:09:08 +0100 Subject: make format --- game/enemy/BattleScript.cpp | 15 +++++++------ game/enemy/EnemyScript.cpp | 51 ++++++++++++++++++++++----------------------- 2 files changed, 32 insertions(+), 34 deletions(-) (limited to 'game/enemy/BattleScript.cpp') diff --git a/game/enemy/BattleScript.cpp b/game/enemy/BattleScript.cpp index f482d5a..7bea79a 100644 --- a/game/enemy/BattleScript.cpp +++ b/game/enemy/BattleScript.cpp @@ -1,6 +1,6 @@ #include "BattleScript.h" -#include "EnemyScript.h" #include "../enemy/EnemyConfig.h" +#include "EnemyScript.h" #include "api/Transform.h" #include #include @@ -20,8 +20,7 @@ void BattleScript::init() { void BattleScript::fixed_update(duration_t dt) { if (!battle_active) return; bool enemies_alive = false; - RefVector enemy_ai - = this->get_components_by_tag("enemy"); + RefVector enemy_ai = this->get_components_by_tag("enemy"); for (AI & ai : enemy_ai) { if (ai.active) { @@ -39,13 +38,13 @@ bool BattleScript::create_battle(const BattleStartEvent & e) { return false; } void BattleScript::spawn_enemies(int amount) { - RefVector enemy_ai - = this->get_components_by_tag("enemy"); + RefVector enemy_ai = this->get_components_by_tag("enemy"); std::uniform_real_distribution dist(70, 150); int spawned = 0; for (int i = 0; i < 7; i++) { - AI& ai = enemy_ai[i]; - Transform& enemy_transform = this->get_components_by_id(ai.game_object_id).front(); + AI & ai = enemy_ai[i]; + Transform & enemy_transform + = this->get_components_by_id(ai.game_object_id).front(); if (ai.active == true || enemy_transform.position != ENEMY_POOL_LOCATION) continue; this->queue_event( SpawnEnemyEvent { @@ -55,7 +54,7 @@ void BattleScript::spawn_enemies(int amount) { ai.game_object_id ); spawned++; - if(spawned >= amount){ + if (spawned >= amount) { return; } } diff --git a/game/enemy/EnemyScript.cpp b/game/enemy/EnemyScript.cpp index 0b1a8b7..c677dac 100644 --- a/game/enemy/EnemyScript.cpp +++ b/game/enemy/EnemyScript.cpp @@ -32,7 +32,7 @@ void EnemyScript::init() { }); }; void EnemyScript::fixed_update(duration_t dt) { - if(!spawned) return; + if (!spawned) return; auto now = std::chrono::steady_clock::now(); std::chrono::duration elapsed_hit = now - last_hit; //hit blink timer @@ -45,12 +45,12 @@ void EnemyScript::fixed_update(duration_t dt) { Transform & cam_transform = this->get_components_by_name("camera").front(); vec2 half_screen = camera.viewport_size / 2; float x_value = cam_transform.position.x - half_screen.x - 100; - if(transform.position.x < x_value){ + if (transform.position.x < x_value) { this->despawn_enemy(); } return; } - + Transform & player_transform = this->get_components_by_name("player").front(); Rigidbody & enemy_body = this->get_component(); AI & ai_component = this->get_component(); @@ -67,33 +67,32 @@ void EnemyScript::fixed_update(duration_t dt) { path_node.x += player_body.data.linear_velocity.x * dt.count(); } //bullet fire logic: - + std::chrono::duration elapsed = now - last_fired; if (elapsed > shot_delay) { this->shoot(transform.position); last_fired = now; this->shot_delay = std::chrono::duration(Random::f(4, 1)); } - } bool EnemyScript::spawn_enemy(const SpawnEnemyEvent & e) { - + this->speed = e.speed; this->alive = true; this->spawned = true; RefVector animators = this->get_components(); - for (Animator & anim : animators) { - anim.active = false; - anim.set_anim(0); - } + for (Animator & anim : animators) { + anim.active = false; + anim.set_anim(0); + } RefVector sprites = this->get_components(); for (Sprite & sprite : sprites) { - sprite.data.position_offset.x = 0; + sprite.data.position_offset.x = 0; } - Sprite& jetpack = sprites[2]; + Sprite & jetpack = sprites[2]; jetpack.data.position_offset.x = 20; - Sprite& gun = sprites[3]; + Sprite & gun = sprites[3]; gun.data.position_offset.x = -20; AI & ai_component = this->get_component(); Transform & transform = this->get_component(); @@ -114,7 +113,7 @@ bool EnemyScript::spawn_enemy(const SpawnEnemyEvent & e) { ai_component.make_oval_path(10, 30, vec2 {x_value, random_height}, 1.5708, true); ai_component.active = true; this->last_fired = std::chrono::steady_clock::now(); - + return false; } @@ -130,7 +129,7 @@ void EnemyScript::set_hit_blink(bool status) { } bool EnemyScript::on_collide(const CollisionEvent & e) { - if(!this->alive)return false; + if (!this->alive) return false; if (e.info.other.metadata.tag == "player_bullet") { this->health--; last_hit = std::chrono::steady_clock::now(); @@ -144,10 +143,10 @@ bool EnemyScript::on_collide(const CollisionEvent & e) { return false; } -void EnemyScript::death(){ - - Rigidbody& rb = this->get_component(); - Transform& tr = this->get_component(); +void EnemyScript::death() { + + Rigidbody & rb = this->get_component(); + Transform & tr = this->get_component(); RefVector animators = this->get_components(); for (Animator & anim : animators) { anim.active = false; @@ -155,26 +154,26 @@ void EnemyScript::death(){ } RefVector sprites = this->get_components(); for (Sprite & sprite : sprites) { - sprite.data.position_offset.x = 15; - } + sprite.data.position_offset.x = 15; + } rb.data.linear_velocity_coefficient = {0.5, 1}; //rb.add_force_linear(vec2{0,20}); rb.data.gravity_scale = 20; tr.rotation = 90; - AI& ai = this->get_component(); + AI & ai = this->get_component(); ai.active = false; this->alive = false; - AI& ai_component = this->get_component(); + AI & ai_component = this->get_component(); ai_component.active = false; } void EnemyScript::despawn_enemy() { Transform & transform = this->get_component(); - Rigidbody& rb = this->get_component(); + Rigidbody & rb = this->get_component(); rb.data.gravity_scale = 0; - rb.data.linear_velocity = {0,0}; + rb.data.linear_velocity = {0, 0}; transform.rotation = 0; transform.position = ENEMY_POOL_LOCATION; - + this->spawned = false; } -- cgit v1.2.3 From cb17c2aad3ca8c89d0f6d8305ba4f3a5c3c2a971 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Fri, 10 Jan 2025 10:19:02 +0100 Subject: added to config enemy_pool_max --- game/enemy/BattleScript.cpp | 4 ++-- game/enemy/EnemyConfig.h | 1 + game/enemy/EnemyPool.cpp | 2 +- game/enemy/EnemyPool.h | 4 +--- 4 files changed, 5 insertions(+), 6 deletions(-) (limited to 'game/enemy/BattleScript.cpp') diff --git a/game/enemy/BattleScript.cpp b/game/enemy/BattleScript.cpp index 7bea79a..798cbb9 100644 --- a/game/enemy/BattleScript.cpp +++ b/game/enemy/BattleScript.cpp @@ -1,5 +1,5 @@ #include "BattleScript.h" -#include "../enemy/EnemyConfig.h" +#include "EnemyConfig.h" #include "EnemyScript.h" #include "api/Transform.h" #include @@ -41,7 +41,7 @@ void BattleScript::spawn_enemies(int amount) { RefVector enemy_ai = this->get_components_by_tag("enemy"); std::uniform_real_distribution dist(70, 150); int spawned = 0; - for (int i = 0; i < 7; i++) { + for (int i = 0; i < ENEMY_POOL_MAX; i++) { AI & ai = enemy_ai[i]; Transform & enemy_transform = this->get_components_by_id(ai.game_object_id).front(); diff --git a/game/enemy/EnemyConfig.h b/game/enemy/EnemyConfig.h index f7b660a..f9fb469 100644 --- a/game/enemy/EnemyConfig.h +++ b/game/enemy/EnemyConfig.h @@ -5,3 +5,4 @@ // static constexpr crepe::vec2 PLAYER_BULLET_POOL_LOCATION = {0, -850}; static constexpr crepe::vec2 ENEMY_BULLET_POOL_LOCATION = {0, -750}; static constexpr crepe::vec2 ENEMY_POOL_LOCATION = {0, -650}; +static constexpr int ENEMY_POOL_MAX = 12; diff --git a/game/enemy/EnemyPool.cpp b/game/enemy/EnemyPool.cpp index a7179bf..135fc35 100644 --- a/game/enemy/EnemyPool.cpp +++ b/game/enemy/EnemyPool.cpp @@ -4,7 +4,7 @@ using namespace std; void EnemyPool::create_enemies(crepe::Scene & scn) { EnemySubScene enemy; int amount = 0; - while (amount < this->MAXIMUM_AMOUNT) { + while (amount < ENEMY_POOL_MAX) { amount = enemy.create(scn, amount); } } diff --git a/game/enemy/EnemyPool.h b/game/enemy/EnemyPool.h index ec96ac4..25f7fb8 100644 --- a/game/enemy/EnemyPool.h +++ b/game/enemy/EnemyPool.h @@ -1,11 +1,9 @@ #pragma once #include - +#include "EnemyConfig.h" class EnemyPool { public: void create_enemies(crepe::Scene & scn); -private: - static constexpr int MAXIMUM_AMOUNT = 7; }; -- cgit v1.2.3