refactor: 把大量parseInt取整换为Math.floor

This commit is contained in:
unanmed 2024-11-07 20:43:50 +08:00
parent 844651ef34
commit 1f09332cb5
7 changed files with 82 additions and 94 deletions

View File

@ -365,22 +365,6 @@ actions.prototype._sys_keyDown_lockControl = function (keyCode) {
};
actions.prototype._sys_keyDown = function (keyCode) {
if (!core.status.played) return true;
return true;
switch (keyCode) {
case 37:
core.moveHero('left');
break;
case 38:
core.moveHero('up');
break;
case 39:
core.moveHero('right');
break;
case 40:
core.moveHero('down');
break;
}
return true;
};
@ -496,10 +480,10 @@ actions.prototype._sys_keyUp = function (keyCode, altKey) {
////// 点击(触摸)事件按下时 //////
actions.prototype.ondown = function (loc) {
var x = parseInt(loc.x / loc.size),
y = parseInt(loc.y / loc.size);
var px = parseInt(loc.x / core.domStyle.scale),
py = parseInt(loc.y / core.domStyle.scale);
var x = Math.floor(loc.x / loc.size),
y = Math.floor(loc.y / loc.size);
var px = Math.floor(loc.x / core.domStyle.scale),
py = Math.floor(loc.y / core.domStyle.scale);
this.doRegisteredAction('ondown', x, y, px, py);
};
@ -628,10 +612,10 @@ actions.prototype._sys_ondown = function (x, y, px, py) {
////// 当在触摸屏上滑动时 //////
actions.prototype.onmove = function (loc) {
var x = parseInt(loc.x / loc.size),
y = parseInt(loc.y / loc.size);
var px = parseInt(loc.x / core.domStyle.scale),
py = parseInt(loc.y / core.domStyle.scale);
var x = Math.floor(loc.x / loc.size),
y = Math.floor(loc.y / loc.size);
var px = Math.floor(loc.x / core.domStyle.scale),
py = Math.floor(loc.y / core.domStyle.scale);
this.doRegisteredAction('onmove', x, y, px, py);
};
@ -681,8 +665,8 @@ actions.prototype._sys_onmove = function (x, y, px, py) {
if ((core.status.stepPostfix || []).length > 0) {
var pos = {
x: parseInt((px + core.bigmap.offsetX) / 32),
y: parseInt((py + core.bigmap.offsetY) / 32)
x: Math.floor((px + core.bigmap.offsetX) / 32),
y: Math.floor((py + core.bigmap.offsetY) / 32)
};
var pos0 = core.status.stepPostfix[core.status.stepPostfix.length - 1];
var directionDistance = [
@ -725,10 +709,10 @@ actions.prototype._sys_onmove = function (x, y, px, py) {
////// 当点击(触摸)事件放开时 //////
actions.prototype.onup = function (loc) {
var x = parseInt(loc.x / loc.size),
y = parseInt(loc.y / loc.size);
var px = parseInt(loc.x / core.domStyle.scale),
py = parseInt(loc.y / core.domStyle.scale);
var x = Math.floor(loc.x / loc.size),
y = Math.floor(loc.y / loc.size);
var px = Math.floor(loc.x / core.domStyle.scale),
py = Math.floor(loc.y / core.domStyle.scale);
this.doRegisteredAction('onup', x, y, px, py);
};
@ -962,7 +946,7 @@ actions.prototype._sys_longClick_lockControl = function (x, y, px, py) {
actions.prototype._getChoicesTopIndex = function (length) {
return (
this._HY_ -
parseInt((length - 1) / 2) +
Math.floor((length - 1) / 2) +
(core.status.event.ui.offset || 0)
);
};
@ -1546,7 +1530,7 @@ actions.prototype._clickQuickShop = function (x, y) {
if (this._out(x)) return;
var topIndex =
this._HY_ -
parseInt(shopIds.length / 2) +
Math.floor(shopIds.length / 2) +
(core.status.event.ui.offset || 0);
if (y >= topIndex && y < topIndex + shopIds.length) {
var shopId = shopIds[y - topIndex];
@ -1625,7 +1609,7 @@ actions.prototype._clickSL = function (x, y) {
if (core.status.event.data.mode == 'fav')
core.ui._drawSLPanel(1, true);
else {
page = parseInt((core.saves.saveIndex - 1) / 5);
page = Math.floor((core.saves.saveIndex - 1) / 5);
offset = core.saves.saveIndex - 5 * page;
core.ui._drawSLPanel(10 * page + offset, true);
}
@ -1633,8 +1617,8 @@ actions.prototype._clickSL = function (x, y) {
return;
}
// 点存档名
var xLeft = parseInt(core._WIDTH_ / 3),
xRight = parseInt((core._WIDTH_ * 2) / 3);
var xLeft = Math.floor(core._WIDTH_ / 3),
xRight = Math.floor((core._WIDTH_ * 2) / 3);
var topY1 = 0,
topY2 = this._HY_;
if (y >= topY1 && y <= topY1 + 1) {
@ -1884,8 +1868,8 @@ actions.prototype._clickSwitchs_sounds = function (x, y) {
var width = choices[selection].width;
var leftPos = (core._PX_ - width) / 2,
rightPos = (core._PX_ + width) / 2;
var leftGrid = parseInt(leftPos / 32),
rightGrid = parseInt(rightPos / 32) - 1;
var leftGrid = Math.floor(leftPos / 32),
rightGrid = Math.floor(rightPos / 32) - 1;
core.status.event.selection = selection;
switch (selection) {
case 0:
@ -1977,8 +1961,8 @@ actions.prototype._clickSwitchs_display = function (x, y) {
var width = choices[selection].width;
var leftPos = (core._PX_ - width) / 2,
rightPos = (core._PX_ + width) / 2;
var leftGrid = parseInt(leftPos / 32),
rightGrid = parseInt(rightPos / 32) - 1;
var leftGrid = Math.floor(leftPos / 32),
rightGrid = Math.floor(rightPos / 32) - 1;
core.status.event.selection = selection;
switch (selection) {
case 0:
@ -2113,8 +2097,8 @@ actions.prototype._clickSwitchs_action = function (x, y) {
var width = choices[selection].width;
var leftPos = (core._PX_ - width) / 2,
rightPos = (core._PX_ + width) / 2;
var leftGrid = parseInt(leftPos / 32),
rightGrid = parseInt(rightPos / 32) - 1;
var leftGrid = Math.floor(leftPos / 32),
rightGrid = Math.floor(rightPos / 32) - 1;
core.status.event.selection = selection;
switch (selection) {
case 0:
@ -2789,7 +2773,7 @@ actions.prototype._clickReplay_fromLoad = function () {
core.status.event.selection = null;
core.clearUI();
var saveIndex = core.saves.saveIndex;
var page = parseInt((saveIndex - 1) / 5),
var page = Math.floor((saveIndex - 1) / 5),
offset = saveIndex - 5 * page;
core.ui._drawSLPanel(10 * page + offset);
};
@ -2807,7 +2791,7 @@ actions.prototype._clickReplay_replayRemain = function () {
core.status.event.id = 'replayRemain';
core.lockControl();
var saveIndex = core.saves.saveIndex;
var page = parseInt((saveIndex - 1) / 5),
var page = Math.floor((saveIndex - 1) / 5),
offset = saveIndex - 5 * page;
core.ui._drawSLPanel(10 * page + offset);
}
@ -2828,7 +2812,7 @@ actions.prototype._clickReplay_replaySince = function () {
core.status.event.id = 'replaySince';
core.lockControl();
var saveIndex = core.saves.saveIndex;
var page = parseInt((saveIndex - 1) / 5),
var page = Math.floor((saveIndex - 1) / 5),
offset = saveIndex - 5 * page;
core.ui._drawSLPanel(10 * page + offset);
}

View File

@ -905,7 +905,7 @@ control.prototype.setHeroOpacity = function (
var fromOpacity = core.getFlag('__heroOpacity__', 1);
var step = 0,
steps = parseInt(time / 10);
steps = Math.floor(time / 10);
if (steps <= 0) steps = 1;
var moveFunc = core.applyEasing(moveMode);
@ -996,8 +996,8 @@ control.prototype.updateViewport = function () {
core.bigmap.offsetY >= core.bigmap.posY * 32 + 32 ||
core.bigmap.offsetY <= core.bigmap.posY * 32 - 32
) {
core.bigmap.posX = parseInt(core.bigmap.offsetX / 32);
core.bigmap.posY = parseInt(core.bigmap.offsetY / 32);
core.bigmap.posX = Math.floor(core.bigmap.offsetX / 32);
core.bigmap.posY = Math.floor(core.bigmap.offsetY / 32);
core.redrawMap();
}
} else {
@ -1027,8 +1027,8 @@ control.prototype.updateViewport = function () {
if (ox != null && oy != null) {
core.relocateCanvas(
one,
parseInt(ox) - core.bigmap.offsetX,
parseInt(oy) - core.bigmap.offsetY
Number(ox) - core.bigmap.offsetX,
Number(oy) - core.bigmap.offsetY
);
}
}
@ -1062,7 +1062,7 @@ control.prototype.moveViewport = function (x, y, moveMode, time, callback) {
time /= Math.max(core.status.replay.speed, 1);
var per_time = 10,
step = 0,
steps = parseInt(time / per_time);
steps = Math.floor(time / per_time);
if (steps <= 0) {
this.setViewport(32 * x, 32 * y);
if (callback) callback();
@ -1499,7 +1499,7 @@ control.prototype._replay_SL = function () {
core.lockControl();
core.status.event.id = 'save';
var saveIndex = core.saves.saveIndex;
var page = parseInt((saveIndex - 1) / 5),
var page = Math.floor((saveIndex - 1) / 5),
offset = saveIndex - 5 * page;
core.ui._drawSLPanel(10 * page + offset);
@ -2259,7 +2259,7 @@ control.prototype._doSL_replayRemain_afterGet = function (id, data) {
core.status.event.id = 'replayRemain';
core.lockControl();
var saveIndex = core.saves.saveIndex;
var page = parseInt((saveIndex - 1) / 5),
var page = Math.floor((saveIndex - 1) / 5),
offset = saveIndex - 5 * page;
core.ui._drawSLPanel(10 * page + offset);
}
@ -2959,7 +2959,7 @@ control.prototype._setCurtain_animate = function (
time /= Math.max(core.status.replay.speed, 1);
var per_time = 10,
step = 0,
steps = parseInt(time / per_time);
steps = Math.floor(time / per_time);
if (steps <= 0) steps = 1;
var curr = nowColor;
var moveFunc = core.applyEasing(moveMode);

View File

@ -2947,15 +2947,15 @@ events.prototype.__action_wait_getValue = function (value) {
value %= 1e8;
if (value >= 1000000) {
core.setFlag('type', 1);
var px = parseInt((value - 1000000) / 1000),
var px = Math.floor((value - 1000000) / 1000),
py = value % 1000;
core.setFlag('px', px);
core.setFlag('py', py);
core.setFlag('x', parseInt(px / 32));
core.setFlag('y', parseInt(py / 32));
core.setFlag('x', Math.floor(px / 32));
core.setFlag('y', Math.floor(py / 32));
} else if (value >= 10000) {
core.setFlag('type', 1);
var x = parseInt((value - 10000) / 100),
var x = Math.floor((value - 10000) / 100),
y = value % 100;
core.setFlag('px', 32 * x + 16);
core.setFlag('py', 32 * y + 16);
@ -3404,7 +3404,7 @@ events.prototype.save = function (fromUserAction) {
return;
if (!this._checkStatus('save', fromUserAction)) return;
var saveIndex = core.saves.saveIndex;
var page = parseInt((saveIndex - 1) / 5),
var page = Math.floor((saveIndex - 1) / 5),
offset = saveIndex - 5 * page;
core.playSound('打开界面');
core.ui._drawSLPanel(10 * page + offset);
@ -3414,7 +3414,7 @@ events.prototype.save = function (fromUserAction) {
events.prototype.load = function (fromUserAction) {
if (core.isReplaying()) return;
var saveIndex = core.saves.saveIndex;
var page = parseInt((saveIndex - 1) / 5),
var page = Math.floor((saveIndex - 1) / 5),
offset = saveIndex - 5 * page;
// 游戏开始前读档
if (!core.isPlaying()) {
@ -3788,8 +3788,8 @@ events.prototype._moveTextBox_moving = function (ctx, moveInfo, callback) {
var dy = moveInfo.dy * moveFunc(step / steps);
core.relocateCanvas(
ctx,
parseInt(moveInfo.ox + dx),
parseInt(moveInfo.oy + dy)
Math.floor(moveInfo.ox + dx),
Math.floor(moveInfo.oy + dy)
);
ctx.canvas.setAttribute('_text_left', moveInfo.sx + dx);
ctx.canvas.setAttribute('_text_top', moveInfo.sy + dy);
@ -4018,7 +4018,7 @@ events.prototype.moveImage = function (
events.prototype._moveImage_moving = function (name, moveInfo, callback) {
var per_time = 10,
step = 0,
steps = parseInt(moveInfo.time / per_time);
steps = Math.floor(moveInfo.time / per_time);
if (steps <= 0) steps = 1;
var fromX = moveInfo.fromX,
fromY = moveInfo.fromY,
@ -4033,8 +4033,8 @@ events.prototype._moveImage_moving = function (name, moveInfo, callback) {
var animate = setInterval(function () {
step++;
currOpacity = opacity + (toOpacity - opacity) * moveFunc(step / steps);
currX = parseInt(fromX + (toX - fromX) * moveFunc(step / steps));
currY = parseInt(fromY + (toY - fromY) * moveFunc(step / steps));
currX = Math.floor(fromX + (toX - fromX) * moveFunc(step / steps));
currY = Math.floor(fromY + (toY - fromY) * moveFunc(step / steps));
core.setOpacity(name, currOpacity);
core.relocateCanvas(name, currX, currY);
if (step == steps) {
@ -4089,7 +4089,7 @@ events.prototype.rotateImage = function (
events.prototype._rotateImage_rotating = function (name, rotateInfo, callback) {
var per_time = 10,
step = 0,
steps = parseInt(rotateInfo.time / per_time);
steps = Math.floor(rotateInfo.time / per_time);
if (steps <= 0) steps = 1;
var moveFunc = core.applyEasing(rotateInfo.moveMode);
var animate = setInterval(function () {
@ -4183,7 +4183,7 @@ events.prototype._scaleImage_scale = function (ctx, scaleInfo, callback) {
var per_time = 10,
step = 0,
steps = parseInt(scaleInfo.time / per_time);
steps = Math.floor(scaleInfo.time / per_time);
if (steps <= 0) steps = 1;
var moveFunc = core.applyEasing(scaleInfo.moveMode);
@ -4239,7 +4239,7 @@ events.prototype.setVolume = function (value, time, callback) {
time /= Math.max(core.status.replay.speed, 1);
var per_time = 10,
step = 0,
steps = parseInt(time / per_time);
steps = Math.floor(time / per_time);
if (steps <= 0) steps = 1;
var animate = setInterval(function () {
step++;
@ -4265,7 +4265,7 @@ events.prototype.vibrate = function (direction, time, speed, power, callback) {
speed = speed || 10;
power = power || 10;
var shakeInfo = {
duration: parseInt(time / 10),
duration: Math.floor(time / 10),
speed: speed,
power: power,
direction: 1,

View File

@ -68,7 +68,7 @@ icons.prototype.getTilesetOffset = function (id) {
height = Math.floor(parseInt(img.getAttribute('_height')) / 32);
if (id >= startOffset && id < startOffset + width * height) {
var x = (id - startOffset) % width,
y = parseInt((id - startOffset) / width);
y = Math.floor((id - startOffset) / width);
return { image: imgName, x: x, y: y };
}
startOffset += this.tilesetStartOffset;

View File

@ -2069,7 +2069,7 @@ maps.prototype._drawAutotile = function (
yy = block.y;
var autotile = core.material.images['autotile'][block.event.id];
status = status || 0;
status %= parseInt(autotile.width / 96);
status %= Math.floor(autotile.width / 96);
var done = {};
var isGrass = function (x, y) {
if (
@ -2341,7 +2341,7 @@ maps.prototype._drawAutotile_renderCut = function (
16,
16,
x + ((i % 2) * size) / 2,
y + (parseInt(i / 2) * size) / 2,
y + (Math.floor(i / 2) * size) / 2,
size / 2,
size / 2
);
@ -2361,7 +2361,7 @@ maps.prototype._drawAutotile_drawBlockByIndex = function (
var sx = 16 * ((index - 1) % 6),
sy = 16 * ~~((index - 1) / 6);
status = status || 0;
status %= parseInt(autotileImg.width / 96);
status %= Math.floor(autotileImg.width / 96);
core.drawImage(
ctx,
autotileImg,
@ -3339,7 +3339,8 @@ maps.prototype.setBlock = function (number, x, y, floorId, noredraw) {
)
return;
if (typeof number == 'string') {
if (/^\d+$/.test(number)) number = parseInt(number);
const num = Number(number);
if (!isNaN(num)) number = num;
else number = core.getNumberById(number);
}
@ -3414,7 +3415,8 @@ maps.prototype.animateSetBlock = function (
return;
}
if (typeof number == 'string') {
if (/^\d+$/.test(number)) number = parseInt(number);
const num = Number(number);
if (!isNaN(num)) number = num;
else number = core.getNumberById(number);
}
var originBlock = core.getBlock(x, y, floorId, true);
@ -3606,7 +3608,8 @@ maps.prototype.setBgFgBlock = function (name, number, x, y, floorId) {
if (!name || (!name.startsWith('bg') && !name.startsWith('fg'))) return;
if (typeof number == 'string') {
if (/^\d+$/.test(number)) number = parseInt(number);
const num = Number(number);
if (!isNaN(num)) number = num;
else number = core.getNumberById(number);
}
@ -4151,7 +4154,7 @@ maps.prototype._animateBlock_doAnimate = function (
callback
) {
var step = 0,
steps = Math.max(parseInt(time / 10), 1);
steps = Math.max(Math.floor(time / 10), 1);
var cb = function () {
list.forEach(function (t) {
if (t.blockInfo) core.maps._deleteDetachedBlock(t.canvases);

View File

@ -1686,7 +1686,7 @@ ui.prototype.drawTextContent = function (ctx, content, config) {
Math.round(config.fontSize / 4),
config.lineHeight - config.fontSize
);
config.topMargin = parseInt(config.lineMargin / 2);
config.topMargin = Math.floor(config.lineMargin / 2);
config.lineMaxHeight = config.lineMargin + config.fontSize;
config.offsetX = 0;
config.offsetY = 0;
@ -2359,10 +2359,10 @@ ui.prototype._drawTextBox_getVerticalPosition = function (
} else if (titleInfo.image) height = Math.max(height, 90);
var yoffset = 16;
var top = parseInt((core._PY_ - height) / 2);
var top = Math.floor((core._PY_ - height) / 2);
switch (posInfo.position) {
case 'center':
top = parseInt((core._PY_ - height) / 2);
top = Math.floor((core._PY_ - height) / 2);
break;
case 'up':
if (posInfo.px == null || posInfo.py == null)
@ -2933,7 +2933,7 @@ ui.prototype.drawConfirmBox = function (text, yesCallback, noCallback, ctx) {
var strokeLeft =
core._PX_ / 2 +
(76 * core.status.event.selection - 38) -
parseInt(len / 2) -
Math.floor(len / 2) -
5;
if (isWindowSkin) {
@ -2973,7 +2973,7 @@ ui.prototype._drawConfirmBox_getRect = function (contents, ctx) {
var max_width = contents.reduce(function (pre, curr) {
return Math.max(pre, core.calWidth(ctx, curr));
}, 0);
var left = Math.min(core._PX_ / 2 - 40 - parseInt(max_width / 2), 100),
var left = Math.min(core._PX_ / 2 - 40 - Math.floor(max_width / 2), 100),
right = core._PX_ - left;
var top = core._PY_ / 2 - 68 - (contents.length - 1) * 30,
bottom = core._PY_ / 2 + 68;
@ -2995,7 +2995,7 @@ ui.prototype.drawWaiting = function (text) {
text = core.replaceText(text || '');
var text_length = core.calWidth('ui', text, this._buildFont(19, true));
var width = Math.max(text_length + 80, 220),
left = core._PX_ / 2 - parseInt(width / 2),
left = core._PX_ / 2 - Math.floor(width / 2),
right = left + width;
var top = core._PY_ / 2 - 48,
height = 96,
@ -3204,7 +3204,7 @@ ui.prototype.drawPagination = function (page, totalPage, y) {
core.fillText(
'ui',
page + ' / ' + totalPage,
parseInt((core._PX_ - length) / 2),
Math.floor((core._PX_ - length) / 2),
y * 32 + 19
);
@ -3434,8 +3434,8 @@ ui.prototype._drawViewMaps_buildData = function (index, x, y) {
var floorId = core.floorIds[index],
mw = core.floors[floorId].width,
mh = core.floors[floorId].height;
if (x == null) x = parseInt(mw / 2);
if (y == null) y = parseInt(mh / 2);
if (x == null) x = Math.floor(mw / 2);
if (y == null) y = Math.floor(mh / 2);
x = core.clamp(x, core._HALF_WIDTH_, mw - core._HALF_WIDTH_ - 1);
y = core.clamp(y, core._HALF_HEIGHT_, mh - core._HALF_HEIGHT_ - 1);
@ -3473,7 +3473,7 @@ ui.prototype._drawSLPanel = function (index, refresh) {
if (index == null) index = 1;
if (index < 0) index = 0;
var page = parseInt(index / 10),
var page = Math.floor(index / 10),
offset = index % 10;
var max_page = main.savePages || 30;
if (core.status.event.data && core.status.event.data.mode == 'fav')
@ -3617,7 +3617,7 @@ ui.prototype._drawSLPanel_drawRecord = function (
'ui',
data.hard,
x,
parseInt(y + 22 + h / 2),
Math.floor(y + 22 + h / 2),
data.hero.flags.__hardColor__ || 'white',
this._buildFont(30, true)
);
@ -3695,7 +3695,7 @@ ui.prototype._drawSLPanel_drawRecord = function (
'ui',
'空',
x,
parseInt(y + 22 + h / 2),
Math.floor(y + 22 + h / 2),
'#FFFFFF',
this._buildFont(30, true)
);
@ -3729,10 +3729,10 @@ ui.prototype._drawSLPanel_drawRecords = function (n) {
title = (core.saves.favoriteName[real_id] || name + real_id) + ' ✐';
}
var charSize = 32; // 字体占用像素范围
var topSpan = parseInt(
var topSpan = Math.floor(
(core._PY_ - charSize - 2 * (charSize * 2 + size * core._PY_)) / 3
); // Margin
var yTop1 = topSpan + parseInt(charSize / 2) + 8; // 文字的中心
var yTop1 = topSpan + Math.floor(charSize / 2) + 8; // 文字的中心
var yTop2 = yTop1 + charSize * 2 + size * core._PY_ + topSpan;
if (i < 3) {
this._drawSLPanel_drawRecord(

View File

@ -535,17 +535,18 @@ utils.prototype.formatDate2 = function (date) {
utils.prototype.formatTime = function (time) {
return (
core.setTwoDigits(parseInt(time / 3600000)) +
core.setTwoDigits(Math.floor(time / 3600000)) +
':' +
core.setTwoDigits(parseInt(time / 60000) % 60) +
core.setTwoDigits(Math.floor(time / 60000) % 60) +
':' +
core.setTwoDigits(parseInt(time / 1000) % 60)
core.setTwoDigits(Math.floor(time / 1000) % 60)
);
};
////// 两位数显示 //////
utils.prototype.setTwoDigits = function (x) {
return parseInt(x) < 10 && parseInt(x) >= 0 ? '0' + x : x;
const num = Number(x);
return num < 10 && num >= 0 ? '0' + x : x;
};
utils.prototype.formatSize = function (size) {