猫史档案馆


【岛三教程】多人载具升级版,来了!

用户:Dark_forestDark_forest查看:14 回复:11 评论:14 创建时间:2023-01-06T15:26:53


上次很多评论说写的太屑了(狗头)

那这一次,就给大家来一个优化后的升级版!

上代码!

首先我们新建一个文件,命名成:载具.js

接着里面写上这一串

const car = [
    {
        name: '4座跑车',//名字和id
        mesh: 'FCX New Era 2 super',//模型
        scale: [0.0325, 0.0325, 0.0325],//模型大小
        seat_num: 4,//座位数(包括驾驶)
        quat: new Box3Quaternion(0, 1, 0, 1),//旋转角度
        hp: 350,//载具血量
        hurt: 50,//全速时撞人的伤害
        sensitive: 0.1,//全速时的灵敏度(越大越灵敏)
        speed: 1,//最高速度(1为中等)
        quicken: 0.08,//加/减速度
    },
]
module.exports = {
    car,
}

这里想要什么车自己加哈,我给个示例)

接着在你想写载具的代码的地方写上这一段

const Cars = require('./载具参数.js').car;//读取载具参数
world.onPlayerJoin(({ entity }) => {//数据初始化
    entity.pl = -1
})
world.onEntityContact(({ entity, other }) => {
    if (entity.hasTag('载具')) {
        if (entity.carmd[0] != '无') {
            if (entity.speed > 0) {//加上判断的原因是为了避免bug出现
                other.hurt(entity.hu * (entity.speed / entity.maxspeed), {//载具撞人
                    attacker: entity,
                    damageType: '被车喵'
                })
            }
        }
    }
})
async function seat_set(entity, m) {//初始化座位
    for (let i = 1; i < Cars[m].seat_num; i++) {
        entity.carmd.push('无')
    }
}
async function seat_count(entity) {//判断车上是否还有人
    let a = false;
    for (let i = 0; i < entity.seat; i++) {
        if (entity.carmd[i] != '无') a = true;
    }
    return a;
}
const PI2 = Math.PI * 2;
const PI3 = Math.PI * 0.5
car_set()//调用生成载具的参数
async function car_set() {
    for (let i = 0; i < 10; i++) {//个数根据自己的需求而定
        const m = randint(0, Cars.length) - 1;//随机数,为的是生成不同的车
        const qxs = world.createEntity({
            mesh: `mesh/${Cars[m].mesh}.vb`,
            meshScale: Cars[m].scale,
            collides: true,// 碰撞
            fixed: false,// 固定
            gravity: true,// 重力
            position: [randint(10, 246), randint(30, 50), randint(10, 246)],// 位置根据需求而定
        })
        qxs.id = Cars[m].name // 设置id
        qxs.enableInteract = true// 打开互动
        qxs.interactRadius = 3// 设置互动范围
        qxs.mass = 100000000 ** 喵000//重量(调成这么重是为了不被玩家撞飞)
        qxs.angle = Math.random() * 16;//生成时面向随机方向
        qxs.quat = Cars[m].quat;//旋转角调用
        qxs.meshOrientation = qxs.quat.rotateY(PI3).rotateZ(0).rotateX(0).rotateY(qxs.angle);//设置随机旋转角
        qxs.carmd = ['无'];//初始化座位参数(这个无是驾驶位,避免后面的bug发生)
        qxs.maxspeed = Cars[m].speed;//定义最大速度
        qxs.speed = 0;//初始化速度
        qxs.seat = Cars[m].seat_num;//初始化座位数
        qxs.sensitive = Cars[m].sensitive;//初始化灵敏度
        qxs.hp = qxs.maxHp = Cars[m].hp//初始化血量
        qxs.hu = Cars[m].hurt;//定义伤害
        qxs.quicken = Cars[m].quicken;//定义加速度
        await seat_set(qxs, m);//初始化座位
        qxs.addTag('载具');//命名标签
        qxs.onInteract(async function ({ entity, targetEntity }) {//当互动时
            if (entity.player.cameraEntity != entity) return;//如果玩家在车上,则跳过
            options = ['驾驶位:' + targetEntity.carmd[0]];//初始化选项
            for (let i = 1; i < targetEntity.seat; i++) {//按照座位数增加乘客选项
                options.push('乘客' + i + ':' + targetEntity.carmd[i]);
            }
            const sc = await select(entity, '上载具', '该载具有以下座位', options)//询问
            if (sc) {//如果玩家有选择
                if (targetEntity.carmd[sc.index] == '无') {//判断此位置是否有人,如果没人,则触发
                    targetEntity.enableDamage = true;//打开伤害(不在一开始打开是避免空车出bug)
                    targetEntity.carmd[sc.index] = entity.player.name//位置赋值
                    entity.player.cameraEntity = targetEntity//更改视角
                    entity.pl = sc.index//将自己的位置参数设置为车上的位置
                    entity.player.invisible = true//关闭显示
                    entity.position.set(86, 4, 125)//移到密室
                }
                else {//否则
                    entity.player.directMessage('此座位已经有人了哦')//说此座位已有人
                }
            }
        })
        qxs.onDie(async ({ entity, attacker }) => {//车被击杀
            const a = attacker;//设置常量a等于击杀车的人
            for (let i = 0; i < entity.seat; i++) {//判断车上的人
                if (entity.carmd[i] != '无') {//如果此位置有人
                    const e = world.querySelectorAll('player').filter(a => a.player.name === entity.carmd[i])[0]//判断玩家
                    const pp = entity.position//设置位置
                    e.position.set(pp.x + 3, pp.y + 2, pp.z + 3);//玩家下车
                    e.pl = -1//玩家的车上位置参数设置为-1
                    e.player.invisible = false//玩家显示
                    e.player.showName = true//名字显示
                    e.player.cameraEntity = e//视角回归
                    e.hurt(e.hp, {//玩家喵(伤害者是击杀车的人)
                        attacker: a,
                        damageType: '载具爆炸'
                    })
                }
            }
            entity.destroy();//删除车辆
        })
    }
}
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] = '无'//车上的玩家位置设置成无
                if (!entity.pl) {//如果是驾驶员
                    entity.player.cameraEntity.velocity.set(0, 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//视角回归
                entity.player.cameraEntity.enableDamage = !seat_count(entity.player.cameraEntity);//如果车上没人,则车关掉受伤害(避免bug发生)
            }
        }

    }
})

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)//驾驶员开车
        }
        if (e.position.y <= -10) {//如果车的y坐标小于-10
            e.hurt(e.hp, {//车喵
                attacker: 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 += plane.quicken//增加加速度
        }
    }
    else {//否则
        if (plane.speed >= plane.quicken) {//如果车的速度大于加速度(不是0为避免bug发生)
            plane.speed -= plane.quicken//减一个加速度的值
        }
    }
    if (plane.speed < plane.quicken) {//如果即将发生bug
        plane.speed = 0;//将车速设置成0,避免bug发生
    }
    plane.direction = Math.atan2(entity.player.facingDirection.z, entity.player.facingDirection.x)//设置车的朝向参数(主意,不是朝向)
    drive1(plane, plane.direction);//判断方向向量
    plane.velocity.set(//速度设置
        Math.cos(plane.angle) * plane.speed,//三角学函数cos向量乘速度
        0,
        Math.sin(plane.angle) * plane.speed//三角学函数sin向量乘速度
    );
    plane.meshOrientation = plane.quat.rotateY(PI3).rotateZ(0).rotateX(0).rotateY(plane.angle);//设置朝向
}

