-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.cpp
113 lines (90 loc) · 3.42 KB
/
car.cpp
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
#include "car.hpp"
#include "util.hpp"
#include <cmath>
Car::Car(cAudio::IAudioSource* audio_source,
cAudio::IAudioSource* honk_audio_source,
cAudio::IAudioSource* swear_audio_source) {
this->audio_source = audio_source;
this->honk_audio_source = honk_audio_source;
this->swear_audio_source = swear_audio_source;
}
void Car::update_position(float dt) {
this->pos += this->velocity * dt;
this->audio_source->move(util::sf_to_caudio_vect(this->pos));
this->audio_source->setVelocity(
util::sf_to_caudio_vect(this->velocity));
this->honk_audio_source->move(util::sf_to_caudio_vect(this->pos));
this->honk_audio_source->setVelocity(
util::sf_to_caudio_vect(this->velocity));
this->swear_audio_source->move(util::sf_to_caudio_vect(this->pos));
this->swear_audio_source->setVelocity(
util::sf_to_caudio_vect(this->velocity));
}
sf::Vector2<float> Car::get_position() const {
return this->pos;
}
void Car::honk_if_close_to(sf::Vector2<float> pos, float distance) {
play_if_close_to(pos, distance, this->honk_audio_source, HONK_STRENGTH);
}
void Car::swear_if_close_to(sf::Vector2<float> pos, float distance) {
play_if_close_to(pos, distance, this->swear_audio_source,
SWEAR_STRENGTH);
}
void Car::play_if_close_to(sf::Vector2<float> pos,
float distance, cAudio::IAudioSource* sound, float strength) {
float dist = util::distance(pos, this->pos);
// we assume the car only goes straight in the x or y direction
float p;
bool in_front;
if (this->velocity.x == 0) {
// The car is moving along the y direction
if (this->pos.y > pos.y) {
// the car is above pos
// pos is in front if the car is traveling down
in_front = this->velocity.y < 0;
} else {
// the car is below pos
// pos is in front if the car is traveling up
in_front = this->velocity.y > 0;
}
p = fabs(this->pos.x - pos.x);
} else {
// The car is moving along the x direction
if (this->pos.x > pos.x) {
// the car is above pos
// pos is in front if the car is traveling down
in_front = this->velocity.x < 0;
} else {
// the car is below pos
// pos is in front if the car is traveling up
in_front = this->velocity.x > 0;
}
p = fabs(this->pos.y - pos.y);
}
if (in_front && p < CAR_WIDTH && dist < distance && !sound->isPlaying()) {
sound->play3d(util::sf_to_caudio_vect(this->pos), strength, false);
}
}
bool Car::collides_with(sf::Vector2<float> pos) const {
sf::Vector2<float> p_pos = pos - this->pos;
return sqrt(p_pos.x * p_pos.x + p_pos.y * p_pos.y) < CAR_WIDTH;
}
void Car::start(sf::Vector2<float> pos, sf::Vector2<float> velocity) {
this->pos = pos;
this->velocity = velocity;
this->audio_source->play3d(
util::sf_to_caudio_vect(this->pos), ENGINE_NOISE_STRENGTH, true);
}
void Car::stop() {
this->audio_source->stop();
}
bool Car::out_of_bounds(int width, int height) const {
return pos.x < -width || pos.y < -width ||
pos.x > width || pos.y > height;
}
int Car::get_road_index() const {
return this->road_index;
}
void Car::set_road_index(int index) {
this->road_index = index;
}