diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2025-01-09 14:46:30 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2025-01-09 14:46:30 +0100 |
commit | 7739a80176cea889ce240d18d354c5174825b25a (patch) | |
tree | e61974e217791cd86f519bf1c4cfd3b082d55166 /game/enemy | |
parent | 8b32dbc33c434f84b4aab98819147c3b8416ff69 (diff) |
workers during normal sequence working
Diffstat (limited to 'game/enemy')
-rw-r--r-- | game/enemy/BattleScript.cpp | 15 | ||||
-rw-r--r-- | game/enemy/BattleScript.h | 2 | ||||
-rw-r--r-- | game/enemy/EnemyBulletScript.cpp | 2 | ||||
-rw-r--r-- | game/enemy/EnemyBulletSubScene.cpp | 7 | ||||
-rw-r--r-- | game/enemy/EnemyScript.cpp | 61 | ||||
-rw-r--r-- | game/enemy/EnemyScript.h | 15 | ||||
-rw-r--r-- | game/enemy/EnemySubScene.cpp | 41 |
7 files changed, 120 insertions, 23 deletions
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 <crepe/api/AI.h> +#include <iostream> #include <crepe/api/BehaviorScript.h> #include <crepe/api/Metadata.h> 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<BattleWonEvent>(); } } 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<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++) { + std::uniform_real_distribution<float> 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>( SpawnEnemyEvent { @@ -47,5 +55,4 @@ bool BattleScript::create_battle(const BattleStartEvent & e) { script.game_object_id ); } - return false; } diff --git a/game/enemy/BattleScript.h b/game/enemy/BattleScript.h index ddd0be1..57aa16c 100644 --- a/game/enemy/BattleScript.h +++ b/game/enemy/BattleScript.h @@ -9,6 +9,7 @@ struct BattleWonEvent : public crepe::Event {}; struct BattleStartEvent : public crepe::Event { public: int num_enemies = 0; + bool battle = false; }; class BattleScript : public crepe::Script { public: @@ -20,5 +21,6 @@ private: bool battle_active = false; std::random_device rd; std::default_random_engine engine; + void spawn_enemies(int amount); bool create_battle(const BattleStartEvent & e); }; diff --git a/game/enemy/EnemyBulletScript.cpp b/game/enemy/EnemyBulletScript.cpp index 65c0c23..d208a88 100644 --- a/game/enemy/EnemyBulletScript.cpp +++ b/game/enemy/EnemyBulletScript.cpp @@ -18,7 +18,7 @@ void EnemyBulletScript::fixed_update(crepe::duration_t dt) { Transform & cam_transform = this->get_components_by_name<Transform>("camera").front(); Rigidbody & bullet_body = this->get_component<Rigidbody>(); //move - transform.position.x += bullet_body.data.linear_velocity.x * dt.count(); + transform.position += bullet_body.data.linear_velocity * dt.count(); vec2 half_screen = camera.viewport_size / 2; float despawn_location = cam_transform.position.x - half_screen.x - 50; if (transform.position.x < despawn_location) { diff --git a/game/enemy/EnemyBulletSubScene.cpp b/game/enemy/EnemyBulletSubScene.cpp index ad2ca9d..eb43f0a 100644 --- a/game/enemy/EnemyBulletSubScene.cpp +++ b/game/enemy/EnemyBulletSubScene.cpp @@ -27,14 +27,13 @@ int EnemyBulletSubScene::create(Scene & scn, int counter) { Rigidbody & bullet_body = bullet.add_component<Rigidbody>(Rigidbody::Data { .gravity_scale = 0, .body_type = Rigidbody::BodyType::KINEMATIC, - - .linear_velocity = vec2 {-250, 0}, + .linear_velocity = vec2 {-400, 0}, .kinematic_collision = false, - .collision_layers = {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; - BoxCollider & bullet_collider = bullet.add_component<BoxCollider>(vec2(60, 30)); + BoxCollider & bullet_collider = bullet.add_component<BoxCollider>(vec2(40, 10)); //bullet_collider.active = false; Asset bullet_asset {"asset/other_effects/effect_smgbullet_x2.png"}; Sprite & bullet_sprite = bullet.add_component<Sprite>( diff --git a/game/enemy/EnemyScript.cpp b/game/enemy/EnemyScript.cpp index 5c03539..04d577a 100644 --- a/game/enemy/EnemyScript.cpp +++ b/game/enemy/EnemyScript.cpp @@ -2,6 +2,7 @@ #include "../Config.h" #include "../Random.h" #include "EnemyConfig.h" +#include <iostream> #include <crepe/api/AI.h> #include <crepe/api/Animator.h> #include <crepe/api/AudioSource.h> @@ -9,7 +10,11 @@ #include <crepe/api/ParticleEmitter.h> #include <crepe/api/Rigidbody.h> #include <crepe/api/Transform.h> +#include <crepe/api/Sprite.h> #include <crepe/types.h> +#include "../Random.h" +#include "api/Color.h" +#include "api/Sprite.h" #include <random> using namespace crepe; using namespace std; @@ -40,23 +45,33 @@ void EnemyScript::fixed_update(duration_t dt) { //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<Rigidbody>("player").front(); 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(); } //bullet fire logic: auto now = std::chrono::steady_clock::now(); std::chrono::duration<float> elapsed = now - last_fired; if (elapsed > shot_delay) { - this->shoot(transform.position, 0); + this->shoot(transform.position); last_fired = now; this->shot_delay = std::chrono::duration<float>(Random::f(4, 1)); } + std::chrono::duration<float> 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; AI & ai_component = this->get_component<AI>(); @@ -65,7 +80,7 @@ bool EnemyScript::spawn_enemy(const SpawnEnemyEvent & e) { Transform & cam_transform = this->get_components_by_name<Transform>("camera").front(); vec2 half_screen = camera.viewport_size / 2; - float x_value = cam_transform.position.x + half_screen.x - 50 * (1 + e.column); + float x_value = cam_transform.position.x + half_screen.x - 70 * (1 + e.column); uniform_real_distribution<float> dist( cam_transform.position.y - half_screen.y + 100, cam_transform.position.y + half_screen.y - 100 @@ -75,31 +90,52 @@ bool EnemyScript::spawn_enemy(const SpawnEnemyEvent & e) { = {cam_transform.position.x + camera.viewport_size.x / 2 + 100, random_height}; transform.position = spawn_location; ai_component.path.clear(); - ai_component.make_oval_path(10, 10, vec2 {x_value, random_height}, 1.5708, true); + 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; } +void EnemyScript::set_hit_blink(bool status){ + RefVector<Sprite> sprites = this->get_components<Sprite>(); + for(Sprite& sprite : sprites){ + if(status){ + sprite.data.color = Color::RED; + continue; + } + sprite.data.color = Color::WHITE; + } +} + bool EnemyScript::on_collide(const CollisionEvent & e) { if (e.info.other.metadata.tag == "player_bullet") { + this->health--; + last_hit = std::chrono::steady_clock::now(); + //Sprite& sprite; + set_hit_blink(true); + + } + if(health <= 0){ this->despawn_enemy(); } - Animator & body_animator = this->get_components<Animator>().front(); - body_animator.data.col = 2; //body_animator.play(); - BehaviorScript & enemy_script = this->get_component<BehaviorScript>(); - enemy_script.active = false; + return false; } + void EnemyScript::despawn_enemy() { Transform & transform = this->get_component<Transform>(); + BehaviorScript & enemy_script = this->get_component<BehaviorScript>(); + enemy_script.active = false; + Animator & body_animator = this->get_components<Animator>().front(); + body_animator.data.col = 2; transform.position = ENEMY_POOL_LOCATION; AI & ai_component = this->get_component<AI>(); // Rigidbody& enemy_body ai_component.active = false; } -void EnemyScript::shoot(const vec2 & location, float angle) { + +void EnemyScript::shoot(const vec2 & location) { RefVector<Transform> bullet_transforms = this->get_components_by_tag<Transform>("enemy_bullet"); @@ -120,3 +156,10 @@ void EnemyScript::shoot(const vec2 & location, float angle) { } } } + +void EnemyScript::create_tank(){ + RefVector<Sprite> sprites = this->get_components<Sprite>(); + Sprite& tank_body = sprites[2]; + tank_body.active = true; + +} diff --git a/game/enemy/EnemyScript.h b/game/enemy/EnemyScript.h index 42ecac4..24799a5 100644 --- a/game/enemy/EnemyScript.h +++ b/game/enemy/EnemyScript.h @@ -13,19 +13,24 @@ public: EnemyScript(); void init() override; void fixed_update(crepe::duration_t dt) override; - void shoot(const crepe::vec2 & position, float angle); + void shoot(const crepe::vec2 & position); bool on_collide(const crepe::CollisionEvent & collisionData); void despawn_enemy(); bool spawn_enemy(const SpawnEnemyEvent & e); - + void create_tank(); + void create_soldier(); + void set_hit_blink(bool status); private: std::random_device rd; std::default_random_engine engine; bool alive = false; float speed = 50; - const float MIN_SPEED = 10; - const float MAX_SPEED = 130; - const float MAX_DISTANCE = 100; + int health = 2; + const float MIN_SPEED = 20; + const float MAX_SPEED = 150; + const float MAX_DISTANCE = 200; std::chrono::time_point<std::chrono::steady_clock> last_fired; + std::chrono::time_point<std::chrono::steady_clock> last_hit; std::chrono::duration<float> shot_delay = std::chrono::duration<float>(0); + std::chrono::duration<float> blink_time = std::chrono::duration<float>(0.1); }; diff --git a/game/enemy/EnemySubScene.cpp b/game/enemy/EnemySubScene.cpp index 607b9a9..edc537f 100644 --- a/game/enemy/EnemySubScene.cpp +++ b/game/enemy/EnemySubScene.cpp @@ -31,6 +31,7 @@ int EnemySubScene::create(Scene & scn, int enemy_counter) { .collision_layer = COLL_LAY_ENEMY, }); + // normal body Asset enemy_body_asset {"asset/workers/worker2Body.png"}; enemy.add_component<BoxCollider>(vec2(50, 50)); Sprite & enemy_body_sprite = enemy.add_component<Sprite>( @@ -51,7 +52,9 @@ int EnemySubScene::create(Scene & scn, int enemy_counter) { .looping = false, } ); + enemy_body_sprite.active = false; body_animator.pause(); + body_animator.active = true; enemy.add_component<BoxCollider>(vec2(40, 60), vec2(-20, 0)); Asset enemy_head_asset {"asset/workers/worker2Head.png"}; Sprite & enemy_head_sprite = enemy.add_component<Sprite>( @@ -71,6 +74,32 @@ int EnemySubScene::create(Scene & scn, int enemy_counter) { .looping = true, } ); + // tanky body + Asset tank_body_asset {"asset/workers/workerFatBody.png"}; + enemy.add_component<BoxCollider>(vec2(50, 50)); + Sprite & tank_body_sprite = enemy.add_component<Sprite>( + 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<Animator>( + 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<CircleCollider>(25, vec2(0, -20)); Asset enemy_jetpack_asset {"asset/barry/jetpackDefault.png"}; Sprite & enemy_jetpack_sprite = enemy.add_component<Sprite>( @@ -91,6 +120,18 @@ int EnemySubScene::create(Scene & scn, int enemy_counter) { .looping = true, } ); + //gun + Asset enemy_pistol_asset{"asset/workers/gun.png"}; + Sprite & enemy_pistol_sprite = enemy.add_component<Sprite>( + enemy_pistol_asset, + Sprite::Data { + .flip = {false, false}, + .sorting_in_layer = SORT_IN_LAY_WORKERS_FRONT, + .order_in_layer = 2, + .size = vec2(0, 20), + .position_offset = vec2(-20, 0), + } + ); enemy.add_component<AudioSource>(Asset("asset/sfx/bike_gun_2.ogg")).volume = 0.1; AI & ai_component = enemy.add_component<AI>(3000); ai_component.path_follow_on(); |