From 7739a80176cea889ce240d18d354c5174825b25a Mon Sep 17 00:00:00 2001
From: WBoerenkamps <wrj.boerenkamps@student.avans.nl>
Date: Thu, 9 Jan 2025 14:46:30 +0100
Subject: workers during normal sequence working

---
 game/Config.h                       |  1 +
 game/enemy/BattleScript.cpp         | 15 ++++++---
 game/enemy/BattleScript.h           |  2 ++
 game/enemy/EnemyBulletScript.cpp    |  2 +-
 game/enemy/EnemyBulletSubScene.cpp  |  7 ++---
 game/enemy/EnemyScript.cpp          | 61 +++++++++++++++++++++++++++++++------
 game/enemy/EnemyScript.h            | 15 ++++++---
 game/enemy/EnemySubScene.cpp        | 41 +++++++++++++++++++++++++
 game/main.cpp                       |  1 +
 game/player/PlayerScript.cpp        |  6 ++++
 game/player/PlayerScript.h          |  5 ++-
 game/player/PlayerSubScene.cpp      |  7 +++--
 game/scheduler/ObjectsScheduler.cpp | 26 ++++++++++++++--
 13 files changed, 159 insertions(+), 30 deletions(-)

(limited to 'game')

diff --git a/game/Config.h b/game/Config.h
index 8fa41ba..3242a03 100644
--- a/game/Config.h
+++ b/game/Config.h
@@ -34,6 +34,7 @@ static constexpr int COLL_LAY_MISSILE = 8; // Only for GameScene
 static constexpr int COLL_LAY_BULLET = 9; // Only for GameScene
 static constexpr int COLL_LAY_ENEMY = 10; // Only for GameScene
 static constexpr int COLL_LAY_PLAYER_BULLET = 11; // Only for GameScene
+static constexpr int COLL_LAY_PLAYER_SHIELD = 12; // Only for GameScene
 
 static constexpr float GAME_HEIGHT = 800; // In game units
 static constexpr float HALLWAY_HEIGHT = 475; // In game units
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();
diff --git a/game/main.cpp b/game/main.cpp
index 14eec99..a34121e 100644
--- a/game/main.cpp
+++ b/game/main.cpp
@@ -17,6 +17,7 @@ int main() {
 	Config::get_instance() = ENGINE_CONFIG;
 
 	Engine gameloop;
+	
 	gameloop.add_scene<MainMenuScene>();
 	gameloop.add_scene<ShopMenuScene>();
 	gameloop.add_scene<GameScene>();
diff --git a/game/player/PlayerScript.cpp b/game/player/PlayerScript.cpp
index fadca9c..c072e06 100644
--- a/game/player/PlayerScript.cpp
+++ b/game/player/PlayerScript.cpp
@@ -104,6 +104,12 @@ void PlayerScript::fixed_update(crepe::duration_t dt) {
 			last_fired = now;
 		}
 	}
