aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2025-01-09 16:06:45 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2025-01-09 16:06:45 +0100
commitdc8140877c1ae638285d812035473591a1814ce7 (patch)
treeb8e2538e98a9b3d461cc54455a62095a48ddb32b
parentb091cb7910fa82e9fb00abdb163d277ba574f804 (diff)
enemy death working
-rw-r--r--game/enemy/BattleScript.cpp30
-rw-r--r--game/enemy/EnemyPool.h2
-rw-r--r--game/enemy/EnemyScript.cpp88
-rw-r--r--game/enemy/EnemyScript.h2
-rw-r--r--game/enemy/EnemySubScene.cpp29
5 files changed, 93 insertions, 58 deletions
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 <crepe/api/AI.h>
#include <crepe/api/BehaviorScript.h>
#include <crepe/api/Metadata.h>
@@ -18,11 +20,11 @@ void BattleScript::init() {
void BattleScript::fixed_update(duration_t dt) {
if (!battle_active) return;
bool enemies_alive = false;
- RefVector<BehaviorScript> enemy_scripts
- = this->get_components_by_tag<BehaviorScript>("enemy");
+ RefVector<AI> enemy_ai
+ = this->get_components_by_tag<AI>("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<BehaviorScript> enemy_scripts
- = this->get_components_by_tag<BehaviorScript>("enemy");
+ RefVector<AI> enemy_ai
+ = this->get_components_by_tag<AI>("enemy");
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;
+ int spawned = 0;
+ for (int i = 0; i < 7; i++) {
+ AI& ai = enemy_ai[i];
+ Transform& enemy_transform = this->get_components_by_id<Transform>(ai.game_object_id).front();
+ if (ai.active == true || enemy_transform.position != ENEMY_POOL_LOCATION) continue;
this->queue_event<SpawnEnemyEvent>(
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 <crepe/api/AI.h>
@@ -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<float> elapsed_hit = now - last_hit;
+ //hit blink timer
+ if (elapsed_hit > blink_time) {
+ set_hit_blink(false);
}
Transform & transform = this->get_component<Transform>();
+ if (!this->alive) {
+ Camera & camera = this->get_components_by_name<Camera>("camera").front();
+ 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 - 100;
+ if(transform.position.x < x_value){
+ this->despawn_enemy();
+ }
+ return;
+ }
+
Transform & player_transform = this->get_components_by_name<Transform>("player").front();
Rigidbody & enemy_body = this->get_component<Rigidbody>();
AI & ai_component = this->get_component<AI>();
@@ -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<float> elapsed = now - last_fired;
if (elapsed > shot_delay) {
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;
+ this->alive = true;
+ this->spawned = true;
+ RefVector<Animator> animators = this->get_components<Animator>();
+ for (Animator & anim : animators) {
+ anim.active = false;
+ anim.set_anim(0);
+ }
+ RefVector<Sprite> sprites = this->get_components<Sprite>();
+ 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<AI>();
Transform & transform = this->get_component<Transform>();
Camera & camera = this->get_components_by_name<Camera>("camera").front();
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 - 70 * (1 + e.column);
+ float x_value = cam_transform.position.x + half_screen.x - 60 * (1 + e.column);
uniform_real_distribution<float> 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<Rigidbody>();
+ Transform& tr = this->get_component<Transform>();
+ RefVector<Animator> animators = this->get_components<Animator>();
+ for (Animator & anim : animators) {
+ anim.active = false;
+ anim.set_anim(3);
+ }
+ RefVector<Sprite> sprites = this->get_components<Sprite>();
+ 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>();
+ ai.active = false;
+ this->alive = false;
+ AI& ai_component = this->get_component<AI>();
+ ai_component.active = 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;
+ Rigidbody& rb = this->get_component<Rigidbody>();
+ 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<AI>();
- // 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<BoxCollider>(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<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));
@@ -135,8 +111,9 @@ int EnemySubScene::create(Scene & scn, int enemy_counter) {
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();
+ ai_component.active = false;
BehaviorScript & enemy_script
= enemy.add_component<BehaviorScript>().set_script<EnemyScript>();
- enemy_script.active = false;
+ //enemy_script.active = false;
return enemy_counter;
}