猫史档案馆


跑酷代码【基本】

用户:不爱做跑酷不爱做跑酷查看:9 回复:9 评论:9 创建时间:2022-05-06T21:21:52


跑酷代码其实很简单!

emotion_编程猫_加油

首先引入吉吉喵的基础代码:

// 当玩家进入地图时
world.onPlayerJoin(({ entity }) => {
    entity.player.spawnPoint = new Box3Vector3(0,0,0)
    //玩家的出生点坐标,y值要加一
    // 强制玩家重生
    entity.player.forceRespawn()
})


world.onPlayerJoin(({ entity }) => {
    entity.onFluidEnter(() => {//当玩家掉到水里
        entity.position.copy(entity.player.spawnPoint)
        entity.position.y += 4
    })
})

之后,建筑跑酷,这我就不教了。

再然后,如果你的跑酷有关太难,玩家过不去怎么办?

当然要加商店!

var money = 0

world.onPress(async ({ entity, button }) => {
    if (button === Box3ButtonType.ACTION1) {
        const result = await entity.player.dialog({
            type: Box3DialogType.SELECT,
            title: "右键菜单",
            titleTextColor: new Box3RGBAColor(0, 0, 0, 1),
            titleBackgroundColor: new Box3RGBAColor(0.968, 0.702, 0.392, 1),
            content: `
        名称:${entity.player.name}
        金币数:${entity.money}
        `,
            options: ['商店'],   // 将提供玩家选择的选项放入数组里。
            contentTextColor: new Box3RGBAColor(0, 0, 0, 1),
            contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
            lookEye: entity.position.add(entity.player.facingDirection.scale(5)),
            lookTarget: entity,
        });
        if (!result || result === null) {
            return;
        }
        // 判断玩家选了什么选项。
        switch (result.index) {
            case 0:
                // 如果选择了第一项,即:'没问题'
                const resulqt = await entity.player.dialog({
                    type: Box3DialogType.SELECT,
                    title: "商店",
                    titleTextColor: new Box3RGBAColor(0, 0, 0, 1),
                    titleBackgroundColor: new Box3RGBAColor(0.968, 0.702, 0.392, 1),
                    content: `${entity.player.name},你要买什么?
        `,
                    options: ['3秒飞行券(100元)', '10秒二段跳券(50元)'],   // 将提供玩家选择的选项放入数组里。
                    contentTextColor: new Box3RGBAColor(0, 0, 0, 1),
                    contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
                    lookEye: entity.position.add(entity.player.facingDirection.scale(5)),
                    lookTarget: entity,
                });
                if (!resulqt || resulqt === null) {
                    return;
                }
                // 判断玩家选了什么选项。
                switch (resulqt.index) {
                    case 0:
                        // 如果选择了第一项,即:'没问题'
                        if (entity.money > 99) {
                 entity.money=entity.money-100
                entity.player.canFly=true
                entity.player.directMessage('购买成功')
                setTimeout(() => {
            entity.player.canFly=false
       }, 10000)
                        } else {
                            entity.player.directMessage('喵')
                        }
                        break;
                    case 1:
                        // 如果选择了第二项,即:'现在没空'
                        if (entity.money > 49) {
                entity.money=entity.money-50
                entity.player.doubleJumpPower=true
                entity.player.directMessage('购买成功')
                setTimeout(() => {
            entity.player.doubleJumpPower=false
       }, 10000)
                        } else {
                            entity.player.directMessage('喵')
                        }
                        break;
                    default:
                    // 注意,使用 switch 分支的时候,不要漏了后面的 break; 
                }}}

        })

之后呢?

有了商店,那就要有钱啊!

world.onRelease(async ({ button, position, entity }) => {
    if (button === Box3ButtonType.JUMP || button === Box3ButtonType.DOUBLE_JUMP) {
        entity.x++
        if (entity.x > 4) {
            await entity.x - 5
            entity.money++
        }
    }
})

