Skip to content

Commit

Permalink
Make soundtrack state configurable: closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
dmrib committed Oct 19, 2019
1 parent 9f972dd commit c85d642
Showing 1 changed file with 53 additions and 6 deletions.
59 changes: 53 additions & 6 deletions src/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,68 @@
* DEFINITIONS
*/

// sound controller container
let soundOn = false;

// perlin noise terrain container
let terrain;

// soundtrack p5.SoundFile container
let soundtrack;

/**
* I draw this sketch sound indicator.
*
* Returns:
* undefined.
*/
function drawSoundIndicator()
{
// push transformation matrix
push();

// translate to bottom right corner
translate((WIDTH / 2) - 50, (HEIGHT / 2) - 50);

// set indicator transparency
soundOn ? tint(255, 100) : tint(255, 255);

// draw image
image(keyboard, 0, 0, 50, 50);

// pop transformation matrix
pop();
}

/**
* p5.js preload callback.
*
* Returns:
* undefined;
*/
function preload() {
// load soundtrack song
soundtrack = loadSound('assets/audio/da_beat-nr1.mp3');
function preload()
{
// load soundtrack song
soundtrack = loadSound('assets/audio/da_beat-nr1.mp3');

// load keyboard on image
keyboard = loadImage('assets/images/keyboard.png');
}

/**
* p5.js mouse clicked callback.
*
* Returns:
* undefined.
*/
function mouseClicked()
{
// soundtrack icon was clicked: update soundtrack state
if(mouseX > WIDTH - 50 && mouseX < WIDTH && mouseY > HEIGHT - 50 && mouseY < HEIGHT)
{
soundOn = !soundOn;
soundOn ? soundtrack.loop() : soundtrack.stop();
}
}

/**
Expand All @@ -33,9 +80,6 @@ function setup()
// create canvas and set renderer as WEBGL
createCanvas(WIDTH, HEIGHT, WEBGL);

// play soundtrack in a loop
soundtrack.loop();

// setup terrain
terrain = new Terrain(WIDTH + WIDTH_OFFSET, HEIGHT + HEIGHT, RESOLUTION);
terrain.setup();
Expand All @@ -57,4 +101,7 @@ function draw()

// draw terrain
terrain.render();

// draw soundtrack indicator
drawSoundIndicator();
}

0 comments on commit c85d642

Please sign in to comment.