Permalink
Browse files

More accurate sound.

  • Loading branch information...
1 parent cf98e97 commit 460612fa8d574e9721d3aaea48384e156fad8e38 @Osmose committed May 12, 2012
View
BIN audio/house.ogg
Binary file not shown.
View
BIN audio/sfx/stairs.ogg
Binary file not shown.
View
4 js/demo/entities.js
@@ -15,13 +15,15 @@ define(function(require, exports) {
this.y = this.orig_y;
};
- function Door(x, y, width, height, to, to_x, to_y) {
+ function Door(x, y, width, height, to, to_x, to_y, cell_x, cell_y) {
ZeldaEntity.call(this, x, y);
this.setHitbox(0, 0, width, height);
this.type = 'door';
this.to = to;
this.to_x = to_x;
this.to_y = to_y;
+ this.cell_x = cell_x;
+ this.cell_y = cell_y;
}
Door.prototype = Object.create(ZeldaEntity.prototype);
exports.Door = Door;
View
1 js/demo/player.js
@@ -71,6 +71,7 @@ define(function(require) {
var door = this.getCollideEntity('door', dx, dy);
if (door !== false) {
+ this.world.sounds.stairs.play();
this.world.transition(Fade, door);
};
View
48 js/demo/tilemap.js
@@ -11,13 +11,16 @@ define(function(require) {
this.cell_x = 0;
this.cell_y = 0;
+ this.height_cells = Math.floor(map.height_pixels / this.cell_height);
+ this.width_cells = Math.floor(map.width_pixels / this.cell_height);
+
// Init entity containers. First index is the cell_y coordinate,
// second is cell_x.
this.entities = [];
- for (var ty = 0; ty < this.heightTiles; ty++) {
- this.entities[ty] = [];
- for (var tx = 0; tx < this.widthTiles; tx++) {
- this.entities[ty][tx] = [];
+ for (var cy = 0; cy < this.height_cells; cy++) {
+ this.entities[cy] = [];
+ for (var cx = 0; cx < this.width_cells; cx++) {
+ this.entities[cy][cx] = [];
}
}
@@ -33,18 +36,45 @@ define(function(require) {
entity.width, entity.height,
entity.properties.to,
parseInt(entity.properties.x),
- parseInt(entity.properties.y));
+ parseInt(entity.properties.y),
+ parseInt(entity.properties.cell_x),
+ parseInt(entity.properties.cell_y));
self.entities[cell_y][cell_x].push(door);
} else if (entity.type === 'entity') {
var type = entity.properties.type;
self.entities[cell_y][cell_x].push(new entities[type](x, y));
}
});
+
+ // Store what music should play in each cell.
+ this.music = [];
+ for (cy = 0; cy < this.height_cells; cy++) {
+ this.music[cy] = [];
+ for (cx = 0; cx < this.width_cells; cx++) {
+ this.music[cy][cx] = null;
+ }
+ }
+
+ var map_music_areas = map.objectGroups['music'].objects;
+ map_music_areas.forEach(function(music_area) {
+ var area_x = Math.floor(music_area.x / cell_width);
+ var area_y = Math.floor(music_area.y / cell_height);
+ var area_width = Math.floor(music_area.width / cell_width);
+ var area_height = Math.floor(music_area.height / cell_height);
+
+ for (cy = 0; cy < area_height; cy++) {
+ for (cx = 0; cx < area_width; cx++) {
+ self.music[area_y + cy][area_x + cx] = music_area.type;
+ }
+ }
+ });
}
ZeldaTilemap.prototype = Object.create(Tilemap.prototype);
ZeldaTilemap.prototype.setCell = function(cell_x, cell_y, handle_entities) {
+ var world = this.world;
+
if (handle_entities === undefined) handle_entities = true;
if (handle_entities && this.engine) {
this.removeEntities(this.engine);
@@ -74,5 +104,13 @@ define(function(require) {
});
};
+ ZeldaTilemap.prototype.currentMusic = function() {
+ return this.music[this.cell_y][this.cell_x];
+ };
+
+ ZeldaTilemap.prototype.musicForCell = function(cell_x, cell_y) {
+ return this.music[cell_y][cell_x];
+ };
+
return ZeldaTilemap;
});
View
30 js/demo/transitions.js
@@ -7,6 +7,8 @@ define(function(require, exports) {
this.cell_x = world.tilemap.cell_x;
this.cell_y = world.tilemap.cell_y;
+ var old_music = world.tilemap.music[this.cell_y][this.cell_x];
+
this.camera = world.engine.camera;
this.player = world.player;
@@ -47,6 +49,16 @@ define(function(require, exports) {
}
this.world.tilemap.removeEntities(this.world.engine);
+
+ var new_music = world.tilemap.music[this.cell_y][this.cell_x];
+ if (old_music !== new_music) {
+ var new_sound = world.sounds[new_music];
+ world.sounds[old_music].fade(0, 300).done(function() {
+ new_sound.audio.volume = 0;
+ new_sound.stop();
+ new_sound.fade(1, 300);
+ });
+ }
}
exports.Slide = Slide;
@@ -95,6 +107,15 @@ define(function(require, exports) {
this.frame = 0;
this.door = args[0];
+ this.new_tilemap = world.maps.get(this.door.to);
+ this.new_tilemap.setCell(this.door.cell_x, this.door.cell_y, false);
+
+ this.old_music = world.tilemap.currentMusic();
+ this.new_music = this.new_tilemap.musicForCell(this.door.cell_x,
+ this.door.cell_y);
+ if (this.old_music !== this.new_music) {
+ world.sounds[this.old_music].fade(0, 300);
+ }
this.camera = world.engine.camera;
this.player = world.player;
@@ -117,9 +138,16 @@ define(function(require, exports) {
}
if (this.frame === 32) {
- world.tilemap = world.maps.get(this.door.to);
+ world.tilemap = this.new_tilemap;
this.player.x = this.door.to_x;
this.player.y = this.door.to_y;
+
+ if (this.old_music !== this.new_music) {
+ var new_sound = world.sounds[this.new_music];
+ new_sound.audio.volume = 0;
+ new_sound.stop();
+ new_sound.fade(1, 300);
+ }
}
this.frame++;
View
7 js/demo/world.js
@@ -8,6 +8,8 @@ define(function(require) {
var loader = require('demo/loader');
loader.register('mabe_village', 'audio/mabe_village.ogg', 'audio');
+ loader.register('house', 'audio/house.ogg', 'audio');
+ loader.register('stairs', 'audio/sfx/stairs.ogg', 'audio');
function ZeldaWorld() {
DefaultWorld.call(this);
@@ -19,8 +21,11 @@ define(function(require) {
this.hud = new HUD(this);
this.sounds = {
- mabe_village: new Sound(loader.get('mabe_village'))
+ mabe_village: new Sound(loader.get('mabe_village')),
+ house: new Sound(loader.get('house')),
+ stairs: new Sound(loader.get('stairs'))
};
+ this.bg = null;
}
ZeldaWorld.prototype = Object.create(DefaultWorld.prototype);
2 js/lib/flux
@@ -1 +1 @@
-Subproject commit 3ac1248952b05e86f48d3e78335d39be01b98c31
+Subproject commit 5a20c667824a6f099a034e39c25af4dceff9243e
View
5 maps/houses/house_marin.tmx
@@ -18,10 +18,15 @@
<objectgroup name="entities" width="10" height="8">
<object type="door" x="72" y="120" width="16" height="8">
<properties>
+ <property name="cell_x" value="1"/>
+ <property name="cell_y" value="1"/>
<property name="to" value="overworld"/>
<property name="x" value="80"/>
<property name="y" value="72"/>
</properties>
</object>
</objectgroup>
+ <objectgroup name="music" width="10" height="8" visible="0">
+ <object type="house" x="0" y="0" width="160" height="128"/>
+ </objectgroup>
</map>
View
5 maps/overworld.tmx
@@ -34,6 +34,8 @@
<objectgroup name="entities" width="30" height="24">
<object type="door" x="240" y="192" width="16" height="8">
<properties>
+ <property name="cell_x" value="0"/>
+ <property name="cell_y" value="0"/>
<property name="to" value="house_marin"/>
<property name="x" value="72"/>
<property name="y" value="100"/>
@@ -80,4 +82,7 @@
</properties>
</object>
</objectgroup>
+ <objectgroup name="music" width="30" height="24" visible="0">
+ <object type="mabe_village" x="0" y="0" width="480" height="384"/>
+ </objectgroup>
</map>

0 comments on commit 460612f

Please sign in to comment.