Skip to content

Commit

Permalink
Use more reusable sound controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrick committed Oct 26, 2017
1 parent 8966e5d commit 8d877e2
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions rose/web/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@ var ROSE = (function() {
this.controller = new Controller();
this.rate = new Rate([0.5, 1.0, 2.0, 5.0, 10.0]);

var imageLoader = new ImageLoader(function() {
var image_loader = new ImageLoader(function() {
this.client = new Client(this.onmessage.bind(this), 2000);
}.bind(this));

var soundLoader = new SoundLoader(function() {});
soundLoader.load("res/soundtrack/Nyan_Cat.ogg", function(sound) {
sound.play();
});
var sound_loader = new SoundLoader(function() {});

this.context = $("#game").get(0).getContext("2d");
this.dashboard = new Dashboard(imageLoader);
this.track = new Track(imageLoader);
this.obstacles = new Obstacles(imageLoader);
this.cars = new Cars(imageLoader);
this.finish_line = new FinishLine(imageLoader);
this.dashboard = new Dashboard(image_loader);
this.track = new Track(image_loader);
this.obstacles = new Obstacles(image_loader);
this.cars = new Cars(image_loader);
this.finish_line = new FinishLine(image_loader);
this.sound_controller = new SoundController(sound_loader);
}

App.prototype.onmessage = function(m) {
Expand Down Expand Up @@ -405,6 +402,28 @@ var ROSE = (function() {
}
}

function SoundController(loader) {
this.loader = loader;
this.sounds = [null];
var self = this;

loader.load("res/soundtrack/Nyan_Cat.ogg", function (sound) {
sound.loop = true;
self.sounds[0] = sound;
self.toggle_sound(0);
});
}

SoundController.prototype.toggle_sound = function(sound_id) {
var sound = this.sounds[sound_id];

if (sound.paused) {
sound.play();
} else {
sound.pause();
}
}

function ImageLoader(done) {
this.loading = 0;
this.done = done;
Expand Down

0 comments on commit 8d877e2

Please sign in to comment.