玩家的进度不能保存,就把每个存档点打上不重复的数字标签

之后,输入以下代码:

var de = ''
async function showplayers() {
    //console.clear();
    const playerlist = await db.sql`SELECT * FROM player`;
    for (const p of playerlist) {
        ;
        console.log(JSON.stringify(p));
    };
};

async function createtable() {
    await db.sql`CREATE TABLE IF NOT EXISTS player (
    money INTEGER DEFAULT 0,--金钱
    userKey TEXT PRIMARY KEY UNIQUE DEFAULT '',--玩家的识别码
    de TEXT DEFAULT ''
    )`
    showplayers()
}
createtable()
async function saveplayer(entity) {
    await db.sql`INSERT INTO player (
            money,
            userKey,
            de
        )
        VALUES(
            ${entity.money},
            ${entity.player.userKey},
            ${entity.de}
        )
        ON CONFLICT(userKey)
        DO UPDATE Set

        money=excluded.money,
        de=excluded.de
        `

}


async function loadplayer(entity) {
    const data = (await (db.sql`SELECT * FROM player WHERE userkey=${entity.player.userKey} limit 1`))[0]
    if (data) {
        entity.money = data.money
    }
}
for (const e of world.querySelectorAll('*')) {//遍历所有实体
    if (e.id.startsWith('存档点')) {//如果当前实体名左边部分刚好是"存档点", 比如 存档点-1 存档点-2...
        e.collides = true //开启碰撞
        e.fixed = true //固定实体不被推移
        e.onEntityContact(({ other, entity }) => { //每当存档点碰到另一个实体
            if (other.isPlayer) { //另一个实体是玩家
                if (e.position !== other.player.spawnPoint) { // 检测当前存档点是否玩家重生点, 避免反复在同一个存档点做多余的保存
                    other.player.directMessage(`到达新的重生点${e.tags()}号`) // 给玩家发消息
                    other.player.spawnPoint = e.position // 喵家重生点坐标设置成存档点的坐标
                    other.de = `${e.tags()}`
                }
            }
        })
    }
}
world.onPlayerLeave(async({entity})=>{
    await savePlayer(entity)
})
world.onPlayerJoin(async({entity})=>{
    await loadplayer(entity)
})

我在神奇代码岛也叫不爱做跑酷!欢迎关注!

 

https://box3.codemao.cn/p/a121ced870692ce733e2

请进

 


回复

上一页1 页 / 共 1下一页
灵清帝王吃爆一切厕所灵清帝王吃爆一切厕所

entity.player.doubleJumpPower=true

你确定这是开启二段跳??????

 

点赞0


评论


不爱做跑酷不爱做跑酷

前面要删去:

world.onPlayerJoin(({ entity }) => {
    entity.player.spawnPoint = new Box3Vector3(0,0,0)
    //玩家的出生点坐标,y值要加一
    // 强制玩家重生
    entity.player.forceRespawn()
})

后面还要加上


world.onPlayerJoin(({ entity }) => {
    entity.player.spawnPoint = new Box3Vector3(0,0,0)
    //玩家的出生点坐标,y值要加一
    // 强制玩家重生
    entity.player.forceRespawn()
    entity.position.set(world.querySelector('.' + entity.de).position.x, world.querySelector('.' + entity.de).position.y + 2, world.querySelector('.' + entity.de).position.z)
})

点赞0


评论


不爱做跑酷不爱做跑酷

顶顶顶顶

点赞0


评论


豹2a7豹2a7

示例咋知道我的岛3账号的,我没登陆呀

点赞1


评论


可爱的电电猴可爱的电电猴

那上面怎么是我的名字。。。

点赞1


评论


可爱的电电猴可爱的电电猴

请问如果要落到方块white_grass上就重生的代码怎么写

点赞1


评论


熠心逸意熠心逸意

可以用嘛

点赞0


评论


yyds二营长yyds二营长

ddd

点赞0


评论


天天fUg-天天fUg-

这名字

 

点赞0


评论