電梯代碼-甜甜的又又us
const?elevator?=?world.querySelector('#電梯地板-1')?//?獲取電梯地板實體
move?=?0?//用于判斷電梯是否正在移動的變量
elevator.collides?=?true?//?電梯開啟碰撞檢測
elevator.fixed?=?true?//?電梯不受外力影響
world.onEntityContact(({entity,?other})=>{?//?實時監(jiān)測整個世界的實體碰撞事件。entity指被碰撞的實體,?other則是主動撞過來的實體
????if?(move?==?0){?//避免重復(fù)引發(fā)事件帶來的電梯鬼畜
????????if?(entity.id=='電梯地板-1'?&&?other.isPlayer){?//?如果?玩家踏上了電梯
????????????if?(elevator.position.y<=28){?//?如果電梯y低于2樓地板,?則判定玩家在1樓
????????????????entity.velocity.y?=?0.1?//?電梯持續(xù)上移
????????????????world.say(other.player.name+'?坐電梯上樓')
????????????????move?=?1?//設(shè)置值為1,表示電梯開始移動
????????????}else{
????????????????entity.velocity.y?=?-0.1?//?電梯持續(xù)下移
????????????????world.say(other.player.name+'?坐電梯下樓')
????????????????move?=?1?//設(shè)置值為1,表示電梯開始移動
????????????}
????????}
????}
????})
world.onTick(async()=>{?//?實時檢測電梯位置,?進(jìn)行檢測是否到達(dá)1樓或2樓
????if?(elevator.position.y>34){?//?如果電梯y坐標(biāo)高于2樓的地板,?電梯到達(dá)2樓
????????elevator.position.y?=?34?//?復(fù)位到2樓的準(zhǔn)確位置
????????elevator.velocity.y?=?0?//?電梯停止移動
????????await?sleep(1000)?//等待1秒,避免電梯停止時玩家彈起再次與電梯發(fā)生碰撞讓電梯下行
????????move?=?0?//設(shè)置為0,表示電梯停止移動
????}
????if?(elevator.position.y<28){?//?如果電梯y坐標(biāo)低于1樓的地板,?電梯到達(dá)1樓
????????elevator.position.y?=?28?//?復(fù)位到1樓的準(zhǔn)確位置
????????elevator.velocity.y?=?0?//?電梯停止移動
????????await?sleep(1000)?//等待1秒,避免電梯停止時玩家彈起再次與電梯發(fā)生碰撞讓電梯上行
????????move?=?0?//設(shè)置為0,表示電梯停止移動
????}
})