猫史档案馆


【代码教程】 电梯

用户:AK亨利佐加DanJamesThomasAK亨利佐加DanJamesThomas查看:2 回复:2 评论:2 创建时间:2022-07-23T16:47:19


(本贴由亨利佐用AK的号发布)

 

首先呢,先点个赞

(台下扔鞋中···)
哎,别扔鞋,这一个教程,包你学会,而且很好用!

(台下鼓掌)

ehem,如标题,今天的主题是:

电梯(包括平行电梯、自动扶梯、上下电梯···)
(台下鼓掌)

别鼓掌了,先简单说一下:

电梯,上下移动、横着移动啥的,很常见,基本上,只能用方块:

center_image

这方块呢,向下移挺好滴,想上?不可能

解决方案:

1.用动画!

嗯,吉吉喵的代码已来,请查收:

//先从模型库搜索"电梯"的模型, 并把它拖入场景中
const elevator = world.querySelector('#电梯-1') //获取电梯板实体
elevator.fixed = true//不受外力影响
elevator.collides = true//允许碰撞
const startPos = [喵, 11, 喵]//电梯的最低位置
const endPos = [喵, 50, 喵]//电梯的最高位置

const ani = elevator.animate([// 1+5+1+2=9, 总时间15秒被切成9份
    { position: startPos, duration: 1 },// 1/9 的时间停留在地面
    { position: startPos, duration: 5 },// 5/9 的时间用于地面往楼顶移动
    { position: endPos, duration: 1 },// 1/9 的时间停留在楼顶
    { position: endPos, duration: 2 },// 2/9 的时间从楼顶回到地面
], {
    duration: 16 * 15, //播放一个循环共用时15秒,
    iterations: 1,//动画只播放1次
    direction: Box3AnimationDirection.WRAP,//让动画实体从终点回到起点
})

ani.onReady(() => {//当动画开始播放时
    world.say('电梯准备启动')
})

ani.onFinish(() => {//当动画结束播放时
    world.say('电梯自己停了')
})

world.onPress(({ button }) => {
    if (button === Box3ButtonType.ACTION0) {//左键重新播放动画
        ani.play({
            duration: 16 * 15, //播放一个循环共用时15秒(每秒16帧),
            iterations: Infinity,//动画无限次循环播放
            direction: Box3AnimationDirection.WRAP,//让动画实体从终点回到起点
        })
    }
    else if (button === Box3ButtonType.ACTION1) {//右键停止动画
        ani.cancel()
    }
})

 

2.其他方法

啊哈,挺好用,只不过视觉不好,那,不做动画,学《美味厨房》?

代码已来,认真学哦!

const lift_position = [[11, 2, 15], [8, 10, 15], [8, 20, 15], [8, 33, 15]]

entity.dialogans = await selectDialog(entity, '请问你要去哪一层?', '电梯', targetEntity.id == 'F1' ? ['F2', 'F3', 'F4'] : targetEntity.id == 'F2' ? ['F1', 'F3', 'F4'] : targetEntity.id == 'F3' ? ['F1', 'F2', 'F4'] : ['F1', 'F2', 'F3']);
        if (entity.dialogans) {
                let nub = Number((entity.dialogans.value).slice(1, 2))
                entity.position = lift_position[nub - 1];
                entity.dialogans = null;
            }

自己加上互动代码~

 

要视觉感?好,亨利佐撸起了袖子,自己干!

代码:

//电梯代码(亨利佐版本)
         if (targetEntity.hasTag('lift')) {
             if(targetEntity.id.slice(1,2)==lift_floor){ entity.player.directMessage('您已进入电梯');entity.position.set(81,10,71);}
            else entity.player.directMessage('当前电梯不在这层(在F'+lift_floor+'层),您大概要等'+Math.abs(Number(targetEntity.id.slice(1,2))-lift_floor)*5+'秒')
         }
         if (targetEntity.hasTag('lift_02')) {
             entity.dialogans = await selectDialog(entity, '请问你要去哪一层?', '电梯', targetEntity.id == 'F1' ? ['F2'] : ['F1']);
           if (entity.dialogans) {
                 let nub = Number((entity.dialogans.value).slice(1, 2))
                entity.position = lift_position_02[nub - 1];
                 entity.dialogans = null;
             }
         }
        if(targetEntity.hasTag('door_in_lift')){
            entity.position = lift_position[lift_floor - 1];
        }
 var lift_floor=0,lift_on='up'
 async function lift(){
 while(true){
 if(lift_on=='up') lift_floor++;
 if(lift_on=='down') lift_floor--;
 if(lift_floor>=4) lift_on='down';
 if(lift_floor==1) lift_on='up';
 if(!lift_floor==0)
 world.say('电梯到达F'+lift_floor);
 await sleep(5000);
 }
 }
 lift()

嗯,不错,建个框架就可以了!

3.自动扶梯?

有人尝试过做自动扶梯吗?

(台下:“没有!”)

好吧,亨利佐写了一个自动扶梯的代码,

上代码:

world.querySelectorAll('#简陋得电梯-1').forEach((e)=>{
    if(!e.hasTag('2')){
        const bounds = getEntityBounds(e);
        world.addZone({
            bounds,
            force: [0,-0.5,-0.5],
        })
    }
})//向下
function getEntityBounds(entity){
    const lo = entity.position.sub(entity.bounds);
    const hi = entity.position.add(entity.bounds);
    const bounds = new Box3Bounds3(lo,hi);
    return bounds;
}
world.querySelectorAll('.2').forEach((e)=>{//向上
const bounds = getEntityBounds(e);
world.addZone({
    bounds,
    force: [0,1.5,1],
})
}

OK,电梯方块的名称自拟~

向上的时候可能会有bug(会卡住)

向上的电梯模型要加上标签:2

 

 

好啦,这一期教程结束了,学废了吗?

(台下:“没学废!”)
棒棒哒,亨利佐已经口干舌燥了,

再见!

 


回复

上一页1 页 / 共 1下一页
busterbuster

sofaaaaaaaaaaaaaaaaaaaaaaaaa

点赞0


评论


AK亨利佐加DanJamesThomasAK亨利佐加DanJamesThomas

告诉大家,我是ak本人。这个亨利左是神岛火图美味厨房的合作者。代码功底较深哦

点赞0


评论