forked from agateau/pixelwheels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioComponent.java
More file actions
114 lines (102 loc) · 3.95 KB
/
Copy pathAudioComponent.java
File metadata and controls
114 lines (102 loc) · 3.95 KB
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
/*
* Copyright 2018 Aurélien Gâteau <mail@agateau.com>
*
* This file is part of Pixel Wheels.
*
* Tiny Wheels is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.agateau.tinywheels;
import com.agateau.tinywheels.sound.AudioClipper;
import com.agateau.tinywheels.sound.AudioManager;
import com.agateau.tinywheels.sound.EngineSoundPlayer;
import com.agateau.tinywheels.sound.SoundPlayer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
/**
* A component to play the racer audio
*/
class AudioComponent implements Racer.Component, Disposable {
private static final float FULL_VOLUME_DRIFT_DURATION = 0.6f;
private static final float MIN_IMPACT_SPEED = 3;
private final EngineSoundPlayer mEngineSoundPlayer;
private final Racer mRacer;
private final SoundPlayer mDriftingSoundPlayer;
private final SoundPlayer mTurboSoundPlayer;
private final SoundPlayer mCollisionSoundPlayer;
private final Array<SoundPlayer> mSoundPlayers = new Array<SoundPlayer>();
private float mDriftDuration = 0;
private boolean mTurboTriggered = false;
private boolean mJustCollided = false;
public AudioComponent(SoundAtlas atlas, AudioManager audioManager, Racer racer) {
mEngineSoundPlayer = new EngineSoundPlayer(atlas, audioManager);
mDriftingSoundPlayer = audioManager.createSoundPlayer(atlas.get("drifting"));
mTurboSoundPlayer = audioManager.createSoundPlayer(atlas.get("turbo"));
mCollisionSoundPlayer = audioManager.createSoundPlayer(atlas.get("collision"));
mSoundPlayers.addAll(mDriftingSoundPlayer, mTurboSoundPlayer, mCollisionSoundPlayer);
mRacer = racer;
}
@Override
public void act(float delta) {
if (mRacer.getVehicle().isDrifting()) {
mDriftDuration += delta;
} else {
mDriftDuration = 0;
}
}
public void render(AudioClipper clipper) {
float speed = mRacer.getVehicle().getSpeed();
float normSpeed = MathUtils.clamp(speed / 50, 0, 1);
float maxVolume = clipper.clip(mRacer);
mEngineSoundPlayer.play(normSpeed, maxVolume);
if (mDriftDuration > 0) {
float volume = MathUtils.clamp(mDriftDuration / FULL_VOLUME_DRIFT_DURATION, 0f, 1f) * maxVolume;
mDriftingSoundPlayer.setVolume(volume);
if (!mDriftingSoundPlayer.isLooping()) {
mDriftingSoundPlayer.loop();
}
} else {
mDriftingSoundPlayer.stop();
}
if (mTurboTriggered) {
mTurboSoundPlayer.setVolume(maxVolume);
mTurboSoundPlayer.play();
mTurboTriggered = false;
}
if (mJustCollided) {
mCollisionSoundPlayer.setVolume(maxVolume);
if (!mCollisionSoundPlayer.isLooping()) {
mCollisionSoundPlayer.loop();
}
mJustCollided = false;
} else {
mCollisionSoundPlayer.stop();
}
}
public void triggerTurbo() {
mTurboTriggered = true;
}
public void onCollision() {
if (mRacer.getVehicle().getSpeed() > MIN_IMPACT_SPEED) {
mJustCollided = true;
}
}
@Override
public void dispose() {
mEngineSoundPlayer.stop();
for (SoundPlayer soundPlayer : mSoundPlayers) {
soundPlayer.stop();
}
}
}