猫史档案馆


【教程贴子】跑酷必备代码

用户:灵清帝王吃爆一切厕所灵清帝王吃爆一切厕所查看:21 回复:19 评论:21 创建时间:2022-05-08T11:26:59


备注:以下代码全由本人自己肝的()如有雷同,纯属巧合()

 

碰水回存档点代码:

world.onFluidEnter(async({ entity , voxel }) => {//当实体进入液体中时
    if(entity.isPlayer) {//如果实体是玩家
        if(voxel == voxels.id('water')) {//如果液体方块名称是water
            entity.player.forceRespawn()//玩家重生
        }
    }else {
        return//不处理非玩家实体
    }
})

 

存档点代码(要先给存档点打上标签:存档点):

world.querySelectorAll('.存档点').forEach(async( mesh ) => {//搜索所有标签为存档点的实体
    mesh.onEntityContact(async({ other }) => {//当它和另外一个实体碰撞时。注意onEntityContact参数要填other,而不是entity
        if(other.isPlayer) {//如果另一个实体是玩家
            if(other.player.spawnPoint.x != mesh.position.x && other.player.spawnPoint.y != mesh.position.y + 1 && other.player.spawnPoint.z != mesh.position.z) {//如果玩家的重生点和存档点的位置不一样
                other.player.directMessage('到达新的重生点') // 给玩家发消息
                other.player.spawnPoint.x = mesh.position.x;
                other.player.spawnPoint.y = mesh.position.y + 1;//为了防止卡在方块里
                other.player.spawnPoint.z = mesh.position.z;
            }
        }
    })
})

 

碰到方块趋势,自己填方块名称:

world.onVoxelContact(async({ entity , voxel }) => {//当实体碰到方块时
    if(entity.isPlyaer) {//如果实体是玩家
        if(voxel == voxels.id('方块的名称')) {
            entity.hurt(100);
            for(let i = 0; i <= 3; i++) {
                entity.player.directMessage(`复活倒计时:${String(3 - t)}秒`);
                await sleep(1000);
            }
            entity.player.forceRespawn()
        }
    }
})

 

有时候可能跑酷太难()一次性跑不完,这时候就要用SQL了():

(async function() {
    await createTable()
})();

world.onPlayerJoin(async({ entity }) => {
    try {
        await loadUser(entity)
    }catch(error1) {
        world.say(`loadUser sql error : ${error1}`)
    };
})


async function createTable() {
    await db.sql`
        CREATE TABLE IF NOT EXISTS "player"(
            "x" REAL NOT NULL,
            "y" REAL NOT NULL,
            "z" REAL NOT NULL,
            "userKey" CHAR(16) PRIMARY KEY UNIQUE NOT NULL
        )
    `
}


async function savePlayer(entity) {
    if(entity.player.userKey) {
        await db.sql`
            INSERT INTO "player"(
                "x", 
                "y",
                "z",
                "userKey"
            )
            VALUES(
                ${entity.position.x},
                ${entity.position.y},
                ${entity.position.z},
                ${entity.player.userKey}
            )
            
            ON CONFLICT("userKey")
            DO UPDATE SET
            "x" = excluded."x",
            "y" = excluded."y",
            "z" = excluded."z"
        `
    }
}

async function loadUser(user) {
    const [data] = await db.sql`SELECT * FROM "player" WHERE "userKey" = ${user.player.userKey} LIMIT 1`;
    if(data) {
        user.position.x = data.x;
        user.position.y = data.y;
        user.position.z = data.z;
    }
}


async function removeplayer (entity) {
    await db.sql`DELETE FROM "player" WHERE "userKey" = ${entity.player.userKey}`
}

world.onPress(async({ entity , button }) => {
    if(button == Box3ButtonType.ACTION1) {
        if(entity.player.walkState == Box喵layerWalkState.CROUCH) {
            const delete_player = await entity.player.dialog({
                type:Box3DialogType.SELECT,
                title:null,
                content:'你要删除存档吗?',
                options:['确定']
            })
            if(delete_player.value == '确定') {
                await removeplayer(entity)
            }
        }
    }
})

 

然后在存档点代码那里加载savePlayer,最后给出全部代码:

world.onFluidEnter(async({ entity , voxel }) => {//当实体进入液体中时
    if(entity.isPlayer) {//如果实体是玩家
        if(voxel == voxels.id('water')) {//如果液体方块名称是water
            entity.player.forceRespawn()//玩家重生
        }
    }else {
        return//不处理非玩家实体
    }
})

world.querySelectorAll('.存档点').forEach(async( mesh ) => {//搜索所有标签为存档点的实体
    mesh.onEntityContact(async({ other }) => {//当它和另外一个实体碰撞时。注意onEntityContact参数要填other,而不是entity
        if(other.isPlayer) {//如果另一个实体时玩家
            if(other.player.spawnPoint.x != mesh.position.x && other.player.spawnPoint.y != mesh.position.y + 1 && other.player.spawnPoint.z != mesh.position.z) {//如果玩家的重生点和存档点的位置不一样
                other.player.directMessage('到达新的重生点') // 给玩家发消息
                other.player.spawnPoint.x = mesh.position.x;
                other.player.spawnPoint.y = mesh.position.y + 1;//为了防止卡在方块里
                other.player.spawnPoint.z = mesh.position.z;
                await savePlayer(other)
            }
        }
    })
})


world.onVoxelContact(async({ entity , voxel }) => {//当实体碰到方块时
    if(entity.isPlyaer) {//如果实体是玩家
        if(voxel == voxels.id('方块的名称')) {
            entity.hurt(100);
            for(let i = 0; i <= 3; i++) {
                entity.player.directMessage(`复活倒计时:${String(3 - t)}秒`);
                await sleep(1000);
            }
            entity.player.forceRespawn()
        }
    }
})



