Skip to content

Commit

Permalink
Examples: Added bullet impact sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Oct 11, 2018
1 parent 383ceb9 commit 0283aed
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 21 deletions.
13 changes: 13 additions & 0 deletions build/yuka.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@

}

/**
* Computes a random integer value within a given min/max range.
*
* @param {min} value - The min value.
* @param {max} value - The max value.
* @return {Number} The random integer value.
*/
static randInt( min, max ) {

return min + Math.floor( Math.random() * ( max - min + 1 ) );

}

/**
* Computes a random float value within a given min/max range.
*
Expand Down
34 changes: 17 additions & 17 deletions build/yuka.min.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions build/yuka.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ class MathUtils {

}

/**
* Computes a random integer value within a given min/max range.
*
* @param {min} value - The min value.
* @param {max} value - The max value.
* @return {Number} The random integer value.
*/
static randInt( min, max ) {

return min + Math.floor( Math.random() * ( max - min + 1 ) );

}

/**
* Computes a random float value within a given min/max range.
*
Expand Down
Binary file added examples/entity/shooter/audio/impact1.ogg
Binary file not shown.
Binary file added examples/entity/shooter/audio/impact2.ogg
Binary file not shown.
Binary file added examples/entity/shooter/audio/impact3.ogg
Binary file not shown.
11 changes: 11 additions & 0 deletions examples/entity/shooter/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,20 @@
const step2 = new THREE.Audio( listener );
const shot = new THREE.PositionalAudio( listener );
const reload = new THREE.PositionalAudio( listener );
const impact1 = new THREE.PositionalAudio( listener );
const impact2 = new THREE.PositionalAudio( listener );
const impact3 = new THREE.PositionalAudio( listener );

shot.setVolume( 0.3 );
reload.setVolume( 0.3 );

audioLoader.load( 'audio/step1.ogg', ( buffer ) => { step1.setBuffer( buffer ) } );
audioLoader.load( 'audio/step2.ogg', ( buffer ) => { step2.setBuffer( buffer ) } );
audioLoader.load( 'audio/shot.ogg', ( buffer ) => { shot.setBuffer( buffer ) } );
audioLoader.load( 'audio/reload.ogg', ( buffer ) => { reload.setBuffer( buffer ) } );
audioLoader.load( 'audio/impact1.ogg', ( buffer ) => { impact1.setBuffer( buffer ) } );
audioLoader.load( 'audio/impact2.ogg', ( buffer ) => { impact2.setBuffer( buffer ) } );
audioLoader.load( 'audio/impact3.ogg', ( buffer ) => { impact3.setBuffer( buffer ) } );

//

Expand Down Expand Up @@ -217,6 +225,9 @@
player.head.setRenderComponent( camera, syncCamera );
player.weapon.sounds.set( 'shot', shot );
player.weapon.sounds.set( 'reload', reload );
player.weapon.sounds.set( 'impact1', impact1 );
player.weapon.sounds.set( 'impact2', impact2 );
player.weapon.sounds.set( 'impact3', impact3 );

controls = new FirstPersonControls( player );
controls.lookSpeed = 2;
Expand Down
9 changes: 7 additions & 2 deletions examples/entity/shooter/src/Bullet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @author Mugen87 / https://github.com/Mugen87
*/

import { Projectile, Ray, Vector3 } from '../../../../build/yuka.module.js';
import { Projectile, MathUtils, Ray, Vector3 } from '../../../../build/yuka.module.js';
import world from './World.js';

const intersectionPoint = new Vector3();
Expand Down Expand Up @@ -53,9 +53,14 @@ class Bullet extends Projectile {

if ( distanceToIntersction <= validDistance ) {

const audio = this.owner.weapon.sounds.get( 'impact' + MathUtils.randInt( 1, 3 ) );

if ( audio.isPlaying === true ) audio.stop();
audio.play();

// hit!

world.addBulletHole( intersectionPoint, normal );
world.addBulletHole( intersectionPoint, normal, audio );

// TODO: inform game entity about hit. need referene to game entity

Expand Down
3 changes: 2 additions & 1 deletion examples/entity/shooter/src/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ class World {

}

addBulletHole( position, normal ) {
addBulletHole( position, normal, audio ) {

const bulletHole = this.renderComponents.get( 'bulletHole' ).clone();
bulletHole.add( audio );

const s = 1 + ( Math.random() * 0.5 );
bulletHole.scale.set( s, s, s );
Expand Down
13 changes: 13 additions & 0 deletions src/math/MathUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ class MathUtils {

}

/**
* Computes a random integer value within a given min/max range.
*
* @param {min} value - The min value.
* @param {max} value - The max value.
* @return {Number} The random integer value.
*/
static randInt( min, max ) {

return min + Math.floor( Math.random() * ( max - min + 1 ) );

}

/**
* Computes a random float value within a given min/max range.
*
Expand Down
20 changes: 19 additions & 1 deletion test/unit_test/math/MathUtils.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,27 @@ describe( 'Math', function () {

} );

describe( '#randInt()', function () {

it( 'should return a random integer between two integer values', function () {

let int = MathUtils.randInt( 4, 6 );

expect( int ).to.be.within( 4, 6 );
expect( int % 1 ).to.equal( 0 );

int = MathUtils.randInt( - 2, 0 );

expect( int ).to.be.within( - 2, 0 );
expect( int % 1 ).to.equal( 0 );

} );

} );

describe( '#randFloat()', function () {

it( 'should return a random number between two float values', function () {
it( 'should return a random float between two float values', function () {

expect( MathUtils.randFloat( 4, 6 ) ).to.be.within( 4, 6 );
expect( MathUtils.randFloat( - 2, - 1 ) ).to.be.within( - 2, - 1 );
Expand Down

0 comments on commit 0283aed

Please sign in to comment.