Lv.1
朗朗
在 Box3穿戴装备 中回复
world.onPlayerJoin(({entity}) => { if(entity.player.name=='宇宙喵cl'){ entity.player.addWearable({ bodyPart: Box3BodyPart.RIGHT_HAND, mesh: 'mesh/黄金沙漠之鹰.vb', orientation: new Box3Quaternion(0,1,0,0).rotateY(Math.PI), offset: new Box3Vector3(-0.05,-0.1,0.5), scale: Box3Vector3 = new Box3Vector3(0.2, 0.2, 0.2), }) }; })
2021-08-04T16:52:57 点赞:0
在 Box3穿戴装备 中回复
world.onPlayerJoin(({entity}) => {
if(entity.player.name=='宇宙喵cl'){
entity.player.addWearable({
bodyPart: Box3BodyPart.RIGHT_HAND,
mesh: 'mesh/黄金沙漠之鹰.vb',
orientation: new Box3Quaternion(0,1,0,0).rotateY(Math.PI),
offset: new Box3Vector3(-0.05,-0.1,0.5),
scale: Box3Vector3 = new Box3Vector3(0.2, 0.2, 0.2),
})
};
})2021-08-04T16:53:16 点赞:0
在 求助!如何在代码岛3.0中用玩家的昵称保存? 中回复
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
`
}
}
async function createTable() {
await db.sql`
CREATE TABLE IF NOT EXISTS player (
money INTEGER DEFAULT 0,--金钱, INTEGER类型用于存储整数
junk INTEGER DEFAULT 0,--废品, INTEGER类型用于存储整数
itemList TEXT DEFAULT '',--道具列表, TEXT类型用于存储字符串
userKey TEXT PRIMARY KEY UNIQUE DEFAULT ''--玩家的唯一识别码, TEXT类型用于存储字符串
)
`
showPlayers() //每次运行代码都能查看数据库里所有记录
}
async function loadPlayer(entity) {
const data = (await (db.sql`SELECT * FROM player WHERE userKey=${entity.player.userKey} limit 1`))[0]
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()
}
2021-08-19T09:33:07 点赞:0