此页案例供专业开发者参考,以拓展更多玩法;部分案例无法直接复制使用,或需要插件等配合使用,建议根据需要自行更改脚本数据。
房主盯着怪物时,怪物无法移动和攻击;房主视线离开,怪物可以移动和攻击
local uin,oid=0,0
local function GameStart()
_,uin=Player:getHostUin()
Player:setAttr(uin,19,999)
local _,x,y,z=Actor:getPosition(uin)
local _,as=World:spawnCreature(x,y,z,3130,1)
oid=as[1]
end
ScriptSupportEvent:registerEvent('Game.Start',GameStart)
local function run()
local _,x,y,z=Player:getAimPos(uin)
local _,x1,y1,z1=Actor:getPosition(oid)
if math.abs(x-x1)<1 and math.abs(y-y1)<1 and math.abs(z-z1)<1 then
Actor:setActionAttrState(oid,1,false)
Actor:setActionAttrState(oid,32,false)
else
Actor:setActionAttrState(oid,1,true)
Actor:setActionAttrState(oid,32,true)
end
end
ScriptSupportEvent:registerEvent('Game.Run',run)
拓展:不看怪物时,怪物会瞬移至玩家背后;5秒冷却时间
local uin,oid,t=0,0,0
local function GameStart()
_,uin=Player:getHostUin()
Player:setAttr(uin,19,999)
local _,x,y,z=Actor:getPosition(uin)
local _,as=World:spawnCreature(x,y,z,3130,1)
oid=as[1]
end
ScriptSupportEvent:registerEvent('Game.Start',GameStart)
local function run()
if t>0 then t=t-1 end
local _,x,y,z=Player:getAimPos(uin)
local _,x1,y1,z1=Actor:getPosition(oid)
if math.abs(x-x1)<1 and math.abs(y-y1)<1 and math.abs(z-z1)<1 then
Actor:setActionAttrState(oid,1,false)
Actor:setActionAttrState(oid,32,false)
else
Actor:setActionAttrState(oid,1,true)
Actor:setActionAttrState(oid,32,true)
if t==0 then
_,x1,y1,z1=Actor:getPosition(uin)
local r=math.sqrt((x1-x)*(x1-x)+(z1-z)*(z1-z))/2
Actor:setPosition(oid,x1+(x1-x)/r,y1,z1+(z1-z)/r)
t=20*5
end
end
end
ScriptSupportEvent:registerEvent('Game.Run',run)
扩展:队伍B的玩家被队伍A的玩家看到(盯着)会无法移动持续5秒;冷却20秒:
local team1,team2=1,2--两个游戏队伍。team2的玩家被team1的玩家看到后无法移动
local time1,time2=5,20--无法移动持续秒数,冷却秒数
local ps1={}
local function run()
local r1,n1,as1=Team:getTeamPlayers(team1,2)
local r2,n2,as2=Team:getTeamPlayers(team2,2)
for r1=1,n1 do
if ps1[as1[r1]]==nil or ps1[as1[r1]]<=0 then
local _,x1,y1,z1=Player:getAimPos(as1[r1])
for r2=1,n2 do
local _,x2,y2,z2=Actor:getPosition(as2[r2])
if math.abs(x1-x2)<1 and math.abs(y1-y2)<1 and math.abs(z1-z2)<1 then
Actor:addBuff(as2[r2],46,1,time1*20)
ps1[as1[r1]]=time2*20
end
end
elseif ps1[as1[r1]]>0 then
ps1[as1[r1]]=ps1[as1[r1]]-1
end
end
end
ScriptSupportEvent:registerEvent('Game.Run',run)
房主看向方块,方块立即被破坏
local uin=0
local function GameStart()
_,uin=Player:getHostUin()
end
ScriptSupportEvent:registerEvent('Game.Start',GameStart)
local function run()
local _,x,y,z=Player:getAimPos(uin)
local _,x1,y1,z1=Actor:getPosition(uin)
y1=y1+1--这行可以根据实际效果调整,测试+1还行
local dx,dy,dz=x-x1,y-y1,z-z1
local r=math.sqrt(dx*dx+dy*dy+dz*dz)
Block:destroyBlock(x+dx/r,y+dy/r,z+dz/r,false)--需要掉落就改成true
end
ScriptSupportEvent:registerEvent('Game.Run',run)
拓展:全部玩家生效:
local function run()
local _,n,as=World:getAllPlayers(-1)
for n=1,#as do
local _,x,y,z=Player:getAimPos(as[n])
local _,x1,y1,z1=Actor:getPosition(as[n])
y1=y1+1
local dx,dy,dz=x-x1,y-y1,z-z1
local r=math.sqrt(dx*dx+dy*dy+dz*dz)
Block:destroyBlock(x+dx/r,y+dy/r,z+dz/r,false)
end
end
ScriptSupportEvent:registerEvent('Game.Run',run)
点击方块,在点击位置上方随机生成方块
local ids={200,201,202}--方块id
local function ClickBlock(e)
local x,y,z=e.x,e.y+1,e.z
local id=ids[math.random(1,#ids)]
Block:setBlockAll(x,y,z,id,1)
end
ScriptSupportEvent:registerEvent([=[Player.ClickBlock]=], ClickBlock)
扩展:随机生成生物:
local ids={3400,3401,3402}--生物id
local function ClickBlock(e)
local x,y,z=e.x,e.y+1,e.z
local id=ids[math.random(1,#ids)]
World:spawnCreature(x,y,z,id,1)
end
ScriptSupportEvent:registerEvent([=[Player.ClickBlock]=], ClickBlock)
扩展:每隔几秒自动在周围随机生成生物
local ids={3400,3401,3402}--生物id
local range,num,time=5,2,3--生成范围,生成数量,间隔秒数
local function RunTime(e)
if e.second%time==0 then
local i,r,n,as=0,World:getAllPlayers(-1)
for r=1,n do
local _,x,y,z=Actor:getPosition(as[r])
for i=1,num do
x=x+math.random(-range,range)
z=z+math.random(-range,range)
local id=ids[math.random(1,#ids)]
World:spawnCreature(x,y,z,id,1)
end
end
end
end
ScriptSupportEvent:registerEvent([=[Game.RunTime]=], RunTime)
二段跳(可扩展为N段跳)
local ps={}
local function msc(e)
if e.playermotion==4 then ps[e.eventobjid]=1
elseif e.playermotion==32 then ps[e.eventobjid]=nil
end
end
ScriptSupportEvent:registerEvent('Player.MotionStateChange', msc)
local function ikd(e)
if e.vkey=="SPACE" and ps[e.eventobjid]==1 then
Actor:appendSpeed(e.eventobjid,0,0.5,0)--修改0.5可以更改第二段跳跃高度
ps[e.eventobjid]=nil
end
end
ScriptSupportEvent:registerEvent('Player.InputKeyDown', ikd)
点击方块可爬墙3秒(配合装备插件)
local ps={}
local function f(e)
local p=e.eventobjid
Backpack:actCreateEquip(p,4099)--自定义装备插件id,要求是靴子,且添加爬墙技能
Player:setAttr(p,10,80)
ps[p]=3*20
end
ScriptSupportEvent:registerEvent([=[Player.ClickBlock]=], f)
local a,t=0,0
local function r()
for a,t in pairs(ps) do
if ps[a]>0 then
ps[a]=ps[a]-1
elseif ps[a]==0 then
Backpack:actDestructEquip(a,3)
Player:setAttr(a,10,10)
ps[a]=nil
end
end
end
ScriptSupportEvent:registerEvent([=[Game.Run]=], r)
点击方块闪现一定距离
function ClickBlock(obj)
local p=obj.eventobjid
Actor:addBuff(p,43,1,10)
local r,x,y,z=Actor:getFaceDirection(p)
r,y=math.sqrt(x*x+z*z),2.5
Actor:appendSpeed(p,x/r*y,0,z/r*y)
end
ScriptSupportEvent:registerEvent([=[Player.ClickBlock]=], ClickBlock)--点击方块
延时执行函数
--新建脚本,粘贴以下内容
--_G.postDelayed(20,getNum,123)
--表示1秒后执行函数getNum(123)
local ts,i,as={},0,{}
_G.postDelayed=function(tick,fun,param)
ts[#ts+1]={["tick"]=tick,["fun"]=fun,["param"]=param}
end
local function run()
for i,as in pairs(ts) do
if as["tick"]>0 then
as["tick"]=as["tick"]-1
else
as["fun"](as["param"])
ts[i]=nil
end
end
end
ScriptSupportEvent:registerEvent('Game.Run',run)
双人游戏,受到的伤害会转移到对方身上
local ps={}
local function GameAnyPlayerEnterGame(e)
ps[#ps+1]=e.eventobjid
end
ScriptSupportEvent:registerEvent([=[Game.AnyPlayer.EnterGame]=], GameAnyPlayerEnterGame)
local function PlayerBeHurt(e)
local p1,p2,n=0,e.eventobjid,e.hurtlv
if p2==ps[1] then p1=ps[2]
else p1=ps[1]
end
local _,ph1=Player:getAttr(p1,2)
local _,ph2=Player:getAttr(p2,2)
Player:setAttr(p1,2,ph1+n)
Player:setAttr(p2,2,ph2-n)
end
ScriptSupportEvent:registerEvent([=[Player.BeHurt]=], PlayerBeHurt)
可改善(避免玩家被秒杀):把玩家生命值循环设置为9999,并隐藏血条;自己创建变量作为玩家的生命值,用自定义UI做自定义血条。
每隔一段时间重置玩家背包物品
local bp1={{1,64},{200,64}}--快捷栏物品id和数量
local bp2={{201,10},{202,20},{203,64}}--背包栏物品id和数量
local retime=15--刷新间隔秒数
local function reBackpack(ps)
local i,j=0,0
for i=1,#ps do
Backpack:clearAllPack(ps[i])
for j=1,#bp1 do
Backpack:setGridItem(ps[i],999+j,bp1[j][1],bp1[j][2],nil)
end
for j=1,#bp2 do
Backpack:setGridItem(ps[i],j-1,bp2[j][1],bp2[j][2],nil)
end
end
end
local function RunTime(e)
if e.second%retime==0 then
local r,n,as=World:getAllPlayers(-1)
reBackpack(as)
end
end
ScriptSupportEvent:registerEvent('Game.RunTime',RunTime)
扩展:每隔一段时间把玩家背包的所有物品随机替换为等数量的其他物品:
local bp={1,200,201,202,203}--随机替换为这些物品
local retime=15--刷新间隔秒数
local function reBackpack(ps)
local i,j,r,id,n=0,0,0,0,0
for i=1,#ps do
for j=1,8 do
r,id,n=Backpack:getGridItemID(ps[i],999+j)
if r==0 and n>0 then Backpack:setGridItem(ps[i],999+j,bp[math.random(1,#bp)],n,nil) end
end
for j=1,30 do
r,id,n=Backpack:getGridItemID(ps[i],j-1)
if r==0 and n>0 then Backpack:setGridItem(ps[i],j-1,bp[math.random(1,#bp)],n,nil) end
end
end
end
local function RunTime(e)
if e.second%retime==0 then
local r,n,as=World:getAllPlayers(-1)
reBackpack(as)
end
end
ScriptSupportEvent:registerEvent('Game.RunTime',RunTime)
传入玩家id和物品id,返回玩家拥有该物品的数量
--传入玩家id和物品id:返回玩家拥有的物品数量
local function getbpnum(p,id)
local r1,n1,as1=Backpack:getItemNumByBackpackBar(p,1,id)
if r1~=0 then n1=0 end
local r2,n2,as2=Backpack:getItemNumByBackpackBar(p,2,id)
if r2~=0 then n2=0 end
local r3,n3,as3=Backpack:getItemNumByBackpackBar(p,3,id)
if r3~=0 then n3=0 end
return n1+n2+n3
end
每隔一段时间清除一层地图,从上往下
local nowh=150--开始高度
local posA,posB={0,0},{50,50}--地图范围
local function runtime(e)
if e.second%15==0 then
local x,y=0,0
for x=posA[1],posB[1] do
for y=posA[2],posB[2] do
Block:destroyBlock(x,nowh,y)
end
end
nowh=nowh-1
end
end
ScriptSupportEvent:registerEvent('Game.RunTime',runtime)
手持指定道具点击指定生物可直接击杀,并掉落物品
local itemid,actorids=1,{3400,3401}--手持道具id,生物id
local function ClickActor(e)
local p,a=e.eventobjid,e.toobjid
local r,id=Player:getCurToolID(p)
if id==itemid then
r,id=Creature:getActorID(a)
for r=1,#actorids do
if id==actorids[r] then
Actor:killSelf(a)
return
end
end
end
end
ScriptSupportEvent:registerEvent('Player.ClickActor',ClickActor)
玩家点击宠物即可驯养跟随,玩家攻击敌人宠物会自动追击
local acs,petids={},{3400,3401}--宠物id
local time_,attackid=3,12054--跟随和追击时间间隔秒数(整数),投掷物id
local function ClickActor(e)
local p,a=e.eventobjid,e.toobjid
if acs[a]~=nil then return end
local r,id=Creature:getActorID(a)
for r=1,#petids do
if id==petids[r] then
acs[a]={p}
local graphicsInfo=Graphics:makeflotageText("#G驯服成功", 20, 1)
Graphics:createflotageTextByActor(a, graphicsInfo, {x=0,y=0,z=0}, 0, 0, 0)
return
end
end
end
ScriptSupportEvent:registerEvent([=[Player.ClickActor]=], ClickActor)
local function AttackHit(e)
local p,a=e.eventobjid,e.toobjid
if acs[a]~=nil and (acs[a][1]==p or acs[a][1]==a) then return end
local i,js=0,{}
for i,js in pairs(acs) do
if p==js[1] then js[2]=a end
end
end
ScriptSupportEvent:registerEvent([=[Player.AttackHit]=],AttackHit)
local function die(e)
local a=e.eventobjid
acs[a]=nil
local i,js=0,{}
for i,js in pairs(acs) do
if js[2]==a then js[2]=nil end
end
end
ScriptSupportEvent:registerEvent([=[Actor.Die]=],die)
local function RunTime(e)
if e.second%time_==0 then
local a,as,x,y,z=0,{},0,0,0
for a,as in pairs(acs) do
if as[2]==nil then
_,x,y,z=Actor:getPosition(as[1])
Actor:tryMoveToPos(a,x,y,z,3)--这个3是跟随移速
else
_,x,y,z=Actor:getPosition(as[2])
local _,x1,y1,z1=Actor:getPosition(a)
Actor:tryMoveToPos(a,x,y,z,3)--这个3是追击移速
World:spawnProjectile(a, attackid, x1, y1+2, z1, x, y, z, 500)--500是投掷物移速
end
end
end
end
ScriptSupportEvent:registerEvent([=[Game.RunTime]=],RunTime)
每隔一段时间区域整体上升一格
local pos1,pos2={0,0,0},{20,6,20}--范围
local count=20--移动次数
local retime=2--间隔秒数
local h=0
local function RunTime(e)
if e.second%retime==0 and h<count then
r,n,as=World:getAllPlayers(-1)
for i=1,n do
r,x,y,z=Actor:getPosition(as[i])
Actor:setPosition(as[i],x,y+1,z)
end
for i=pos1[1],pos2[1] do
for j=pos2[2]+h,pos1[2]+h,-1 do
for k=pos1[3],pos2[3] do
r,id=Block:getBlockID(i,j,k)
if r==0 and id~=0 then
r,data=Block:getBlockData(i,j,k)
Block:setBlockAll(i,j+1,k,id,data)
Block:destroyBlock(i,j,k,false)
end
end
end
end
h=h+1
end
end
ScriptSupportEvent:registerEvent('Game.RunTime',RunTime)
设置玩家的破坏和放置范围
--范围
local d=2
local function PlaceBy(e)
local p,id,x,y,z=e.eventobjid,e.blockid,e.x,e.y,e.z
local r,px,py,pz=Actor:getPosition(p)
if math.abs(x-px)>d or math.abs(y-py)>d or math.abs(z-pz)>d then
Block:destroyBlock(x,y,z,true)
end
end
ScriptSupportEvent:registerEvent('Block.PlaceBy',PlaceBy)
local function Begin(e)
local p,id,x,y,z=e.eventobjid,e.blockid,e.x,e.y,e.z
local r,px,py,pz=Actor:getPosition(p)
if math.abs(x-px)>d or math.abs(y-py)>d or math.abs(z-pz)>d then
Player:setActionAttrState(p,8,false)
end
end
ScriptSupportEvent:registerEvent('Block.Dig.Begin',Begin)
local function Cancel(e)
local p,id,x,y,z=e.eventobjid,e.blockid,e.x,e.y,e.z
Player:setActionAttrState(p,8,true)
end
ScriptSupportEvent:registerEvent('Block.Dig.Cancel',Cancel)
区域方块循环滚动
--区域方块斜向循环滚动
local pos,h={0,0},7--起点,高度
local ax,az=20,20--区域尺寸,单块尺寸整数倍
local sx,sz=4,4--单块尺寸
local mx,mz=1,1--单次移动尺寸
local ret=3--刷新间隔tick
local function start()
--在这里生成初始方块
local ids={ 1127,1128,0,0,1135,
1128,0,0,1135,1127,
0,0,1135,1127,1128,
0,1135,1127,1128,0,
1135,1127,1128,0,0 }
local i1,i2,j1,j2=0,0,0,0
for i1=1,ax/sx do
for i2=1,az/sz do
for j1=1,sx do
for j2=1,sz do
if ids[(i1-1)*az/sz+i2]~=0 then
Block:setBlockAll(pos[1]+(i2-1)*sx+j1-1,h,pos[2]+(i1-1)*sz+j2-1,ids[(i1-1)*az/sz+i2],0)
end
end
end
end
end
end
ScriptSupportEvent:registerEvent('Game.Start',start)
local rti,ri1,ri2,ri3,ri4,rs=0,0,0,0,0,{}
local function run()
if rti==ret then
rs={}
for ri1=1,ax do
for ri2=1,az do
ri3,ri4=Block:getBlockID(pos[1]+ri1-1,h,pos[2]+ri2-1)
if ri3~=0 then ri4=0 end
rs[#rs+1]=ri4
end
end
for ri1=1,ax do
for ri2=1,az do
ri3=rs[(ri1+ax-mx-1)%ax*az+(ri2+az-mz-1)%az+1]
if ri3==0 then Block:destroyBlock(pos[1]+ri1-1,h,pos[2]+ri2-1)
else Block:setBlockAll(pos[1]+ri1-1,h,pos[2]+ri2-1,ri3,0) end
end
end
rti=0
end
rti=rti+1
end
ScriptSupportEvent:registerEvent('Game.Run',run)
每隔一段时间清除方块缩小区域
local pos1,pos2={0,0},{20,20}--范围起点终点
local function runt(e)
if e.second%2==0 and pos2[1]>pos1[1] and pos2[2]>pos1[2] then
for i=0,7 do
for j=pos1[1],pos2[1] do
Block:destroyBlock(pos1[1]+j-pos1[1],i,pos1[2],false)
Block:destroyBlock(pos1[1]+j-pos1[1],i,pos2[2],false)
end
for j=pos1[2],pos2[2] do
Block:destroyBlock(pos1[1],i,pos1[2]+j-pos1[2],false)
Block:destroyBlock(pos2[1],i,pos1[2]+j-pos1[2],false)
end
end
pos1={pos1[1]+1,pos1[2]+1}
pos2={pos2[1]-1,pos2[2]-1}
end
end
ScriptSupportEvent:registerEvent('Game.RunTime',runt)
箭头指向附近正在移动的玩家
--箭头指向附近移动的玩家
local d=5
local ps={}--ps[uin]={motion}
local function pin(e)
ps[e.eventobjid]={0}
end
ScriptSupportEvent:registerEvent('Game.AnyPlayer.EnterGame',pin)
local function pot(e)
ps[e.eventobjid]=nil
end
ScriptSupportEvent:registerEvent('Game.AnyPlayer.LeaveGame',pot)
local function mot(e)
ps[e.eventobjid][1]=e.playermotion
end
ScriptSupportEvent:registerEvent('Player.MotionStateChange',mot)
local r,n,as,i,j,t,t1,x,y,z,x1,y1,z1,info,xd,zd=0,0,{},0,0,0,0,0,0,0,0,0,0,{},0,0
local function run()
r,n,as=World:getAllPlayers(-1)
for i=1,n-1 do
for j=i+1,n do
r,t=Player:getTeam(as[i])
r,t1=Player:getTeam(as[j])
if t~=t1 then
r,x,y,z=Actor:getPosition(as[i])
r,x1,y1,z1=Actor:getPosition(as[j])
Graphics:removeGraphicsByObjID(as[i],as[j],5)
Graphics:removeGraphicsByObjID(as[j],as[i],5)
Graphics:snycGraphicsInfo2Client()
xd=math.abs(x-x1)
zd=math.abs(z-z1)
if xd<d and zd<d then
r=math.sqrt(xd*xd+zd*zd)
if ps[as[i]][1]~=0 then
info=Graphics:makeGraphicsArrowToActor(as[i],0.5,0xff0000,as[i])
Graphics:createGraphicsArrowByActorToActor(as[j],info,{x=(x-x1)/r*5,y=5,z=(z-z1)/r*5},5)
end
if ps[as[j]][1]~=0 then
info=Graphics:makeGraphicsArrowToActor(as[j],0.5,0xff0000,as[j])
Graphics:createGraphicsArrowByActorToActor(as[i],info,{x=(x1-x)/r*5,y=5,z=(z1-z)/r*5},5)
end
end
end
end
end
end
ScriptSupportEvent:registerEvent('Game.Run',run)
玩家上方显示某个对应变量的排名
--排名
local ns={[0]={"红"},{"蓝"},{"绿"},{"黄"},{"橙"},{"紫"}}
local ps={}
local function pin(e)
local p=e.eventobjid
local _,t=Player:getTeam(p)
local info=Graphics:makeGraphicsText("-",15,50,p)
local _,id=Graphics:createGraphicsTxtByActor(p,info,{x=0,y=15,z=0},10,0,0)
ps[p]={t,id}
end
ScriptSupportEvent:registerEvent('Game.AnyPlayer.EnterGame',pin)
local function pot(e)
ps[e.eventobjid]=nil
end
ScriptSupportEvent:registerEvent('Game.AnyPlayer.LeaveGame',pot)
local function getNo(a)
local c,i,as=1,0,{}
for i,as in pairs(ns) do
if as[2]>ns[a][2] then c=c+1 end
end
return c
end
local function run()
for ri,ras in pairs(ns) do
rr,rv=VarLib2:getGlobalVarByName(3,ras[1])
if rr~=0 then rv=0 end
ras[2]=rv
end
for ri,ras in pairs(ps) do
Graphics:updateGraphicsTextById(ras[2],"#B第#G".. getNo(ras[1]) .."#B名",10,50)
Graphics:snycGraphicsInfo2Client()
end
end
ScriptSupportEvent:registerEvent('Game.Run',run)
做表情持续产出掉落一段时间(需要用到延时执行函数)
local function spawn(e)
local r,x,y,z=Actor:getPosition(e.eventobjid)
World:spawnItem(x,y+2,z,1,1)
end
local function PlayAction(e)
if e.act==3 then
local i=0
for i=1,20 do
_G.postDelayed(10*i,spawn,e)
end
end
end
ScriptSupportEvent:registerEvent('Player.PlayAction',PlayAction)