用户:
迷失冷光查看:6 回复:12 评论:6 创建时间:2020-10-21T20:01:07
今天我们要讨论的代码是去而复返
意思呢就是玩家离开再进入时会回到原来的位置
我也是和好几个人研究了半小时的结果
话不多说,直接上代码
首先,要创建一个SQL表格:
//创造SQL表格
db.sql`CREATE TABLE IF NOT EXISTS leaderboard(
id VARCHAR(500) PRIMARY KEY UNIQUE NOT NULL,
x REAL NOT NULL,
y REAL NOT NULL,
z REAL NOT NULL,
)`
id可以保存人的boxId
这样改名数据也不会丢失啦!
x,y,z是坐标的三个分量
接着,我们设置玩家没有存档时的初始位置:
const joinPosition = new Box3Vector3(128,55,128);//设置玩家出生点
(数据可修改)
然后,在玩家进入时
如果玩家有数据时就移动到指定地点
否则就移动到初始地点:
world.onPlayerJoin(async({entity})=>{
const rows = await db.sql`SELECT * FROM leaderboard WHERE id = ${entity.player.boxId}`;//判断玩家数据是否为空
if(rows && !rows.length){//如果玩家没有数据
entity.position = joinPosition;//将玩家移动到出生点
await db.sql`INSERT INTO leaderboard(id,x,y,z) VALUES (${entity.player.boxId},${joinPosition.x},${joinPosition.y},${joinPosition.z})`;//初始位置
}else{//否则
const id = entity.player.boxId;
for await(const row of db.sql`SELECT * FROM leaderboard WHERE id = ${id} ORDER BY x ASC LIMIT 1`){//查找玩家数据
var x = row.x;
}
for await(const row of db.sql`SELECT * FROM leaderboard WHERE id = ${id} ORDER BY y ASC LIMIT 1`){//查找玩家数据
var y = row.y;
}
for await(const row of db.sql`SELECT * FROM leaderboard WHERE id = ${id} ORDER BY z ASC LIMIT 1`){//查找玩家数据
var z = row.z;
}
entity.position.set(x,y,z);//将玩家移动到数据点
}
})
最后,在玩家离开时保存数据:
world.onPlayerLeave(({entity})=>{
db.sql`UPDATE leaderboard SET x = ${entity.position.x} WHERE id = ${entity.player.boxId}`;
db.sql`UPDATE leaderboard SET y = ${entity.position.y} WHERE id = ${entity.player.boxId}`;
db.sql`UPDATE leaderboard SET z = ${entity.position.z} WHERE id = ${entity.player.boxId}`;
})
那我的本期讲解就到此结束啦!

完整代码:
//创造SQL表格
db.sql`CREATE TABLE IF NOT EXISTS leaderboard(
id VARCHAR(500) PRIMARY KEY UNIQUE NOT NULL,
x REAL NOT NULL,
y REAL NOT NULL,
z REAL NOT NULL,
)`
c喵t joinPosition = new Box3Vector3(128,55,128);//设置玩家出生点
world.onPlayerJoin(async({entity})=>{
c喵t rows = await db.sql`SELECT * FROM leaderboard WHERE id = ${entity.player.boxId}`;//判断玩家数据是否为空
if(rows && !rows.length){//如果玩家没有数据
entity.position = joinPosition;//将玩家移动到出生点
await db.sql`INSERT INTO leaderboard(id,x,y,z) VALUES (${entity.player.boxId},${joinPosition.x},${joinPosition.y},${joinPosition.z})`;//初始位置
}else{//否则
c喵t id = entity.player.boxId;
for await(c喵t row of db.sql`SELECT * FROM leaderboard WHERE id = ${id} ORDER BY x ASC LIMIT 1`){//查找玩家数据
var x = row.x;
}
for await(c喵t row of db.sql`SELECT * FROM leaderboard WHERE id = ${id} ORDER BY y ASC LIMIT 1`){//查找玩家数据
var y = row.y;
}
for await(c喵t row of db.sql`SELECT * FROM leaderboard WHERE id = ${id} ORDER BY z ASC LIMIT 1`){//查找玩家数据
var z = row.z;
}
entity.position.set(x,y,z);//将玩家移动到数据点
}
})
world.onPlayerLeave(({entity})=>{
db.sql`UPDATE leaderboard SET x = ${entity.position.x} WHERE id = ${entity.player.boxId}`;
db.sql`UPDATE leaderboard SET y = ${entity.position.y} WHERE id = ${entity.player.boxId}`;
db.sql`UPDATE leaderboard SET z = ${entity.position.z} WHERE id = ${entity.player.boxId}`;
})点赞0
评论
神岛吉吉喵建议的改进:
(async function() {
console.clear();
await db.sql`CREATE TABLE IF NOT EXISTS players (
id VARCHAR(16) PRIMARY KEY UNIQUE NOT NULL,
x REAL NOT NULL,
y REAL NOT NULL,
z REAL NOT NULL
)`
}());
const joinPosition = new Box3Vector3(128,55,128);//设置玩家出生点
world.onPlayerJoin(async({entity})=>{
const rows = await db.sql`SELECT x,y,z FROM players WHERE id = ${entity.player.userKey}`;//判断玩家数据是否为空
if(rows && !rows.length){//如果玩家没有数据
entity.position = joinPosition;//将玩家移动到出生点
await db.sql`INSERT INTO players(id, x, y, z) VALUES (${entity.player.userKey}, ${joinPosition.x}, ${joinPosition.y}, ${joinPosition.z})`;//初始位置
}else{//否则
const {x, y, z} = rows[0];
entity.position.set(x,y,z);//将玩家移动到数据点
}
})
world.onPlayerLeave(({entity})=>{
db.sql`UPDATE players SET x = ${entity.position.x}, y = ${entity.position.y}, z = ${entity.position.z} WHERE id = ${entity.player.userKey}`;
})

点赞3
评论
迷失冷光//继续改进:
db.sql`CREATE TABLE IF NOT EXISTS pos(
id VARCHAR(16) PRIMARY KEY UNIQUE NOT NULL,
x REAL NOT NULL,
y REAL NOT NULL,
z REAL NOT NULL
)`
const joinPosition = new Box3Vector3(x,y,z);//出生点
world.onPlayerJoin(({entity})=>{
const rows = await db.sql`SELECT * FROM pos WHERE id = ${entity.player.userKey}`;
if(rows && rows.length){
entity.position.copy(joinPosition);
db.sql`INSERT INTO pos VALUES(${entity.player.userKey},0,0,0)`;
}else{
const row = rows[0];
entity.position.set(row.x,row.y,row.z);
}
})
world.onPlayerLeave(({entity})=>{
db.sql`UPDATE pos SET x = ${entity.position.x},y = ${entity.position.y},z = ${entity.position.z} WHERE id = ${entity.player.userKey}`;
})点赞0
评论
还是那个小橙子丫imgsrc="https://static.cod喵/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E7%82%B9%E8%B5%9E.gif"alt="emotion_编程猫_点赞"
点赞0
评论