Forward plugin & drawThumbnail

This commit is contained in:
ckcz123 2019-03-14 19:27:22 +08:00
parent 56f80b0a88
commit 70075763fb
3 changed files with 115 additions and 150 deletions

View File

@ -351,7 +351,6 @@ core.prototype.init = function (coreData, callback) {
});
core.loader._load(function () {
console.log(core.material);
// 设置勇士高度
core.material.icons.hero.height = core.material.images.hero.height/4;
// 行走图
@ -369,6 +368,7 @@ core.prototype.init = function (coreData, callback) {
};
core.plugin.__init__ = functions_d6ad677b_427a_4623_b50f_a445a3b0ef8a.plugins.plugin;
core.plugin.__init__();
core._forwardFunc("plugin");
}
core.showStartAnimate();
@ -379,29 +379,25 @@ core.prototype.init = function (coreData, callback) {
}
core.prototype._forwardFuncs = function () {
var list = {};
for (var i = 0; i < main.loadList.length; ++i) {
var name = main.loadList[i];
if (name == 'core') continue;
for (var funcname in core[name]) {
if (funcname.charAt(0) != "_" && core[name][funcname] instanceof Function) {
if (list[funcname]) {
main.log("Error forward: "+name+"."+funcname);
}
else {
list[funcname] = name;
}
}
}
}
for (var funcname in list) {
this._forwardFunc(list[funcname], funcname);
this._forwardFunc(name);
}
}
core.prototype._forwardFunc = function (name, funcname) {
if (funcname == null) {
for (funcname in core[name]) {
if (funcname.charAt(0) != "_" && core[name][funcname] instanceof Function) {
this._forwardFunc(name, funcname);
}
}
return;
}
if (core[funcname]) {
main.log("Error in forwarding "+funcname+" from "+name+"!");
console.error("ERROR: Cannot forward function "+funcname+" from "+name+"!");
return;
}
var parameterInfo = /^\s*function\s*[\w_$]*\(([\w_,$\s]*)\)\s*\{/.exec(core[name][funcname].toString());

View File

@ -895,6 +895,107 @@ maps.prototype._makeAutotileEdges = function () {
});
}
////// 绘制缩略图 //////
// 此函数将绘制一个缩略图floorId为目标floorIdblocks为地图的图块可为null使用floorId对应默认的
// options为绘制选项可为null包括
// heroLoc: 勇士位置heroIcon勇士图标damage是否绘制显伤flags当前的flags存读档时使用
// toDraw为要绘制到的信息可为null或为一个画布名包括
// ctx要绘制到的画布x,y起点横纵坐标size大小all是否绘制全图centerX,centerY截取中心
maps.prototype.drawThumbnail = function (floorId, blocks, options, toDraw) {
floorId = floorId || core.status.floorId;
if (!core.isset(floorId)) return;
// Step1绘制到tempCanvas上
this._drawThumbnail_drawTempCanvas(floorId, blocks, options);
// Step2从tempCanvas绘制到对应的画布上
this._drawThumbnail_drawToTarget(floorId, toDraw);
}
maps.prototype._drawThumbnail_drawTempCanvas = function (floorId, blocks, options) {
if (!core.isset(blocks)) blocks = core.status.maps[floorId].blocks;
if (!core.isset(options)) options = {};
var width = core.floors[floorId].width;
var height = core.floors[floorId].height;
// 绘制到tempCanvas上面
var tempCanvas = core.bigmap.tempCanvas;
var tempWidth = width * 32, tempHeight = height * 32;
tempCanvas.canvas.width = tempWidth;
tempCanvas.canvas.height = tempHeight;
tempCanvas.clearRect(0, 0, tempWidth, tempHeight);
// --- 暂存 flags
var hasHero = core.isset(core.status.hero), flags = null;
if (options.flags) {
if (!hasHero) core.status.hero = {};
flags = core.status.hero.flags;
core.status.hero.flags = options.flags;
}
this._drawThumbnail_realDrawTempCanvas(floorId, blocks, options, tempCanvas);
// --- 恢复 flags
if (!hasHero) delete core.status.hero;
else if (flags != null) core.status.hero.flags = flags;
}
maps.prototype._drawThumbnail_realDrawTempCanvas = function (floorId, blocks, options, tempCanvas) {
// 缩略图:背景
this.drawBg(floorId, tempCanvas);
// 缩略图:事件
this.drawEvents(floorId, blocks, tempCanvas);
// 缩略图:勇士
if (options.heroLoc) {
options.heroIcon = options.heroIcon || "hero.png";
var icon = core.material.icons.hero[options.heroLoc.direction];
var height = core.material.images.images[options.heroIcon].height/4;
tempCanvas.drawImage(core.material.images.images[options.heroIcon], icon.stop * 32, icon.loc * height, 32, height,
32 * options.heroLoc.x, 32 * options.heroLoc . y + 32 - height, 32, height);
}
// 缩略图:前景
this.drawFg(floorId, tempCanvas);
// 缩略图:显伤
if (options.damage)
core.control.updateDamage(floorId, tempCanvas);
}
maps.prototype._drawThumbnail_drawToTarget = function (floorId, toDraw) {
if (!core.isset(toDraw)) return;
if (typeof toDraw == 'string' || toDraw.canvas) toDraw = {ctx: toDraw};
var ctx = core.getContextByName(toDraw.ctx);
if (ctx == null) return;
var x = toDraw.x || 0, y = toDraw.y || 0, size = toDraw.size || this.DEFAULT_PIXEL_WIDTH;
var width = core.floors[floorId].width, height = core.floors[floorId].height;
var centerX = toDraw.centerX, centerY = toDraw.centerY;
if (!core.isset(centerX)) centerX = Math.floor(width/2);
if (!core.isset(centerY)) centerY = Math.floor(height/2);
var tempCanvas = core.bigmap.tempCanvas, tempWidth = 32 * width, tempHeight = 32 * height;
core.clearMap(ctx, x, y, size, size);
if (toDraw.all) {
// 绘制全景图
if (tempWidth<=tempHeight) {
var realHeight = size, realWidth = realHeight * tempWidth / tempHeight;
var side = (size - realWidth) / 2;
core.fillRect(ctx, x, y, side, realHeight, '#000000');
core.fillRect(ctx, x + size - side, y, side, realHeight);
ctx.drawImage(tempCanvas.canvas, 0, 0, tempWidth, tempHeight, x + side, y, realWidth, realHeight);
}
else {
var realWidth = size, realHeight = realWidth * tempHeight / tempWidth;
var side = (size - realHeight) / 2;
core.fillRect(ctx, x, y, realWidth, side, '#000000');
core.fillRect(ctx, x, y + size - side, realWidth, side);
ctx.drawImage(tempCanvas.canvas, 0, 0, tempWidth, tempHeight, x, y + side, realWidth, realHeight);
}
}
else {
// 只绘制可见窗口
var halfWidth = parseInt(this.DEFAULT_WIDTH / 2), halfHeight = parseInt(this.DEFAULT_HEIGHT / 2);
var offsetX = core.clamp(centerX - halfWidth, 0, width - this.DEFAULT_WIDTH), offsetY = core.clamp(centerY - halfHeight, 0, height - this.DEFAULT_HEIGHT);
ctx.drawImage(tempCanvas.canvas, offsetX * 32, offsetY * 32, this.DEFAULT_PIXEL_WIDTH, this.DEFAULT_PIXEL_HEIGHT, x, y, size, size);
}
}
// -------- 获得某个点的图块信息 -------- //
////// 某个点是否不可通行 //////

View File

@ -2159,140 +2159,8 @@ ui.prototype.drawSLPanel = function(index, refresh) {
////// 绘制一个缩略图 //////
ui.prototype.drawThumbnail = function(floorId, canvas, blocks, x, y, size, centerX, centerY, heroLoc, heroIcon) {
var mw = core.floors[floorId].width;
var mh = core.floors[floorId].height;
// 绘制到tempCanvas上面
var tempCanvas = core.bigmap.tempCanvas;
var tempWidth = mw*32, tempHeight = mh*32;
tempCanvas.canvas.width = tempWidth;
tempCanvas.canvas.height = tempHeight;
tempCanvas.clearRect(0, 0, tempWidth, tempHeight);
// -------- 1. 绘制地板
var groundId = (core.status.maps||core.floors)[floorId].defaultGround || "ground";
var blockIcon = core.material.icons.terrains[groundId];
for (var i = 0; i < mw; i++) {
for (var j = 0; j < mh; j++) {
tempCanvas.drawImage(core.material.images.terrains, 0, blockIcon * 32, 32, 32, i * 32, j * 32, 32, 32);
}
}
var images = [];
if (core.isset((core.status.maps||core.floors)[floorId].images)) {
images = (core.status.maps||core.floors)[floorId].images;
if (typeof images == 'string') {
images = [[0, 0, images]];
}
}
// -------- 2. 绘制背景贴图
images.forEach(function (t) {
if (typeof t == 'string') t = [0,0,t];
var dx=parseInt(t[0]), dy=parseInt(t[1]), p=t[2], frame = core.clamp(parseInt(t[4]), 1, 8);
if (core.isset(dx) && core.isset(dy) &&
!core.hasFlag("__floorImg__"+floorId+"_"+dx+"_"+dy) &&
core.isset(core.material.images.images[p])) {
var image = core.material.images.images[p];
var width = image.width / frame, height = image.height;
if (!t[3])
tempCanvas.drawImage(image, 0, 0, width, height, dx, dy, width, height);
else if (t[3]==2)
tempCanvas.drawImage(image, 0, height-32, width, 32, dx, dy + height - 32, width, 32);
}
})
// -------- 3. 绘制背景图块
core.maps.drawBgFgMap(floorId, tempCanvas, "bg");
// -------- 4. 绘制事件层
var mapArray = core.maps.getMapArray(blocks,mw,mh);
for (var b in blocks) {
var block = blocks[b];
if (core.isset(block.event) && !block.disable) {
if (block.event.cls == 'autotile') {
core.drawAutotile(tempCanvas, mapArray, block, 32, 0, 0);
}
else if (block.event.cls == 'tileset') {
var offset = core.icons.getTilesetOffset(block.event.id);
if (offset!=null) {
tempCanvas.drawImage(core.material.images.tilesets[offset.image], 32*offset.x, 32*offset.y, 32, 32, 32*block.x, 32*block.y, 32, 32);
}
}
else if (block.id==17) {
if (core.isset(core.material.images.airwall)) {
tempCanvas.drawImage(core.material.images.airwall, 32*block.x, 32*block.y);
}
}
else if (block.event.id!='none') {
var blockIcon = core.material.icons[block.event.cls][block.event.id];
var blockImage = core.material.images[block.event.cls];
var height = block.event.height || 32;
tempCanvas.drawImage(blockImage, 0, blockIcon * height, 32, height, 32*block.x, 32*block.y + 32 - height, 32, height);
}
}
}
// -------- 5. 绘制勇士
if (core.isset(heroLoc)) {
if (!core.isset(core.material.images.images[heroIcon]))
heroIcon = "hero.png";
var icon = core.material.icons.hero[heroLoc.direction];
var height = core.material.images.images[heroIcon].height/4;
tempCanvas.drawImage(core.material.images.images[heroIcon], icon.stop * 32, icon.loc * height, 32, height, 32*heroLoc.x, 32*heroLoc.y+32-height, 32, height);
}
// -------- 6. 绘制前景贴图
images.forEach(function (t) {
var dx=parseInt(t[0]), dy=parseInt(t[1]), p=t[2], frame = core.clamp(parseInt(t[4]), 1, 8);
if (core.isset(dx) && core.isset(dy) &&
!core.hasFlag("__floorImg__"+floorId+"_"+dx+"_"+dy) &&
core.isset(core.material.images.images[p])) {
var image = core.material.images.images[p];
var width = image.width / frame, height = image.height;
if (t[3]==1)
tempCanvas.drawImage(image, 0, 0, width, height, dx, dy, width, height);
else if (t[3]==2)
tempCanvas.drawImage(image, 0, 0, width, height-32, dx, dy, width, height-32);
}
})
// -------- 7. 绘制前景图块
core.maps.drawBgFgMap(floorId, tempCanvas, "fg");
// -------- 8. 绘制显伤
if (core.status.event.id=='viewMaps' && (core.status.event.data||{}).damage)
core.control.updateDamage(floorId, tempCanvas);
var ctx = core.getContextByName(canvas);
if (ctx == null) return;
// draw to canvas
core.clearMap(canvas, x, y, size, size);
if (!core.isset(centerX)) centerX=parseInt(mw/2);
if (!core.isset(centerY)) centerY=parseInt(mh/2);
// 如果是浏览地图的全模式
if (core.status.event.id=='viewMaps' && (core.status.event.data||{}).all) {
if (tempWidth<=tempHeight) {
var realHeight = 416, realWidth = realHeight * tempWidth / tempHeight;
var side = (416 - realWidth) / 2;
core.fillRect(canvas, 0, 0, side, realHeight, '#000000');
core.fillRect(canvas, 416-side, 0, side, realHeight);
ctx.drawImage(tempCanvas.canvas, 0, 0, tempWidth, tempHeight, side, 0, realWidth, realHeight);
}
else {
var realWidth = 416, realHeight = realWidth * tempHeight / tempWidth;
var side = (416 - realHeight) / 2;
core.fillRect(canvas, 0, 0, realWidth, side, '#000000');
core.fillRect(canvas, 0, 416-side, realWidth, side);
ctx.drawImage(tempCanvas.canvas, 0, 0, tempWidth, tempHeight, 0, side, realWidth, realHeight);
}
}
else {
var offsetX = core.clamp(centerX-6, 0, mw-13), offsetY = core.clamp(centerY-6, 0, mh-13);
// offsetX~offsetX+12; offsetY~offsetY+12
ctx.drawImage(tempCanvas.canvas, offsetX*32, offsetY*32, 416, 416, x, y, size, size);
}
core.drawThumbnail(floorId, blocks, {heroLoc: heroLoc, heroIcon: heroIcon},
{ctx: canvas, x: x, y: y, size: size, centerX: centerX, centerY: centerY});
}
ui.prototype.drawKeyBoard = function () {