function drive1(entity, direction) {//方向向量判断
    let angle1 = direction;//设置angle1为朝向参数
    if (angle1 < 0) {//如果小于0
        angle1 = PI2 + angle1;//那么就增加一个PI2
    }
    if (entity.angle < 0) {//如果车的朝向小于0
        entity.angle = PI2 + entity.angle;//车的朝向增加PI2
    }
    entity.angle %= PI2;//车的朝向模一个PI2
    const angle2 = entity.angle;//设置angle2为车目前的朝向
    if (Math.min(Math.abs(angle1 - angle2), Math.abs(PI2 - angle1 + angle2)) < 0.1) {//一个判断,如果玩家和车的朝向一样
        entity.angle = entity.direction;//那么车的朝向设置成车的朝向
    }
    else {//否则
        if (angle1 > angle2) {//如果angle1大于2
            if (angle1 - angle2 < Math.PI) {//如果差小于Math.PI
                entity.angle += entity.sensitive * entity.speed;//那么就将方向增加灵敏度乘速度
            }
            else {
                entity.angle -= entity.sensitive * entity.speed;//将方向向量减少灵敏度乘速度
            }
        }
        else {
            if (angle1 + (PI2 - angle2) < Math.PI) {//如果angle1加上PI2-angle2的差小于Math.PI
                entity.angle += entity.sensitive * entity.speed;//方向向量增加灵敏度乘速度
            }
            else {
                entity.angle -= entity.sensitive * entity.speed;//减少灵敏度乘速度
            }
        }
    }
}
function randint(a, b) {//随机整数代码
    if (Math.random() > (b - a) / a) {
        return b
    } else {
        return Math.ceil(Math.random() * (b - a) + a)
    }
}

帖主讲的这么详细了,还不快动动手指,抢个沙发(划掉)()

对了,我是SYH.黑暗森林啊()

——————————————————————华丽的分割线————————————————————————


回复

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