(async function() {
    await createTable()
})();

world.onPlayerJoin(async({ entity }) => {
    try {
        await loadUser(entity)
    }catch(error1) {
        world.say(`loadUser sql error : ${error1}`)
    };
})


async function createTable() {
    await db.sql`
        CREATE TABLE IF NOT EXISTS "player"(
            "x" REAL NOT NULL,
            "y" REAL NOT NULL,
            "z" REAL NOT NULL,
            "userKey" CHAR(16) PRIMARY KEY UNIQUE NOT NULL
        )
    `
}


async function savePlayer(entity) {
    if(entity.player.userKey) {
        await db.sql`
            INSERT INTO "player"(
                "x", 
                "y",
                "z",
                "userKey"
            )
            VALUES(
                ${entity.position.x},
                ${entity.position.y},
                ${entity.position.z},
                ${entity.player.userKey}
            )
            
            ON CONFLICT("userKey")
            DO UPDATE SET
            "x" = excluded."x",
            "y" = excluded."y",
            "z" = excluded."z"
        `
    }
}

async function loadUser(user) {
    const [data] = await db.sql`SELECT * FROM "player" WHERE "userKey" = ${user.player.userKey} LIMIT 1`;
    if(data) {
        user.position.x = data.x;
        user.position.y = data.y;
        user.position.z = data.z;
    }
}


async function removeplayer (entity) {
    await db.sql`DELETE FROM "player" WHERE "userKey" = ${entity.player.userKey}`
}

world.onPress(async({ entity , button }) => {
    if(button == Box3ButtonType.ACTION1) {
        if(entity.player.walkState == Box喵layerWalkState.CROUCH) {
            const delete_player = await entity.player.dialog({
                type:Box3DialogType.SELECT,
                title:null,
                content:'你要删除存档吗?',
                options:['确定']
            })
            if(delete_player.value == '确定') {
                await removeplayer(entity)
            }
        }
    }
})


回复

上一页1 页 / 共 1下一页
智能大黄鸡智能大黄鸡

[Codemao Lang] 自动暖帖~

点赞0


评论


灵清帝王吃爆一切厕所灵清帝王吃爆一切厕所

emotion_doge

点赞0


评论


电电小玩电电小玩

玩家怎么删档?

 

点赞0


评论


灵清帝王吃爆一切厕所灵清帝王吃爆一切厕所

ddddddddddd

点赞0


评论


灵清帝王吃爆一切厕所灵清帝王吃爆一切厕所

写好了教程怎么办?投稿编创协()emotion_doge

点赞0


评论


灵清帝王吃爆一切厕所灵清帝王吃爆一切厕所

喵喵喵喵喵

点赞0


评论


堂主の犬堂主の犬

存档这样好像更简单一些()()()

world.onPlayerJoinn(({entity})=>{
    entity.cundang = []
})
world.querySelectorAll('.存档点').forEach(async( mesh ) => {//搜索所有标签为存档点的实体
    mesh.onEntityContact(async({ other }) => {//当它和另外一个实体碰撞时。注意onEntityContact参数要填other,而不是entity
        if(other.isPlayer) {//如果另一个实体时玩家
                if(other.cundang == mesh.id){return}//防止玩家重复存档
                other.player.directMessage('到达新的重生点') // 给玩家发消息
                other.player.spawnPoint.x = mesh.position.x;
                other.player.spawnPoint.y = mesh.position.y + 1;//为了防止卡在方块里
                other.player.spawnPoint.z = mesh.position.z;
                other.cundang = mesh.id
                await savePlayer(other)
            }
        }
    })
})

点赞0


评论


浮生awa浮生awa

加个计时器emotion_doge(doge)

点赞0


评论


电电小玩电电小玩

会报错

点赞0


评论


银狐残影银狐残影

真是太训了,这么简单我都会了好吧

点赞0


评论


外向的雷电猴-LHA外向的雷电猴-LHA

扣血

world.onVoxelContact(async({ entity , voxel }) => {//当实体碰到方块时
    if(entity.isPlyaer) {//如果实体是玩家
        if(voxel == voxels.id('lava02')) {
            entity.hurt(100);
            for(let i = 0; i <= 3; i++) {
                entity.player.directMessage(`复活倒计时:${String(3 - t)}秒`);
                await sleep(1000);
            }
            entity.player.forceRespawn()
        }
    }
})

这有点问题,扣不了血!

点赞0


评论


我是伊柒我是伊柒

ddd(不是神岛圈的我表示不太懂))

点赞0


评论


guxia666guxia666

就这?emotion_doge

点赞0


评论


一个STUB用户_14302596一个STUB用户_14302596

互勉:https://shequ.codemao.cn/community/427759

点赞0


评论


ssss1457ssss1457

收藏点赞imgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E7%82%B9%E8%B5%9E.gif"alt="emotion_编程猫_点赞"

点赞0


评论


灵清帝王吃爆一切厕所灵清帝王吃爆一切厕所

还漏了一个代码()不然没法伤害():

world.onPlayerJoin(({entity})=>{
     entity.enableDamage = true;
})

点赞0


评论


锦衣侠客锦衣侠客

鹅鹅

 

点赞0


评论


一勺小汤_纪念号一勺小汤_纪念号

考古

点赞0


评论


yee剑走天下yee剑走天下

啊太丢脸了()看着之前写的()感觉写了依托sh*t山

点赞1


评论