用户:
北极熊xry查看:0 回复:1 评论:0 创建时间:2021-11-14T19:53:54
这些代码有问题吗?
console.clear()for (const e of world.querySelectorAll('.a,.b')) { //遍历每一个废品实体 e.enableInteract = true //开启交互 e.interactRadius = 2.5 //交互范围 e.interactHint = e.id //交互提示文本显示标签名 e.interactColor.set(0, 1, 0) //交互提示文本设为绿色
e.onInteract(({ entity }) => { entity.junk += 1 //累加废品 e.destroy() //从地图中删除实体
//跟world.say相似, 但只对指定的玩家显示信息而不是对所有玩家广播 entity.player.directMessage(`${entity.player.name} 累计捡到${entity.junk}个废品`) }) }
//封装成函数是因为它会被多次用到(显示玩家状态, 显示与NPC交互结果) function textDialog(entity, content, title) { return entity.player.dialog({ //弹出对话框 type: Box3DialogType.TEXT, //对话框类型为纯文本 content, //文本内容 title, //对话框左上角标题内容 }) }
loadPlayer(entity)
entity.player.onPress(async ({ button }) => { //每当有按钮被按下 if (button == Box3ButtonType.ACTION1) { //如果按钮是右键 const selection = await entity.player.dialog({ type: Box3DialogType.SELECT, //对话框类型为选择框 content: `你身上有:\n\n${entity.money}元, ${entity.junk}个废品\n道具: [${entity.itemList}]`, options: ['保存'], }) if (selection) { //确保对话框不是点击x关闭 if (selection.value === '保存') { //被点击的是保存按钮 await savePlayer(entity) //等待写入结束才运行下一行 textDialog(entity, '保存完毕') } } } else if (button == Box3ButtonType.ACTION0) { //如果按钮是左键 const eaten = entity.itemList.pop() //口袋列表最右边的一个物品拿出来 if (eaten) { entity.player.directMessage(`${entity.player.name}吃掉了"${eaten}"`) } } }) })
//NPC是non-player character的简称, 表示游戏里无人控制的虚构人物 for (const npc of world.querySelectorAll('.NPC')) { npc.enableInteract = true //允许当前npc实体触发交互 npc.interactRadius = 4.5 //交互范围 npc.interactHint = npc.id //交互提示文本显示实体名字 npc.interactColor.set(0, 1, 0) //交互提示文本颜色为绿色
if (npc.id == '学生') { //如果实体名是'学生' npc.onInteract(async ({ entity }) => { //当玩家对这个npc按E触发交互, npc对玩家打招呼 const killed = entity.itemList.pop() if(killed){ textDialog(entity, `谢谢你的${killed}~`, npc.id) } }) } else if (npc.id == '回收处员工') { //如果实体名是'回收处员工' npc.onInteract(async ({ entity }) => { //当玩家对这个npc按E触发交互, npc问你想卖多少废品 const amount = await entity.player.dialog({ type: Box3DialogType.INPUT, //对话框类型为输入框 content: `你有${entity.junk}废品, 想卖掉多少呢?`, title: npc.id, //对话框左上角显示NPC名字 }) if (amount > 0 && amount <= entity.junk) { //如果持有的废品数符合输入的数值 entity.junk -= amount //交出废品 const gain = 2 * amount //废品换算成钱 entity.money += gain //钱入账到玩家 textDialog(entity, `${amount}废品卖得${gain}元`) } }) } else if (npc.id == '小卖部老板') { //如果实体名是'小卖部老板' npc.onInteract(async ({ entity }) => { //当玩家对这个npc按E触发交互, npc问你想买哪些商品 const goodsList = ['薯片', '可乐', '糖果','热狗'] //货品列表 const priceList = [7, 5, 4, 10] //价格列表 const selection = await entity.player.dialog({ type: Box3DialogType.SELECT, //对话框类型为选择框 content: `你有${entity.money}元, 想买点什么呢?`, title: npc.id, //对话框左上角显示NPC名字 options: ['薯片 (7元)', '可乐 (5元)', '糖果 (4元)', '热狗(10元)', '离开'], //选项 }) if (selection) { //如果玩家没有点击'x'关闭对话框 const goods = goodsList[selection.index] //被选中的货物 const price = priceList[selection.index] //被选中货物的价格 if (entity.money >= price) { //如果玩家的钱足够 entity.money -= price //扣钱 entity.itemList.push(goods) //货物装进玩家口袋列表最右边 textDialog(entity, `"${goods}"入手`) } else { textDialog(entity, `要买"${goods}"还差${price - entity.money}元`) } } }) } }
async function savePlayer(entity) {//定义保存玩家状态的函数 if (entity.player.userKey) {//拥有userKey的玩家, 则玩家不是游客, 可以保存 await db.sql` --尝试向player表插入一条记录, 向各个字段写入玩家身上对应的属性值 INSERT INTO player ( userName, money, junk, itemList, userKey ) VALUES( ${entity.player.name}, ${entity.money}, ${entity.junk}, ${JSON.stringify(entity.itemList)},--itemList是数组, 需要用JSON.stringify才能存入类型为TEXT的字段 ${entity.player.userKey} ) ON CONFLICT(userKey)--如果玩家记录已经存在, 则不需要插入, 而是更新各个字段的值 DO UPDATE SET
userName=excluded.userName, money=excluded.money, junk=excluded.junk, itemList=excluded.itemList ` }else{ textDialog(entity, '请你先登录哦') }
}
}
}
await db.sql`CREATE TABLE IF NOT EXISTS player ( userName TEXT DEFAULT '',--玩家名字 money INTEGER DEFAULT 0,--金钱 junk INTEGER DEFAULT 0,--废品 itemList TEXT DEFAULT '',--道具列表 userKey TEXT PRIMARY KEY UNIQUE DEFAULT ''--玩家的识别码
)`
showPlayers()
}
try {//代码已整体优化 var door = world.querySelector('#辅导机器人1')
door.enableInteract = true door.interactRadius = 2.5
door.onInteract(({ entity }) => { entity.player.link(' https://docs.box3.codemao.cn') }) var door = world.querySelector('#辅导机器人2')
door.enableInteract = true door.interactRadius = 2.5
door.onInteract(({ entity }) => { entity.player.link(' https://shequ.codemao.cn/community/358369') })
var door = world.querySelector('#CK工作室')//有趣的是,我不在这个工作室里,我只是受人委托
door.enableInteract = true door.interactRadius = 2.5
door.onInteract(({ entity }) => { entity.player.link(' https://shequ.codemao.cn/work_shop/6720') })
//用于官方、作者与大队长(未经作者或副图者的允许禁止更改本代码) world.onPlayerJoin(({ entity }) => {//这段代码表示一开始被括号括起来的代码是在这个实体玩家上执行的 const TEST_PLAYER = ['吉吉喵', '和平队长', '美术喵', '搬砖喵', '可爱的编程喵', '工藤.北极熊xry']//这里是一个列表,打中括号,引号引起来的是列表中的项目 if (!TEST_PLAYER.includes(entity.player.name)) return;//如果自己的昵称在列表中 entity.player.canFly = true;//如果上一段代码成立的话,就让这个玩家飞行 entity.player.emissive = 0.03;//...这个玩家发光,可以更改发光亮度 }); //用于合作者 var admin = ['A1林烁', '工藤.北极熊xry', '可爱的编程喵', '编程技术喵', '墨小生-CK工作室', '刘润泽Baky']//创建一个白名单列表 world.onChat(async({ entity, message, entity:{player} }) => {//这段代码表示一开始被括号括起来的代码是在这个实体玩家说话后执行的 if (admin.includes(entity.player.name)) { if (message.startsWith('~>',0)) {// var code = message.slice(2) console.log(player.name + ':\n~>' + code) try { out = await eval(code) player.directMessage('✅\n' + out) console.debug(`<~${out} at ChatCommand`) } catch (error) { player.directMessage('❌\n' + error) console.error(`<~${error} at ChatCommand`) } finally { } } if (message == '特殊权限') {//当玩家说‘特殊权限’ player.directMessage('亲爱的创作者你好!特殊功能包含加速、幽灵、解除幽灵、发光、还原发光、全部还原、全部开启、禁言(禁言+禁言者名字)、解除禁言(解除禁言+被禁言者名字)、进监狱(进监狱者+进监狱进监狱、出监狱(出监狱者+进监狱')//在这个玩家的上面显示 } else if (message == '加速') { player.walkSpeed = 2//这个玩家走路速度设置为2 player.runSpeed = 2//这个玩家游泳速度设置为2 player.flySpeed = 2//这个玩家飞行速度设置为2 world.say(player.name + ' 创作者为了更快找到玩家反馈地点,速度加快')//时间播报引号中的文字 } else if (message == '幽灵') { player.spectator = true;//这个玩家开启幽灵 world.say(player.name + ' 创作者为了找到玩家反馈地点,穿墙幽灵 模式开启') } else if (message == '解除幽灵') { player.spectator = false;//这个玩家取消幽灵 world.say(player.name + '关闭了幽灵模式') } else if (message == '发光') { player.emissive = 0.03;//这个玩家唉发光,数值表示发光系数 world.say(player.name + ' 创作者为了能让玩家看清楚创作者,开启了发光效果') } else if (message == '还原发光') { player.emissive = 0; world.say(player.name + '还原了发光效果') } else if (message == '全部还原') { player.scale = 1; player.spectator = false; player.invisible = false; player.emissive = 0; player.shininess = 0; player.color.set(1, 1, 1); Object.assign(entity, { particleRate: 250, }); world.say(player.name + '全部还原了'); } else if (message == '关闭粒子特效') { Object.assign(entity, { particleRate: 0, }); world.say(player.name + '关闭了粒子特效'); } else if (message == '开启粒子特效') { Object.assign(entity, { particleRate: 100, }); world.say(player.name + '开启了粒子特效'); } else if (message.startsWith('禁言')) { if (message.slice(2) == player.name) { player.directMessage('无法禁言自己,请重新尝试!') } else { world.querySelectorAll('player').forEach((x) => { if (x.player.name == message.slice(2)) { x.player.directMessage('你已被管喵禁言,请遵守地图秩序!【禁言原因可能是骂人(会举报)、脏话、刷屏(会举报)】') world.say('有人犯了错,被管喵禁言了!(请各位不要骂人,不要说脏话,不要刷屏)') x.player.muted = true } }) } } else if (message.startsWith('解除禁言')) { if (message.slice(4) == player.name) { player.directMessage('无法解除禁言自己,请重新尝试!') } else { world.querySelectorAll('player').forEach((x) => { if (x.player.name == message.slice(4)) { x.player.directMessage('你已被管喵解除禁言') world.say('有人已被管喵解除禁言了!') x.player.muted = false } }) } } } }) //粒子特效 world.onPlayerJoin(({ entity }) => { world.onTick(() => { PlayerUpdate(entity) }); });
// 蓝色粒子 const particle_blueCrystal = { particleRate: 1000, particleLifetime: 2, particleSize: [1, 0.8, 0.6, 0.4, 0.2], particleColor: [ new Box3RGBColor(1, 1, 0), new Box3RGBColor(1, 1, 0), new Box3RGBColor(1, 0, 0), new Box3RGBColor(1, 0, 0), new Box3RGBColor(1, 1, 1) ], }
// 火焰粒子 const particle_flame = { particleRate: 20, particleLifetime: 1.75, particleSize: [2, 0.65, 0.23], particleColor: [ new Box3RGBColor(0, 0, 1), new Box3RGBColor(0, 1, 1), new Box3RGBColor(1, 1, 1) ], }
function PlayerUpdate(entity) { // 判断行走状态 switch (entity.player.walkState) { // 正在奔跑 case Box喵layerWalkState.RUN: Object.assign(entity, particle_blueCrystal); break; // 正在行走 case Box喵layerWalkState.WALK: Object.assign(entity, particle_flame); break; // 在其他行走状态不显示粒子 default: Object.assign(entity, { particleRate: 0 }); }; }; //这个是开发API里面的例子,给你利用这些好的,一开始时是开启效果的 //例子用了PlayerUpdate这个函数,我逝世 // /* if (message == '关闭粒子特效') { Object.assign(entity, { particleRate: 0, }); world.say(entity.player.name + '关闭了粒子特效'); } if (message == '开启粒子特效') { Object.assign(entity, { particleRate: 100, }); world.say(entity.player.name + '开启了粒子特效'); }*/ function contains(arr, obj) { var i = arr.length; while (i >= 0) { if (arr[i] === obj) { return true; } i--; } return false; }
function find(arr, obj) { var i = arr.length; while (i >= 0) { if (arr[i] === obj) { return i; } i--; } return -1; }
var in_jail = new Array(); var all_player_name = new Array(); var jail_count = 0;
world.onPlayerJoin(({ entity }) => { all_player_name.push(entity.player.name); }) //玩家的互动 world.onPlayerJoin(({ entity }) => {//有玩家进入地图时 entity.player.showName = false //为有更好的效果,隐藏玩家的名字(否则会很奇怪) entity.enableInteract = true //允许实体互动 entity.interactRadius = 5 //互动范围 entity.interactHint = `和 "${entity.player.name}" 互动` //可以互动时显示的文字 }) world.onInteract(async ({ entity, targetEntity }) => { const result = await entity.player.dialog({ type: Box3DialogType.SELECT, title: "系统", lookEye: entity, lookTarget: targetEntity, content: `向" ${targetEntity.player.name} "互动`, options: ['对话', '取消'], }); if (result.index === 0) { const result1 = await entity.player.dialog({ type: Box3DialogType.INPUT, title: "系统", lookTarget: entity, content: `请输入对话内容`, confirmText: "发出对话" }); if (result1 != null) { await targetEntity.player.dialog({ type: Box3DialogType.TEXT, title: entity.player.name, content: result1, }); } } }); } catch (error) { world.say(error + ` at index,js`) console.error(`<~${error} at index.js`) } finally { console.clear() console.log(`程序运行中...`) }