用户:
神岛吉吉喵查看:40 回复:55 评论:40 创建时间:2021-03-24T19:22:03

大神在线写代码第二期圆满结束!
这期所有的代码如下
Q1:我想让一个粒子朝玩家面向的方向发射怎么办?
async function loop(entity, fn) {
while(!entity.destroyed){ //不断循环直到实体不在场景里
fn()
await sleep(60) //每60毫秒执行一次fn, 相当于一秒内能执行16.666次
}
}
const RED = new Box3RGBColor(10, 2, 2) //发光的红色
world.onPlayerJoin(({ entity }) => {
entity.particleRate = 80 //每秒发出80个粒子
entity.particleLifetime = 2 //每个粒子存活2秒
entity.particleSize = [2, 2, 2, 2, 2] //粒子在5个阶段的大小都是2
entity.particleColor = [RED, RED, RED, RED, RED] //粒子在5个阶段的颜色都是红色
loop(entity, () => { //不断更新玩家身上粒子的朝向
entity.particleVelocity = entity.player.facingDirection.scale(50) //粒子朝玩家面向的方向50倍速移动
})
})
Q2: 我想要实现子弹发射效果,以及飞刀碰到实体减血量的效果
// Q1: 我想要实现子弹发射效果,需要全部的代码
// Q2: 我想要飞刀碰到实体减血量的效果
//注意, mesh/刀.vb的模型需要在模型编辑器里将它旋转到刀头指向X轴的正方向
const MeshScale = [1 / 16, 1 / 16, 1 / 16] //正常的模型缩放比例
const Quat = new Box3Quaternion(0, 0, 0, 1) //默认的旋转状态
world.onPress(async ({ entity, button }) => {
if (button === Box3ButtonType.ACTION0) {
const d = entity.player.facingDirection //玩家的朝向
const knife = world.createEntity({
mesh: 'mesh/刀.vb',
meshScale: MeshScale,
collides: true, // 开启碰撞
gravity: false, // 不受重力影响
position: entity.position, // 在玩家的位置生成飞刀
velocity: d.scale(2), // 飞刀的初速度是玩家朝向向量的2倍
meshOrientation: Quat.rotateY(Math.atan2(d.z, d.x)), // 刀头指向玩家的朝向
})
knife.id = entity.player.userKey //将小刀的id设置成玩家userKey, 便于碰撞过滤器识别
knife.onEntityContact(({ other }) => {//每当飞刀碰到另一个实体
other.hurt(10)//被碰实体受10点伤害
})
await sleep(2000)//等2秒后
knife.destroy()//清除飞刀, 防止飞刀数量太多超过服务器负载
}
if(button===Box3ButtonType.CROUCH){
entity.hp += 20
}
})
world.onPlayerJoin(({ entity }) => { //当玩家进入地图时
if(entity.player.name==='搬砖喵'||entity.player.name==='吉吉喵'){
entity.addTag('ghost') //本喵不受攻击
}
entity.enableDamage = true //允许这个玩家受伤
entity.id = entity.player.userKey//把userKey作为玩家的id
world.addCollisionFilter('#' + entity.id, '#' + entity.id)//让id跟玩家id一样的实体不会相互碰撞, 比如玩家的飞刀不会碰到玩家, 自己的飞刀之间也不会碰撞
})
world.addCollisionFilter('.ghost', '*')
world.onPlayerLeave(({ entity }) => { //当玩家离开地图时
world.removeCollisionFilter('#' + entity.id, '#' + entity.id)//取消这个玩家的碰撞过滤器
})
world.onDie(async ({ entity }) => {
await sleep(3000)
entity.hp = entity.maxHp //复活
})
Q3: 我需要碰到lime_juice方块变成绿色,并减去一半的血的效果,需要伤害代码
world.onPlayerJoin(({ entity }) => {
entity.enableDamage = true //允许这个玩家受伤
entity.onFluidEnter(({ voxel }) => { //每当玩家进入液体方块
if (voxel === voxels.id('lime_juice')) { //如果液体方块类型是lime_juice
entity.player.color.set(0, 1, 0) //玩家颜色设成绿色
entity.hurt(entity.maxHp * 0.5) //减去一半的生命
}
})
})
Q4: 我想要实现飞机在空中转弯和飞行效果,需要这部分代码
//注意, 飞机的模型需要在模型编辑器里将它旋转到机头指向X轴的正方向
async function loop(entity, fn) {
while (!entity.destroyed) { //不断循环直到实体不在场景里
fn()
await sleep(60) //每60毫秒执行一次fn, 相当于一秒内能执行16.666次
}
}
const Quat = new Box3Quaternion(0, 0, 0, 1) //默认的旋转状态
function flyAround(plane) {
plane.gravity = false //取消重力对飞机的影响
let speed = 0.1 //飞机的前进速度
let rotation = 0 //飞机的旋转弧度
let rotSpeed = 0.1 //飞机当前的旋转速度
loop(plane, () => {
const sdx = Math.cos(rotation) * speed //用余弦计算x轴上的速度
const vz = Math.sin(rotation) * speed //用正弦计算z轴上的速度
plane.velocity.set(sdx, 0, vz) //更新飞机当前的速度
plane.meshOrientation = Quat.rotateY(rotation) //按当前旋转弧度更新飞机的朝向
if (rotation > Math.PI || rotation < -Math.PI) {//当飞机旋转到达完全相反的方向(180度)
rotSpeed *= -1 //飞机改变旋转方向
}
rotation += rotSpeed //更新飞机的旋转弧度
})
}
const plane = world.querySelector('#飞机-1') //获取场景中的飞机实体
flyAround(plane) //让飞机动起来
Q5:粒子攻击特效(Q2的改良版)
const MeshScale = [1 / 16, 1 / 16, 1 / 16] //正常的模型缩放比例
const Quat = new Box3Quaternion(0, 0, 0, 1) //默认的旋转状态
const RED = new Box3RGBColor(10, 2, 2) //发光的红色
world.onPress(async ({ entity, button }) => {
if (button === Box3ButtonType.ACTION0) {
const d = entity.player.facingDirection //玩家的朝向
const knife = world.createEntity({
meshScale: MeshScale,
collides: true, // 开启碰撞
gravity: false, // 不受重力影响
position: entity.position, // 在玩家的位置生成飞刀
velocity: d.scale(2), // 飞刀的初速度是玩家朝向向量的2倍
meshOrientation: Quat.rotateY(Math.atan2(d.z, d.x)), // 刀头指向玩家的朝向
particleRate: 20, //每秒发出80个粒子
particleLifetime: 2, //每个粒子存活2秒
particleSize: [2, 2, 2, 2, 2], //粒子在5个阶段的大小都是2
particleColor: [RED, RED, RED, RED, RED], //粒子在5个阶段的颜色都是红色
// particleTarget: entity,
particleVelocity: d.scale(-2),
bounds: [0.2, 0.2, 0.2],// 子弹实体0.2个方块大小
})
knife.id = entity.player.userKey //将小刀的id设置成玩家userKey, 便于碰撞过滤器识别
knife.onEntityContact(({ other }) => {//每当飞刀碰到另一个实体
other.hurt(10)//被碰实体受10点伤害
})
await sleep(2000)//等2秒后
knife.destroy()//清除飞刀, 防止飞刀数量太多超过服务器负载
}
})
world.onPlayerJoin(({ entity }) => { //当玩家进入地图时
if (entity.player.name === '搬砖喵' || entity.player.name === '吉吉喵') {
entity.addTag('ghost') //本喵不受攻击
}
entity.enableDamage = true //允许这个玩家受伤
entity.id = entity.player.userKey//把userKey作为玩家的id
world.addCollisionFilter('#' + entity.id, '#' + entity.id)//让id跟玩家id一样的实体不会相互碰撞, 比如玩家的飞刀不会碰到玩家, 自己的飞刀之间也不会碰撞
})
world.addCollisionFilter('.ghost', '*')
world.onPlayerLeave(({ entity }) => { //当玩家离开地图时
world.removeCollisionFilter('#' + entity.id, '#' + entity.id)//取消这个玩家的碰撞过滤器
})
world.onDie(async ({ entity }) => {
await sleep(3000)
entity.hp = entity.maxHp //复活
})
开发API文档:https://docs.box3.codemao.cn/
加入代码岛3.0官方内测Q喵
下面的二维码会扫出来什么呢?( =•ω•= )m