+	if (this->get_key_state(Keycode::P)) {
+		this->trigger_event<BattleStartEvent>(BattleStartEvent{
+			.num_enemies = 4,
+			.battle = true,
+		});
+	}
 	if (this->get_key_state(Keycode::SPACE)) {
 		rb.add_force_linear(vec2(0, -PLAYER_GRAVITY_SCALE / 2.5) * dt.count() / 0.02);
 		if (prev_anim != 1) {
diff --git a/game/player/PlayerScript.h b/game/player/PlayerScript.h
index e7d860a..0fe21d1 100644
--- a/game/player/PlayerScript.h
+++ b/game/player/PlayerScript.h
@@ -15,8 +15,11 @@ private:
 
 private:
 	int prev_anim = 0;
+	bool gravity_mode = true;
+	bool fall_direction = false;
 	std::chrono::time_point<std::chrono::steady_clock> last_fired;
+	std::chrono::time_point<std::chrono::steady_clock> last_switched;
 	std::chrono::duration<float> shot_delay = std::chrono::duration<float>(0.5);
-
+	std::chrono::duration<float> switch_delay = std::chrono::duration<float>(0.01);
 	int current_jetpack_sound = 0;
 };
diff --git a/game/player/PlayerSubScene.cpp b/game/player/PlayerSubScene.cpp
index c4d689a..245f19c 100644
--- a/game/player/PlayerSubScene.cpp
+++ b/game/player/PlayerSubScene.cpp
@@ -22,7 +22,7 @@ using namespace crepe;
 using namespace std;
 
 PlayerSubScene::PlayerSubScene(Scene & scn) {
-	GameObject player = scn.new_object("player", "player", vec2(-100, 200));
+	GameObject player = scn.new_object("player", "player", vec2(200, 200));
 
 	Asset player_bullet {"asset/other_effects/effect_smgbullet.png"};
 	Sprite & player_bullet_sprite = player.add_component<Sprite>(
@@ -106,7 +106,7 @@ PlayerSubScene::PlayerSubScene(Scene & scn) {
 			.looping = true,
 		}
 	);
-	player.add_component<BoxCollider>(vec2(50, 50));
+	player.add_component<BoxCollider>(vec2(35, 35));
 	Asset player_head_asset {"asset/barry/defaultHead.png"};
 	Sprite & player_head_sprite = player.add_component<Sprite>(
 		player_head_asset,
@@ -143,7 +143,7 @@ PlayerSubScene::PlayerSubScene(Scene & scn) {
 			.looping = true,
 		}
 	);
-	player.add_component<BoxCollider>(vec2(40, 60), vec2(-20, 0));
+	player.add_component<BoxCollider>(vec2(40, 50), vec2(-20, 0));
 	player.add_component<Rigidbody>(Rigidbody::Data {
 		.gravity_scale = PLAYER_GRAVITY_SCALE,
 		.body_type = Rigidbody::BodyType::DYNAMIC,
@@ -153,6 +153,7 @@ PlayerSubScene::PlayerSubScene(Scene & scn) {
 		},
 		.collision_layer = COLL_LAY_PLAYER,
 	});
+
 	player.add_component<BehaviorScript>().set_script<PlayerScript>().active = false;
 	player.add_component<BehaviorScript>().set_script<CoinScript>();
 	player.add_component<BehaviorScript>().set_script<PlayerEndScript>().active = false;
diff --git a/game/scheduler/ObjectsScheduler.cpp b/game/scheduler/ObjectsScheduler.cpp
index 3ce2018..a802806 100644
--- a/game/scheduler/ObjectsScheduler.cpp
+++ b/game/scheduler/ObjectsScheduler.cpp
@@ -5,6 +5,7 @@
 #include "../Config.h"
 #include "../Random.h"
 #include "../missile/SpawnEvent.h"
+#include "../enemy/EnemyScript.h"
 #include "api/Rigidbody.h"
 #include "api/Transform.h"
 #include "enemy/BattleScript.h"
@@ -14,15 +15,34 @@ using namespace crepe;
 void ObjectsScheduler::preset_0() {
 	trigger_event<MissileSpawnEvent>(MissileSpawnEvent {});
 	trigger_event<MissileSpawnEvent>(MissileSpawnEvent {});
+	this->trigger_event<BattleStartEvent>(BattleStartEvent {
+		.num_enemies = Random::i(3,1),
+		.battle = false,
+	});
+}
+void ObjectsScheduler::preset_1() { 
+	trigger_event<MissileSpawnEvent>(MissileSpawnEvent {}); 
+	this->trigger_event<BattleStartEvent>(BattleStartEvent {
+		.num_enemies = Random::i(4,1),
+		.battle = false,
+		});
+}
+void ObjectsScheduler::preset_2() { 
+	trigger_event<CreateZapperEvent>(CreateZapperEvent {});
+	this->trigger_event<BattleStartEvent>(BattleStartEvent {
+		.num_enemies = Random::i(2,1),
+		.battle = false,
+		});
 }
-void ObjectsScheduler::preset_1() { trigger_event<MissileSpawnEvent>(MissileSpawnEvent {}); }
-void ObjectsScheduler::preset_2() { trigger_event<CreateZapperEvent>(CreateZapperEvent {}); }
 void ObjectsScheduler::preset_3() { trigger_event<CreateZapperEvent>(CreateZapperEvent {}); }
 void ObjectsScheduler::preset_4() {}
 void ObjectsScheduler::boss_fight_1() {
 	this->get_components_by_name<Rigidbody>("camera").front().get().data.linear_velocity.x = 0;
 	this->get_components_by_name<Rigidbody>("player").front().get().data.linear_velocity.x = 0;
-	this->trigger_event<BattleStartEvent>(BattleStartEvent {.num_enemies = 2});
+	this->trigger_event<BattleStartEvent>(BattleStartEvent {
+		.num_enemies = 7,
+		.battle = true,
+		});
 
 	RefVector<Rigidbody> rb_back_forest
 		= this->get_components_by_tag<Rigidbody>("forest_background");
-- 
cgit v1.2.3