
Lv.1
In this world... It's KILL or BE killed!
签名:https://shequ.codemao.cn/community/330512 请看此贴,我已经回归了! https://classic.minecraft.net/ 代码岛100.0网址
在 【代码岛3.0】忽然曝光的年兽随机移动代码 中回复
//改编代码:僵尸围城,此代码改编部分原创,转载请注上原作者名字鬼知道,盗版必究
const lions = world.querySelectorAll('#僵尸');
lions[0].collides = true; // 开启碰撞
lions[0].gravity = true; // 开启重力
lions[0].friction = 0; // 关闭摩擦力
lions[0].enableDamage = true;
lions[0].maxHp = 50;
lions[0].hp = 50;
lions[0].a喵Tag('僵尸')
// 创建一堆相同的实体并push到lions中
function jiangshiweicheng(shuliang){
for (let i = 0; i < shuliang; i++) {
const e = world.createEntity(lions[0]);
e.position.y += Math.random() * 50
e.position.x = Math.random() * 128
e.position.z = Math.random() * 128
lions.push(e);
}
}
jiangshiweicheng(100)
// onTick中处理实体的运动
world.onTick(() => {
// 处理每个实体
lions.forEach((e) => {
// 计算X和Z轴方向的速度的和
const sdxz = Math.abs(e.velocity.x) + Math.abs(e.velocity.z);
if (sdxz > 0.1) {
// sdxz大于一, 判定为运动中
// 计算当前方向
const angle = Math.floor(Math.atan2(e.velocity.z, e.velocity.x) / (2. * Math.PI) * 256) * 2. * Math.PI / 256
// 设置朝向, 如果朝向不对, 可自行在后面添加相应的rotateX/rotateY/rotateZ
const orientation = new Box3Quaternion(0, 0, 0, 1).rotateY(angle).rotateY(Math.PI);
e.meshOrientation.copy(orientation);
} else {
// 随机角度
const randTheta = Math.random() * Math.PI * 2;
// 随机速度大小
const mag = Math.random() + Math.random();
// 设置速度
e.velocity.x = mag * Math.sin(randTheta);
e.velocity.z = mag * Math.cos(randTheta);
// 跳一下
if (e.velocity.y < 1) {
e.velocity.y += 0.5;
}
}
});
});
//原本代码
world.querySelectorAll('.僵尸').forEach((e)=>{
e.onEntityContact(async({entity,other})=>{//当玩家和僵尸碰撞时
if(!other.isPlayer || 10>=other.hp/*此处侦测玩家血量,如果小于11则不攻击,可以删*/){
return;
}
other.hurt(20);//伤害玩家20血
other.player.directMessage('你被僵尸打了一下,hp-20,当前hp:'+other.hp)//私聊玩家
e.destroy()//僵尸喵
})
e.onClick(async({entity,clicker})=>{//当僵尸被点击时
e.destroy()//僵尸喵亡
clicker.player.directMessage('你杀了一只僵尸')//私聊玩家提示
})
if(0>=e.position.y){
e.destroy()//防掉虚空
}
})
//增加代码
2020-08-06T13:18:14 点赞:2