猫史档案馆


【求助】为什么不能用代码设置实体能否互动

用户:聋猪up聋猪up查看:2 回复:3 评论:2 创建时间:2022-02-23T18:55:15


我尝试了遍历,递归,强制添加标签等方法,全图代码如下:

console.clear()

function saj(){
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, //对话框左上角标题内容
    })
}

async function create(){
    
    
    
    world.createEntity({
        mesh:'mesh/冰.vb',
        position:new Box3Vector3(喵+ 10*Math.random(), 9, 喵 + 10*Math.random()),
        meshScale:new Box3Vector3(0.1, 0.1, 0.1),
        collides:true,
        fixed:true,
        gravity:false,
        enableInteract:true,
    })
    
await sleep(1000);
create();
    
}
create();
const npc = world.querySelector('#冰');
npc.enableInteract = true; // 允许进行互动
npc.interactRadius = 16;   // 实体的互动范围
npc.interactHint = npc.id; // 互动提示框显示实体的名称
npc.interactColor = new Box3RGBColor(1,0,1);  // 互动提示的文字颜色

// 玩家与实体进行交互时触发
npc.onInteract(async({entity}) => {
    e.onInteract(({ entity }) => {
        entity.junk += 1 //累加废品
        e.destroy() //从地图中删除实体

        //跟world.say相似, 但只对指定的玩家显示信息而不是对所有玩家广播
        entity.player.directMessage(`${entity.player.name} 累计捡到${entity.junk}个废品`)
    })
});
world.onPlayerJoin(({ entity }) => {//每次玩家进入游戏都会运行
    entity.money = 0 //钱
    entity.junk = 0 //废品
    entity.itemList = [] //口袋里的货品

    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, '请你先登录哦')
    }

}



async function loadPlayer(entity) {
    const [data] = await (db.sql`SELECT * FROM player WHERE userKey=${entity.player.userKey} limit 1`);
    if (data) { //如果存在这个玩家的存档
        entity.money = data.money //恢复金钱
        entity.junk = data.junk //恢复废品
        entity.itemList = JSON.parse(data.itemList) //恢复道具列表, 这里的JSON.parse用于把字符串变回数组
    }
}



async function showPlayers() {
    console.clear() // 清空控制台
    const playerList = await db.sql`SELECT * FROM player`
    for (const p of playerList) {
        console.log(JSON.stringify(p))

    }

}



async function createTable() {

    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()

}



createTable()


回复

上一页1 页 / 共 1下一页
编码喵编码喵

便利实体的参数为什么有个逗号?是标签真有?

点赞0


评论


沙拉黄沙拉黄

报错我猜一下,是不是TypeError

点赞0


评论


沙拉黄沙拉黄

我也同感

点赞0


评论