aboutsummaryrefslogtreecommitdiff
path: root/game/missile/MissileScript.cpp
blob: 6d0e40e9b8e2be70519444d7b366f055930dcbc1 (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
#include "MissileScript.h"
#include "../Config.h"
#include "api/BehaviorScript.h"

#include <crepe/api/Animator.h>
#include <crepe/api/AudioSource.h>
#include <crepe/api/Transform.h>
#include <crepe/system/CollisionSystem.h>
#include <crepe/types.h>

#include <cmath>
#include <crepe/api/AI.h>
#include <crepe/api/KeyCodes.h>
#include <crepe/api/Sprite.h>

using namespace std;
using namespace crepe;

void MissileScript::init() {
	subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool {
		return this->on_collision(ev);
	});
	this->seeking_disabled = false;
}
void MissileScript::kill_missile() {
	auto animations = this->get_components<Animator>();
	auto sprites = this->get_components<Sprite>();
	auto & fly_sound = this->get_components<AudioSource>().front().get();
	auto & this_script = this->get_components<BehaviorScript>().back().get();

	animations[0].get().active = false;
	animations[1].get().active = false;
	animations[2].get().active = true;
	sprites[0].get().active = false;
	sprites[1].get().active = false;
	sprites[2].get().active = true;

	this_script.active = false;
	this->seeking_disabled = false;

	fly_sound.stop();
}
void MissileScript::activate() {
	auto anim = this->get_components<Animator>();
	auto sprites = this->get_components<Sprite>();

	anim[0].get().active = true;
	anim[1].get().active = true;
	anim[2].get().stop();
	sprites[0].get().active = true;
	sprites[1].get().active = true;
	sprites[2].get().active = false;
}

bool MissileScript::on_collision(const CollisionEvent & ev) {
	auto & explosion_sound = this->get_components<AudioSource>().back().get();

	this->kill_missile();
	explosion_sound.play();

	return false;
}

bool MissileScript::is_in_x_range(const Transform & missile, const Transform & player) {
	return fabs(missile.position.x - player.position.x) <= this->X_RANGE;
}

void MissileScript::fixed_update(crepe::duration_t dt) {
	auto & explosion_anim = this->get_components<Animator>().back().get();
	auto & missile = this->get_component<Transform>();
	auto & m_ai = this->get_component<AI>();

	const auto & player = this->get_components_by_name<Transform>("player").front().get();
	const auto & cam = this->get_components_by_name<Transform>("camera").front().get();
	const auto & velocity = this->get_component<Rigidbody>().data.linear_velocity;

	if (missile.position.x < (cam.position.x - VIEWPORT_X / 1.8)) {
		this->kill_missile();
		return;
	}

	// check if animation is at the end
	if (explosion_anim.data.row == 7) {
		this->activate();
		this->seeking_disabled = false;
	}

	if (this->seeking_disabled) {
		m_ai.seek_off();
	} else {
		m_ai.seek_target = player.position;
		m_ai.seek_on();

		if (is_in_x_range(missile, player)) {
			this->seeking_disabled = true;
			m_ai.seek_off();
		}
	}

	vec2 angle_pos = velocity;
	float angle = atan2(angle_pos.y, angle_pos.x) * (180 / M_PI);

	missile.rotation = angle;
	missile.position += velocity * dt.count();
}