-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiDirectionalEar.cpp
45 lines (34 loc) · 1.66 KB
/
MultiDirectionalEar.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
#include "MultiDirectionalEar.h"
#include "Environment.h"
#include "Object.h"
MultiDirectionalEar::MultiDirectionalEar() : Sensor("SENSOR_MULTI_DIRECTIONAL_EAR") {
}
MultiDirectionalEar::~MultiDirectionalEar() {
}
void MultiDirectionalEar::handleStimulus(Object *object, Environment *environment, double dt) {
nearestObjects = environment->getNearestObjects(object);
}
void MultiDirectionalEar::renderFeedback(Object *object, Environment *environment) {
if (object == environment->getPlayer()) {
Vector2D windowPos = environment->getWindowPos(object->getPosition());
for (int i = 0; i < nearestObjects.size(); i++) {
Object *nearestObject = nearestObjects[i];
if (nearestObject->getVoice() && nearestObject->getVoiceInterval() <= 0.0) {
nearestObject->resetVoiceInterval();
Vector2D objectWindowPos = environment->getWindowPos(nearestObject->getPosition());
Vector2D relativePos = objectWindowPos.add(windowPos.mult(-1.0));
relativePos = relativePos.normalize();
double pan = relativePos.getX();
double nearestObjectDistance = windowPos.distanceTo(objectWindowPos);
double gain = 1.0 - (nearestObjectDistance / 1000.0);
if (gain < 0.0) {
gain = 0.0;
}
al_play_sample(nearestObject->getVoice(), gain, pan, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
}
}
}
}
Sensor *MultiDirectionalEar::clone() {
return new MultiDirectionalEar();
}