沙发()()

点赞0


评论


反是个屑反是个屑

我挤一挤楼下的沙发()

点赞0


评论


Dark_forestDark_forest

顶()

点赞0


评论


馫龘飝鱻灥麤靐飍朤馫譶龘飝虪齺馫龘飝鱻灥麤靐飍朤馫譶龘飝虪齺

 

const Cars = require('./载具参数.js').car;//读取载具参数
world.onPlayerJoin(({ entity }) => {//数据初始化
    entity.pl = -1
})
world.onEntityContact(({ entity, other }) => {
    if (entity.hasTag('载具')) {
        if (entity.carmd[0] != '无') {
            if (entity.speed > 0) {//加上判断的原因是为了避免bug出现
                other.hurt(entity.hu * (entity.speed / entity.maxspeed), {//载具撞人
                    attacker: entity,
                    damageType: '被车喵'
                })
            }
        }
    }
})
async function seat_set(entity, m) {//初始化座位
    for (let i = 1; i < Cars[m].seat_num; i++) {
        entity.carmd.push('无')
    }
}
async function seat_count(entity) {//判断车上是否还有人
    let a = false;
    for (let i = 0; i < entity.seat; i++) {
        if (entity.carmd[i] != '无') a = true;
    }
    return a;
}
const PI2 = Math.PI * 2;
const PI3 = Math.PI * 0.5
car_set()//调用生成载具的参数
async function car_set() {
    for (let i = 0; i < 10; i++) {//个数根据自己的需求而定
        const m = randint(0, Cars.length) - 1;//随机数,为的是生成不同的车
        const qxs = world.createEntity({
            mesh: `mesh/${Cars[m].mesh}.vb`,
            meshScale: Cars[m].scale,
            collides: true,// 碰撞
            fixed: false,// 固定
            gravity: true,// 重力
            position: [randint(10, 246), randint(30, 50), randint(10, 246)],// 位置根据需求而定
        })
        qxs.id = Cars[m].name // 设置id
        qxs.enableInteract = true// 打开互动
        qxs.interactRadius = 3// 设置互动范围
        qxs.mass = 100000000 ** 喵000//重量(调成这么重是为了不被玩家撞飞)
        qxs.angle = Math.random() * 16;//生成时面向随机方向
        qxs.quat = Cars[m].quat;//旋转角调用
        qxs.meshOrientation = qxs.quat.rotateY(PI3).rotateZ(0).rotateX(0).rotateY(qxs.angle);//设置随机旋转角
        qxs.carmd = ['无'];//初始化座位参数(这个无是驾驶位,避免后面的bug发生)
        qxs.maxspeed = Cars[m].speed;//定义最大速度
        qxs.speed = 0;//初始化速度
        qxs.seat = Cars[m].seat_num;//初始化座位数
        qxs.sensitive = Cars[m].sensitive;//初始化灵敏度
        qxs.hp = qxs.maxHp = Cars[m].hp//初始化血量
        qxs.hu = Cars[m].hurt;//定义伤害
        qxs.quicken = Cars[m].quicken;//定义加速度
        await seat_set(qxs, m);//初始化座位
        qxs.addTag('载具');//命名标签
        qxs.onInteract(async function ({ entity, targetEntity }) {//当互动时
            if (entity.player.cameraEntity != entity) return;//如果玩家在车上,则跳过
            options = ['驾驶位:' + targetEntity.carmd[0]];//初始化选项
            for (let i = 1; i < targetEntity.seat; i++) {//按照座位数增加乘客选项
                options.push('乘客' + i + ':' + targetEntity.carmd[i]);
            }
            const sc = await select(entity, '上载具', '该载具有以下座位', options)//询问
            if (sc) {//如果玩家有选择
                if (targetEntity.carmd[sc.index] == '无') {//判断此位置是否有人,如果没人,则触发
                    targetEntity.enableDamage = true;//打开伤害(不在一开始打开是避免空车出bug)
                    targetEntity.carmd[sc.index] = entity.player.name//位置赋值
                    entity.player.cameraEntity = targetEntity//更改视角
                    entity.pl = sc.index//将自己的位置参数设置为车上的位置
                    entity.player.invisible = true//关闭显示
                    entity.position.set(86, 4, 125)//移到密室
                }
                else {//否则
                    entity.player.directMessage('此座位已经有人了哦')//说此座位已有人
                }
            }
        })
        qxs.onDie(async ({ entity, attacker }) => {//车被击杀
            const a = attacker;//设置常量a等于击杀车的人
            for (let i = 0; i < entity.seat; i++) {//判断车上的人
                if (entity.carmd[i] != '无') {//如果此位置有人
                    const e = world.querySelectorAll('player').filter(a => a.player.name === entity.carmd[i])[0]//判断玩家
                    const pp = entity.position//设置位置
                    e.position.set(pp.x + 3, pp.y + 2, pp.z + 3);//玩家下车
                    e.pl = -1//玩家的车上位置参数设置为-1
                    e.player.invisible = false//玩家显示
                    e.player.showName = true//名字显示
                    e.player.cameraEntity = e//视角回归
                    e.hurt(e.hp, {//玩家喵(伤害者是击杀车的人)
                        attacker: a,
                        damageType: '载具爆炸'
                    })
                }
            }
            entity.destroy();//删除车辆
        })
    }
}
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] = '无'//车上的玩家位置设置成无
                if (!entity.pl) {//如果是驾驶员
                    entity.player.cameraEntity.velocity.set(0, 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//视角回归
                entity.player.cameraEntity.enableDamage = !seat_count(entity.player.cameraEntity);//如果车上没人,则车关掉受伤害(避免bug发生)
            }
        }

    }
})

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)//驾驶员开车
        }
        if (e.position.y <= -10) {//如果车的y坐标小于-10
            e.hurt(e.hp, {//车喵
                attacker: 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 += plane.quicken//增加加速度
        }
    }
    else {//否则
        if (plane.speed >= plane.quicken) {//如果车的速度大于加速度(不是0为避免bug发生)
            plane.speed -= plane.quicken//减一个加速度的值
        }
    }
    if (plane.speed < plane.quicken) {//如果即将发生bug
        plane.speed = 0;//将车速设置成0,避免bug发生
    }
    plane.direction = Math.atan2(entity.player.facingDirection.z, entity.player.facingDirection.x)//设置车的朝向参数(主意,不是朝向)
    drive1(plane, plane.direction);//判断方向向量
    plane.velocity.set(//速度设置
        Math.cos(plane.angle) * plane.speed,//三角学函数cos向量乘速度
        0,
        Math.sin(plane.angle) * plane.speed//三角学函数sin向量乘速度
    );
    plane.meshOrientation = plane.quat.rotateY(PI3).rotateZ(0).rotateX(0).rotateY(plane.angle);//设置朝向
}

