用户:yee剑走天下查看:7 回复:6 评论:7 创建时间:2022-08-12T12:43:02
大佬误入))
很水的代码))
1.森林冰火人:
index.js部分:
console.clear();
world.sunPhase = 0.9;
for (const e of world.querySelectorAll('*')) {//遍历所有实体
if (e.id.startsWith('存档点')) {//如果当前实体名左边部分刚好是"存档点", 比如 存档点-1 存档点-2...
e.collides = true //开启碰撞
e.fixed = true //固定实体不被推移
e.meshScale = e.meshScale.scale(1.3) //放大2倍
e.onEntityContact(({ other }) => { //每当存档点碰到另一个实体
if (other.isPlayer) { //另一个实体是玩家
if (e.position.x !== other.player.spawnPoint.x && e.position.z !== other.player.spawnPoint.z) { // 检测当前存档点是否玩家重生点, 避免反复在同一个存档点做多余的保存
other.player.directMessage('到达新的重生点'); // 给玩家发消息
other.goNum++;
other.player.spawnPoint.copy(e.position.add({ x: 0, y: 1, z: 0 })); // 喵家重生点坐标设置成存档点的坐标
}
}
})
}
}
world.onChat(({ entity, message }) => {
if (entity && ['一剑走天下', '羽之_HY', '可乐OωO', '一剑走天下-小号'].includes(entity.player.name) && message[0] == '$') {
try {
world.say('<~ ' + eval(message.split('$')[1]))
} catch (err) {
world.say('Error:' + err)
}
}
})
world.addCollisionFilter('player', 'player');
//require('./右键权限.js');
require('./interact.js');
const { saveData, loadData } = require('./SQL.js');
let loop = true
world.onPlayerJoin(async ({ entity }) => {
entity.player.scale = 0.75;
entity.player.cameraMode = "relative"
entity.player.cameraTarget.y = 1
entity.player.cameraPosition.set(0, 2, -8)
//设置
entity.level = 1;
entity.sj = 0;
entity.guan = false;
entity.jumpcounttime = 3;
entity.goNum = 0;
entity.keyNum = 0;
world.say(`${entity.player.name} 来到森林冰火人`);
//禁用WS
entity.player.disableInputDirection = Box3InputDirection.VERTICAL;
entity.player.swapInputDirection = true
//防止移动出错
entity.player.freezedForwardDirection = new Box3Vector3(1, 0, 0);
entity.player.jumpPower = 0.7;
//await loadData(entity);
setInterval(() => {
entity.sj++;
}, 1000);
});
//world.onPlayerLeave(async ({ entity }) => {
//await saveData(entity);
//});
world.onFluidEnter(({ entity, voxel }) => {
if (voxel == voxels.id('blueberry_juice')) {
if (entity.isPlayer) {
if (entity.hasTag('red')) {
entity.enableDamage = true
entity.hurt(100);
entity.player.forceRespawn();
}
}
} else if (voxel == voxels.id('strawberry_juice')) {
if (entity.isPlayer) {
if (entity.hasTag('blue')) {
entity.enableDamage = true
entity.hurt(100);
entity.player.forceRespawn()
}
}
} else if (voxel == voxels.id('lime_juice')) {
if (entity.isPlayer) {
entity.player.forceRespawn();
entity.player.directMessage(`这个水有剧毒哦!不管是火人还是冰人碰到都会趋势`)
}
}
});
world.onVoxelContact(async ({ entity, voxel }) => {
if (entity.isPlayer) {
if (voxel == voxels.id('purple_surface_02')) {
entity.player.forceRespawn()
entity.player.directMessage('你因失误导致碰到了地面,已为您回到重生点')
}
}
});
function joinRed(entity) {
entity.addTag('red')//加红队标签
entity.player.color.set(1, 0, 0)//变红
entity.red_ball = 0;
entity.position.set(253, 12, 253)
world.say(`${entity.player.name} 被分配到红队`)
}
function joinBlue(entity) {
entity.addTag('blue')//加蓝队标签
entity.player.color.set(0, 0, 1)//变蓝
entity.blue_ball = 0;
entity.position.set(253, 17, 253)
world.say(`${entity.player.name} 被分配到蓝队`)
}
world.onPlayerJoin(({ entity }) => {
const red = world.querySelectorAll('.red') //红队玩家列表
const blue = world.querySelectorAll('.blue') //蓝队玩家列表
if (red.length === blue.length) {//如果两队人数相同
if (Math.random() < 0.5) {//随机加入其中一队
joinBlue(entity)//加入蓝队
}
else {
joinRed(entity)//加入红队
}
}
else if (red.length > blue.length) {//如果红队人多
joinBlue(entity)//加入蓝队
}
else {
joinRed(entity)//加入红队
}
})
interact.js部分:
const { saveData } = require('./SQL.js');
async function textDialog(entity, title, content, hasArrow) {
return await entity.player.dialog({
type: 'text',
title,
content,
hasArrow,
})
}
world.querySelectorAll('#一个小片').forEach(async (mesh) => {
mesh.enableInteract = true;
mesh.interactHint = mesh.id;
mesh.interactRadius = 4;
mesh.onInteract(async ({ entity }) => {
if (entity.hasTag('red')) {
mesh.interactColor.set(1, 0, 0);
const xz = await entity.player.dialog({
type: 'select',
title: mesh.id,
content: `咦?前面过不去了?要不要我帮你传一下?`,
options: ['探索新路', '按原路走!']
})
if (xz) {
if (xz.value == '探索新路') {
entity.position.set(228, 12, 253);
entity.player.directMessage('因为您的冒险精神,你比别人快了很多')
}
if (xz.value == '按原路走!') {
entity.position.set(242.5, 13, 253)
entity.player.directMessage('已为您传送到按原路走的地方')
}
}
}
else if (entity.hasTag('blue')) {
mesh.interactColor.set(0, 0, 1);
const xz2 = await entity.player.dialog({
type: 'select',
title: mesh.id,
content: `嗨害嗨,要不要给你个奖励?`,
options: ['不要!', '要!']
})
if (xz2) {
if (xz2.value == '不要!') {
entity.position.set(228, 12, 253);
entity.player.directMessage('因为您不收利益喵,我给了你个真正的奖励!')
}
if (xz2.value == '要!') {
entity.position.set(242.5, 13, 253)
entity.player.directMessage('好吧()awa,给了你个小小的奖励()')
}
}
}
});
});
world.querySelectorAll('#蓝色球').forEach(async (mesh) => {
mesh.enableInteract = true;
mesh.interactHint = '冰球';
mesh.interactRadius = 2;
mesh.interactColor.set(0, 0, 1);
mesh.onInteract(async ({ entity }) => {
if (entity.hasTag('blue')) {
entity.blue_ball += 1;
entity.player.directMessage(`您已捡到${entity.blue_ball}个冰球`);
} else {
entity.player.directMessage('这不是火人捡的球哦!这是冰人捡的球!')
}
});
});
world.querySelectorAll('#红球').forEach(async (mesh) => {
mesh.enableInteract = true;
mesh.interactHint = '火球';
mesh.interactRadius = 2;
mesh.interactColor.set(1, 0, 0);
mesh.onInteract(async ({ entity }) => {
if (entity.hasTag('red')) {
entity.red_ball += 1;
entity.player.directMessage(`您已捡到${entity.red_ball}个火球`);
} else {
entity.player.directMessage('这不是冰人捡的球哦!这是火人捡的球!')
}
});
});
world.querySelectorAll('#碰撞').forEach(async (x) => {
x.onEntityContact(async ({ other }) => {
if (other.isPlayer) {
if (other.hasTag('blue')) {
if (other.blue_ball < 80) {
other.player.directMessage(`你捡的冰球不够哦!一共需要80个冰球,还差${80 - other.blue_ball}个球!`)
} else {
other.player.directMessage('传送ing…………')
await sleep(3000);
other.position.set(212, 14, 253)
other.blue_ball = 0;
textDialog(other, x.id, '第二关:努力闯关,通过,同时也要捡球哦!\n\n已将你的冰球数量清零', true)
}
} else {
other.player.directMessage('这是冰人的传送地哦!不是火人的!')
}
}
});
});
function tran喵ission(selector, type, position, text, ballNum, levelNum) {
let mesh = world.querySelector(selector);
if (type == 'red') {
mesh.onEntityContact(async ({ other }) => {
if (other.isPlayer) {
if (other.hasTag('red')) {
if (other.red_ball < ballNum) {
other.player.directMessage(`你捡的火球不够哦!一共需要${ballNum}个火球,还差${ballNum - other.red_ball}个球!`)
} else {
if ([2].includes(other.level) && other.keyNum == 0) {
other.player.directMessage('你还没有捡钥匙呢,快去捡钥匙吧!');
} else {
other.player.directMessage('传送ing…………');
await sleep(3000);
other.position.set(position[0], position[1], position[2]);
other.level = levelNum;
other.red_ball = 0;
other.keyNum = 0;
textDialog(other, `第${other.level}关`, text, true);
}
}
} else {
other.player.directMessage('这是火人的传送地哦!不是冰人的!');
}
}
});
}
if (type == 'blue') {
mesh.onEntityContact(async ({ other }) => {
if (other.isPlayer) {
if (other.hasTag('blue')) {
if (other.blue_ball < ballNum) {
other.player.directMessage(`你捡的冰球不够哦!一共需要${ballNum}个冰球,还差${ballNum - other.blue_ball}个球!`)
} else {
if ([2].includes(other.level) && other.keyNum == 0) {
other.player.directMessage('你还没有捡钥匙呢,快去捡钥匙吧!');
} else {
other.player.directMessage('传送ing…………');
await sleep(3000);
other.position.set(position[0], position[1], position[2]);
other.level = levelNum;
other.red_ball = 0;
other.keyNum = 0;
textDialog(other, `第${other.level}关`, text, true);
}
}
} else {
other.player.directMessage('这是冰人的传送地哦!不是火人的!', true)
}
}
});
}
}
tran喵ission('#碰撞', 'blue', [212, 14, 253], '第二关:努力闯关,通过,同时也要捡球哦!\n\n已将你的冰球数量清零', 80, 2);
tran喵ission('#火人', 'red', [212, 14, 253], '第二关:努力闯关,通过,同时也要捡球哦!,\n\n已将你的火球数量清零',80, 2);
tran喵ission('#火人-2', 'red', [172, 13, 253], '第三关:同理,继续闯关,但是这关会有多人合作完成的地方,快邀请你的小伙伴一起来玩吧!(多人合作功能暂未完成,请期待~)', 40, 3);
tran喵ission('#碰撞-2', 'blue', [172, 13, 253], '第三关', '继续捡球吧,同理,我又把你的球清零了!', 40, 3);
tran喵ission('#火人-3', 'red', [132, 13, 253], '第四关:这一关还是有上一关的多人合作功能,但是不要忘了捡球哦!(多人合作功能暂未完成,请期待~)', 20, 4);
tran喵ission('#碰撞-3', 'blue', [132, 13, 253], '第四关', '这一关还要捡球哦!并且还要闯关!', 20, 4);
tran喵ission('#碰撞-4', 'blue', [81, 19, 253], '第五关:要小心哦!一路上会有毒蛇的啊!', 10, 5);
tran喵ission('#火人-4', 'red', [33, 17, 253], '第五关:要小心哦!一路上会有毒蛇的啊!', 10, 5);
tran喵ission('#碰撞-5', 'blue', [81, 19, 253], '第五关:要小心哦!一路上会有毒蛇的啊!', 10, 5);
tran喵ission('#火人-5', 'red', [33, 17, 253], '第五关:要小心哦!一路上会有毒蛇的啊!', 10, 5);
function bindSnake(selector, type) {
world.querySelector(selector).onEntityContact(({ other }) => {
if (other.isPlayer) {
if (type == 'red') {
if (other.hasTag('blue')) {
other.enableDamage = true;
other.hurt(100);
other.player.directMessage(`你被火蛇咬喵了!`)
other.player.forceRespawn()
} else {
other.player.directMessage('因为你是火人,你可以免疫火蛇对你的伤害!')
}
}
if (type == 'blue') {
if (other.hasTag('red')) {
other.enableDamage = true;
other.hurt(100);
other.player.directMessage('你被冰蛇咬喵了!')
other.player.forceRespawn()
} else {
other.player.directMessage(`因为你是冰人,你可以免疫冰蛇对你的伤害`)
}
}
}
});
}
function getKey(selector) {
let key = world.querySelector(selector);
key.enableInteract = true;
key.interactHint = key.id;
key.interactRadius = 3;
key.interactColor.set(0.8, 0.7, 1);
key.onInteract(({ entity }) => {
if (entity.level == 2 && entity.keyNum == 0) {
entity.keyNum++;
textDialog(entity, key.id, '恭喜你!捡到了这把关键的钥匙!', true)
} else {
textDialog(entity, key.id, '你已经捡过我了哦~不能重复捡!', true)
}
});
}
async function animate(selector, startPos, endPos, durations, duration) {
let elevator = world.querySelector(`${selector}`) //获取电梯板实体
elevator.fixed = true//不受外力影响
elevator.collides = true//允许碰撞
elevator.animate([
{ position: startPos, duration: durations[0] },//停留在地面
{ position: startPos, duration: durations[1] },//用于地面往楼顶移动
{ position: endPos, duration: durations[2] },// 停留在楼顶
{ position: endPos, duration: durations[3] },//从楼顶回到地面
], {
duration: 16 * duration,
iterations: Infinity,
direction: Box3AnimationDirection.WRAP,
});
}
animate('#门', [244.5, 38.5, 187.6], [244.5, 33.7, 187.6], [1, 1, 1, 1], 4);
animate('#电梯-1', [225.5, 20.1, 253.4], [225.5, 28, 253.4], [1, 2, 1, 2], 6);
animate('#电梯-2', [253.5, 25, 253.4], [253.5, 36, 253.4], [1, 2, 1, 2], 6);
animate('#荷叶', [208.5, 11.2, 253], [190, 11.2, 253], [1, 2, 1, 2], 12);
animate('#荷叶-2', [169, 11.1, 253], [158, 11.1, 253], [2, 2.5, 2, 2.5], 9);
animate('#红蛇-1', [102, 22.7, 253.4], [102, 19.5, 253.4], [5, 0.5, 0.5, 1], 7)
bindSnake('#红蛇-1', 'red');
bindSnake('#红蛇-2', 'red');
animate('#红蛇-2', [106, 19.5, 253.4], [106, 22.7, 253.4], [5, 0.5, 0.5, 1], 7)
bindSnake('#红蛇-3', 'red');
animate('#红蛇-3', [110, 22.7, 253.4], [110, 19.5, 253.4], [5, 0.5, 0.5, 1], 7)
bindSnake('#红蛇-4', 'red');
animate('#红蛇-4', [114, 19.5, 253.4], [114, 22.7, 253.4], [5, 0.5, 0.5, 1], 7)
bindSnake('#蓝蛇-1', 'blue');
animate('#蓝蛇-1', [118, 31, 253], [118, 27, 253], [5, 0.5, 0.5, 1], 7);
bindSnake('#蓝蛇-2', 'blue');
animate('#蓝蛇-2', [114, 27, 253], [114, 31, 253], [5, 0.5, 0.5, 1], 7);
bindSnake('#蓝蛇-3', 'blue');
animate('#蓝蛇-3', [110, 31, 253], [110, 27, 253], [5, 0.5, 0.5, 1], 7);
bindSnake('#蓝蛇-4', 'blue');
animate('#蓝蛇-4', [106, 27, 253], [106, 31, 253], [5, 0.5, 0.5, 1], 7)
animate('#电梯地板5', [71.6, 42.2, 253.3], [71.6, 55, 253.3], [1, 2, 1, 2], 3)
animate('#电梯地板6', [44, 44, 253.3], [44, 55, 253.3], [1, 2, 1, 2], 3)
animate('#ds', [73.5, 34, 253.5], [73.5, 30, 253.5], [7, 0.3, 1, 0.3], 8.6);
animate('#ds-2', [67.5, 51.5, 253.5], [72, 51.5, 253.5], [6, 1, 0, 1], 4);
animate('#ds-3', [41.5, 30, 253.5], [41.5, 34, 253.5], [6, 1, 0, 1], 4);
animate('#ds-4', [47, 48.5, 253.5], [43.5, 48, 253.5], [6, 1, 0, 1], 4);
getKey('#钥匙-1');
getKey('#钥匙-2');
getKey('#钥匙-3');
getKey('#钥匙-4');
world.querySelectorAll('.ds').forEach((ds) => {
ds.onEntityContact(({ other }) => {
if (other.isPlayer) {
other.player.forceRespawn();
other.player.directMessage(`这是剧毒的蛇哦!冰人和火人都无法免疫它的毒性`)
}
})
});
world.onFluidEnter(({ entity, voxel }) => {
if (voxel == voxels.id('lime_juice')) {
if (entity.isPlayer) {
entity.player.forceRespawn();
entity.player.directMessage(`这个水有剧毒哦!不管是火人还是冰人碰到都会趋势`)
}
}
})
function find(name) {
var p = world.querySelectorAll('player').filter(
e => e.player.name == name
)[0]
if (p) {
return p
}
}
SQL.js部分:
b.sql`
create table if not exists playerData (
userKey text primary key unique default '',
spawnPoint text default '',
playerType text default '',
level text default '',
goNum integer default 0
)
`
async function saveData(entity) {
await db.sql`
insert into playerData (
userKey,
spawnPoint,
playerType,
level,
goNum
) values (
${entity.player.userKey},
${JSON.stringify([entity.player.spawnPoint.x, entity.player.spawnPoint.y, entity.player.spawnPoint.z])},
${entity.tags()[0]},
${entity.level},
${entity.goNum}
)
on conflict(userKey)
do update set
userKey = excluded.userKey,
spawnPoint = excluded.spawnPoint,
playerType = excluded.playerType,
level = excluded.level,
goNum = excluded.goNum
`
}
async function loadData(entity) {
const data = (
await (
db.sql`
SELECT * FROM playerData WHERE userKey = ${entity.player.userKey} limit 1
`
)
)[0];
if (data) {
let s = data.spawnPoint;
entity.player.spawnPoint.set(s[0], s[1], s[2]);
entity.addTag(data.playerType);
entity.level = data.level;
entity.goNum = data.goNum;
if (entity.hasTag('blue')) {
entity.player.color.set(0, 0, 1);
entity.blue_ball = 0;
entity.can_get_blue_ball = true;
}
if (entity.hasTag('red')) {
entity.player.color.set(1, 0, 0);
entity.red_ball = 0;
entity.can_get_red_ball = true;
}
} else {
await join(entity);
}
}
module.exports = {
saveData,
loadData,
};