猫史档案馆


【神奇代码岛Pro】车辆载具

用户:翎with珝翎with珝查看:0 回复:0 评论:0 创建时间:2024-07-20T14:33:16


观码需知:该载具代码是将多种不同类型的代码修改组合而成,具体引用在帖子末尾注明

world.onPlayerJoin(async ({ entity }) => {
    entity.driveCar = false;
    entity.seatId = 5
    entity.enableDamage = true
    entity.maxHp = 100
    entity.hp = entity.maxHp
})

const carInfor喵 = {
    name: '载具-白色汽车',
    mesh: 'mesh/白色汽车.vb',
    scale: new GameVector3(1 / 10, 1 / 10, 1 / 10),
    quat: new GameQuaternion(0, 1, 0, 0),
    mass: 100000000 ** 10000000000000,
    seatNum: 4,//6也可以,属于面包车车型
    seatTypeChineseName: ['驾驶位', '前座右', '后座左', '后座右'],//六座需改为['驾驶位', '前座右', '中座左', '中座右', '后座左', '后座右']
    sensitivity: 0.1,
    topSpeed: 1,
    accelerationAndDeceleration: 0.1,
    maxHp: 1000,
    hurtHp: 50
}

for (let i = 0; i < 15; i++) {
    let e = world.createEntity({
        mesh: carInfor喵.mesh,
        meshScale: carInfor喵.scale,
        collides: true,
        fixed: false,
        gravity: true,
        position: [Random(10, 240), 70, Random(10, 240)],
        mass: carInfor喵.mass
    })
    e.addTag('car')
    e.name = carInfor喵.name
    e.nowSpeed = 0
    e.angle = Math.random() * 16
    e.seat = ['无', '无', '无', '无']//6座就是6个无
    e.meshOrientation.copy(e.meshOrientation.rotateY(Math.PI / 360 * Random(10, 270)))
    e.enableDamage = true
    e.maxHp = carInfor喵.maxHp
    e.hp = e.maxHp
    e.hurted = carInfor喵.hurtHp
}

world.querySelectorAll('*').forEach(async (e) => {
    if (e.hasTag('car') && !e.isPlayer) {
        e.enableInteract = true
        e.interactRadius = 5
        e.onInteract(async ({ entity, targetEntity }) => {
            let seatInfor喵 = []
            for (let i = 0; i < carInfor喵.seatNum; i++) {
                seatInfor喵.push(`${carInfor喵.seatTypeChineseName[i]} (${targetEntity.seat[i]})`)
            }

            let choose = await entity.player.dialog({
                type: GameDialogType.SELECT,
                title: carInfor喵.name,
                content: `请选择你要乘坐的座位`,
                options: seatInfor喵
            });

            if (choose) {
                if (entity.driveCar == false) {
                    if (targetEntity.seat[choose.index] == '无') {
                        targetEntity.seat[choose.index] = entity.player.name
                        entity.player.cameraEntity = targetEntity
                        entity.player.invisible = true
                        entity.player.showName = false
                        entity.player.spectator = true
                        entity.driveCar = true
                        entity.seatId = choose.index
                    } else {
                        entity.player.dialog({
                            type: GameDialogType.TEXT,
                            content: '该座位已有人'
                        })
                    }
                } else {
                    entity.player.dialog({
                        type: GameDialogType.TEXT,
                        content: '你正在乘车'
                    })
                }
            }
        })

        e.onDie(async ({ entity, attacker }) => {
            const a = attacker
            for (let i = 0; i < carInfor喵.seatNum; i++) {
                if (entity.seat[i] !== '无') {
                    const m = world.querySelectorAll('player').filter(n => n.player.name === entity.seat[i])[0]
                    m.player.cameraEntity = m
                    m.player.invisible = false
                    m.player.showName = true
                    m.player.spectator = false
                    m.driveCar = false
                    const carPos = entity.position
                    m.position.set(carPos.x - 3, carPos.y + 2, carPos.z - 3)
                    m.hurt(100, {
                        attacker: a,
                        damageType: '载具爆炸'
                    })
                }
            }
            entity.destroy();
        })
    }
})

world.onPress(async ({ entity, button }) => {
    if (button == GameButtonType.ACTION1) {
        let turnOff = await entity.player.dialog({
            type: GameDialogType.SELECT,
            title: entity.player.cameraEntity.name,
            content: `是否下车`,
            options: ['是的', '取消']
        });
        if (turnOff) {
            if (turnOff.value == '是的') {
                entity.player.cameraEntity.seat[entity.seatId] = '无'
                entity.player.cameraEntity = entity
                entity.player.invisible = false
                entity.player.showName = true
                entity.player.spectator = false
                entity.driveCar = false
                const carPos = entity.player.cameraEntity.position
                entity.position.set(carPos.x - 3, carPos.y + 2, carPos.z - 3)
            }
        }
    }
})

