eventMoveHero & jumpHero

This commit is contained in:
oc 2019-03-18 03:00:18 +08:00
parent 6e38f533bf
commit 9ac105ebff
4 changed files with 86 additions and 133 deletions

View File

@ -175,7 +175,7 @@ control.prototype._animationFrame_animate = function (timestamp) {
}
control.prototype._animationFrame_heroMoving = function (timestamp) {
if (!core.isMoving()) return;
if (core.status.heroMoving <= 0) return;
var x=core.getHeroLoc('x'), y=core.getHeroLoc('y'), direction = core.getHeroLoc('direction');
// 换腿
if (timestamp - core.animateFrame.moveTime > (core.values.moveSpeed||100)) {

View File

@ -1789,131 +1789,96 @@ events.prototype._vibrate_update = function (shakeInfo) {
}
/////// 使用事件让勇士移动。这个函数将不会触发任何事件 //////
control.prototype.eventMoveHero = function(steps, time, callback) {
events.prototype.eventMoveHero = function(steps, time, callback) {
time = time || core.values.moveSpeed || 100;
// 要运行的轨迹将steps展开
var moveSteps=[];
steps.forEach(function (e) {
if (typeof e=="string") {
moveSteps.push(e);
}
else {
if (!core.isset(e.value)) {
moveSteps.push(e.direction)
}
else {
for (var i=0;i<e.value;i++) {
moveSteps.push(e.direction);
}
}
}
var step = 0, moveSteps = (steps||[]).filter(function (t) {
return ['up','down','left','right','forward','backward'].indexOf(t)>=0;
});
moveSteps = moveSteps.filter(function (t) { return ['up','down','left','right','forward','backward'].indexOf(t)>=0;});
var step=0;
var animate=window.setInterval(function() {
var x=core.getHeroLoc('x'), y=core.getHeroLoc('y');
if (moveSteps.length==0) {
delete core.animateFrame.asyncId[animate];
clearInterval(animate);
core.drawHero(null, x, y);
if (core.isset(callback)) callback();
core.drawHero();
if (callback) callback();
}
else {
var direction = moveSteps[0];
// ------ 前进/后退
var o = direction == 'backward' ? -1 : 1;
if (direction == 'forward' || direction == 'backward') direction = core.getHeroLoc('direction');
core.setHeroLoc('direction', direction);
step++;
if (step <= 4) {
core.drawHero(direction, x, y, 'leftFoot', 4 * o * step);
}
else if (step <= 8) {
core.drawHero(direction, x, y, 'rightFoot', 4 * o * step);
}
if (step == 8) {
if (core.events._eventMoveHero_moving(++step, moveSteps))
step = 0;
core.setHeroLoc('x', x + o * core.utils.scan[direction].x, true);
core.setHeroLoc('y', y + o * core.utils.scan[direction].y, true);
core.updateFollowers();
moveSteps.shift();
}
}
}, time / 8 / core.status.replay.speed);
core.animateFrame.asyncId[animate] = true;
}
events.prototype._eventMoveHero_moving = function (step, moveSteps) {
var direction = moveSteps[0], x = core.getHeroLoc('x'), y = core.getHeroLoc('y');
// ------ 前进/后退
var o = direction == 'backward' ? -1 : 1;
if (direction == 'forward' || direction == 'backward') direction = core.getHeroLoc('direction');
core.setHeroLoc('direction', direction);
if (step <= 4) {
core.drawHero(direction, x, y, 'leftFoot', 4 * o * step);
}
else if (step <= 8) {
core.drawHero(direction, x, y, 'rightFoot', 4 * o * step);
}
if (step == 8) {
core.setHeroLoc('x', x + o * core.utils.scan[direction].x, true);
core.setHeroLoc('y', y + o * core.utils.scan[direction].y, true);
core.updateFollowers();
moveSteps.shift();
return true;
}
return false;
}
////// 勇士跳跃事件 //////
control.prototype.jumpHero = function (ex, ey, time, callback) {
events.prototype.jumpHero = function (ex, ey, time, callback) {
var sx = core.getHeroLoc('x'), sy = core.getHeroLoc('y');
if (ex == null) ex = sx;
if (ey == null) ey = sy;
var sx=core.status.hero.loc.x, sy=core.status.hero.loc.y;
if (!core.isset(ex)) ex=sx;
if (!core.isset(ey)) ey=sy;
core.maps.__playJumpSound();
var jumpInfo = core.maps.__generateJumpInfo(sx, sy, ex, ey, time || 500);
jumpInfo.icon = core.material.icons.hero[core.getHeroLoc('direction')];
jumpInfo.height = core.material.icons.hero.height;
time = time || 500;
this._jumpHero_doJump(jumpInfo, callback);
}
core.playSound('jump.mp3');
var dx = ex-sx, dy=ey-sy, distance = Math.round(Math.sqrt(dx * dx + dy * dy));
var jump_peak = 6 + distance, jump_count = jump_peak * 2;
var currx = sx, curry = sy;
var heroIcon = core.material.icons.hero[core.getHeroLoc('direction')];
var status = 'stop';
var height = core.material.icons.hero.height;
var drawX = function() {
return currx * 32;
}
var drawY = function() {
var ret = curry * 32;
if(jump_count >= jump_peak){
var n = jump_count - jump_peak;
}else{
var n = jump_peak - jump_count;
}
return ret - (jump_peak * jump_peak - n * n) / 2;
}
var updateJump = function() {
jump_count--;
currx = (currx * jump_count + ex) / (jump_count + 1.0);
curry = (curry * jump_count + ey) / (jump_count + 1.0);
}
core.clearMap('hero');
var animate=window.setInterval(function() {
if (jump_count>0) {
core.clearMap('hero', drawX()-core.bigmap.offsetX, drawY()-height+32-core.bigmap.offsetY, 32, height);
updateJump();
var nowx = drawX(), nowy = drawY();
core.bigmap.offsetX = core.clamp(nowx - 32*core.__HALF_SIZE__, 0, 32*core.bigmap.width-core.__PIXELS__);
core.bigmap.offsetY = core.clamp(nowy - 32*core.__HALF_SIZE__, 0, 32*core.bigmap.height-core.__PIXELS__);
core.control.updateViewport();
core.drawImage('hero', core.material.images.hero, heroIcon[status] * 32, heroIcon.loc * height, 32, height,
nowx - core.bigmap.offsetX, nowy + 32-height - core.bigmap.offsetY, 32, height);
}
else {
delete core.animateFrame.asyncId[animate];
clearInterval(animate);
core.setHeroLoc('x', ex);
core.setHeroLoc('y', ey);
core.drawHero();
if (core.isset(callback)) callback();
}
}, time / 16 / core.status.replay.speed);
events.prototype._jumpHero_doJump = function (jumpInfo, callback) {
var animate = window.setInterval(function () {
if (jumpInfo.jump_count > 0)
core.events._jumpHero_jumping(jumpInfo)
else
core.events._jumpHero_finished(animate, jumpInfo.ex, jumpInfo.ey, callback);
}, jumpInfo.per_time);
core.animateFrame.asyncId[animate] = true;
}
events.prototype._jumpHero_jumping = function (jumpInfo) {
core.clearMap('hero');
core.maps.__updateJumpInfo(jumpInfo);
var nowx = jumpInfo.px, nowy = jumpInfo.py, height = jumpInfo.height;
core.bigmap.offsetX = core.clamp(nowx - 32*core.__HALF_SIZE__, 0, 32*core.bigmap.width-core.__PIXELS__);
core.bigmap.offsetY = core.clamp(nowy - 32*core.__HALF_SIZE__, 0, 32*core.bigmap.height-core.__PIXELS__);
core.control.updateViewport();
core.drawImage('hero', core.material.images.hero, jumpInfo.icon.stop, jumpInfo.icon.loc * height, 32, height,
nowx - core.bigmap.offsetX, nowy + 32-height - core.bigmap.offsetY, 32, height);
}
events.prototype._jumpHero_finished = function (animate, ex, ey, callback) {
delete core.animateFrame.asyncId[animate];
clearInterval(animate);
core.setHeroLoc('x', ex);
core.setHeroLoc('y', ey);
core.drawHero();
if (callback) callback();
}
////// 打开一个全局商店 //////
events.prototype.openShop = function (shopId, needVisited) {
var shop = core.status.shops[shopId];

View File

@ -1545,7 +1545,9 @@ maps.prototype.moveBlock = function (x, y, steps, time, keep, callback) {
return;
}
var block = blockArr[0], blockInfo = blockArr[1];
var moveSteps = core.utils.expandMoveSteps(steps);
var moveSteps = (steps||[]).filter(function (t) {
return ['up','down','left','right'].indexOf(t)>=0;
});
var canvases = this._initDetachedBlock(blockInfo, x, y, block.event.animate !== false);
this._moveDetachedBlock(blockInfo, 32 * x, 32 * y, 1, canvases);
@ -1609,19 +1611,25 @@ maps.prototype.jumpBlock = function (sx, sy, ex, ey, time, keep, callback) {
var canvases = this._initDetachedBlock(blockInfo, sx, sy, block.event.animate !== false);
this._moveDetachedBlock(blockInfo, 32 * sx, 32 * sy, 1, canvases);
this._jumpBlock_playSound();
this.__playJumpSound();
var jumpInfo = ths.__generateJumpInfo(sx, sy, ex, ey, time);
jumpInfo.keep = keep;
var dx = ex - sx, dy = ey - sy, distance = Math.round(Math.sqrt(dx * dx + dy * dy));
var jump_peak = 6 + distance, jump_count = jump_peak * 2;
var jumpInfo = {
x: sx, y: sy, ex: ex, ey: ey, px: 32 * sx, py: 32 * sy, opacity: 1, keep: keep,
jump_peak: jump_peak, jump_count: jump_count,
step: 0, per_time: time / 16 / core.status.replay.speed
};
this._jumpBlock_doJump(blockInfo, canvases, jumpInfo, callback);
}
maps.prototype._jumpBlock_playSound = function () {
maps.prototype.__generateJumpInfo = function (sx, sy, ex, ey, time) {
var dx = ex - sx, dy = ey - sy, distance = Math.round(Math.sqrt(dx * dx + dy * dy));
var jump_peak = 6 + distance, jump_count = jump_peak * 2;
return {
x: sx, y: sy, ex: ex, ey: ey, px: 32 * sx, py: 32 * sy, opacity: 1,
jump_peak: jump_peak, jump_count: jump_count,
step: 0, per_time: time / 16 / core.status.replay.speed
};
}
maps.prototype.__playJumpSound = function () {
core.playSound('jump.mp3');
}
@ -1636,7 +1644,7 @@ maps.prototype._jumpBlock_doJump = function (blockInfo, canvases, jumpInfo, call
core.animateFrame.asyncId[animate] = true;
}
maps.prototype._jumpBlock_updateJump = function (jumpInfo) {
maps.prototype.__updateJumpInfo = function (jumpInfo) {
jumpInfo.jump_count--;
jumpInfo.x = (jumpInfo.x * jumpInfo.jump_count + jumpInfo.ex) / (jumpInfo.jump_count + 1.0);
jumpInfo.y = (jumpInfo.y * jumpInfo.jump_count + jumpInfo.ey) / (jumpInfo.jump_count + 1.0);
@ -1646,7 +1654,7 @@ maps.prototype._jumpBlock_updateJump = function (jumpInfo) {
}
maps.prototype._jumpBlock_jumping = function (blockInfo, canvases, jumpInfo) {
this._jumpBlock_updateJump(jumpInfo);
this.__updateJumpInfo(jumpInfo);
core.maps._moveDetachedBlock(blockInfo, jumpInfo.px, jumpInfo.py, jumpInfo.opacity, canvases);
}

View File

@ -604,26 +604,6 @@ utils.prototype.getCookie = function (name) {
return match ? match[2] : null;
}
utils.prototype.expandMoveSteps = function (steps) {
var moveSteps = [];
steps.forEach(function (e) {
if (typeof e == "string") {
moveSteps.push(e);
}
else {
if (e.value == null) {
moveSteps.push(e.direction)
}
else {
for (var i = 0; i < e.value; i++) {
moveSteps.push(e.direction);
}
}
}
});
return moveSteps;
}
////// 设置statusBar的innerHTML会自动斜体和放缩也可以增加自定义css //////
utils.prototype.setStatusBarInnerHTML = function (name, value, css) {
if (typeof value == 'number') value = this.formatBigNumber(value);