This commit is contained in:
oc 2017-12-29 12:28:59 +08:00
parent 5f7a03ec70
commit 95686a637c
14 changed files with 1016 additions and 178 deletions

View File

@ -113,5 +113,6 @@
<canvas class='gameCanvas' id='data' width='416' height='416'>此浏览器不支持HTML5</canvas>
</div>
<script id='mainScript' src='main.js'></script>
<script src='libs/thirdparty/mid.js'></script>
</body>
</html>

View File

@ -7,12 +7,14 @@ function core() {
this.statusBar = {};
this.canvas = {};
this.images = [];
this.sounds = {};
this.bgms = [];
this.sounds = [];
this.floorIds = [];
this.floors = {};
this.firstData = {};
this.material = {
'images': {},
'bgms': {},
'sounds': {},
'ground': null,
'items': {},
@ -35,12 +37,12 @@ function core() {
'openDoorAnimate': null
}
this.musicStatus = {
'isIOS': false,
'loaded': false,
'bgmStatus': false,
'soundStatus': true,
'playedSound': null,
'playedBgm': null,
'audioContext': null, // WebAudioContext
'startDirectly': false, // 是否直接播放(加载)音乐
'bgmStatus': false, // 是否播放BGM
'soundStatus': true, // 是否播放SE
'playingBgm': null, // 正在播放的BGM
'isPlaying': false,
}
// 样式
this.domStyle = {
@ -102,11 +104,12 @@ function core() {
/////////// 系统事件相关 ///////////
core.prototype.init = function (dom, statusBar, canvas, images, sounds, floorIds, floors, coreData) {
core.prototype.init = function (dom, statusBar, canvas, images, bgms, sounds, floorIds, floors, coreData) {
core.dom = dom;
core.statusBar = statusBar;
core.canvas = canvas;
core.images = images;
core.bgms = bgms;
core.sounds = sounds;
core.floorIds = floorIds;
core.floors = floors;
@ -134,16 +137,43 @@ core.prototype.init = function (dom, statusBar, canvas, images, sounds, floorIds
core.material.icons = core.icons.getIcons();
core.material.events = core.events.getEvents();
// test if iOS
core.musicStatus.soundStatus = core.getLocalStorage('soundStatus', true);
var userAgent = navigator.userAgent;
if (userAgent.indexOf('iPhone') > -1 || userAgent.indexOf('iPad') > -1) {
console.log("你的设备为iphone不自动播放音乐");
core.musicStatus.isIOS = true;
core.musicStatus.soundStatus = false;
if (location.protocol.indexOf("http")==0) {
window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.msAudioContext;
try {
core.musicStatus.audioContext = new window.AudioContext();
} catch (e) {
console.log("该浏览器不支持AudioContext");
core.musicStatus.audioContext = null;
}
}
// 音效设置部分
var isPC = true;
["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"].forEach(function (t) {
if (navigator.userAgent.indexOf(t)>=0) isPC=false;
});
if (isPC) {
// 如果是PC端直接加载
core.musicStatus.startDirectly = true;
}
else {
var connection = navigator.connection;
if (core.isset(connection) && connection.type=='wifi')
core.musicStatus.startDirectly = true;
}
// 先从存储中读取BGM状态
core.musicStatus.bgmStatus = core.getLocalStorage('bgmStatus', true);
if (!core.musicStatus.startDirectly) // 如果当前网络环境不允许
core.musicStatus.bgmStatus = false;
core.setLocalStorage('bgmStatus', core.musicStatus.bgmStatus);
core.musicStatus.soundStatus = core.getLocalStorage('soundStatus', true);
core.setLocalStorage('soundStatus', core.musicStatus.soundStatus);
// switchs
core.flags.battleAnimate = core.getLocalStorage('battleAnimate', core.flags.battleAnimate);
core.flags.displayEnemyDamage = core.getLocalStorage('enemyDamage', core.flags.displayEnemyDamage);
@ -232,7 +262,7 @@ core.prototype.loader = function (callback) {
core.material.images.autotile[autotileId]=image;
if (Object.keys(core.material.images.autotile).length==autotileIds.length) {
// 音频
core.loadSounds(callback);
core.loadMusic(callback);
}
})
}
@ -259,65 +289,112 @@ core.prototype.loadImage = function (imgName, callback) {
}
}
core.prototype.loadSounds = function (callback) {
for (var key in core.sounds) {
for (var i = 0; i < core.sounds[key].length; i++) {
var soundName=core.sounds[key][i];
soundName = soundName.split('-');
var sound = new Audio();
sound.preload = 'none';
sound.src = 'sounds/' + soundName[0] + '.' + key;
if (soundName[1] == 'loop') {
sound.loop = 'loop';
core.prototype.loadMusic = function (callback) {
core.bgms.forEach(function (t) {
// 判断是不是mid
if (/^.*\.mid$/.test(t)) {
if (core.musicStatus.audioContext!=null) {
core.material.bgms[t] = 'loading';
var xhr = new XMLHttpRequest();
xhr.open('GET', 'sounds/'+t, true);
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.onload = function(e) { //下载完成
try {
var ff = [];
var mx = this.responseText.length;
for (var z = 0; z < mx; z++)
ff[z] = String.fromCharCode(this.responseText.charCodeAt(z) & 255);
var shouldStart = core.material.bgms[t] == 'starting';
core.material.bgms[t] = AudioPlayer(core.musicStatus.audioContext, Replayer(MidiFile(ff.join("")), Synth(44100)), true);
if (shouldStart)
core.playBgm(t);
}
catch (ee) {
console.log(ee);
core.material.bgms[t] = null;
}
};
xhr.ontimeout = function(e) {
console.log(e);
core.material.bgms[t] = null;
}
xhr.onerror = function(e) {
console.log(e);
core.material.bgms[t] = null;
}
xhr.send();
}
else {
core.material.bgms[t] = null;
}
if (!core.isset(core.material.sounds[key]))
core.material.sounds[key] = {};
core.material.sounds[key][soundName[0]] = sound;
}
}
else {
var music = new Audio();
if (!core.musicStatus.startDirectly)
music.preload = 'none'; // 默认不加载
music.src = 'sounds/'+t;
music.loop = 'loop';
core.material.bgms[t] = music;
}
});
core.sounds.forEach(function (t) {
if (core.musicStatus.audioContext != null) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'sounds/'+t, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) { //下载完成
try {
core.musicStatus.audioContext.decodeAudioData(this.response, function (buffer) {
core.material.sounds[t] = buffer;
}, function (e) {
console.log(e);
core.material.sounds[t] = null;
})
}
catch (ee) {
console.log(ee);
core.material.sounds[t] = null;
}
};
xhr.ontimeout = function(e) {
console.log(e);
core.material.sounds[t] = null;
}
xhr.onerror = function(e) {
console.log(e);
core.material.sounds[t] = null;
}
xhr.send();
}
else {
var music = new Audio();
music.src = 'sounds/'+t;
core.material.sounds[t] = music;
}
});
// 直接开始播放
if (core.musicStatus.startDirectly && core.bgms.length>0)
core.playBgm(core.bgms[0]);
callback();
}
core.prototype.loadSound = function() {
if (!core.isset(core.material.sounds.mp3)) return;
if (core.musicStatus.isIOS) return;
if (core.musicStatus.loaded) return;
core.musicStatus.loaded=true;
console.log("加载音乐");
var toLoadList = [];
for (var key in core.material.sounds) {
for (var name in core.material.sounds[key]) {
toLoadList.push(core.material.sounds[key][name]);
}
}
core.loadSoundItem(toLoadList);
}
core.prototype.loadSoundItem = function (toLoadList) {
if (toLoadList.length==0) {
if (core.musicStatus.bgmStatus>0) return;
core.musicStatus.bgmStatus=1;
if (core.musicStatus.soundStatus)
core.playBgm('bgm', 'mp3');
return;
}
var item = toLoadList.shift();
item.oncanplay = function() {
core.loadSoundItem(toLoadList);
}
item.load();
}
core.prototype.isPlaying = function() {
if (core.isset(core.status.played) && core.status.played)
return true;
return false;
}
core.prototype.clearStatus = function() {
// 停止各个Timeout和Interval
for (var i in core.interval) {
@ -1472,7 +1549,7 @@ core.prototype.openDoor = function (id, x, y, needKey, callback) {
}
}
// open
core.playSound("door", "ogg");
core.playSound("door.ogg");
var state = 0;
var doorId = id;
if (!(doorId.substring(doorId.length-4)=="Door")) {
@ -1518,7 +1595,7 @@ core.prototype.battle = function (id, x, y, force, callback) {
});
}
else {
core.playSound('attack', 'ogg');
core.playSound('attack.ogg');
core.afterBattle(id, x, y, callback);
}
}
@ -1613,7 +1690,7 @@ core.prototype.changeFloor = function (floorId, stair, heroLoc, time, callback)
}
window.setTimeout(function () {
core.playSound('floor', 'mp3');
core.playSound('floor.mp3');
core.mapChangeAnimate('show', time/2, function () {
// 根据文字判断是否斜体
@ -2770,7 +2847,7 @@ core.prototype.getNextItem = function() {
core.prototype.getItem = function (itemId, itemNum, itemX, itemY, callback) {
// core.getItemAnimate(itemId, itemNum, itemX, itemY);
core.playSound('item', 'ogg');
core.playSound('item.ogg');
var itemCls = core.material.items[itemId].cls;
core.items.getItemEffect(itemId, itemNum);
core.removeBlock(itemX, itemY);
@ -3375,23 +3452,106 @@ core.prototype.isset = function (val) {
return true
}
core.prototype.playSound = function (soundName, soundType) {
if (!core.musicStatus.soundStatus || !core.musicStatus.loaded) {
core.prototype.playBgm = function (bgm) {
// 如果不允许播放
if (!core.musicStatus.bgmStatus) return;
// 音频不存在
if (!core.isset(core.material.bgms[bgm])) return;
// 延迟播放
if (core.material.bgms[bgm] == 'loading') {
core.material.bgms[bgm] = 'starting';
return;
}
if (!core.isset(core.material.sounds[soundType][soundName])) return;
core.musicStatus.playedSound = core.material.sounds[soundType][soundName];
core.musicStatus.playedSound.play();
try {
// 如果当前正在播放且和本BGM相同直接忽略
if (core.musicStatus.playingBgm == bgm && core.musicStatus.isPlaying) {
return;
}
// 如果正在播放中,暂停
if (core.isset(core.musicStatus.playingBgm) && core.musicStatus.isPlaying) {
core.material.bgms[core.musicStatus.playingBgm].pause();
}
// 播放当前BGM
core.musicStatus.playingBgm = bgm;
core.material.bgms[bgm].play();
core.musicStatus.isPlaying = true;
}
catch (e) {
console.log("无法播放BGM "+bgm);
console.log(e);
core.musicStatus.playingBgm = null;
}
}
core.prototype.playBgm = function (bgmName, bgmType) {
if (core.musicStatus.isIOS || !core.musicStatus.loaded) return;
if (core.isset(core.musicStatus.playedBgm)) {
core.musicStatus.playedBgm.pause();
core.prototype.pauseBgm = function () {
// 直接暂停播放
try {
if (core.isset(core.musicStatus.playingBgm)) {
core.material.bgms[core.musicStatus.playingBgm].pause();
}
core.musicStatus.isPlaying = false;
}
catch (e) {
console.log("无法暂停BGM "+bgm);
console.log(e);
}
}
core.prototype.resumeBgm = function () {
// 恢复BGM
try {
if (core.isset(core.musicStatus.playingBgm)) {
core.material.bgms[core.musicStatus.playingBgm].play();
core.musicStatus.isPlaying = true;
}
else {
if (core.bgms.length>0) {
core.playBgm(core.bgms[0]);
core.musicStatus.isPlaying = true;
}
}
}
catch (e) {
console.log("无法恢复BGM "+bgm);
console.log(e);
}
}
core.prototype.playSound = function (sound) {
// 如果不允许播放
if (!core.musicStatus.soundStatus) return;
// 音频不存在
if (!core.isset(core.material.sounds[sound])) return;
try {
if (core.musicStatus.audioContext != null) {
var source = core.musicStatus.audioContext.createBufferSource();
source.buffer = core.material.sounds[sound];
source.connect(core.musicStatus.audioContext.destination);
try {
source.start(0);
}
catch (e) {
try {
source.noteOn(0);
}
catch (ee) {
}
}
}
else {
core.material.sounds[sound].play();
}
}
catch (eee) {
console.log("无法播放SE "+bgm);
console.log(eee);
}
core.musicStatus.playedBgm = core.material.sounds[bgmType][bgmName];
if (core.musicStatus.soundStatus)
core.musicStatus.playedBgm.play();
}
core.prototype.changeSoundStatus = function () {

View File

@ -140,12 +140,12 @@ data.prototype.init = function() {
// 系统FLAG在游戏运行中中请不要修改它。
this.flags = {
/****** 状态栏相关 ******/
"enableFloor": false, // 是否在状态栏显示当前楼层
"enableLv": true, // 是否在状态栏显示当前等级
"enableFloor": true, // 是否在状态栏显示当前楼层
"enableLv": false, // 是否在状态栏显示当前等级
"enableMDef": true, // 是否在状态栏及战斗界面显示魔防(护盾)
"enableMoney": true, // 是否在状态栏、怪物手册及战斗界面显示金币
"enableExperience": true, // 是否在状态栏、怪物手册及战斗界面显示经验
"enableLevelUp": true, // 是否允许等级提升进阶如果上面enableExperience为false则此项恒视为false
"enableLevelUp": false, // 是否允许等级提升进阶如果上面enableExperience为false则此项恒视为false
"enableDebuff": true, // 是否涉及毒衰咒如果此项为false则不会在状态栏中显示毒衰咒的debuff
////// 上述的几个开关将直接影响状态栏的显示效果 //////
/****** 道具相关 ******/

View File

@ -133,6 +133,17 @@ events.prototype.afterChangeFloor = function (floorId) {
this.doEvents(core.floors[floorId].firstArrive);
core.setFlag("visited_"+floorId, true);
}
// 播放BGM
if (floorId == 'sample0') {
core.playBgm('bgm.mp3');
}
if (floorId == 'sample1') {
core.playBgm('star.mid');
}
if (floorId == 'sample2') {
core.playBgm('058-G2_koko.mid');
}
}
////// 实际事件的处理 //////
@ -285,11 +296,6 @@ events.prototype.doAction = function() {
block = block.block;
if (core.isset(block.event) && block.event.trigger=='action') {
// 触发
/*
core.status.event = {'id': 'action', 'data': {
'list': core.clone(block.event.data), 'x': block.x, 'y': block.y, 'callback': core.status.event.data.callback
}}
*/
core.status.event.data.list = core.clone(block.event.data);
core.status.event.data.x=block.x;
core.status.event.data.y=block.y;
@ -298,11 +304,21 @@ events.prototype.doAction = function() {
this.doAction();
break;
case "playSound":
var name=data.name.split(".");
if (name.length==2)
core.playSound(name[0],name[1]);
core.playSound(data.name);
this.doAction();
break;
case "playBgm":
core.playBgm(data.name);
this.doAction();
break
case "pauseBgm":
core.pauseBgm();
this.doAction();
break
case "resumeBgm":
core.resumeBgm();
this.doAction();
break
case "setValue":
try {
var value=core.calValue(data.value);
@ -623,6 +639,12 @@ events.prototype.changeLight = function(x, y) {
// 改变灯后的事件
events.prototype.afterChangeLight = function(x,y) {
}
// 使用炸弹/圣锤后的事件
events.prototype.afterUseBomb = function () {
}
// 存档事件前一刻的处理
@ -651,6 +673,13 @@ events.prototype.keyDownCtrl = function () {
}
}
events.prototype.clickConfirmBox = function (x,y) {
if ((x == 4 || x == 5) && y == 7 && core.isset(core.status.event.data.yes))
core.status.event.data.yes();
if ((x == 7 || x == 8) && y == 7 && core.isset(core.status.event.data.no))
core.status.event.data.no();
}
events.prototype.keyUpConfirmBox = function (keycode) {
if (keycode==37) {
core.status.event.selection=0;
@ -673,12 +702,6 @@ events.prototype.keyUpConfirmBox = function (keycode) {
}
}
}
events.prototype.clickConfirmBox = function (x,y) {
if ((x == 4 || x == 5) && y == 7 && core.isset(core.status.event.data.yes))
core.status.event.data.yes();
if ((x == 7 || x == 8) && y == 7 && core.isset(core.status.event.data.no))
core.status.event.data.no();
}
// 正在处理事件时的点击操作...
events.prototype.clickAction = function (x,y) {
@ -769,7 +792,6 @@ events.prototype.keyUpBook = function (keycode) {
}
}
// 飞行器
events.prototype.clickFly = function(x,y) {
if ((x==10 || x==11) && y==9) core.ui.drawFly(core.status.event.data-1);
if ((x==10 || x==11) && y==5) core.ui.drawFly(core.status.event.data+1);
@ -1115,21 +1137,27 @@ events.prototype.keyUpSL = function (keycode) {
events.prototype.clickSwitchs = function (x,y) {
if (x<5 || x>7) return;
var choices = [
"背景音乐", "战斗动画", "怪物显伤", "领域显伤", "返回主菜单"
"背景音乐", "背景音效", "战斗动画", "怪物显伤", "领域显伤", "返回主菜单"
];
var topIndex = 6 - parseInt((choices.length - 1) / 2);
if (y>=topIndex && y<topIndex+choices.length) {
var selection = y-topIndex;
switch (selection) {
case 0:
if (core.musicStatus.isIOS) {
core.drawTip("iOS设备不支持播放音乐");
return;
}
core.changeSoundStatus();
core.musicStatus.bgmStatus = !core.musicStatus.bgmStatus;
if (core.musicStatus.bgmStatus)
core.resumeBgm();
else
core.pauseBgm();
core.setLocalStorage('bgmStatus', core.musicStatus.bgmStatus);
core.ui.drawSwitchs();
break;
case 1:
core.musicStatus.soundStatus = !core.musicStatus.soundStatus;
core.setLocalStorage('soundStatus', core.musicStatus.soundStatus);
core.ui.drawSwitchs();
break;
case 2:
if (!core.flags.canOpenBattleAnimate) {
core.drawTip("本塔不能开启战斗动画!");
}
@ -1139,19 +1167,19 @@ events.prototype.clickSwitchs = function (x,y) {
core.ui.drawSwitchs();
}
break;
case 2:
case 3:
core.flags.displayEnemyDamage=!core.flags.displayEnemyDamage;
core.updateFg();
core.setLocalStorage('enemyDamage', core.flags.displayEnemyDamage);
core.ui.drawSwitchs();
break;
case 3:
case 4:
core.flags.displayExtraDamage=!core.flags.displayExtraDamage;
core.updateFg();
core.setLocalStorage('extraDamage', core.flags.displayExtraDamage);
core.ui.drawSwitchs();
break;
case 4:
case 5:
core.status.event.selection=0;
core.ui.drawSettings(false);
break;
@ -1161,7 +1189,7 @@ events.prototype.clickSwitchs = function (x,y) {
events.prototype.keyDownSwitchs = function (keycode) {
var choices = [
"背景音乐", "战斗动画", "怪物显伤", "领域显伤", "返回主菜单"
"背景音乐", "背景音效", "战斗动画", "怪物显伤", "领域显伤", "返回主菜单"
];
if (keycode==38) {
core.status.event.selection--;
@ -1182,7 +1210,7 @@ events.prototype.keyUpSwitchs = function (keycode) {
return;
}
var choices = [
"背景音乐", "战斗动画", "怪物显伤", "领域显伤", "返回主菜单"
"背景音乐", "背景音效", "战斗动画", "怪物显伤", "领域显伤", "返回主菜单"
];
if (keycode==13 || keycode==32 || keycode==67) {
var topIndex = 6 - parseInt((choices.length - 1) / 2);

View File

@ -1,48 +0,0 @@
// 这里需要改楼层名请和文件名及下面的floorId保持完全一致
// 楼层唯一标识符仅能由字母、数字、下划线组成,且不能由数字开头
// 推荐用法第20层就用MT20第38层就用MT38地下6层就用MT_6用下划线代替负号隐藏3层用MT3hh表示隐藏等等
main.floors.test = {
"floorId": "test", // 楼层唯一标识符,需要和名字完全一致
"title": "test", // 楼层中文名
"name": "", // 显示在状态栏中的层数
"canFlyTo": true, // 该楼能否被楼传器飞到(不能的话在该楼也不允许使用楼传器)
"canUseQuickShop": true, // 该层是否允许使用快捷商店
"defaultGround": "ground", // 默认地面的图块IDterrains中
"map": [ // 地图数据需要是13x13建议使用地图生成器来生成
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 1, 0, 237, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 220, 246,0, 246, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 219, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 45, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
],
"firstArrive": [ // 第一次到该楼层触发的事件
],
"events": { // 该楼的所有可能事件列表
"6,4": [
"test",
{"type": 'hide'}
]
},
"changeFloor": { // 楼层转换事件该事件不能和上面的events有冲突同位置点否则会被覆盖
},
"afterBattle": { // 战斗后可能触发的事件列表
},
"afterGetItem": { // 获得道具后可能触发的事件列表
},
"afterOpenDoor": { // 开完门后可能触发的事件列表
}
}

View File

@ -157,6 +157,9 @@ items.prototype.useItem = function (itemId) {
core.drawHero(core.getHeroLoc('direction'), core.getHeroLoc('x'), core.getHeroLoc('y'), 'stop');
core.updateFg();
core.drawTip(core.material.items[itemId].name + "使用成功");
if (itemId == 'bomb' || itemId == 'hammer')
core.events.afterUseBomb();
});
}
if (itemId == 'centerFly') {

701
libs/thirdparty/mid.js vendored Normal file
View File

@ -0,0 +1,701 @@
var sampleRate = 44100; /* hard-coded in Flash player */
function AudioPlayer(context, generator, loop) {
// Uses Webkit Web Audio API if available
sampleRate = context.sampleRate;
var channelCount = 2;
var bufferSize = 4096*4; // Higher for less gitches, lower for less latency
var node = context.createScriptProcessor(bufferSize, 0, channelCount);
node.onaudioprocess = function(e) { process(e) };
function process(e) {
if (generator.finished) {
if (loop) {
generator.reset();
generator.finished = false;
}
else {
node.disconnect();
return;
}
}
var dataLeft = e.outputBuffer.getChannelData(0);
var dataRight = e.outputBuffer.getChannelData(1);
var generate = generator.generate(bufferSize);
for (var i = 0; i < bufferSize; ++i) {
dataLeft[i] = generate[i*2];
dataRight[i] = generate[i*2+1];
}
}
// start
// node.connect(context.destination);
return {
'play': function () {
node.connect(context.destination);
},
'pause': function() {
node.disconnect();
}
}
}
/*
class to parse the .mid file format
(depends on stream.js)
*/
function MidiFile(data) {
function readChunk(stream) {
var id = stream.read(4);
var length = stream.readInt32();
return {
'id': id,
'length': length,
'data': stream.read(length)
};
}
var lastEventTypeByte;
function readEvent(stream) {
var event = {};
event.deltaTime = stream.readVarInt();
var eventTypeByte = stream.readInt8();
if ((eventTypeByte & 0xf0) == 0xf0) {
/* system / meta event */
if (eventTypeByte == 0xff) {
/* meta event */
event.type = 'meta';
var subtypeByte = stream.readInt8();
var length = stream.readVarInt();
switch(subtypeByte) {
case 0x00:
event.subtype = 'sequenceNumber';
if (length != 2) throw "Expected length for sequenceNumber event is 2, got " + length;
event.number = stream.readInt16();
return event;
case 0x01:
event.subtype = 'text';
event.text = stream.read(length);
return event;
case 0x02:
event.subtype = 'copyrightNotice';
event.text = stream.read(length);
return event;
case 0x03:
event.subtype = 'trackName';
event.text = stream.read(length);
return event;
case 0x04:
event.subtype = 'instrumentName';
event.text = stream.read(length);
return event;
case 0x05:
event.subtype = 'lyrics';
event.text = stream.read(length);
return event;
case 0x06:
event.subtype = 'marker';
event.text = stream.read(length);
return event;
case 0x07:
event.subtype = 'cuePoint';
event.text = stream.read(length);
return event;
case 0x20:
event.subtype = 'midiChannelPrefix';
if (length != 1) throw "Expected length for midiChannelPrefix event is 1, got " + length;
event.channel = stream.readInt8();
return event;
case 0x2f:
event.subtype = 'endOfTrack';
if (length != 0) throw "Expected length for endOfTrack event is 0, got " + length;
return event;
case 0x51:
event.subtype = 'setTempo';
if (length != 3) throw "Expected length for setTempo event is 3, got " + length;
event.microsecondsPerBeat = (
(stream.readInt8() << 16)
+ (stream.readInt8() << 8)
+ stream.readInt8()
)
return event;
case 0x54:
event.subtype = 'smpteOffset';
if (length != 5) throw "Expected length for smpteOffset event is 5, got " + length;
var hourByte = stream.readInt8();
event.frameRate = {
0x00: 24, 0x20: 25, 0x40: 29, 0x60: 30
}[hourByte & 0x60];
event.hour = hourByte & 0x1f;
event.min = stream.readInt8();
event.sec = stream.readInt8();
event.frame = stream.readInt8();
event.subframe = stream.readInt8();
return event;
case 0x58:
event.subtype = 'timeSignature';
if (length != 4) throw "Expected length for timeSignature event is 4, got " + length;
event.numerator = stream.readInt8();
event.denominator = Math.pow(2, stream.readInt8());
event.metronome = stream.readInt8();
event.thirtyseconds = stream.readInt8();
return event;
case 0x59:
event.subtype = 'keySignature';
if (length != 2) throw "Expected length for keySignature event is 2, got " + length;
event.key = stream.readInt8(true);
event.scale = stream.readInt8();
return event;
case 0x7f:
event.subtype = 'sequencerSpecific';
event.data = stream.read(length);
return event;
default:
// console.log("Unrecognised meta event subtype: " + subtypeByte);
event.subtype = 'unknown'
event.data = stream.read(length);
return event;
}
event.data = stream.read(length);
return event;
} else if (eventTypeByte == 0xf0) {
event.type = 'sysEx';
var length = stream.readVarInt();
event.data = stream.read(length);
return event;
} else if (eventTypeByte == 0xf7) {
event.type = 'dividedSysEx';
var length = stream.readVarInt();
event.data = stream.read(length);
return event;
} else {
throw "Unrecognised MIDI event type byte: " + eventTypeByte;
}
} else {
/* channel event */
var param1;
if ((eventTypeByte & 0x80) == 0) {
/* running status - reuse lastEventTypeByte as the event type.
eventTypeByte is actually the first parameter
*/
param1 = eventTypeByte;
eventTypeByte = lastEventTypeByte;
} else {
param1 = stream.readInt8();
lastEventTypeByte = eventTypeByte;
}
var eventType = eventTypeByte >> 4;
event.channel = eventTypeByte & 0x0f;
event.type = 'channel';
switch (eventType) {
case 0x08:
event.subtype = 'noteOff';
event.noteNumber = param1;
event.velocity = stream.readInt8();
return event;
case 0x09:
event.noteNumber = param1;
event.velocity = stream.readInt8();
if (event.velocity == 0) {
event.subtype = 'noteOff';
} else {
event.subtype = 'noteOn';
}
return event;
case 0x0a:
event.subtype = 'noteAftertouch';
event.noteNumber = param1;
event.amount = stream.readInt8();
return event;
case 0x0b:
event.subtype = 'controller';
event.controllerType = param1;
event.value = stream.readInt8();
return event;
case 0x0c:
event.subtype = 'programChange';
event.programNumber = param1;
return event;
case 0x0d:
event.subtype = 'channelAftertouch';
event.amount = param1;
return event;
case 0x0e:
event.subtype = 'pitchBend';
event.value = param1 + (stream.readInt8() << 7);
return event;
default:
throw "Unrecognised MIDI event type: " + eventType
}
}
}
stream = Stream(data);
var headerChunk = readChunk(stream);
if (headerChunk.id != 'MThd' || headerChunk.length != 6) {
throw "Bad .mid file - header not found";
}
var headerStream = Stream(headerChunk.data);
var formatType = headerStream.readInt16();
var trackCount = headerStream.readInt16();
var timeDivision = headerStream.readInt16();
if (timeDivision & 0x8000) {
throw "Expressing time division in SMTPE frames is not supported yet"
} else {
ticksPerBeat = timeDivision;
}
var header = {
'formatType': formatType,
'trackCount': trackCount,
'ticksPerBeat': ticksPerBeat
}
var tracks = [];
for (var i = 0; i < header.trackCount; i++) {
tracks[i] = [];
var trackChunk = readChunk(stream);
if (trackChunk.id != 'MTrk') {
throw "Unexpected chunk - expected MTrk, got "+ trackChunk.id;
}
var trackStream = Stream(trackChunk.data);
while (!trackStream.eof()) {
var event = readEvent(trackStream);
tracks[i].push(event);
//console.log(event);
}
}
return {
'header': header,
'tracks': tracks
}
}
function Replayer(midiFile, synth) {
var trackStates = [];
var beatsPerMinute = 120;
var ticksPerBeat = midiFile.header.ticksPerBeat;
var channelCount = 16;
var channels = [];
var nextEventInfo;
var samplesToNextEvent;
function Channel() {
var generatorsByNote = {};
var currentProgram = PianoProgram;
function noteOn(note, velocity) {
if (generatorsByNote[note] && !generatorsByNote[note].released) {
/* playing same note before releasing the last one. BOO */
generatorsByNote[note].noteOff(); /* TODO: check whether we ought to be passing a velocity in */
}
generator = currentProgram.createNote(note, velocity);
synth.addGenerator(generator);
generatorsByNote[note] = generator;
}
function noteOff(note, velocity) {
if (generatorsByNote[note] && !generatorsByNote[note].released) {
generatorsByNote[note].noteOff(velocity);
}
}
function setProgram(programNumber) {
currentProgram = PROGRAMS[programNumber] || PianoProgram;
}
return {
'noteOn': noteOn,
'noteOff': noteOff,
'setProgram': setProgram
}
}
function getNextEvent() {
var ticksToNextEvent = null;
var nextEventTrack = null;
var nextEventIndex = null;
for (var i = 0; i < trackStates.length; i++) {
if (
trackStates[i].ticksToNextEvent != null
&& (ticksToNextEvent == null || trackStates[i].ticksToNextEvent < ticksToNextEvent)
) {
ticksToNextEvent = trackStates[i].ticksToNextEvent;
nextEventTrack = i;
nextEventIndex = trackStates[i].nextEventIndex;
}
}
if (nextEventTrack != null) {
/* consume event from that track */
var nextEvent = midiFile.tracks[nextEventTrack][nextEventIndex];
if (midiFile.tracks[nextEventTrack][nextEventIndex + 1]) {
trackStates[nextEventTrack].ticksToNextEvent += midiFile.tracks[nextEventTrack][nextEventIndex + 1].deltaTime;
} else {
trackStates[nextEventTrack].ticksToNextEvent = null;
}
trackStates[nextEventTrack].nextEventIndex += 1;
/* advance timings on all tracks by ticksToNextEvent */
for (var i = 0; i < trackStates.length; i++) {
if (trackStates[i].ticksToNextEvent != null) {
trackStates[i].ticksToNextEvent -= ticksToNextEvent
}
}
nextEventInfo = {
'ticksToEvent': ticksToNextEvent,
'event': nextEvent,
'track': nextEventTrack
}
var beatsToNextEvent = ticksToNextEvent / ticksPerBeat;
var secondsToNextEvent = beatsToNextEvent / (beatsPerMinute / 60);
samplesToNextEvent += secondsToNextEvent * synth.sampleRate;
} else {
nextEventInfo = null;
samplesToNextEvent = null;
self.finished = true;
}
}
function generate(samples) {
var data = new Array(samples*2);
var samplesRemaining = samples;
var dataOffset = 0;
while (true) {
if (samplesToNextEvent != null && samplesToNextEvent <= samplesRemaining) {
/* generate samplesToNextEvent samples, process event and repeat */
var samplesToGenerate = Math.ceil(samplesToNextEvent);
if (samplesToGenerate > 0) {
synth.generateIntoBuffer(samplesToGenerate, data, dataOffset);
dataOffset += samplesToGenerate * 2;
samplesRemaining -= samplesToGenerate;
samplesToNextEvent -= samplesToGenerate;
}
handleEvent();
getNextEvent();
} else {
/* generate samples to end of buffer */
if (samplesRemaining > 0) {
synth.generateIntoBuffer(samplesRemaining, data, dataOffset);
samplesToNextEvent -= samplesRemaining;
}
break;
}
}
return data;
}
function handleEvent() {
var event = nextEventInfo.event;
switch (event.type) {
case 'meta':
switch (event.subtype) {
case 'setTempo':
beatsPerMinute = 60000000 / event.microsecondsPerBeat
}
break;
case 'channel':
switch (event.subtype) {
case 'noteOn':
channels[event.channel].noteOn(event.noteNumber, event.velocity);
break;
case 'noteOff':
channels[event.channel].noteOff(event.noteNumber, event.velocity);
break;
case 'programChange':
//console.log('program change to ' + event.programNumber);
channels[event.channel].setProgram(event.programNumber);
break;
}
break;
}
}
function reset() {
for (var i = 0; i < midiFile.tracks.length; i++) {
trackStates[i] = {
'nextEventIndex': 0,
'ticksToNextEvent': (
midiFile.tracks[i].length ?
midiFile.tracks[i][0].deltaTime :
null
)
};
}
for (var i = 0; i < channelCount; i++) {
channels[i] = Channel();
}
samplesToNextEvent = 0;
getNextEvent();
}
reset();
var self = {
'reset': reset,
'generate': generate,
'finished': false
}
return self;
}
/* Wrapper for accessing strings through sequential reads */
function Stream(str) {
var position = 0;
function read(length) {
var result = str.substr(position, length);
position += length;
return result;
}
/* read a big-endian 32-bit integer */
function readInt32() {
var result = (
(str.charCodeAt(position) << 24)
+ (str.charCodeAt(position + 1) << 16)
+ (str.charCodeAt(position + 2) << 8)
+ str.charCodeAt(position + 3));
position += 4;
return result;
}
/* read a big-endian 16-bit integer */
function readInt16() {
var result = (
(str.charCodeAt(position) << 8)
+ str.charCodeAt(position + 1));
position += 2;
return result;
}
/* read an 8-bit integer */
function readInt8(signed) {
var result = str.charCodeAt(position);
if (signed && result > 127) result -= 256;
position += 1;
return result;
}
function eof() {
return position >= str.length;
}
/* read a MIDI-style variable-length integer
(big-endian value in groups of 7 bits,
with top bit set to signify that another byte follows)
*/
function readVarInt() {
var result = 0;
while (true) {
var b = readInt8();
if (b & 0x80) {
result += (b & 0x7f);
result <<= 7;
} else {
/* b is the last byte */
return result + b;
}
}
}
return {
'eof': eof,
'read': read,
'readInt32': readInt32,
'readInt16': readInt16,
'readInt8': readInt8,
'readVarInt': readVarInt
}
}
function SineGenerator(freq) {
var self = {'alive': true};
var period = sampleRate / freq;
var t = 0;
self.generate = function(buf, offset, count) {
for (; count; count--) {
var phase = t / period;
var result = Math.sin(phase * 2 * Math.PI);
buf[offset++] += result;
buf[offset++] += result;
t++;
}
}
return self;
}
function SquareGenerator(freq, phase) {
var self = {'alive': true};
var period = sampleRate / freq;
var t = 0;
self.generate = function(buf, offset, count) {
for (; count; count--) {
var result = ( (t / period) % 1 > phase ? 1 : -1);
buf[offset++] += result;
buf[offset++] += result;
t++;
}
}
return self;
}
function ADSRGenerator(child, attackAmplitude, sustainAmplitude, attackTimeS, decayTimeS, releaseTimeS) {
var self = {'alive': true}
var attackTime = sampleRate * attackTimeS;
var decayTime = sampleRate * (attackTimeS + decayTimeS);
var decayRate = (attackAmplitude - sustainAmplitude) / (decayTime - attackTime);
var releaseTime = null; /* not known yet */
var endTime = null; /* not known yet */
var releaseRate = sustainAmplitude / (sampleRate * releaseTimeS);
var t = 0;
self.noteOff = function() {
if (self.released) return;
releaseTime = t;
self.released = true;
endTime = releaseTime + sampleRate * releaseTimeS;
}
self.generate = function(buf, offset, count) {
if (!self.alive) return;
var input = new Array(count * 2);
for (var i = 0; i < count*2; i++) {
input[i] = 0;
}
child.generate(input, 0, count);
childOffset = 0;
while(count) {
if (releaseTime != null) {
if (t < endTime) {
/* release */
while(count && t < endTime) {
var ampl = sustainAmplitude - releaseRate * (t - releaseTime);
buf[offset++] += input[childOffset++] * ampl;
buf[offset++] += input[childOffset++] * ampl;
t++;
count--;
}
} else {
/* dead */
self.alive = false;
return;
}
} else if (t < attackTime) {
/* attack */
while(count && t < attackTime) {
var ampl = attackAmplitude * t / attackTime;
buf[offset++] += input[childOffset++] * ampl;
buf[offset++] += input[childOffset++] * ampl;
t++;
count--;
}
} else if (t < decayTime) {
/* decay */
while(count && t < decayTime) {
var ampl = attackAmplitude - decayRate * (t - attackTime);
buf[offset++] += input[childOffset++] * ampl;
buf[offset++] += input[childOffset++] * ampl;
t++;
count--;
}
} else {
/* sustain */
while(count) {
buf[offset++] += input[childOffset++] * sustainAmplitude;
buf[offset++] += input[childOffset++] * sustainAmplitude;
t++;
count--;
}
}
}
}
return self;
}
function midiToFrequency(note) {
return 440 * Math.pow(2, (note-69)/12);
}
PianoProgram = {
'attackAmplitude': 0.2,
'sustainAmplitude': 0.1,
'attackTime': 0.02,
'decayTime': 0.3,
'releaseTime': 0.02,
'createNote': function(note, velocity) {
var frequency = midiToFrequency(note);
return ADSRGenerator(
SineGenerator(frequency),
this.attackAmplitude * (velocity / 128), this.sustainAmplitude * (velocity / 128),
this.attackTime, this.decayTime, this.releaseTime
);
}
}
StringProgram = {
'createNote': function(note, velocity) {
var frequency = midiToFrequency(note);
return ADSRGenerator(
SineGenerator(frequency),
0.5 * (velocity / 128), 0.2 * (velocity / 128),
0.4, 0.8, 0.4
);
}
}
PROGRAMS = {
41: StringProgram,
42: StringProgram,
43: StringProgram,
44: StringProgram,
45: StringProgram,
46: StringProgram,
47: StringProgram,
49: StringProgram,
50: StringProgram
};
function Synth(sampleRate) {
var generators = [];
function addGenerator(generator) {
generators.push(generator);
}
function generate(samples) {
var data = new Array(samples*2);
generateIntoBuffer(samples, data, 0);
return data;
}
function generateIntoBuffer(samplesToGenerate, buffer, offset) {
for (var i = offset; i < offset + samplesToGenerate * 2; i++) {
buffer[i] = 0;
}
for (var i = generators.length - 1; i >= 0; i--) {
generators[i].generate(buffer, offset, samplesToGenerate);
if (!generators[i].alive) generators.splice(i, 1);
}
}
return {
'sampleRate': sampleRate,
'addGenerator': addGenerator,
'generate': generate,
'generateIntoBuffer': generateIntoBuffer
}
}

3
libs/thirdparty/mid.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -334,7 +334,8 @@ ui.prototype.drawSwitchs = function() {
core.status.event.id = 'switchs';
var choices = [
"背景音乐:"+(core.musicStatus.soundStatus ? "[ON]" : "[OFF]"),
"背景音乐:"+(core.musicStatus.bgmStatus ? "[ON]" : "[OFF]"),
"背景音效:"+(core.musicStatus.soundStatus ? "[ON]" : "[OFF]"),
"战斗动画: " + (core.flags.battleAnimate ? "[ON]" : "[OFF]"),
"怪物显伤: " + (core.flags.displayEnemyDamage ? "[ON]" : "[OFF]"),
"领域显伤: " + (core.flags.displayExtraDamage ? "[ON]" : "[OFF]"),
@ -571,7 +572,7 @@ ui.prototype.drawBattleAnimate = function(monsterId, callback) {
core.fillText("ui", "S", right_start-8, 208+15, "#FFFFFF", "italic bold 40px Verdana");
var battleInterval = setInterval(function() {
core.playSound("attack", "ogg");
core.playSound("attack.ogg");
if (turn==0) {
// 勇士攻击

27
main.js
View File

@ -45,12 +45,13 @@ function main() {
];
this.images = [
'animates', 'enemys', 'hero', 'items', 'npcs', 'terrains'
// Autotile 动态添加
];
this.sounds = {
'mp3': ['bgm-loop', 'floor'],
'ogg': ['attack', 'door', 'item']
}
this.bgms = [ // 在此存放所有的bgm和文件名一致。第一项为默认播放项
'058-Slow01.mid', 'bgm.mp3', 'G2_koko.mid', 'star.mid'
];
this.sounds = [ // 在此存放所有的SE和文件名一致
'floor.mp3', 'attack.ogg', 'door.ogg', 'item.ogg'
]
this.statusBar = {
'image': {
'floor': document.getElementById('img-floor'),
@ -97,7 +98,7 @@ function main() {
// 如果要进行剧本的修改请务必将其改成false。
this.floorIds = [ // 在这里按顺序放所有的楼层;其顺序直接影响到楼层传送器的顺序和上楼器/下楼器的顺序
"sample0", "sample1", "sample2", "test"
"sample0", "sample1", "sample2"
]
//------------------------ 用户修改内容 END ------------------------//
@ -119,7 +120,7 @@ main.prototype.init = function () {
coreData[name] = main[name];
}
main.loaderFloors(function() {
main.core.init(main.dom, main.statusBar, main.canvas, main.images, main.sounds, main.floorIds, main.floors, coreData);
main.core.init(main.dom, main.statusBar, main.canvas, main.images, main.bgms, main.sounds, main.floorIds, main.floors, coreData);
main.core.resize(main.dom.body.clientWidth, main.dom.body.clientHeight);
})
});
@ -222,18 +223,6 @@ main.dom.body.onselectstart = function () {
return false;
}
document.onmousemove = function() {
try {
main.core.loadSound();
}catch (e) {}
}
document.ontouchstart = function() {
try {
main.core.loadSound();
}catch (e) {}
}
main.dom.data.onmousedown = function (e) {
try {
e.stopPropagation();

BIN
sounds/058-Slow01.mid Normal file

Binary file not shown.

BIN
sounds/G2_koko.mid Normal file

Binary file not shown.

BIN
sounds/star.mid Normal file

Binary file not shown.

0
test.js Normal file
View File