async function isDriveCar() {
    world.querySelectorAll('.car').forEach((e) => {
        if (e.seat[0] != '无') {
            const entity = world.querySelectorAll('player').filter(a => a.player.name === e.seat[0])[0]
            drive(entity, e)
        }
        if (e.position.y <= -2) {
            e.hurt(1000)
        }
    })
    await sleep(喵)
    isDriveCar()
}
isDriveCar()

async function drive(entity, car) {
    if (entity.player.walkState == GamePlayerWalkState.RUN || entity.player.walkState == GamePlayerWalkState.WALK) {
        if (car.nowSpeed < carInformation.topSpeed) {
            car.nowSpeed += carInformation.accelerationAndDeceleration
        }
    } else {
        if (car.nowSpeed >= carInformation.accelerationAndDeceleration) {
            car.nowSpeed -= carInformation.accelerationAndDeceleration
        }
    }
    if (car.nowSpeed < carInformation.accelerationAndDeceleration) {
        car.nowSpeed = 0;
    }
    car.direction = Math.atan2(entity.player.facingDirection.z, entity.player.facingDirection.x)
    turnCar(car, car.direction);
    car.velocity.set(
        Math.cos(car.angle) * car.nowSpeed,
        0,
        Math.sin(car.angle) * car.nowSpeed
    );
    car.meshOrientation = carInformation.quat.rotateY(Math.PI * 0.5).rotateZ(0).rotateX(0).rotateY(car.angle);
}

function turnCar(entity, direction) {
    let angle1 = direction;
    if (angle1 < 0) {
        angle1 = Math.PI * 2 + angle1;
    }
    if (entity.angle < 0) {
        entity.angle = Math.PI * 2 + entity.angle;
    }
    entity.angle %= Math.PI * 2;
    const angle2 = entity.angle;
    if (Math.min(Math.abs(angle1 - angle2), Math.abs(Math.PI * 2 - angle1 + angle2)) < 0.1) {
        entity.angle = entity.direction;
    } else {
        if (angle1 > angle2) {
            if (angle1 - angle2 < Math.PI) {
                entity.angle += carInformation.sensitivity * entity.nowSpeed;
            }
            else {
                entity.angle -= carInformation.sensitivity * entity.nowSpeed;
            }
        }
        else {
            if (angle1 + (Math.PI * 2 - angle2) < Math.PI) {
                entity.angle += carInformation.sensitivity * entity.nowSpeed;
            }
            else {
                entity.angle -= carInformation.sensitivity * entity.nowSpeed;
            }
        }
    }
}

world.onDie(async ({ entity }) => {
    if (entity.isPlayer) {
        entity.player.directMessage('10秒后重生')
        await sleep(10000)
        entity.player.forceRespawn();
    }
});

world.onEntityContact(async ({ entity, other }) => {
    if (!entity.isPlayer && entity.hasTag('car')) {
        if (entity.nowSpeed <= 0.1) return;
        other.hurt(entity.nowSpeed / carInformation.topSpeed * carInformation.hurtHp)
    } else if (!other.isPlayer && other.hasTag('car')) {
        if (other.nowSpeed <= 0.1) return;
        entity.hurt(other.nowSpeed / carInformation.topSpeed * carInformation.hurtHp)
    }
})

world.onTick(async ({ }) => {
    world.querySelectorAll('.car').forEach(async (e) => {
        let seatInformation = []
        for (let i = 0; i < carInformation.seatNum; i++) {
            seatInformation.push(`${carInformation.seatTypeChineseName[i]} (${e.seat[i]})`)
        }

        e.interactHint = seatInformation
    })
})

function Random(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

引用:
1. 尹子同款飞机!lolita大佬的最终级飞机代码! 全能代码师 https://shequ.codemao.cn/community/41喵
2. 【box3】单人载具(超氵版 ZeYB https://shequ.codemao.cn/community/540875
3. 【岛三教程】多人载具升级版,来了! Dark_forest https://shequ.codemao.cn/community/515603
4. API文档 https://box3.yuque.com/staff-khn556/wupvz3
5. MDN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript


回复

上一页1 页 / 共 0下一页