猫史档案馆


【岛三教程】有转弯半径的多人载具?一个教程给你搞定

用户:Dark_forestDark_forest查看:14 回复:9 评论:14 创建时间:2023-01-05T18:47:51


某天,反(香肠派对的那位)找我要个多人载具代码

作为一名合格的顶级代码师,

安排!

world.onPlayerJoin(({ entity }) => {//数据初始化
    entity.pl = -1
})
const PI2 = Math.PI * 2;
const PI3 = Math.PI * 0.5
const plane_Quat = new Box3Quaternion(0, 1, 0, 1)
for (let i = 0; i < 10; i++) {
    const qxs = world.createEntity({
        tags: ['载具'],
        mesh: `mesh/FCX New Era 2 super.vb`,
        meshScale: [0.0325, 0.0325, 0.0325],
        collides: true,// 碰撞
        fixed: false,// 固定
        gravity: true,// 重力
        position: [randint(10, 120), randint(1, 6) * 10, randint(10, 120)],// 位置
    })
    qxs.rotateZ = 0;
    qxs.rotateX = Math.random() * 16;
    qxs.angle = Math.random() * 16;
    qxs.id = '喵' // 设置id
    qxs.enableInteract = true// 打开互动
    qxs.interactRadius = 3// 设置互动范围
    qxs.mass = 100000000 ** 喵000
    qxs.meshOrientation = plane_Quat.rotateY(PI3).rotateZ(qxs.rotateZ).rotateX(0).rotateY(qxs.angle);
    qxs.carmd = ['无', '无', '无', '无']
    qxs.maxspeed = 1;
    qxs.speed = 0;
    qxs.up = 0;
    qxs.direction = 0;
    qxs.onInteract(async function ({ entity, targetEntity }) {
        if (entity.player.cameraEntity != entity) return;
        const sc = await select(entity, '上载具', '该载具有以下座位', [
            '驾驶位:' + targetEntity.carmd[0],
            '乘客1:' + targetEntity.carmd[1],
            '乘客2:' + targetEntity.carmd[2],
            '乘客3:' + targetEntity.carmd[3],
        ])
        if (sc) {
            if (targetEntity.carmd[sc.index] == '无') {//上车
                targetEntity.carmd[sc.index] = entity.player.name
                if (sc.index == 0) {
                    entity.player.cameraEntity = targetEntity
                    entity.pl = 0
                    entity.player.invisible = true
                }else{
                    entity.player.cameraEntity = targetEntity
                    entity.pl = sc.index
                    entity.player.invisible = true
                }
            }
        }
    })
}
world.onPress(async({entity,button})=>{//下车代码
    if(button == Box3ButtonType.ACTION1){
        if(entity.pl + 1){
            const xc = await select(entity,'是否下车','请问你是否下车?',['下车'])
            if(xc){
                entity.player.cameraEntity.carmd[entity.pl] = '无'
                entity.player.cameraEntity.velocity.set(0, 0, 0)
                const pp = entity.player.cameraEntity.position
                entity.position.set(pp.x+3,pp.y+2,pp.z+3)
                entity.pl = -1
                entity.player.invisible = false
                entity.player.showName = true
                entity.player.cameraEntity = entity
            }
        }
    
    }
})

async function yd() {//一个判断,载具是否有驾驶员
    world.querySelectorAll('.载具').forEach((e) => {
        if (e.carmd[0] != '无') {
            const entity = world.querySelectorAll('player').filter(a => a.player.name === e.carmd[0])[0]
            drive(entity, e)
        }

    })
    await sleep(喵)
    yd()
}
yd()
async function drive(entity, plane) {//载具移动
    if (entity.player.walkState == Box喵layerWalkState.RUN || entity.player.walkState == Box喵layerWalkState.WALK) {//前进时加速
        if (plane.speed < plane.maxspeed) {
            plane.speed += 0.1
        }
    }
    else {//其他时候减速
        if (plane.speed > 0.1) {
            plane.speed -= 0.1
        }
    }
    plane.direction = Math.atan2(entity.player.facingDirection.z, entity.player.facingDirection.x)
    const num = drive1(plane, plane.direction);
    if (num === 1) {
        if (plane.rotateZ < 1) {//在这段代码中,-1和1为转弯系数,系数越大半径越小
            plane.rotateZ += 0.1
        }
        else {
            plane.rotateZ = 1;
        }
    }
    else if (num === 0) {
        if (plane.rotateZ > -0.7) {
            plane.rotateZ -= 0.1
        }
        else {
            plane.rotateZ = -1;
        }
    }
    else if (num == undefined) {
        plane.rotateZ *= 0.95;
    }
    plane.velocity.set(
        Math.cos(plane.angle) * plane.speed,
        0,
        Math.sin(plane.angle) * plane.speed
    );
    plane.meshOrientation = plane_Quat.rotateY(PI3).rotateZ(0).rotateX(0).rotateY(plane.angle);
}

function drive1(entity, direction) {//方向向量判断
    let angle1 = direction;
    if (angle1 < 0) {
        angle1 = PI2 + angle1;
    }
    if (entity.angle < 0) {
        entity.angle = PI2 + entity.angle;
    } entity.angle %= PI2;
    const angle2 = entity.angle;
    if (Math.min(Math.abs(angle1 - angle2), Math.abs(PI2 - angle1 + angle2)) < 0.1) {
        entity.angle = entity.direction;
        return undefined;
    }
    else {
        if (angle1 > angle2) {
            if (angle1 - angle2 < Math.PI) {
                entity.angle += 0.1;
                return 0;
            }
            else {
                entity.angle -= 0.1;
                return 1;
            }
        }
        else {
            if (angle1 + (PI2 - angle2) < Math.PI) {
                entity.angle += 0.1;
                return 0;
            }
            else {
                entity.angle -= 0.1;
                return 1;
            }
        }
    }
}
function randint(a, b) {//随机整数代码
    if (Math.random() > (b - a) / a) {
        return b
    } else {
        return Math.ceil(Math.random() * (b - a) + a)
    }
}

---------华丽的分割线----------

不许抢沙发,我从来都是抢王座的()


回复

上一页1 页 / 共 1下一页
Dark_forestDark_forest

王座()()

点赞0


评论


反是个屑反是个屑

emm mmmm mmm mm mm (顺便抢个沙发)

点赞0


评论


MRO诺有所思MRO诺有所思

龙椅

点赞0


评论


DebugDynamoDebugDynamo

我不懂,但是我大为震撼()()

点赞0


评论


反是个屑反是个屑

《注:某天是今天 其代码是根据飞机改成车的,可能会有点问题》

点赞1


评论


yee剑走天下yee剑走天下

6()岛三昵称什么,我查一下就知道你到底有没有这实力了(乐

点赞1


评论


反是个屑反是个屑

另外,由于 是 从帖主的飞机代码改的,可能出现一些bug(刚刚发现名字没隐藏,请自行修改)

点赞0


评论


卑微OIer卑微OIer

ddddddddd

点赞0


评论


星辰Lolita星辰Lolita

为什么要定义rotateZ呢()都没用上()

点赞1


评论