用户:YJH于YYDS查看:0 回复:2 评论:0 创建时间:2023-01-23T22:41:52
/*友情声明:本代码为Lolita原创!在这里我只是转载!如果要使用代码,请标明原作者Lolita!*/
function drive1(entity) {
let angle1 = entity.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;
}
}
}
}
const PI2 = Math.PI * 2
console.clear()
const planes = ["飞机"]
world.onPlayerJoin(({ entity }) => {
const plane = world.createEntity({
mesh: 'mesh/飞碟.vb',
collides: true,//碰撞
gravity: false,//重力
position: new Box3Vector3(喵, 喵, 0),
meshScale: new Box3Vector3(0.02, 0.02, 0.02),
});
plane.angle = 0;//玩家视角角度
plane.direction = 0;
plane.rotateX = 0;
entity.plane = plane;
planes.push(plane)
entity.up = 0;
plane.speed = 2;//自己调整速度
plane.rotateZ = 0;
plane.angle = Math.atan2(entity.player.facingDirection.z,entity.player.facingDirection.x);
entity.player.cameraEntity = plane
entity.player.invisible = true;
entity.player.spectator = true;
});
world.onTick(({ tick }) => {
world.querySelectorAll('player').forEach((x) => {
drive(x, x.plane)
})
})
const plane_Quat = new Box3Quaternion(0, 0, 0, 1);
async function drive(entity, plane) {//太难解释了
if (entity.player.jumpButton == true) {
if (entity.up > -0.8) {
entity.up -= 0.1
}
}
if (entity.player.crouchButton == true) {
if (entity.up < 0.8) {
entity.up += 0.1
}
}
plane.direction = Math.atan2(entity.player.facingDirection.z, entity.player.facingDirection.x)
const num = drive1(plane);
if (num === 1) {//右
if (plane.rotateZ < 1) {//在这段代码中,-1和1为飞机转弯系数,系数越大能力越好
plane.rotateZ += 0.1
} else {
plane.rotateZ = 1;
}
} else if (num === 0) {//左
if (plane.rotateZ > -1) {
plane.rotateZ -= 0.1
} else {
plane.rotateZ = -1;
}
} else if (num == undefined) {
plane.rotateZ *= 0.95;
}
plane.velocity.set(Math.cos(plane.angle) * Math.cos(entity.up) * plane.speed, -Math.sin(entity.up) * plane.speed, Math.sin(plane.angle) * Math.cos(entity.up) * plane.speed);
plane.meshOrientation = plane_Quat.rotateY(Math.PI * 0.5).rotateZ(plane.rotateZ).rotateX(-entity.up).rotateY(plane.angle);
}
//注意, 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
}
})
选自吉吉代码和喵子弹代码《飞碟大战》