猫史档案馆


【代码岛3.0】如何做出一个简单的摔落引擎?

用户:高贵的木叶龙A35d高贵的木叶龙A35d查看:4 回复:10 评论:4 创建时间:2020-12-02T14:30:54


--------------------------------------------------分割线--------------------------------------------------------------------

效果图:

center_image

思路:

首先,要做出摔落引擎,必须要有方块碰撞事件

world.onVoxelContact(async({entity,x,y,z,voxel})=>{

搭建框架

然后,要用玩家变量记录上次摔落摔在哪里(简单起见不用字典了)

world.onPlayerJoin(({entity})=>{
    entity.player.fall=0//创建一个玩家变量,名为fall(注意:由于是教程,就不用字典来防止player禁止扩展,作品中可以用字典)
})

补进去

world.onVoxelContact(async({entity,x,y,z,voxel})=>{
    let i=entity.player.fall-y//用上次记录减去
    entity.hurt(i/5)//防止伤害过高
})

这样一个基本的摔落就弄好了

 

 

 

 

 

 

个喵emotion_doge,你没记录岂不是永远摔不喵?????

world.onVoxelContact(async({entity,x,y,z,voxel})=>{
    let i=entity.player.fall-y
    entity.hurt(i/5)
    entity.player.fall=y
})

很快,你会发现你从2格跳下去也会受伤……emotion_doge

所以加个距离判定

world.onVoxelContact(async({entity,x,y,z,voxel})=>{
    let i=entity.player.fall-y
    if(entity.player.fall-y>20){//摔落距离小于19不计算
        entity.hurt(i/5)
    }
    entity.player.fall=y
})

好了,继续玩下去吧

如果你的地图里有模型可以移动,你又会发现报错了……

因为普通模型不属于player类,更没有player变量!!!

world.onVoxelContact(async({entity,x,y,z,voxel})=>{
    if (!entity.isPlayer) return;//如果entity不是player取消本事件
    let i=entity.player.fall-y
    if(entity.player.fall-y>20){
        entity.hurt(i/5)
    }
    entity.player.fall=y
})

可以了吧~

但是fps的人们注意了,你如果这样的话扣血根本不能发现!然后慢慢KO……

world.onVoxelContact(async({entity,x,y,z,voxel})=>{
    if (!entity.isPlayer) return;
    let i=entity.player.fall-y
    if(entity.player.fall-y>20){
        entity.hurt(i/5)
        entity.player.directMessage('你受到了'+i/5+'点摔落伤害')
    }
    entity.player.fall=y
})

现在好了吧?

你试试从256格跳进水中emotion_doge……牛顿的喵都压不住了emotion_doge……

    world.onFluidEnter(async({entity,voxel})=>{
        if(!entity.isPlayer){return;}
        entity.player.fall=0
    })
    world.onFluidLeave(async({entity,voxel})=>{
        if(!entity.isPlayer){return;}
        entity.player.fall=0
    })

这下好了,跳入水中时把记录清零,就喵不掉啦~()

喵亡时清空一下数据:

world.onDie(async({entity,attacker})=>{
    entity.player.fall=0
})

完整代码,这次不(po)出(tian)所(huang)料(di)的允许白嫖,反正做他没花啥力气……:

    world.onFluidEnter(async({entity,voxel})=>{
        if(!entity.isPlayer){return;}
        entity.player.fall=0
    })
    world.onFluidLeave(async({entity,voxel})=>{
        if(!entity.isPlayer){return;}
        entity.player.fall=0
    })
world.onVoxelContact(async({entity,x,y,z,voxel})=>{
    if (!entity.isPlayer) return;
    let i=entity.player.fall-y
    if(entity.player.fall-y>20){
        entity.hurt(i/5)
        entity.player.directMessage('你受到了'+i/5+'点摔落伤害')
    }
    entity.player.fall=y
})
world.onPlayerJoin(({entity})=>{
    entity.player.fall=0
})
world.onDie(async({entity,attacker})=>{
    entity.player.fall=0
})


回复

上一页1 页 / 共 1下一页
无限正义无限正义

666

点赞0


评论


Chengxuyee_已退Chengxuyee_已退

()()()

点赞0


评论


IaeaIaea

666

点赞0


评论


指令者指令者

666666666666

点赞0


评论


指令者指令者

有BUG!center_image

点赞1


评论


Architect_皮卡awaArchitect_皮卡awa

emotion_doge

点赞0


评论


TobylaiTobylai

似乎出现个问题(

从高处position.set到地下

然后就...emotion_doge

这种情况会发生吗

点赞0


评论


指令者指令者

名鬼改名了?

点赞0


评论


名字送冷吖O名字送冷吖O

onVoxelContact落地立即起跳有时候不会摔伤,这很不真实)但这个bug可以修复)手写一个方块碰撞检测系统)

点赞0


评论


HIM_PETERHIM_PETER

所以我掉在模型上会发生什么事情)))

点赞0


评论