用户:
0ttB查看:1 回复:2 评论:1 创建时间:2021-05-30T11:25:21
world.onPlayerJoin(({ entity }) => {
entity.junk = 0
entity.money = 0
entity.itemList = []
entity.storagetime = new Date().toLocaleString( 'zh' ,{timeZone: 'Asia/Shanghai'})
loadPlayer(entity)
entity.player.onPress(async({ button }) => {
if (button == 'action0') {
const eaten = entity.itemList.pop()
if (eaten) {
entity.player.directMessage(`${entity.player.name} 吃掉了'${eaten}'`)
}
}
else if (button == 'action1') {
const selections = await entity.player.dialog({
type: 'select',
content: `你上次保存时间是:${entity.storagetime}\n你身上有:\n\n${entity.money}元,${entity.junk}个废品\n道具:[${entity.itemList}]`,
options: ['保存', '继续游戏']
})
if(selections){
if(selections.index === 0){
await savePlayer(entity, new Date().toLocaleString( 'zh' ,{timeZone: 'Asia/Shanghai'}))
textDialog(entity, '保存完毕')
}
}
}
})
})
for (const e of world.querySelectorAll('.空瓶垃圾')) {
e.enableInteract = true
e.interactRadius = 2.5
e.interactHint = e.tags()
e.interactColor.set(0, 1, 0)
e.onInteract(({ entity }) => {
entity.junk += 1
e.destroy()
world.say(`${entity.player.name} 累计捡到${entity.junk}个废品`)
})
}
function textDialog(entity, content, title) {
return entity.player.dialog({
type: 'text',
content,
title
})
}
for (const npc of world.querySelectorAll('.npc')) {
npc.enableInteract = true
npc.interactRadius = 4.5
npc.interactHint = npc.id
npc.interactColor.set(0, 1, 0)
npc.onInteract(async({ entity }) => {
world.sound('audio/chat.mp3')
if (npc.id == '学生') {
textDialog(entity, `${entity.player.name},你好呀~`, '学生')
}
else if (npc.id == '回收处员工') {
const amount = await entity.player.dialog({
type: 'input',
title: '回收处员工',
content: `你有${entity.junk}个废品,想卖掉几个呢?`
})
if (amount === null) return
if (amount >= 0 && amount <= entity.junk) {
entity.junk -= amount
const gain = amount * 2
entity.money += gain
textDialog(entity, `${amount}废品卖得${gain}元`)
}
else{
textDialog(entity, '你没有那么多废品')
}
}
else if (npc.id == '小卖部老板') {
const dialoggoodsList = ['薯片(7元)', '可乐(5元)', '糖果(3元)', '热狗(6元)']
const goodsList = ['薯片', '可乐', '糖果', '热狗']
const priceList = [7, 5, 3, 6]
const selection = await entity.player.dialog({
type: 'select',
content: `你有${entity.money}元,想买点什么呢?`,
title: '小卖部老板',
options: dialoggoodsList
})
if (selection) {
const price = priceList[selection.index]
const goods = goodsList[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 createTable() {
await db.sql`
CREATE TABLE IF NOT EXISTS player (
money INTEGER DEFAULT 0,
junk INTEGER DEFAULT 0,
itemList TEXT DEFAULT '',
userKey TEXT PRIMARY KEY UNIQUE DEFAULT '',
time NUMERIC DEFAULT ''
)
`
showPlayers()
}
createTable()
async function savePlayer(entity, time){
entity.storagetime = time
if (entity.player.userKey) {
await db.sql`
INSERT INTO player (
userKey,
money,
junk,
itemList,
time
)
VALUES(
${entity.player.userKey},
${entity.money},
${entity.junk},
${JSON.stringify(entity.itemList)},
${time}
)
ON CONFLICT(userKey)
DO UPDATE SET
time = excluded.time,
money = excluded.money,
junk = excluded.junk,
itemList = excluded.itemList
`
}
}
async function showPlayers(){
console.clear()
const playerList = await db.sql`SELECT * FROM player`
for (const players1 of playerList) {
console.log(JSON.stringify(players1))
}
}
async function loadPlayer(entity){
const sqldata01 = (await (db.sql`SELECT * FROM player WHERE userKey=${entity.player.userKey} limit 1`))[0]
if (sqldata01) {
entity.junk = sqldata01.junk
entity.money = sqldata01.money
entity.itemList = JSON.parse(sqldata01.itemList)
entity.storagetime = sqldata01.time
}
}
const reception = world.addZone({
selector: 'player',
bounds: {
lo: [52, 8, 67],
hi: [54, 13, 68],
}
})
reception.onEnter(({ entity }) => {
entity.player.directMessage('请勿进入前台')
entity.position.set(53, 10, 喵)
textDialog(entity, '请勿进入前台')
})
const elevator = world.querySelector('#电梯')
world.onVoxelContact(({ voxel }) => {
if (voxel === 391) {
voxels.setVoxel(55, 13, 31, 'lantern_02')
voxels.setVoxel(55, 13, 30, 'lantern_02')
}
else{
voxels.setVoxel(55, 13, 31, 'air')
voxels.setVoxel(55, 13, 30, 'air')
}
if (voxel === 119) {
elevator.position.y = 9
}
})
const lampswitch = world.querySelector('#灯开关')
const lampswitch1 = world.querySelector('#灯开关1')
const lamp = [
[43, 9, 28],
[37, 9, 28],
[37, 9, 36],
[42, 9, 36],
[42, 9, 47],
[37, 9, 47],
[43, 9, 40],
[43, 9, 44]
]
const lamp2 = [
[42, 15, 47],
[37, 15, 47],
[42, 15, 35],
[37, 15, 28],
[48, 19, 34],
[48, 19, 27]
]
lampswitch.enableInteract = true
lampswitch.interactRadius = 3.5
lampswitch.interactHint = '灯开关'
lampswitch.interactColor.set(1, 1, 1)
lampswitch.onInteract(() => {
if(voxels.getVoxel(42, 9, 47) === 157){
for(const ty of lamp){
voxels.setVoxel(ty[0], ty[1], ty[2], 'carpet_07')
}
}
else{
for(const ty of lamp){
voxels.setVoxel(ty[0], ty[1], ty[2], 'lantern_01')
}
}
world.sound('audio/灯开关.mp3')
})
lampswitch1.enableInteract = true
lampswitch1.interactRadius = 2.5
lampswitch1.interactHint = '灯开关'
lampswitch1.interactColor.set(1, 1, 1)
lampswitch1.onInteract(() => {
if(voxels.getVoxel(42, 15, 47) === 157){
for(const ty2 of lamp2){
voxels.setVoxel(ty2[0], ty2[1], ty2[2], 'carpet_07')
}
}
else{
for(const ty2 of lamp2){
voxels.setVoxel(ty2[0], ty2[1], ty2[2], 'lantern_01')
}
}
world.sound('audio/灯开关.mp3')
})
elevator.collides = true
elevator.fixed = true
world.onEntityContact(async({ entity, other, axis }) => {
if(entity.position.y < 15 && entity.id == '电梯'){
entity.velocity.y = 0.2
}
else if(entity.id == '8iu' && axis.y === -1){
other.velocity.z = -2
}
})
world.onTick(() => {
if(elevator.position.y > 15){
elevator.position.y = 15
elevator.velocity = 0
}
})
const monitors1 = world.querySelector('#监控-1')
monitors1.enableInteract = true
monitors1.interactRadius = 4
monitors1.interactHint = '监控'
monitors1.interactColor.set(1, 0, 0)
monitors1.onInteract(async({ entity }) => {
await Displaymonitoring(entity, 1, world.querySelector('#监控起点'), world.querySelector('#监控终点'), '别墅')
})
const monitors2 = world.querySelector('#监控-2')
monitors2.enableInteract = true
monitors2.interactRadius = 4
monitors2.interactHint = '监控'
monitors2.interactColor.set(1, 0, 0)
monitors2.onInteract(async({ entity }) => {
await Displaymonitoring(entity, 2, world.querySelector('#监控起点1'), world.querySelector('#监控终点1'), '商店')
})
Monitoringnumberprompt()
async function Monitoringnumberprompt(){
while(true){
const monitorss1 = world.querySelector('#监控-1')
const monitorss2 = world.querySelector('#监控-2')
monitorss1.say('监控号:1');
monitorss2.say('监控号:2');
await sleep(5000)
}
}
async function Displaymonitoring(entity, password, lookEye, lookTarget, contents){
world.sound('audio/chat.mp3')
const monitordialogs = await entity.player.dialog({
type: 'input',
content: '输入监控号'
})
if(monitordialogs == password){
const monitordialog = entity.player.dialog({
type: 'select',
content: `${contents}实时监控`,
options: ['离开'],
lookEye,
lookTarget,
contentBackgroundColor: new Box3RGBAColor(1, 1, 1, 0.7),
contentTextColor: new Box3RGBAColor(0, 0, 0, 0.7)
})
}
else{
const monitordialog1 = entity.player.dialog({
type: 'select',
content: '监控号错误',
options: ['关闭']
})
} 这些代码谁能一分钟写完?