道具屬性加成
// 跳躍道具
const jumpProp = world.querySelector('#彈簧-1') // 獲取場景中的“彈簧”實(shí)體,作為跳躍道具
jumpProp.id = "跳躍道具"? ? ?// 為了下面方便使用,將實(shí)體命名為“跳躍道具”
jumpProp.collides = true? ?// 開啟道具的實(shí)體碰撞
// 加速道具
const speedProp = world.querySelector('#MC西瓜-1') // 獲取場景中的“西瓜”實(shí)體,作為加速道具
speedProp.id = "加速道具"? ? ?// 為了下面方便使用,將實(shí)體命名為 加速道具
speedProp.collides = true? ?// 開啟道具的實(shí)體碰撞
// 罰站道具
const trapProp = world.querySelector('#陷阱-1')? ? // 獲取場景中的“陷阱”實(shí)體,作為罰站道具
trapProp.id = "罰站道具"? ? ? ? // 為了下面方便使用,將實(shí)體命名為 罰站道具
trapProp.collides = true? ? ? // 開啟道具的實(shí)體碰撞
// 隱身道具
const invisibleProp = world.querySelector('#隱身技能卡-1')? ?// 獲取場景中的“隱身卡”實(shí)體,作為隱身道具
invisibleProp.id = "隱身道具"? ? // 為了下面方便使用,將實(shí)體命名為 隱身道具
invisibleProp.collides = true? // 開啟道具的實(shí)體碰撞
// 當(dāng)實(shí)體與實(shí)體發(fā)生碰撞時(shí)
world.onEntityContact(async ({ entity, other }) => {
? ? // 首先需要判斷當(dāng)前碰撞的實(shí)體是玩家類型的實(shí)體觸發(fā)的
? ? if (entity && entity.isPlayer) {
? ? ? ? // 由于onEntityContact事件會因?yàn)閮蓚€(gè)實(shí)體碰撞而觸發(fā),
? ? ? ? // 因此,當(dāng)我們確定了其中一個(gè)是“玩家”類型的實(shí)體之后(entity.isPlayer)
? ? ? ? // 另一個(gè)實(shí)體other 便是我們要判斷的實(shí)體道具
? ? ? ? // 接下來會根據(jù)實(shí)體類型的不同,區(qū)分碰撞到之后對玩家產(chǎn)生的效果
? ? ? ? // 如果玩家碰到的是加速道具,那么我們希望玩家可以獲得一個(gè)加速效果
? ? ? ? // 要對玩家的移動進(jìn)行加速,可以通過修改玩家的:行走速度、行走加速度、跑步速度以及跑步加速度來實(shí)現(xiàn)
? ? ? ? if (other.id === "加速道具") {
? ? ? ? ? ? // 加速道具被碰到之后就刪除掉它(這樣才像被玩家"吃掉"了)
? ? ? ? ? ? other.destroy()
? ? ? ? ? ??
? ? ? ? ? ? // 行走速度默認(rèn)為0.22,這里設(shè)置為2倍的速度
? ? ? ? ? ? entity.player.walkSpeed = 0.44
? ? ? ? ? ??
? ? ? ? ? ? // 行走加速度默認(rèn)為0.19,這里設(shè)置為2倍的速度
? ? ? ? ? ? entity.player.walkAcceleration = 0.38
? ? ? ? ? ??
? ? ? ? ? ? // 跑步速度默認(rèn)為0.4,這里設(shè)置為2倍的速度
? ? ? ? ? ? entity.player.runSpeed = 0.8
? ? ? ? ? ??
? ? ? ? ? ? // 跑步加速度默認(rèn)為0.35,這里設(shè)置為2倍的速度
? ? ? ? ? ? entity.player.runAcceleration = 0.7
? ? ? ? ? ? entity.player.directMessage(`你獲得了奔跑加速!`)
? ? ? ? ? ? // 設(shè)置定時(shí)器,定時(shí)時(shí)間為5000毫秒(5秒),5秒后會觸發(fā)定時(shí)器內(nèi)的代碼
? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? // 將玩家的移動和跑步速度復(fù)原
? ? ? ? ? ? ? ? entity.player.walkSpeed = 0.22
? ? ? ? ? ? ? ? entity.player.walkAcceleration = 0.19
? ? ? ? ? ? ? ? entity.player.runSpeed = 0.4
? ? ? ? ? ? ? ? entity.player.runAcceleration = 0.35
? ? ? ? ? ? }, 5000);
? ? ? ? }
? ? ? ? // 如果玩家碰到的是跳躍道具,那么我們希望玩家可以獲得一個(gè)向上跳躍的效果
? ? ? ? if (other.id === "跳躍道具") {
? ? ? ? ? ? other.destroy()
? ? ? ? ? ??
? ? ? ? ? ? // 設(shè)置玩家的跳躍力度為原本的兩倍
? ? ? ? ? ? entity.player.jumpPower = 0.96 * 2
? ? ? ? ? ? entity.player.directMessage(`你獲得了跳躍加成!`)
? ? ? ? ? ? // 設(shè)置定時(shí)器,定時(shí)時(shí)間為5000毫秒(5秒),5秒后會觸發(fā)定時(shí)器內(nèi)的代碼
? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? // 將玩家的跳躍力度復(fù)原
? ? ? ? ? ? ? ? entity.player.jumpPower = 0.96
? ? ? ? ? ? }, 5000);
? ? ? ? }
? ? ? ? // 如果玩家碰到的是罰站道具,那么我們希望玩家完全不能移動
? ? ? ? if (other.id === "罰站道具") {
? ? ? ? ? ? other.destroy()
? ? ? ? ? ? // 參考“加速道具”的實(shí)現(xiàn)方式,只要我們喵家的行走速度、行走加速度、跑步速度以及跑步加速度都設(shè)置為0
? ? ? ? ? ? // 玩家就沒辦法走路和跑步了
? ? ? ? ? ? entity.player.walkSpeed = 0
? ? ? ? ? ? entity.player.walkAcceleration = 0
? ? ? ? ? ? entity.player.runSpeed = 0
? ? ? ? ? ? entity.player.runAcceleration = 0
? ? ? ? ? ? // 順便把玩家的“跳躍”給關(guān)閉了,這樣一來玩家不但不能走和跑,也不能跳了
? ? ? ? ? ? entity.player.enableJump = false
? ? ? ? ? ? entity.player.directMessage(`你撞到了陷阱,暫時(shí)無法行動!`)
? ? ? ? ? ? // 設(shè)置定時(shí)器,定時(shí)時(shí)間為5000毫秒(5秒),5秒后會觸發(fā)定時(shí)器內(nèi)的代碼
? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? // 將玩家的移動和跑步速度復(fù)原
? ? ? ? ? ? ? ? entity.player.walkSpeed = 0.22
? ? ? ? ? ? ? ? entity.player.walkAcceleration = 0.19
? ? ? ? ? ? ? ? entity.player.runSpeed = 0.4
? ? ? ? ? ? ? ? entity.player.runAcceleration = 0.35
? ? ? ? ? ? ? ? // 將玩家的跳躍設(shè)置打開
? ? ? ? ? ? ? ? entity.player.enableJump = true
? ? ? ? ? ? }, 5000);
? ? ? ? }
? ? ? ? // 如果玩家碰到的是隱身道具,那么我們希望玩家隱身不可見
? ? ? ? if (other.id === "隱身道具") {
? ? ? ? ? ? other.destroy()
? ? ? ? ? ? // 隱藏所有身體部件
? ? ? ? ? ? for (const bodyPart in entity.player.skinInvisible) {
? ? ? ? ? ? ? ? entity.player.skinInvisible[bodyPart] = true;
? ? ? ? ? ? }
? ? ? ? ? ? entity.player.directMessage(`你獲得了隱身效果!`)
? ? ? ? ? ? // 設(shè)置定時(shí)器,定時(shí)時(shí)間為5000毫秒(5秒),5秒后會觸發(fā)定時(shí)器內(nèi)的代碼
? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? // 顯示所有身體部件
? ? ? ? ? ? ? ? for (const bodyPart in entity.player.skinInvisible) {
? ? ? ? ? ? ? ? ? ? entity.player.skinInvisible[bodyPart] = false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }, 5000);
? ? ? ? }
? ? }
})