用户:
HenryTheSmart查看:6 回复:7 评论:6 创建时间:2022-08-30T19:20:44
SQL存档跑酷代码
hi!大家好,今天我来更新一期教程
这次代码关于SQL喝跑酷
在此之前,先声明一下:这个代码是@棉花味的冬天的帖子改写的,我改掉了里面的BUG
一条广告:秋天跑酷正在制作当中,敬请期待!
秋天跑酷招两名建筑师,链接如下👇
代码时间!
const SpawnPoint = new Box3Vector3(6,11,9);
const points = world.querySelectorAll('.存档点');
const INIT = {
hp: 100,
maxHp: 100,
enableDamage: true,
x: SpawnPoint.x,
y: SpawnPoint.y,
z: SpawnPoint.z,
qx: SpawnPoint.x,
qy: SpawnPoint.y,
qz: SpawnPoint.z,
jumpcounttime: 12,
level: 0,
mon: 100,
}
db.sql`CREATE TABLE IF NOT EXISTS leaderboard(
id CHAR(16) NOT NULL,
record TEXT NOT NULL
)`
world.addCollisionFilter('player','player');
world.onPlayerJoin(async({entity})=>{
function init(){
Object.assign(entity,INIT);
}
if(entity.player.userKey){
const rows = await db.sql`SELECT * FROM leaderboard WHERE id = ${entity.player.userKey}`;
if(rows && !rows.length){
init();
db.sql`INSERT INTO leaderboard VALUES(${entity.player.userKey},${getRecord.call(entity)})`;
}else{
Object.assign(entity,JSON.parse(rows[0].record));
}
}else init();
entity.position.set(entity.x,entity.y,entity.z);
entity.player.spawnPoint.set(entity.qx,entity.qy,entity.qz);
entity.player.onPress(async({button})=>{
if(button === Box3ButtonType.ACTION0){
entity.hurt(100);
}else if(button === Box3ButtonType.ACTION1){
const result = await entity.player.dialog({
type: Box3DialogType.SELECT,
title: '控制面板',
content: `${INIT.mon}`,
options: ['重新开始','切换视角',`跳关:${INIT.jumpcounttime}`,'观战他人'],
})
if(!result)return;
switch(result.index){
case 0:
Object.assign(entity,INIT);
entity.position.set(entity.x,entity.y,entity.z);
entity.player.spawnPoint.set(entity.qx,entity.qy,entity.qz);
update.call(entity);
break;
case 1:
if(entity.player.cameraMode === 'follow'){
entity.player.cameraMode = 'fps';
}else{
entity.player.cameraMode = 'follow';
}
break;
case 2:
entity.player.cancelDialogs();
if(INIT.jumpcounttime){
let found = false;
points.forEach((e)=>{
if(found)return;
if(e.level === entity.level + 1){
found = true;
entity.position = e.position.add({x: 0,y: 3,z: 0});
entity.player.spawnPoint.copy(entity.position);
entity.level = e.level;
INIT.jumpcounttime--;
}
})
if(!found){
entity.player.dialog({
type: Box3DialogType.TEXT,
title: '跳关失败',
content: '没有可跳过的关卡',
})
}
}else{
entity.player.dialog({
type: Box3DialogType.TEXT,
title: '跳关失败',
content: '你已没有了跳关次数',
})
}
break;
default:
return;
}
}
})
})
world.onDie(({entity})=>{
if(entity.isPlayer){
entity.player.forceRespawn();
}
})
points.forEach((e)=>{
e.level = Number(e.tags()[0]);
e.onEntityContact(({other})=>{
const spawnPoint = e.position.add({x: 0,y: 2.5,z: 0});
if(spawnPoint.equals(other.player.spawnPoint))return;
other.player.spawnPoint = spawnPoint;
other.player.directMessage('存档成功!');
other.level = e.level;
update.call(other);
})
})
function getRecord(){
const result = {};
Object.keys(INIT).forEach((e)=>{
result[e] = this[e];
})
return JSON.stringify(result);
}
function update(){
if(!this.player.userKey)return;
this.x = this.position.x;
this.y = this.position.y;
this.z = this.position.z;
this.qx = this.player.spawnPoint.x;
this.qy = this.player.spawnPoint.y;
this.qz = this.player.spawnPoint.z;
db.sql`UPDATE leaderboard SET record = ${getRecord.call(this)} WHERE id = ${this.player.userKey}`;
}
world.onPlayerLeave(({entity})=>{
update.call(entity);
})
world.onPlayerJoin(({ entity }) => {
entity.onFluidEnter(() => {//当玩家掉到水里
entity.player.forceRespawn();
})
})
原链接:编程猫社区-SQL存档跑酷の代码-编程学习交流 (codemao.cn)
代码要求:地面是水,给每个存档点加上两个标签,第一个是存档点,第二个是存档点的关卡,一个关卡只能有一个存档点,第一个存档点的关卡数是0,首先给所有存档点加上标签“存档点”SpawnPoint里写玩家的出生点
imgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E7%82%B9%E8%B5%9E.gif"alt="emotion_编程猫_点赞"
点赞0
评论