用户:
Nysidla查看:9 回复:5 评论:9 创建时间:2023-04-21T20:01:34
基于对机器人5号和Star_Ink_sans的代码学习,我成功研究(发烧)出了摔落伤害2.0!
摔落伤害似乎不怎么高大上,但把它倒过来,名为 伤害——摔落,听起来就比较高级了(乱BB)
world.onTick(async ({ tick }) => {
world.querySelectorAll('player').filter(e => !e.destroyed).forEach((e) => {
if (e.high == false && e.velocity.y < 0) {
e.high = e.position.y
} if (e.velocity.y >= 0) {
if ((e.high - e.position.y) >= 4) {
e.hurt(Math.round((e.high - e.position.y - 4) * 10) / 10)
e.player.directMessage(`你受到了${Math.round((e.high - e.position.y - 4) * 10) / 10}点摔落伤害,剩余血量${Math.round((e.hp) * 10) / 10}`)
e.dieString = '摔喵了'
} e.high = false;
}
})
})
Q:这个代码和上2者的有何不同?
A:代码更加精简,一段代码就可以同时侦测模型碰撞和方块碰撞,并且有一个极大的优点:上两者的代码是用Box3ContactEvent侦测的,使得玩家只要一直跳跃就可以躲避侦测,而这个代码使用velocity.y>=0侦测,让玩家跳跃时也能受到fall状态下计算出的伤害。
PS:要先打开伤害。
world.onTick(async ({ tick }) => {
world.querySelectorAll('player').filter(e => !e.destroyed).forEach((e) => {
if (e.high == false && e.velocity.y < 0) {
e.high = e.position.y
} if (e.velocity.y >= 0 && e.player.moveState != 'fall') {
if ((e.high - e.position.y) >= 4) {
e.hurt(Math.round((e.high - e.position.y - 4) * 10) / 10)
e.player.directMessage(`你受到了${Math.round((e.high - e.position.y - 4) * 10) / 10}点摔落伤害,剩余血量${Math.round((e.hp) * 10) / 10}`)
e.dieString = '摔死了'
} e.high = false;
}
})
})
最新版)(
原因:之前的在空中受到伤害后可能会让velocity>=1,玩家会在空中受到摔落伤害。
改动:加入e.player.moveState != 'fall',让玩家在空中velocity>=1时不会受到伤害。
点赞0
评论
Ezra__建议不要用onTick,影响性能,用setInterval
world.onFluidEnter(({ entity }) => {
entity.high = 0;
})
setInterval(() => {
world.querySelectorAll('player').filter(e => !e.destroyed).forEach((e) => {
!e.high && e.player.moveState == "fall" ? e.high = e.position.y : null;
if (e.velocity.y >= 0 && e.player.moveState != 'fall') {
if ((e.high - e.position.y) > 4) {
e.hurt((e.high - e.position.y - 4).toFixed(2), {
attacker: undefined,
damageType: "fall",
})
e.player.directMessage(`你受到了${(e.high - e.position.y - 4).toFixed(2)}点摔落伤害,剩余血量${e.hp}`)
}
e.high = 0;
}
})
}, 64)点赞0
评论