Support Animate

This commit is contained in:
oc committed 2018-02-06 13:36:18 +08:00
1 parent 99463c6e5e
commit d02f9df848
18 files changed
+285 -78

No files matched your search

+155 -51
View File
@@ -3,17 +3,8 @@
*/
function core() {
this.dom = {};
this.statusBar = {};
this.canvas = {};
this.images = [];
this.pngs = [];
this.bgms = [];
this.sounds = [];
this.floorIds = [];
this.floors = {};
this.firstData = {};
this.material = {
'animates': {},
'images': {},
'bgms': {},
'sounds': {},
@@ -31,7 +22,8 @@ function core() {
'heroMoveTriggerInterval': null,
'heroMoveInterval': null,
"tipAnimate": null,
'openDoorAnimate': null
'openDoorAnimate': null,
'animateInterval': null,
}
this.animateFrame = {
'background': null,
@@ -147,22 +139,12 @@ function core() {
'boxAnimateObjs': [],
};
this.status = {};
this.flags = {};
}
/////////// 系统事件相关 ///////////
////// 初始化 //////
core.prototype.init = function (dom, statusBar, canvas, images, pngs, bgms, sounds, floorIds, floors, coreData) {
core.dom = dom;
core.statusBar = statusBar;
core.canvas = canvas;
core.images = images;
core.pngs = pngs;
core.bgms = bgms;
core.sounds = sounds;
core.floorIds = floorIds;
core.floors = floors;
core.prototype.init = function (coreData) {
for (var key in coreData) {
core[key] = coreData[key];
}
@@ -187,7 +169,7 @@ core.prototype.init = function (dom, statusBar, canvas, images, pngs, bgms, soun
document.title = core.firstData.title + " - HTML5魔塔";
document.getElementById("startLogo").innerHTML = core.firstData.title;
core.material.items = core.items.getItems();
core.initStatus.maps = core.maps.initMaps(floorIds);
core.initStatus.maps = core.maps.initMaps(core.floorIds);
core.material.enemys = core.clone(core.enemys.getEnemys());
core.material.icons = core.icons.getIcons();
core.material.events = core.events.getEvents();
@@ -386,7 +368,7 @@ core.prototype.setRequestAnimationFrame = function () {
// weather
if (core.isPlaying() && timestamp-core.animateFrame.weather.time>30) {
if (core.animateFrame.weather.type=='rain' && core.animateFrame.weather.level>0) {
if (core.animateFrame.weather.type == 'rain' && core.animateFrame.weather.level > 0) {
core.clearMap('weather', 0, 0, 416, 416);
@@ -402,8 +384,8 @@ core.prototype.setRequestAnimationFrame = function () {
p.x += p.xs;
p.y += p.ys;
if (p.x>416 || p.y>416) {
p.x = Math.random()*416;
if (p.x > 416 || p.y > 416) {
p.x = Math.random() * 416;
p.y = -10;
}
@@ -412,7 +394,7 @@ core.prototype.setRequestAnimationFrame = function () {
core.canvas.weather.fill();
}
else if (core.animateFrame.weather.type=='snow' && core.animateFrame.weather.level>0) {
else if (core.animateFrame.weather.type == 'snow' && core.animateFrame.weather.level > 0) {
core.clearMap('weather', 0, 0, 416, 416);
@@ -421,30 +403,30 @@ core.prototype.setRequestAnimationFrame = function () {
if (!core.isset(core.animateFrame.weather.data))
core.animateFrame.weather.data = 0;
core.animateFrame.weather.data+=0.01;
core.animateFrame.weather.data += 0.01;
var angle = core.animateFrame.weather.data;
core.animateFrame.weather.nodes.forEach(function (p) {
core.canvas.weather.moveTo(p.x, p.y);
core.canvas.weather.arc(p.x, p.y, p.r, 0, Math.PI*2, true);
core.canvas.weather.arc(p.x, p.y, p.r, 0, Math.PI * 2, true);
// update
p.x += Math.sin(angle) * 2;
p.y += Math.cos(angle+p.d) + 1 + p.r/2;
p.y += Math.cos(angle + p.d) + 1 + p.r / 2;
if (p.x>416+5 || p.x<-5 || p.y>416) {
if (Math.random()>1/3) {
p.x = Math.random()*416;
if (p.x > 416 + 5 || p.x < -5 || p.y > 416) {
if (Math.random() > 1 / 3) {
p.x = Math.random() * 416;
p.y = -10;
}
else {
if (Math.sin(angle)>0) {
if (Math.sin(angle) > 0) {
p.x = -5;
p.y = Math.random()*416;
p.y = Math.random() * 416;
}
else {
p.x = 416+5;
p.y=Math.random()*416;
p.x = 416 + 5;
p.y = Math.random() * 416;
}
}
}
@@ -457,20 +439,11 @@ core.prototype.setRequestAnimationFrame = function () {
core.animateFrame.weather.time = timestamp;
}
window.requestAnimationFrame(draw);
}
window.requestAnimationFrame(draw);
}
////// 显示游戏开始界面 //////
core.prototype.showStartAnimate = function (callback) {
core.dom.startPanel.style.opacity=1;
@@ -564,7 +537,7 @@ core.prototype.loadAutotile = function (callback) {
core.material.images.autotile={};
var autotileIds = Object.keys(core.material.icons.autotile);
if (autotileIds.length==0) {
core.loadMusic(callback);
core.loadAnimates(callback);
return;
}
for (var x=0;x<autotileIds.length;x++) {
@@ -572,8 +545,8 @@ core.prototype.loadAutotile = function (callback) {
core.material.images.autotile[autotileId]=image;
if (Object.keys(core.material.images.autotile).length==autotileIds.length) {
// 最后加载音频
core.loadMusic(callback);
// 加载动画
core.loadAnimates(callback);
}
})
}
@@ -601,6 +574,70 @@ core.prototype.loadImage = function (imgName, callback) {
}
}
////// 加载动画 //////
core.prototype.loadAnimates = function (callback) {
core.animates.forEach(function (t) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'animates/' + t + ".animate", true);
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.onload = function (e) {
var content = this.responseText;
try {
content = JSON.parse(content);
var data = {};
data.ratio = content.ratio;
data.images = [];
content.bitmaps.forEach(function (t2) {
if (!core.isset(t2) || t2=="") {
data.images.push(null);
}
else {
try {
var image = new Image();
image.src = t2;
data.images.push(image);
} catch (e) {
data.images.push(null);
}
}
})
data.frame = content.frame_max;
data.frames = [];
content.frames.forEach(function (t2) {
var info = [];
t2.forEach(function (t3) {
info.push({
'index': t3[0],
'x': t3[1],
'y': t3[2],
'zoom': t3[3],
'opacity': t3[4]
})
})
data.frames.push(info);
})
core.material.animates[t] = data;
}
catch (ee) {
console.log(ee);
core.material.animates[t]=null;
}
}
xhr.ontimeout = function(e) {
console.log(e);
core.material.animates[t]=null;
}
xhr.onerror = function (e) {
console.log(e);
core.material.animates[t]=null;
}
xhr.send();
})
core.loadMusic(callback);
}
////// 加载音频 //////
core.prototype.loadMusic = function (callback) {
@@ -2212,7 +2249,16 @@ core.prototype.battle = function (id, x, y, force, callback) {
});
}
else {
core.playSound('attack.ogg');
if (core.flags.equipment && core.getFlag('sword', 'sword0')!='sword0') {
core.playSound('zone.ogg');
core.drawAnimate('sword', x, y);
}
else {
core.playSound('attack.ogg');
core.drawAnimate('hand', x, y);
}
core.afterBattle(id, x, y, callback);
}
}
@@ -2917,7 +2963,8 @@ core.prototype.showBlock = function(x, y, floodId) {
blockImage = core.material.images[block.event.cls];
core.canvas.event.drawImage(core.material.images[block.event.cls], 0, blockIcon * 32, 32, 32, block.x * 32, block.y * 32, 32, 32);
core.addGlobalAnimate(block.event.animate, block.x * 32, block.y * 32, blockIcon, blockImage);
core.setGlobalAnimate(core.values.animateSpeed);
// core.setGlobalAnimate(core.values.animateSpeed);
core.syncGlobalAnimate();
}
}
}
@@ -3066,6 +3113,59 @@ core.prototype.drawBoxAnimate = function () {
}
}
////// 绘制动画 //////
core.prototype.drawAnimate = function (name, x, y, callback) {
// 正在播放录像:不显示动画
if (core.isset(core.status.replay) && core.status.replay.replaying) {
if (core.isset(callback)) callback();
return;
}
// 检测动画是否存在
if (!core.isset(core.material.animates[name]) || !core.isset(x) || !core.isset(y)) {
if (core.isset(callback)) callback();
return;
}
// 清空animate层
clearInterval(core.interval.animateInterval);
core.clearMap('animate', 0, 0, 416, 416);
// 开始绘制
var animate = core.material.animates[name];
var ratio = animate.ratio;
var centerX = 32*x+16, centerY = 32*y+16;
var index=0;
var draw = function (index) {
core.clearMap('animate', 0, 0, 416, 416);
var frame = animate.frames[index];
frame.forEach(function (t) {
var image = animate.images[t.index];
if (!core.isset(image)) return;
var realWidth = image.width * ratio * t.zoom / 100;
var realHeight = image.height * ratio * t.zoom / 100;
core.setAlpha('animate', t.opacity / 255);
core.canvas.animate.drawImage(image, centerX+t.x-realWidth/2, centerY+t.y-realHeight/2, realWidth, realHeight);
})
}
draw(index++);
core.interval.animateInterval = setInterval(function (t) {
if (index == animate.frames.length) {
clearInterval(core.interval.animateInterval);
core.clearMap('animate', 0, 0, 416, 416);
core.setAlpha('animate', 1);
if (core.isset(callback)) callback();
return;
}
draw(index++);
}, 50);
}
////// 更新领域、夹击、阻击的伤害地图 //////
core.prototype.updateCheckBlock = function() {
core.status.checkBlock = {};
@@ -3197,6 +3297,10 @@ core.prototype.checkBlock = function () {
else if (damage>0) {
core.drawTip('受到领域伤害'+damage+'点');
}
core.playSound('zone.ogg');
core.drawAnimate("zone", x, y);
if (core.status.hero.hp<=0) {
core.status.hero.hp=0;
core.updateStatusBar();
+17 -2
View File
@@ -334,6 +334,21 @@ events.prototype.doAction = function() {
}
else this.doAction();
break;
case "animate": // 显示动画
if (core.isset(data.loc)) {
if (data.loc == 'hero') {
x=core.getHeroLoc('x');
y=core.getHeroLoc('y');
}
else if (data.loc instanceof Array) {
x=data.loc[0];
y=data.loc[1];
}
}
core.drawAnimate(data.name, x, y, function () {
core.events.doAction();
})
break;
case "move": // 移动事件
if (core.isset(data.loc)) {
x=data.loc[0];
@@ -367,8 +382,8 @@ events.prototype.doAction = function() {
this.doAction();
break;
case "showImage": // 显示图片
if (core.isset(data.name) && core.isset(data.x) && core.isset(data.y) && core.isset(core.material.images.pngs[data.name])) {
core.canvas.animate.drawImage(core.material.images.pngs[data.name], data.x, data.y);
if (core.isset(data.loc) && core.isset(core.material.images.pngs[data.name])) {
core.canvas.animate.drawImage(core.material.images.pngs[data.name], data.loc[0], data.loc[1]);
}
else core.clearMap('animate', 0, 0, 416, 416);
this.doAction();
+1 -1
View File
@@ -10,7 +10,7 @@ main.floors.MT0 = {
"defaultGround": "ground", // 默认地面的图块ID(terrains中)
"png": [], // 该层默认显示的所有图片;详细用法请查看文档“自定义素材”中的说明。
// "color": [0,0,0,0.3], // 该层的默认画面色调。本项可不写(代表无色调),如果写需要是一个RGBA数组。
// "weather": ["snow",5], // 该层的默认天气。本项可不写,如果写则第一项为"rain"或"snow"代表雨雪,第二项为1-10之间的数代表强度。
// "weather": ["snow",5], // 该层的默认天气。本项可忽略表示晴天,如果写则第一项为"rain"或"snow"代表雨雪,第二项为1-10之间的数代表强度。
// "bgm": "bgm.mp3", // 到达该层后默认播放的BGM。本项可忽略。
"map": [ // 地图数据,需要是13x13,建议使用地图生成器来生成
+1 -1
View File
@@ -10,7 +10,7 @@ main.floors.sample0 = {
"defaultGround": "ground", // 默认地面的图块ID(terrains中)
"png": [], // 该层默认显示的所有图片;详细用法请查看文档“自定义素材”中的说明。
// "color": [0,0,0,0.3] // 该层的默认画面色调。本项可不写(代表无色调),如果写需要是一个RGBA数组。
// "weather": ["snow",5], // 该层的默认天气。本项可不写,如果写则第一项为"rain"或"snow"代表雨雪,第二项为1-10之间的数代表强度。
// "weather": ["snow",5], // 该层的默认天气。本项可忽略表示晴天,如果写则第一项为"rain"或"snow"代表雨雪,第二项为1-10之间的数代表强度。
"bgm": "bgm.mp3", // 到达该层后默认播放的BGM。本项可忽略。
"map": [ // 地图数据,需要是13x13,建议使用地图生成器来生成
[0, 0, 220, 0, 0, 20, 87, 3, 65, 64, 44, 43, 42],
+4 -4
View File
@@ -8,9 +8,9 @@ main.floors.sample1 = {
"canFlyTo": true, // 该楼能否被楼传器飞到(不能的话在该楼也不允许使用楼传器)
"canUseQuickShop": true, // 该层是否允许使用快捷商店
"defaultGround": "grass", // 默认地面的图块ID(terrains中)
"png": [[0,0,"bg.png"]], // // 该层默认显示的所有图片;详细用法请查看文档“自定义素材”中的说明。
"png": [[0,0,"bg"]], // // 该层默认显示的所有图片;详细用法请查看文档“自定义素材”中的说明。
// "color": [0,0,0,0.3] // 该层的默认画面色调。本项可不写(代表无色调),如果写需要是一个RGBA数组。
"weather": ["snow",6], // 该层的默认天气。本项可不写,如果写则第一项为"rain"或"snow"代表雨雪,第二项为1-10之间的数代表强度。
"weather": ["snow",6], // 该层的默认天气。本项可忽略表示晴天,如果写则第一项为"rain"或"snow"代表雨雪,第二项为1-10之间的数代表强度。
// "bgm": "bgm.mp3", // 到达该层后默认播放的BGM。本项可忽略。
"map": [ // 地图数据,需要是13x13,建议使用地图生成器来生成
[7, 131, 8, 152, 9, 130, 10, 152, 166, 165, 132, 165, 166],
@@ -35,8 +35,8 @@ main.floors.sample1 = {
"4,10": [ // 走到中间时的提示
"\t[样板提示]本层楼将会对各类事件进行介绍。",
"左边是一个仿50层的陷阱做法,上方是商店、快捷商店的使用方法,右上是一个典型的杀怪开门的例子,右下是各类可能的NPC事件。",
"本样板目前支持的事件列表大致有:\ntext: 显示一段文字(比如你现在正在看到的)\ntip: 左上角显示提示\nshow: 使一个事件有效(可见、可被交互)\nhide: 使一个事件失效(不可见、不可被交互)\ntrigger: 触发另一个地点的事件\nbattle: 强制和某怪物战斗\nopenDoor: 无需钥匙开门(例如机关门、暗墙)",
"openShop: 打开一个全局商店\ndisableShop: 禁用一个全局商店\nchangeFloor: 传送勇士到某层某位置\nchangePos: 传送勇士到当层某位置;转向\nsetFg: 更改画面色调\nmove: 移动事件效果\nmoveHero: 移动勇士效果\nplayBgm: 播放某个背景音乐\npauseBgm: 暂停背景音乐\nresumeBgm: 恢复背景音乐的播放\nplaySound: 播放某个音频",
"本样板目前支持的事件列表大致有:\ntext: 显示一段文字(比如你现在正在看到的)\ntip: 左上角显示提示\nshow: 使一个事件有效(可见、可被交互)\nhide: 使一个事件失效(不可见、不可被交互)\ntrigger: 触发另一个地点的事件\nanimate: 显示动画\nbattle: 强制和某怪物战斗\nopenDoor: 无需钥匙开门(例如机关门、暗墙)",
"openShop: 打开一个全局商店\ndisableShop: 禁用一个全局商店\nchangeFloor: 传送勇士到某层某位置\nchangePos: 传送勇士到当层某位置;转向\nshowImage: 显示图片\nsetFg: 更改画面色调\nsetWeather: 更改天气\nmove: 移动事件效果\nmoveHero: 移动勇士效果\nplayBgm: 播放某个背景音乐\npauseBgm: 暂停背景音乐\nresumeBgm: 恢复背景音乐的播放\nplaySound: 播放某个音频",
"if: 条件判断\nchoices: 提供选项\nsetValue: 设置勇士属性道具,或某个变量/flag\nupdate: 更新状态栏和地图显伤\nwin: 获得胜利(游戏通关)\nlose: 游戏失败\nsleep: 等待多少毫秒\nexit: 立刻结束当前事件\nrevisit: 立刻结束事件并重新触发\nfunction: 自定义JS脚本\n\n更多支持的事件还在编写中,欢迎您宝贵的意见。",
"有关各事件的样例,可参见本层一些NPC的写法。\n所有事件样例本层都有介绍。\n\n一个自定义事件处理完后,需要调用{\"type\": \"hide\"}该事件才不会再次出现。",
{"type": "hide"}
+18 -3
View File
@@ -10,7 +10,7 @@ main.floors.sample2 = {
"defaultGround": "snowGround", // 默认地面的图块ID(terrains中)
"png": [], // // 该层默认显示的所有图片;详细用法请查看文档“自定义素材”中的说明。
"color": [255,0,0,0.3], // 该层的默认画面色调。本项可不写(代表无色调),如果写需要是一个RGBA数组。
"weather": ["rain",10], // 该层的默认天气。本项可不写,如果写则第一项为"rain"或"snow"代表雨雪,第二项为1-10之间的数代表强度。
"weather": ["rain",10], // 该层的默认天气。本项可忽略表示晴天,如果写则第一项为"rain"或"snow"代表雨雪,第二项为1-10之间的数代表强度。
"bgm": "qianjin.mid", // 到达该层后默认播放的BGM。本项可忽略。
"map": [ // 地图数据,需要是13x13,建议使用地图生成器来生成
[5, 5, 5, 5, 5, 5, 87, 5, 5, 5, 5, 5, 5],
@@ -28,8 +28,7 @@ main.floors.sample2 = {
[5, 5, 5, 5, 5, 5, 88, 5, 5, 5, 5, 5, 5],
],
"firstArrive": [ // 第一次到该楼层触发的事件
"\t[实战!]本楼将尝试复刻《宿命的旋律》40F剧情。",
"由于暂不支持一些动画效果,例如雷电、振动、天气渲染等等,因此做出来的效果远远比不上原版。\n\n不过作为抛砖引玉,还是能展示一下H5的能力。\n(开音效食用更加)"
"\t[实战!]本楼将尝试复刻《宿命的旋律》40F剧情。"
],
"events": { // 该楼的所有可能事件列表
@@ -182,6 +181,7 @@ main.floors.sample2 = {
{"type": "show", "loc": [8,3], "time": 500}, // 依次显示四个角的法师
{"type": "sleep", "time": 500},
"\t[blackMagician]感受绝望吧!冥顽不化的蠢货!",
/*
{"type": "hide", "loc": [4,3], "time": 150}, // 由于没有动画效果,暂时使用“闪一下”表示
{"type": "show", "loc": [4,3], "time": 150},
{"type": "hide", "loc": [4,6], "time": 150}, // 由于没有动画效果,暂时使用“闪一下”表示
@@ -190,8 +190,14 @@ main.floors.sample2 = {
{"type": "show", "loc": [8,6], "time": 150},
{"type": "hide", "loc": [8,3], "time": 150}, // 由于没有动画效果,暂时使用“闪一下”表示
{"type": "show", "loc": [8,3], "time": 150},
*/
{"type": "animate", "name": "yongchang", "loc": [4,3]},
{"type": "animate", "name": "yongchang", "loc": [4,6]},
{"type": "animate", "name": "yongchang", "loc": [8,6]},
{"type": "animate", "name": "yongchang", "loc": [8,3]},
{"type": "sleep", "time": 200},
{"type": "playSound", "name": "attack.ogg"}, // 播放攻击音效
{"type": "animate", "name": "thunder", "loc": "hero"},
{"type": "sleep", "time": 200},
"\t[hero]唔……!!(吐血)",
{"type": "playSound", "name": "item.ogg"},
@@ -267,6 +273,7 @@ main.floors.sample2 = {
"\t[小妖精,fairy]别小瞧咱!咱好歹也是妖精族里实力数一数二的存在!",
{"type": "playSound", "name": "item.ogg"},
"\t[blackMagician]只会耍嘴皮子的恼人苍蝇!我倒要看看一块焦炭会不会说话!\n——招雷弹!!",
/*
{"type": "hide", "loc": [4,3], "time": 150}, // 由于没有动画效果,暂时使用“闪一下”表示
{"type": "show", "loc": [4,3], "time": 150},
{"type": "hide", "loc": [4,6], "time": 150}, // 由于没有动画效果,暂时使用“闪一下”表示
@@ -275,9 +282,17 @@ main.floors.sample2 = {
{"type": "show", "loc": [8,6], "time": 150},
{"type": "hide", "loc": [8,3], "time": 150}, // 由于没有动画效果,暂时使用“闪一下”表示
{"type": "show", "loc": [8,3], "time": 150},
*/
{"type": "animate", "name": "yongchang", "loc": [4,3]},
{"type": "animate", "name": "yongchang", "loc": [4,6]},
{"type": "animate", "name": "yongchang", "loc": [8,6]},
{"type": "animate", "name": "yongchang", "loc": [8,3]},
{"type": "playSound", "name": "attack.ogg"}, // 播放攻击音效
/*
{"type": "hide", "loc": [6,6], "time": 150}, // 妖精也闪一下表示收到了伤害
{"type": "show", "loc": [6,6], "time": 150}, // 妖精也闪一下表示收到了伤害
*/
{"type": "animate", "name": "thunder", "loc": [6,6]},
{"type": "sleep", "time": 500}, // 等待500毫秒
"\t[小妖精,fairy]切,这点伤痛跟他刚才经历的身心地狱相比根本就不算什么。",
{"type": "playSound", "name": "item.ogg"},