basic sounds

This commit is contained in:
RandomityGuy 2021-06-30 15:08:39 +05:30
parent 23a435328d
commit eb59030a76
19 changed files with 185 additions and 45 deletions

30
src/AudioManager.hx Normal file
View file

@ -0,0 +1,30 @@
package src;
import hxd.snd.effect.Spatialization;
import h3d.Vector;
import hxd.res.Sound;
import src.Settings;
import hxd.snd.ChannelGroup;
class AudioManager {
static var manager:hxd.snd.Manager;
static var soundChannel:hxd.snd.ChannelGroup;
static var musicChannel:hxd.snd.ChannelGroup;
public static function init() {
AudioManager.manager = hxd.snd.Manager.get();
AudioManager.soundChannel = new ChannelGroup("sound");
soundChannel.volume = Settings.optionsSettings.soundVolume;
AudioManager.musicChannel = new ChannelGroup("music");
musicChannel.volume = Settings.optionsSettings.musicVolume;
}
public static function playSound(sound:Sound, ?position:Vector) {
AudioManager.manager.play(sound, soundChannel);
if (position != null) {
var audioSrc = new Spatialization();
audioSrc.position = position;
soundChannel.addEffect(audioSrc);
}
}
}

View file

@ -349,16 +349,17 @@ class DtsObject extends GameObject {
}
material.shadows = false;
if (material.texture == null) {
var dtsshader = new DtsTexture();
dtsshader.currentOpacity = 1;
// var dtsshader = new DtsTexture();
// dtsshader.currentOpacity = 1;
// Make a 1x1 white texture
var bitmap = new hxd.BitmapData(1, 1);
bitmap.setPixel(0, 0, 0xFFFFFF);
var texture = new Texture(1, 1);
texture.uploadBitmap(bitmap);
dtsshader.texture = texture;
material.mainPass.addShader(dtsshader);
material.receiveShadows = true;
material.texture = texture;
// dtsshader.texture = texture;
// material.mainPass.addShader(dtsshader);
// material.shadows = false;
}
if (flags & 4 > 0) {
material.blendMode = BlendMode.Alpha;

View file

@ -1,5 +1,6 @@
package;
import src.AudioManager;
import src.Settings;
import src.MarbleGame;
import gui.MainMenuGui;
@ -16,6 +17,7 @@ class Main extends hxd.App {
super.init();
Settings.init();
AudioManager.init();
marbleGame = new MarbleGame(s2d, s3d);
MarbleGame.canvas.setContent(new MainMenuGui());
// world = new MarbleWorld(s3d, s2d, mission);

View file

@ -1,5 +1,7 @@
package src;
import src.Util;
import src.AudioManager;
import src.Settings;
import h3d.scene.Mesh;
import h3d.col.Bounds;
@ -121,6 +123,9 @@ class Marble extends GameObject {
var _minBounceVel = 3;
var _minTrailVel = 10;
var _bounceKineticFriction = 0.2;
var minVelocityBounceSoft = 2.5;
var minVelocityBounceHard = 12.0;
var bounceMinGain = 0.2;
public var _bounceRestitution = 0.5;
@ -349,6 +354,11 @@ class Marble extends GameObject {
var velLen = this.velocity.length();
var surfaceVel = this.contacts[i].normal.multiply(surfaceDot);
if (!_bounceYet) {
_bounceYet = true;
playBoundSound(-surfaceDot);
}
if (noBounce) {
this.velocity = this.velocity.sub(surfaceVel);
} else if (contacts[i].collider != null) {
@ -506,6 +516,7 @@ class Marble extends GameObject {
}
if (sv < this._jumpImpulse) {
this.velocity = this.velocity.add(bestContact.normal.multiply((this._jumpImpulse - sv)));
AudioManager.playSound(ResourceLoader.getAudio("data/sound/jump.wav"));
}
}
for (j in 0...contacts.length) {
@ -607,6 +618,29 @@ class Marble extends GameObject {
this._bounceNormal = normal;
}
function playBoundSound(contactVel:Float) {
if (minVelocityBounceSoft <= contactVel) {
var hardBounceSpeed = minVelocityBounceHard;
var bounceSoundNum = Math.floor(Math.random() * 4);
var sndList = [
"data/sound/bouncehard1.wav",
"data/sound/bouncehard2.wav",
"data/sound/bouncehard3.wav",
"data/sound/bouncehard4.wav"
];
var snd = ResourceLoader.getAudio(sndList[bounceSoundNum]);
var gain = bounceMinGain;
gain = Util.clamp(Math.pow(contactVel / 12, 1.5), 0, 1);
// if (hardBounceSpeed <= contactVel)
// gain = 1.0;
// else
// gain = (contactVel - minVelocityBounceSoft) / (hardBounceSpeed - minVelocityBounceSoft) * (1.0 - gain) + gain;
snd.play(false, Settings.optionsSettings.soundVolume * gain);
}
}
function getIntersectionTime(dt:Float, velocity:Vector) {
var searchbox = new Bounds();
searchbox.addSpherePos(this.x, this.y, this.z, _radius);
@ -697,6 +731,8 @@ class Marble extends GameObject {
}
}
_bounceYet = false;
do {
if (timeRemaining <= 0)
break;

View file

@ -1,5 +1,7 @@
package src;
import src.ResourceLoader;
import src.AudioManager;
import src.Settings;
import gui.LoadingGui;
import gui.PlayMissionGui;
@ -255,7 +257,24 @@ class MarbleWorld extends Scheduler {
this.newOrientationQuat = new Quat();
this.deselectPowerUp();
AudioManager.playSound(ResourceLoader.getAudio('data/sound/spawn.wav'));
this.clearSchedule();
this.schedule(0.5, () -> {
// setCenterText('ready');
AudioManager.playSound(ResourceLoader.getAudio('data/sound/ready.wav'));
return 0;
});
this.schedule(2, () -> {
// setCenterText('set');
AudioManager.playSound(ResourceLoader.getAudio('data/sound/set.wav'));
return 0;
});
this.schedule(3.5, () -> {
// setCenterText('go');
AudioManager.playSound(ResourceLoader.getAudio('data/sound/go.wav'));
return 0;
});
return 0;
}
@ -722,7 +741,7 @@ class MarbleWorld extends Scheduler {
if (this.gemCount == this.totalGems) {
string = "You have all the gems, head for the finish!";
// if (!this.rewinding)
// AudioManager.play('gotallgems.wav');
AudioManager.playSound(ResourceLoader.getAudio('data/sound/gotallgems.wav'));
// Some levels with this package end immediately upon collection of all gems
// if (this.mission.misFile.activatedPackages.includes('endWithTheGems')) {
@ -741,7 +760,7 @@ class MarbleWorld extends Scheduler {
}
// if (!this.rewinding)
// AudioManager.play('gotgem.wav');
AudioManager.playSound(ResourceLoader.getAudio('data/sound/gotgem.wav'));
}
displayAlert(string);
@ -817,7 +836,13 @@ class MarbleWorld extends Scheduler {
if (this.finishTime == null) {
if (spherebounds.collide(this.endPad.finishBounds)) {
if (collision.gjk.GJK.gjk(gjkSphere, this.endPad.finishCollider) != null) {
touchFinish();
if (!endPad.inFinish) {
touchFinish();
endPad.inFinish = true;
}
} else {
if (endPad.inFinish)
endPad.inFinish = false;
}
}
}
@ -830,7 +855,7 @@ class MarbleWorld extends Scheduler {
return;
if (this.gemCount < this.totalGems) {
// AudioManager.play('missinggems.wav');
AudioManager.playSound(ResourceLoader.getAudio('data/sound/missinggems.wav'));
displayAlert("You can't finish without all the gems!!");
} else {
this.endPad.spawnFirework(this.timeState);
@ -935,7 +960,7 @@ class MarbleWorld extends Scheduler {
sky.follow = null;
// this.oobCameraPosition = camera.position.clone();
playGui.setCenterText('outofbounds');
// AudioManager.play('whoosh.wav');
AudioManager.playSound(ResourceLoader.getAudio('data/sound/whoosh.wav'));
// if (this.replay.mode != = 'playback')
this.schedule(this.timeState.currentAttemptTime + 2, () -> this.restart());
}

View file

@ -1,6 +1,7 @@
package src;
import hxd.res.Image;
import hxd.res.Sound;
import h3d.mat.Texture;
import h3d.scene.Object;
import sys.FileSystem;
@ -19,6 +20,7 @@ class ResourceLoader {
static var dtsResources:Map<String, DtsFile> = new Map();
static var textureCache:Map<String, Texture> = new Map();
static var imageCache:Map<String, Image> = new Map();
static var audioCache:Map<String, Sound> = new Map();
// static var threadPool:FixedThreadPool = new FixedThreadPool(4);
@ -77,6 +79,17 @@ class ResourceLoader {
return null;
}
public static function getAudio(path:String) {
if (audioCache.exists(path))
return audioCache.get(path);
if (fileSystem.exists(path)) {
var snd = loader.load(path).toSound();
audioCache.set(path, snd);
return snd;
}
return null;
}
public static function clearInteriorResources() {
interiorResources = new Map();
}

View file

@ -257,36 +257,6 @@ class OptionsDlg extends GuiImage {
shadowsButton.pressed = true;
}
applyFunc = () -> {
if (gfx640480.pressed) {
Settings.optionsSettings.screenWidth = 640;
Settings.optionsSettings.screenHeight = 480;
}
if (gfx800600.pressed) {
Settings.optionsSettings.screenWidth = 800;
Settings.optionsSettings.screenHeight = 600;
}
if (gfx1024768.pressed) {
Settings.optionsSettings.screenWidth = 1024;
Settings.optionsSettings.screenHeight = 768;
}
if (gfxFull.pressed)
Settings.optionsSettings.isFullScreen = true;
else
Settings.optionsSettings.isFullScreen = false;
if (gfx16.pressed)
Settings.optionsSettings.colorDepth = 0;
else
Settings.optionsSettings.colorDepth = 1;
if (gfxopengl.pressed)
Settings.optionsSettings.videoDriver = 0;
else
Settings.optionsSettings.videoDriver = 1;
Settings.optionsSettings.shadows = shadowsButton.pressed;
Settings.applySettings();
}
// AUDIO PANEL
var audioPane = new GuiControl();
@ -337,6 +307,39 @@ Renderer: Software
Extensions: EAX 2.0, EAX 3.0, EAX Unified, and EAX-AC3";
audTxtWndo.addChild(audInfo);
applyFunc = () -> {
if (gfx640480.pressed) {
Settings.optionsSettings.screenWidth = 640;
Settings.optionsSettings.screenHeight = 480;
}
if (gfx800600.pressed) {
Settings.optionsSettings.screenWidth = 800;
Settings.optionsSettings.screenHeight = 600;
}
if (gfx1024768.pressed) {
Settings.optionsSettings.screenWidth = 1024;
Settings.optionsSettings.screenHeight = 768;
}
if (gfxFull.pressed)
Settings.optionsSettings.isFullScreen = true;
else
Settings.optionsSettings.isFullScreen = false;
if (gfx16.pressed)
Settings.optionsSettings.colorDepth = 0;
else
Settings.optionsSettings.colorDepth = 1;
if (gfxopengl.pressed)
Settings.optionsSettings.videoDriver = 0;
else
Settings.optionsSettings.videoDriver = 1;
Settings.optionsSettings.shadows = shadowsButton.pressed;
Settings.optionsSettings.musicVolume = audMusKnob.sliderValue;
Settings.optionsSettings.soundVolume = audSndKnob.sliderValue;
Settings.applySettings();
}
// CONTROLS PANEL
var controlsPane = new GuiControl();
controlsPane.position = new Vector(44, 58);

View file

@ -1,5 +1,6 @@
package shapes;
import src.ResourceLoader;
import mis.MissionElement.MissionElementItem;
import src.TimeState;
import h3d.Vector;
@ -14,6 +15,7 @@ class AntiGravity extends PowerUp {
this.identifier = "AntiGravity";
this.pickUpName = "Gravity Modifier";
this.autoUse = true;
this.pickupSound = ResourceLoader.getAudio("data/sound/gravitychange.wav");
}
public function pickUp():Bool {

View file

@ -1,5 +1,6 @@
package shapes;
import src.AudioManager;
import h3d.Quat;
import h3d.mat.Material;
import h3d.scene.Mesh;
@ -24,6 +25,7 @@ class EndPad extends DtsObject {
var finishCollider:ConvexHull;
var finishBounds:Bounds;
var inFinish:Bool = false;
public function new() {
super();
@ -43,6 +45,7 @@ class EndPad extends DtsObject {
function spawnFirework(time:TimeState) {
var firework = new Firework(this.getAbsPos().getPosition(), time.timeSinceLoad, this.level);
this.fireworks.push(firework);
// AudioManager.playSound(ResourceLoader.getAudio("data/sound/firewrks.wav"));
// AudioManager.play(this.sounds[0], 1, AudioManager.soundGain, this.worldPosition);
}

View file

@ -1,5 +1,6 @@
package shapes;
import src.ResourceLoader;
import mis.MissionElement.MissionElementItem;
import src.TimeState;
import src.DtsObject;
@ -13,6 +14,7 @@ class Helicopter extends PowerUp {
this.showSequences = false;
this.identifier = "Helicopter";
this.pickUpName = "Gyrocopter PowerUp";
this.pickupSound = ResourceLoader.getAudio("data/sound/pugyrocoptervoice.wav");
}
public function pickUp():Bool {

View file

@ -1,5 +1,6 @@
package shapes;
import src.AudioManager;
import src.TimeState;
import collision.CollisionHull;
import collision.CollisionInfo;
@ -120,7 +121,7 @@ class LandMine extends DtsObject {
this.setCollisionEnabled(false);
// if (!this.level.rewinding)
// AudioManager.play(this.sounds[0]);
AudioManager.playSound(ResourceLoader.getAudio("data/sound/explode1.wav"));
this.level.particleManager.createEmitter(landMineParticle, landMineParticleData, this.getAbsPos().getPosition());
this.level.particleManager.createEmitter(landMineSmokeParticle, landMineSmokeParticleData, this.getAbsPos().getPosition());
this.level.particleManager.createEmitter(landMineSparksParticle, landMineSparkParticleData, this.getAbsPos().getPosition());

View file

@ -1,5 +1,7 @@
package shapes;
import src.AudioManager;
import hxd.res.Sound;
import mis.MissionElement.MissionElementItem;
import src.TimeState;
import src.Util;
@ -31,6 +33,7 @@ abstract class PowerUp extends DtsObject {
public var powerupParams:PowerupParams = new PowerupParams();
public var pickUpName:String;
public var element:MissionElementItem;
public var pickupSound:Sound;
public function new(element:MissionElementItem) {
super();
@ -54,6 +57,10 @@ abstract class PowerUp extends DtsObject {
this.level.displayAlert('You picked up a ${this.pickUpName}!');
if (this.element.showhelponpickup == "1" && !this.autoUse)
this.level.displayHelp('Press <func:bind mousefire> to use the ${this.pickUpName}!');
if (pickupSound != null) {
AudioManager.playSound(pickupSound);
}
}
}

View file

@ -1,5 +1,6 @@
package shapes;
import src.ResourceLoader;
import mis.MissionElement.MissionElementItem;
import src.TimeState;
import src.DtsObject;
@ -12,6 +13,7 @@ class ShockAbsorber extends PowerUp {
this.isTSStatic = false;
this.identifier = "ShockAbsorber";
this.pickUpName = "Shock Absorber PowerUp";
this.pickupSound = ResourceLoader.getAudio("data/sound/pushockabsorbervoice.wav");
}
public function pickUp():Bool {

View file

@ -1,5 +1,6 @@
package shapes;
import src.ResourceLoader;
import mis.MissionElement.MissionElementItem;
import src.TimeState;
import src.DtsObject;
@ -12,6 +13,7 @@ class SuperBounce extends PowerUp {
this.isTSStatic = false;
this.identifier = "SuperBounce";
this.pickUpName = "Super Bounce PowerUp";
this.pickupSound = ResourceLoader.getAudio("data/sound/pusuperbouncevoice.wav");
}
public function pickUp():Bool {

View file

@ -1,10 +1,12 @@
package shapes;
import src.ResourceLoader;
import mis.MissionElement.MissionElementItem;
import src.TimeState;
import src.ResourceLoader;
import src.ParticleSystem.ParticleData;
import h3d.Vector;
import src.AudioManager;
import src.DtsObject;
final superJumpParticleOptions:src.ParticleSystem.ParticleEmitterOptions = {
@ -43,6 +45,7 @@ class SuperJump extends PowerUp {
sjEmitterParticleData = new ParticleData();
sjEmitterParticleData.identifier = "superJumpParticle";
sjEmitterParticleData.texture = ResourceLoader.getTexture("data/particles/twirl.png");
this.pickupSound = ResourceLoader.getAudio("data/sound/pusuperjumpvoice.wav");
}
public function pickUp():Bool {
@ -55,7 +58,7 @@ class SuperJump extends PowerUp {
this.level.particleManager.createEmitter(superJumpParticleOptions, this.sjEmitterParticleData, null, () -> marble.getAbsPos().getPosition());
// marble.body.addLinearVelocity(this.level.currentUp.scale(20)); // Simply add to vertical velocity
// if (!this.level.rewinding)
// AudioManager.play(this.sounds[1]);
AudioManager.playSound(ResourceLoader.getAudio("data/sound/dosuperjump.wav"));
// this.level.particles.createEmitter(superJumpParticleOptions, null, () => Util.vecOimoToThree(marble.body.getPosition()));
this.level.deselectPowerUp();
}

View file

@ -8,6 +8,7 @@ import src.ParticleSystem.ParticleEmitterOptions;
import h3d.Quat;
import h3d.Vector;
import src.DtsObject;
import src.AudioManager;
final superSpeedParticleOptions:ParticleEmitterOptions = {
ejectionPeriod: 5,
@ -50,6 +51,7 @@ class SuperSpeed extends PowerUp {
ssEmitterParticleData = new ParticleData();
ssEmitterParticleData.identifier = "superSpeedParticle";
ssEmitterParticleData.texture = ResourceLoader.getTexture("data/particles/spark.png");
this.pickupSound = ResourceLoader.getAudio("data/sound/pusuperspeedvoice.wav");
}
public function pickUp():Bool {
@ -74,7 +76,7 @@ class SuperSpeed extends PowerUp {
// marble.body.addLinearVelocity(Util.vecThreeToOimo(movementVector).scale(24.7)); // Whirligig's determined value
// marble.body.addLinearVelocity(this.level.currentUp.scale(20)); // Simply add to vertical velocity
// if (!this.level.rewinding)
// AudioManager.play(this.sounds[1]);
AudioManager.playSound(ResourceLoader.getAudio("data/sound/dosuperspeed.wav"));
this.level.particleManager.createEmitter(superSpeedParticleOptions, this.ssEmitterParticleData, null, () -> marble.getAbsPos().getPosition());
this.level.deselectPowerUp();
}

View file

@ -1,5 +1,6 @@
package shapes;
import src.ResourceLoader;
import mis.MissionElement.MissionElementItem;
import src.TimeState;
import mis.MisParser;
@ -22,6 +23,7 @@ class TimeTravel extends PowerUp {
this.cooldownDuration = 1e8;
this.useInstancing = true;
this.autoUse = true;
this.pickupSound = ResourceLoader.getAudio("data/sound/putimetravelvoice.wav");
}
public function pickUp():Bool {

View file

@ -6,6 +6,8 @@ import src.Util;
import src.DtsObject;
import h3d.Vector;
import src.ForceObject;
import src.ResourceLoader;
import src.AudioManager;
class Trapdoor extends DtsObject {
var lastContactTime = -1e8;
@ -37,7 +39,7 @@ class Trapdoor extends DtsObject {
direction = -1;
if (direction != 0 && direction != this.lastDirection) {
// If the direction has changed, play the sound
// AudioManager.play(this.sounds[0], 1, AudioManager.soundGain, this.worldPosition);
// AudioManager.playSound(this.sounds[0], 1, AudioManager.soundGain, this.worldPosition);
}
this.lastCompletion = currentCompletion;

View file

@ -1,10 +1,12 @@
package triggers;
import src.TimeState;
import src.ResourceLoader;
import src.AudioManager;
class HelpTrigger extends Trigger {
override function onMarbleEnter(timeState:TimeState) {
// AudioManager.play('infotutorial.wav');
AudioManager.playSound(ResourceLoader.getAudio('data/sound/infotutorial.wav'));
this.level.displayHelp(this.element.text);
// this.level.replay.recordMarbleEnter(this);
}