用户:
一勺小汤_纪念号查看:0 回复:2 评论:0 创建时间:2022-03-28T16:02:58

一勺小汤_纪念号//在开始运行的时候创建表
(async function() {
await db.sql`CREATE TABLE IF NOT EXISTS playerdata (
userKey CHAR(16) PRIMARY KEY UNIQUE NOT NULL,--识别码
money INT NOT NULL,--钱
exps INT NOT NULL,--经验
level INT NOT NULL,--等级
item TEXT NOT NULL--物品
)`
}())
//存档
async function update(entity) {
await db.sql`UPDATE playerdata SET
money=${entity.money},
exps=${entity.exp},
level=${entity.level},
item=${JSON.stringify(entity.item)}
WHERE userKey=${entity.player.userKey}`
}
//清档
async function del(entity) {
await db.sql`DELETE FROM playerdata WHERE userKey=${entity.player.userKey}`
}
//当玩家进入地图
world.onPlayerJoin(async({entity})=>{
var data = await db.sql`SELECT * FROM playerdata WHERE userKey=${entity.player.userKey} LIMIT 1`//读取用户数据
world.say(JSON.stringify(data))
if (data && !data.length) {//当玩家没有数据
//初始化数据
entity.money=1000
entity.exp=0
entity.level=1
entity.item=[]
//插入数据
await db.sql`INSERT INTO playerdata VALUES (
${entity.player.userKey},
${entity.money},
${entity.exp},
${entity.level},
${JSON.stringify(entity.item)}
)`
}else{//当玩家数据存在时
var row = data[0]//作用:本来data是Object(对象),我们可以用[0]这个索引把读取到的数据从data里取出并赋值到row
//恢复数据
entity.money=row.money
entity.exp=row.exps
entity.level=row.level
entity.item=JSON.parse(row.item)//用JSON.parse使item变成数组
}
entity.maxExp=entity.level*100//设置升级所需的经验
entity.onVoxelContact(() => {//玩家每走一步
entity.exp += 1//经验+1
})
entity.player.onPress(async({button})=>{//当按下按键
if(button === Box3ButtonType.ACTION1){//当按下的按键是右键
//打开控制面板
var ctrl = await entity.player.dialog({
type: Box3DialogType.SELECT,
title: `${entity.player.name} 的信息`,
titleTextColor: new Box3RGBAColor(0, 0, 0, 1),
titleBackgroundColor: new Box3RGBAColor(0, 0.3, 1, 1),
content: `血量:${entity.maxHp}/${entity.hp}
金币:${entity.money}
等级:${entity.level}
经验:${entity.exp}/${entity.maxExp}
`,
options: ['手动保存','背包','升级','删档'],
contentTextColor: new Box3RGBAColor(0, 0, 0, 1),
contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
})
if(!ctrl||ctrl==null){
return
}
if(ctrl.value=='手动保存'){
await update(entity)
}else if(ctrl.value=='删档'){
await del(entity)
}else if(ctrl.value=='升级'){
if(entity.exp>=entity.maxExp){
entity.level+=1
entity.exp-=entity.maxExp
entity.maxExp=entity.level*100
await update(entity)
await entity.player.dialog({
type: Box3DialogType.TEXT,
title: `升级`,
titleTextColor: new Box3RGBAColor(0, 0, 0, 1),
titleBackgroundColor: new Box3RGBAColor(0, 0.3, 1, 1),
content: `恭喜升级到第${entity.level}级!`,
contentTextColor: new Box3RGBAColor(0, 0, 0, 1),
contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
})
}else{
await entity.player.dialog({
type: Box3DialogType.TEXT,
title: `升级`,
titleTextColor: new Box3RGBAColor(0, 0, 0, 1),
titleBackgroundColor: new Box3RGBAColor(0, 0.3, 1, 1),
content: `升级失败\n原因:经验不足`,
contentTextColor: new Box3RGBAColor(0, 0, 0, 1),
contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
})
}
}else if(ctrl.value=='背包'){
var bag = await entity.player.dialog({
type: Box3DialogType.SELECT,
title: `背包`,
titleTextColor: new Box3RGBAColor(0, 0, 0, 1),
titleBackgroundColor: new Box3RGBAColor(0, 0.3, 1, 1),
content: `${entity.player.name} 的背包\n共${entity.item.length}件`,
options: entity.item,
contentTextColor: new Box3RGBAColor(0, 0, 0, 1),
contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 1),
})
if(!bag||bag==null){
return
}
}
}
})
})点赞0
评论