function drive1(entity, direction) {//方向向量判断
    let angle1 = direction;//设置angle1为朝向参数
    if (angle1 < 0) {//如果小于0
        angle1 = PI2 + angle1;//那么就增加一个PI2
    }
    if (entity.angle < 0) {//如果车的朝向小于0
        entity.angle = PI2 + entity.angle;//车的朝向增加PI2
    }
    entity.angle %= PI2;//车的朝向模一个PI2
    const angle2 = entity.angle;//设置angle2为车目前的朝向
    if (Math.min(Math.abs(angle1 - angle2), Math.abs(PI2 - angle1 + angle2)) < 0.1) {//一个判断,如果玩家和车的朝向一样
        entity.angle = entity.direction;//那么车的朝向设置成车的朝向
    }
    else {//否则
        if (angle1 > angle2) {//如果angle1大于2
            if (angle1 - angle2 < Math.PI) {//如果差小于Math.PI
                entity.angle += entity.sensitive * entity.speed;//那么就将方向增加灵敏度乘速度
            }
            else {
                entity.angle -= entity.sensitive * entity.speed;//将方向向量减少灵敏度乘速度
            }
        }
        else {
            if (angle1 + (PI2 - angle2) < Math.PI) {//如果angle1加上PI2-angle2的差小于Math.PI
                entity.angle += entity.sensitive * entity.speed;//方向向量增加灵敏度乘速度
            }
            else {
                entity.angle -= entity.sensitive * entity.speed;//减少灵敏度乘速度
            }
        }
    }
}
function randint(a, b) {//随机整数代码
    if (Math.random() > (b - a) / a) {
        return b
    } else {
        return Math.ceil(Math.random() * (b - a) + a)
    }
}

点赞0


评论


DebugDynamoDebugDynamo

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

点赞0


评论


Dark_forestDark_forest

 

本代码由本人亲自研发,有什么教程需求可以在这个帖下面回复:教程:如何XXX……

本人会挑选一些力所能及且有意义的教程发帖

感谢大家的支持

点赞1


评论


烟魂烟魂

dedede

点赞0


评论


yee剑走天下yee剑走天下

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

点赞0


评论


yee剑走天下yee剑走天下

如何用屑oop来实现只要输入参数就可以自动生成sql表格且能保存玩家数据一样的的class类(

点赞0


评论


SCS_user_i1eril0dUySCS_user_i1eril0dUy

ddd

点赞0


评论


宁静倒数28天宁静倒数28天

感觉这种帖子不靠谱,不会的还是不会,可能连调参数的不懂,不过作为学习教材还是可以作为试卷题目出的emotion_编程猫_冷漠

点赞0


评论