aboutsummaryrefslogtreecommitdiff
path: root/game/enemy/EnemyScript.cpp
blob: 8804f5063783b98533adbb23b1472a4998afd7eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "EnemyScript.h"
#include "../Config.h"
#include "../Random.h"
#include "EnemyConfig.h"
#include "api/Color.h"
#include "api/Sprite.h"
#include <crepe/api/AI.h>
#include <crepe/api/Animator.h>
#include <crepe/api/AudioSource.h>
#include <crepe/api/BoxCollider.h>
#include <crepe/api/ParticleEmitter.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Transform.h>
#include <crepe/types.h>
#include <random>
using namespace crepe;
using namespace std;
EnemyScript::EnemyScript() {
	engine.seed(rd());
	this->last_fired = std::chrono::steady_clock::now();
	this->shot_delay = std::chrono::duration<float>(3 + Random::f(1, 0));
}
void EnemyScript::init() {
	Metadata & meta = this->get_component<Metadata>();
	this->subscribe<SpawnEnemyEvent>(
		[this](const SpawnEnemyEvent & e) -> bool { return this->spawn_enemy(e); },
		meta.game_object_id
	);
	this->subscribe<CollisionEvent>([this](const CollisionEvent & e) -> bool {
		return this->on_collide(e);
	});
};
void EnemyScript::fixed_update(duration_t dt) {
	if (this->alive) {
		return;
	}
	Transform & transform = this->get_component<Transform>();
	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>();

	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<Rigidbody>("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();
	}
	//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;
	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);
	uniform_real_distribution<float> dist(
		cam_transform.position.y - half_screen.y + 100,
		cam_transform.position.y + half_screen.y - 100
	);
	float random_height = dist(engine);
	vec2 spawn_location
		= {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, 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();
	}
	//body_animator.play();

	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) {
	RefVector<Transform> bullet_transforms
		= this->get_components_by_tag<Transform>("enemy_bullet");

	for (Transform & bullet_pos : bullet_transforms) {
		if (bullet_pos.position.x == 0 && bullet_pos.position.y == -750) {

			bullet_pos.position = location;
			bullet_pos.position.x -= 20;
			Rigidbody & bullet_body
				= this->get_components_by_id<Rigidbody>(bullet_pos.game_object_id).front();
			BoxCollider bullet_collider
				= this->get_components_by_id<BoxCollider>(bullet_pos.game_object_id).front();
			bullet_collider.active = true;
			bullet_body.active = true;
			AudioSource & audio = this->get_component<AudioSource>();
			audio.play();
			return;
		}
	}
}

void EnemyScript::create_tank() {
	RefVector<Sprite> sprites = this->get_components<Sprite>();
	Sprite & tank_body = sprites[2];
	tank_body.active = true;
}