猫史档案馆


【box3】跑酷比赛代码(第3板

用户:AokenAoken查看:0 回复:0 评论:0 创建时间:2023-10-20T21:38:11


console.clear()
world.addCollisionFilter('player', 'player');

var pos=[123,14,5]//这里填出生点坐标

for (const e of world.querySelectorAll('*')) {//遍历所有实体
    if (e.id.startsWith('出生点')) {//如果当前实体名左边部分刚好是"存档点", 比如 存档点-1 存档点-2...
        e.collides = true //开启碰撞
        e.fixed = true //固定实体不被推移
        e.onEntityContact(({ other }) => { //每当存档点碰到另一个实体
            if (other.isPlayer) { //另一个实体是玩家
                if (e.position !== other.player.spawnPoint) { // 检测当前存档点是否玩家重生点, 避免反复在同一个存档点做多余的保存
                    other.player.directMessage('到达新的重生点,继续加油(ง •_•)ง') // 给玩家发消息
                    other.player.spawnPoint.set(e.position) // 玩家重生点坐标设置成存档点的坐标
                }
            }
        })
    }
}

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

/* 私聊函数 */
function Input_dialog(entity,title,text,input_text){
    return entity.player.dialog({
        type: 'input',// 输入框类型
        title: title,// 标题
        titleTextColor: new Box3RGBAColor(0, 0, 0, 1),
        titleBackgroundColor: new Box3RGBAColor(0.968, 0.702, 0.392, 1),
        content: text, // 文本
        placeholder: input_text// 输入框背景上的提示文字。
    })  
}

async function Target_msg(entity,targetEntity,text){/*entity发起消息的玩家,targetEntity收到消息的玩家,text内容*/
    if(!text)return;//消息为空直接退出函数
    const tar_msg=await Input_dialog(
        entity, 
        targetEntity.player.name, 
        `[${targetEntity.player.name}]说:${text}`, 
        `请输入你要对[${targetEntity.player.name}]说的话`
    );if(!tar_msg||tar_msg===0)return;//消息为空直接退出函数
    await Target_msg(targetEntity,entity,tar_msg)//反复调用私聊函数,实现无限互动,不需要在新开
}

world.onPlayerJoin(({entity}) =>{
    entity.enableInteract = true; //互动开关
    entity.interactRadius = 3;  //互动范围
    entity.onInteract(async({entity,targetEntity}) =>{
        const other = await entity.player.dialog({
            type: 'select', //对话框类型为选择框
            content: `Ta的个人信息:\n\n昵称:${targetEntity.player.name}`,//提示框内容
            options: ['私聊','开始比赛'], //选项
        })
        if (!other)return; //确保对话框不是点击x关闭
        if (other.value === '私聊') { //被点击的是私聊按钮
            const msg=await Input_dialog(entity, '私聊', `你要对TA说什么?`, `请输入你要对[${targetEntity.player.name}]说的话`)
            if(!msg)return;//消息为空直接退出函数
            await Target_msg(targetEntity,entity,msg)     
        }else if(other.index === 1){
            const led_win = await targetEntity.player.dialog({
                type: 'select',// 输入框类型
                title: entity.player.name,// 标题
                titleTextColor: new Box3RGBAColor(0, 0, 0, 1),
                titleBackgroundColor: new Box3RGBAColor(0.968, 0.702, 0.392, 1),
                content: `${entity.player.name} 想要和你进行比赛`, // 文本
                options: ['确定','取消']
            }) 
            if(!led_win)return;
            if(led_win.index === 0){
                entity.player.cancelDialogs()
                targetEntity.player.cancelDialogs()
                if(entity.RACE==0 && targetEntity.RACE==0){
                    entity.PLAYER_race=[entity.player.name, targetEntity.player.name];
                    targetEntity.PLAYER_race=[entity.player.name, targetEntity.player.name];
                    entity.race_list = [entity,targetEntity]
                    targetEntity.race_list = [entity,targetEntity]

                    entity.player.color.set(1,0,0)//比赛者变红
                    entity.RACE=1//加标签
                    entity.position.set(pos[0],pos[1],pos[2]);
                    entity.timer = new Box3Timed()
                    entity.player.canFly=0
                    entity.result=1

                    targetEntity.player.color.set(0,0,1)//参赛者变蓝
                    targetEntity.RACE=1//加标签
                    targetEntity.position.set(pos[0],pos[1],pos[2])
                    targetEntity.timer = new Box3Timed()
                    targetEntity.player.canFly=0
                    targetEntity.result=1
                    world.say(`${entity.player.name}和${targetEntity.player.name}比赛开始!`)
                }else entity.player.directMessage(`${targetEntity.player.name} 当前正在比赛中,请稍后`);
            }else if(led_win.index === 1){entity.player.directMessage(`${targetEntity.player.name}推开了你`)};
        }
    })
})

async function To_end_race(entity,value=0){
    if(entity.RACE==1){
        world.say(`${entity.PLAYER_race[0]}和${entity.PLAYER_race[1]}比赛结束`)
        entity.race_list[0].player.color.set(1, 1, 1);
        entity.race_list[1].player.color.set(1, 1, 1);
        entity.race_list[0].RACE=0
        entity.race_list[1].RACE=0
        [entity.PLAYER_race,entity.race_list]=0
        if(entity.player.name == entity.PLAYER_race[0]){
            world.say(`${entity.PLAYER_race[1-value]}获胜`)
        }else if(entity.player.name == entity.PLAYER_race[1]){
            world.say(`${entity.PLAYER_race[0+value]}获胜`)
        }
    }else if(entity.RACE==0)return;
}

world.onPress(async({entity,button}) =>{
    if(button=='action1'){
        let opt=['切换视角','重新开始','观战','发言','回到存档点','俯视全图']
        if(entity.RACE==1){opt.push('退出比赛')}
        const res = await entity.player.dialog({
            type: 'select',
            title:entity.player.name,
            titleTextColor:new Box3RGBAColor(0, 0, 0, 1),
            titleBackgroundColor:new Box3RGBAColor(0.968, 0.702, 0.392, 1),
            contentBackgroundColor:new Box3RGBAColor(0.084,0.894,0.57,0.789),
            content: `用户名:${entity.player.name}`,
            options: opt
        })
        if(!res)return;
        switch(res.value){
            case '切换视角':
                if (entity.player.cameraMode === 'follow')entity.player.cameraMode = 'fps';
                else  entity.player.cameraMode = 'follow';
                break;
            case '重新开始':
                entity.position.set(pos[0],pos[1],pos[2]);
                entity.timer = new Box3Timed()
                entity.player.canFly=0;
                entity.result=1
                break;
            case '观战':
                entity.player.directMessage(`观战自己可退出观战`);
                var players = world.querySelectorAll('player');
                var names = [];
                for (const i in players)names.push(players[i].player.name);
                const result = await entity.player.dialog({
                    type: 'select',
                    title:'观战系统',
                    titleTextColor: new Box3RGBAColor(0,0,0,1),
                    titleBackgroundColor: new Box3RGBAColor(0.968,0.702,0.392,1),
                    contentBackgroundColor:new Box3RGBAColor(0.354,0.778,0.88,0.789),
                    lookEye: entity.player.position,
                    content: '请问你要观战谁?',
                    options: names,
                })
                if(!result)return;
                world.querySelectorAll('player').forEach((x) =>{
                    if (x.player.name == result.value) {
                        entity.player.cameraEntity = x;
                        x.player.directMessage(`${entity.player.name} 正在观战你`)
                    }
                });
                break;
            case '发言':
                const say=await entity.player.dialog({
                    type:'input',
                    title:'发言',
                    titleTextColor: new Box3RGBAColor(0, 0, 0, 1),
                    titleBackgroundColor: new Box3RGBAColor(0.968, 0.702, 0.392, 1),
                    contentBackgroundColor:new Box3RGBAColor(0.934,0.678,0.88,0.789),
                    content:'注:此功能是为了方便那些不能发言的人'
                });
                if(!say)return;
                else world.say(`${entity.player.name}:${say}`); 
                break;
            case '回到存档点':
                entity.player.directMessage('已回到存档点!');
                entity.player.forceRespawn()
                entity.player.invisible = false;
                break;  
            case '俯视全图':
                const lookTarget = new Box3Vector3(63, 8, 63);
                const lookEye = new Box3Vector3(63, 180, 63);
                const lookUp = new Box3Vector3(1, 0, 0);
                entity.player.dialog({
                    type: 'text',
                    contentBackgroundColor:new Box3RGBAColor(0.654,0.378,0.988,0.789),
                    content: `正在俯视中`,
                    lookEye: lookEye,
                    lookTarget: lookTarget,
                    lookUp: lookUp
                });break;
            default:'退出比赛'
                const r=await entity.player.dialog({
                    type:'select',
                    titleBackgroundColor: new Box3RGBAColor(0.968, 0.702, 0.392, 1),
                    contentBackgroundColor:new Box3RGBAColor(0.934,0.678,0.88,0.789),
                    options:['确定','取消']
                })
                if(!r)return;
                if(r.index==0)To_end_race(entity)      
        }
    }
})

//计时
class Box3Timed {
    constructor() {
        (async () => {
            this.time = 0;
            this.over = false;
            while (!this.over) {
                await sleep(1);
                this.time += 1;
            }
        })()
    }
    stop(){return this.over = true}//停止计时
    getTime(){return this.time}
}

//停止计时
world.onPlayerJoin(({entity}) =>{
    entity.RACE=0
    entity.PLAYER_race=0
    // 设置刚进入游戏的玩家持有通关时间(timer)数量为0
    entity.result = 1;
    entity.timer = new Box3Timed()
    entity.onVoxelContact(async({entity,voxel}) =>{
        if(entity.player.canFly == 0){      
            // 将方块id转换名称
            const voxelName = voxels.name(voxel);  
            // 如果方块名称是carpet_08
            if(entity.result==1 && voxelName == 'carpet_08'){
                entity.result = 0
                world.say(`恭喜 ${entity.player.name} 通关!用时${entity.timer.getTime()/1000}秒 `)
                entity.timer.stop()
                entity.timer = entity.timer.getTime()/1000
                entity.player.directMessage('===你已通关,获得飞行权限===');
                entity.player.canFly = 1;
                To_end_race(entity,1)           
            }else return;
        }else return;
    })
});

world.onPlayerLeave(({entity}) =>{
    To_end_race(entity)
})

 

:此代码包含了许多功能(缩减了几十行代码

1.背包系统

2.比赛系统(修复了一些严重bug——可以循环比赛,防止有人在比赛中和别人比赛

3.存档点(修复了一些bug

 

如要食用请标注初始地!

(这个可能好像可以优化的,作者到时候在慢慢优化)

(如果是新dao3.fun,请将Box3改成Game


回复

上一页1 页 / 共 0下一页