用户:
YDS尹子查看:4 回复:17 评论:4 创建时间:2021-08-11T19:52:21

给乒乓球桌加一个pingpongdesk标签,给球加上pingpongball标签,给球网加上wang标签,应该就能用了
world.onPlayerJoin(({entity})=>{
entity.enableDamage=true;
entity.pressed=false;
entity.pressedTime=0;
entity.player.playingPingPong=true;
entity.player.dialog({
type:Box3DialogType.TEXT,
title:"乒乓球-测试版",
content:`不需要瞄准球,在球附近朝要打的方向长按左键蓄力,松开发射`
})
})
function setInter(query, id, rad, interact, func) {
world.querySelectorAll(query).forEach((e) => {
e.enableInteract = true;
e.interactRadius = rad;
e.interactHint = id;
e.interactColor.set(1,0.3,0.3)
e.interactColor.set(1, 1, 1);
if (interact) {
e.onInteract(({ entity, targetEntity }) => {
func(entity, targetEntity);
})
}
})
}
setInter(".pingpongdesk","打乒乓球",30,true,async function(entity,targetEntity){
entity.player.playingPingPong=true;
world.querySelectorAll(".pingpongball").forEach((e)=>{
e.gravity=true;
e.collides=true;
e.fixed=false;
})
})
world.querySelector(".pingpongball").maxHeight=0;
world.querySelector(".pingpongball").friction=0;
world.onPress(({entity,button})=>{
if(button=='action0'){
entity.pressed=true;
entity.pressedTime=world.currentTick;
entity.player.canFly=true;
}
})
world.onTick(()=>{
var s=world.querySelector(".pingpongball");
if(s.position.y>s.maxHeight)s.maxHeight=s.position.y;
if(s.position.y<=0){
world.say("球出界了,重置完毕")
s.position.set(92.8,23.1,110.5)
s.velocity.set(0,0,0)
s.maxHeight=23;
}
})
world.onRelease(({entity,raycast:{direction},button})=>{
if(button=='action0'&&entity.player.playingPingPong){
if(true){
var force=world.currentTick-entity.pressedTime;
if(force>10)force=10;
world.querySelectorAll(".pingpongball").forEach((e)=>{
if(e.position.distance(entity.position)<20){
e.say(force);
var k=4;
e.velocity.x=direction.x*force/k;
e.velocity.y+=direction.y*force/k*3;
e.velocity.z=direction.z*force/k;
}
})
}
}
})
world.onEntityContact(({entity,other,force})=>{
if(entity.hasTag("pingpongball")&&other.hasTag("pingpongdesk")){
entity.velocity.y+=entity.maxHeight/28;
}else if(entity.hasTag("pingpongball")&&other.hasTag("wang")){
world.say("球下网了,重置完毕")
entity.position.set(92.8,23.1,110.5);
entity.velocity.set(0,0,0)
entity.maxHeight=23;
}
})
world.onVoxelContact(({voxel,entity})=>{
if(entity.hasTag("pingpongball")){
world.say("球出界了,重置完毕")
entity.position.set(92.8,23.1,110.5);
entity.velocity.set(0,0,0)
entity.maxHeight=23;
}
})