制作一款打飞机游戏45:简单攻击
粒子系统修复
首先,我们要加载cow(可能是某个项目或资源),然后直接处理粒子系统。你们看到在粒子系统中,我们仍然有X滚动。这现在已经没什么意义了,因为我们正在使用一个奇怪的新系统。所以我们实际上不再需要这个了。我们可以用为玩家创建的新对象来替代它,即PSPR的X和Y。PSPR是一个功能或特性,它将粒子锁定在玩家的位置上,这样我们就可以将东西附加到玩家上。但我们现在不需要这么做了。顺便说一句,这也可以是PY,这样可以稍微节省一些标记。
编辑器边界问题
在编辑器中,我们可以将敌人移动到负滚动值或当前滚动值之前,这显然是不合理的。我添加了一些检查来确保敌人只能在合理的滚动值范围内生成。
Brain Editor元数据设置
接下来,我们开始为Brain Editor设置元数据。我们的目标是创建一个设置菜单,允许用户为每个Brain设置预览参数,如动画、动画速度、生成位置等。我创建了一个元数据表,并将其导出到一个文件中。然后,我在编辑器中实现了这个设置菜单,允许用户编辑和查看元数据。
pico-8 cartridge // http://www.pico-8.com
version 41
__lua__
--todo
-- - meta brain data
-- - -ui
-- - -create meta on brain create
-- - -delete meta on brain delete
-- - more commandsfunction _init()--- customize here ---#include shmup_brains.txt#include shmup_brains_meta.txtfile="shmup_brains.txt"filem="shmup_brains_meta.txt"arrname="brains"data=brains#include shmup_myspr.txt#include shmup_enlib.txt#include shmup_anilib.txt----------------------debug={}msg={}_drw=draw_brain_upd=update_brainmenuitem(1,"export",export)reload(0x0,0x0,0x2000,"cowshmup.p8")curx=1cury=1scrolly=0scrollx=0selbrain=1cmdlist={"hed","wai"}enemies={}poke(0x5f2d, 1)
endfunction _draw()_drw()if #msg>0 thenbgprint(msg[1].txt,64-#msg[1].txt*2,80,14)msg[1].t-=1if msg[1].t<=0 thendeli(msg,1)end end-- debug --cursor(4,4)color(8)for txt in all(debug) doprint(txt)end
endfunction _update60()dokeys()mscroll=stat(36)_upd()
endfunction dokeys()if stat(30) thenkey=stat(31)if key=="p" thenpoke(0x5f30,1)endelsekey=nilendend
-->8
--drawfunction draw_brain()cls(13)for e in all(enemies) dodrawobj(e)enddrawmenu()
endfunction draw_table()cls(2)--spr(0,0,0,16,16)drawmenu()--[[for i=1,#data dofor j=1,#data[i] dobgprint(data[i][j],2+18*j,2+8*i,7)endend]]
endfunction drawmenu()if menu thenfor i=1,#menu dofor j=1,#menu[i] dolocal mymnu=menu[i][j]local c=mymnu.c or 13if i==cury and j==curx thenc=7if _upd==upd_type thenc=0endendbgprint(mymnu.w,mymnu.x+scrollx,mymnu.y+scrolly,13) bgprint(mymnu.txt,mymnu.x+scrollx,mymnu.y+scrolly,c) endendendif _upd==upd_type thenlocal mymnu=menu[cury][curx]local txt_bef=sub(typetxt,1,typecur-1)local txt_cur=sub(typetxt,typecur,typecur)local txt_aft=sub(typetxt,typecur+1)txt_cur=txt_cur=="" and " " or txt_cur if (time()*2)%1<0.5 thentxt_cur="\^i"..txt_cur.."\^-i"endlocal txt=txt_bef..txt_cur..txt_aftbgprint(txt,mymnu.x+scrollx,mymnu.y+scrolly,7)end
end-->8
--updatefunction update_setup()refresh_setup()if btnp(⬆️) thencury-=1endif btnp(⬇️) thencury+=1endcury=mid(2,cury,#menu)curx=2if btnp(❎) thenlocal mymnu=menu[cury][curx]if mymnu.cmd=="meta" then_upd=upd_typetypetxt=tostr(mymnu.txt)typecur=#typetxt+1callback=enter_metaendendif btnp(🅾️) then_upd=update_brainrefresh_brain()returnendif data[selbrain] thenif #enemies==0 thenlocal selmeta=meta[selbrain]if enlib[selmeta[1]]!=nil thenspawnen(selmeta[1],selmeta[2],selmeta[3])end enddoenemies()elseenemies={}endendfunction update_brain()refresh_brain()if btnp(⬆️) thencury-=1endif btnp(⬇️) thencury+=1endcury=mid(1,cury,#menu)if cury==1 thenif btnp(⬅️) thenselbrain-=1enemies={}endif btnp(➡️) thenselbrain+=1enemies={}endselbrain=mid(1,selbrain,#data+1)elseif btnp(⬅️) thencurx-=1endif btnp(➡️) thencurx+=1endcurx=mid(1,curx,#menu[cury])endif btnp(❎) thenlocal mymnu=menu[cury][curx]if mymnu.cmd=="edit" then_upd=upd_typetypetxt=tostr(mymnu.txt)typecur=#typetxt+1callback=enter_brainelseif mymnu.cmd=="newline" thenadd(data[mymnu.cmdb],"wai")add(data[mymnu.cmdb],"0")add(data[mymnu.cmdb],"0") elseif mymnu.cmd=="setup" thenrefresh_setup()_upd=update_setupreturnelseif mymnu.cmd=="newbrain" thenadd(data,{"wai",0,0})endreturn endif data[selbrain] thenif #enemies==0 thenlocal selmeta=meta[selbrain]if enlib[selmeta[1]]!=nil thenspawnen(selmeta[1],selmeta[2],selmeta[3])endenddoenemies()elseenemies={}end
endfunction update_table()refresh_table()if btnp(⬆️) thencury-=1endif btnp(⬇️) thencury+=1endcury=(cury-1)%#menu+1cury-=mscrollcury=mid(1,cury,#menu)if btnp(⬅️) thencurx-=1endif btnp(➡️) thencurx+=1endif cury<#menu thencurx=(curx-2)%(#menu[cury]-1)+2elsecurx=1endlocal mymnu=menu[cury][curx]if mymnu.y+scrolly>110 thenscrolly-=4endif mymnu.y+scrolly<10 thenscrolly+=4endscrolly=min(0,scrolly)if mymnu.x+scrollx>110 thenscrollx-=2endif mymnu.x+scrollx<20 thenscrollx+=2endscrollx=min(0,scrollx)if btnp(❎) thenlocal mymnu=menu[cury][curx]if mymnu.cmd=="edit" then_upd=upd_typetypetxt=tostr(mymnu.txt)typecur=#typetxt+1callback=enter_tableelseif mymnu.cmd=="newline" thenadd(data,{0}) elseif mymnu.cmd=="newcell" thenadd(data[mymnu.cmdy],0)endend
endfunction upd_type()if key thenif key=="\r" then-- enterpoke(0x5f30,1)callback()returnelseif key=="\b" then--backspaceif typecur>1 thenif typecur>#typetxt thentypetxt=sub(typetxt,1,#typetxt-1)elselocal txt_bef=sub(typetxt,1,typecur-2)local txt_aft=sub(typetxt,typecur)typetxt=txt_bef..txt_aftendtypecur-=1endelseif typecur>#typetxt thentypetxt..=keyelselocal txt_bef=sub(typetxt,1,typecur-1)local txt_aft=sub(typetxt,typecur)typetxt=txt_bef..key..txt_aftendtypecur+=1endendif btnp(⬅️) thentypecur-=1endif btnp(➡️) thentypecur+=1endtypecur=mid(1,typecur,#typetxt+1)
end
-->8
--toolsfunction bgprint(txt,x,y,c)print("\#0"..txt,x,y,c)
endfunction split2d(s)local arr=split(s,"|",false)for k, v in pairs(arr) doarr[k] = split(v)endreturn arr
endfunction spacejam(n)local ret=""for i=1,n doret..=" "endreturn ret
endfunction mspr(si,sx,sy)local _x,_y,_w,_h,_ox,_oy,_fx,_nx=unpack(myspr[si])sspr(_x,_y,_w,_h,sx-_ox,sy-_oy,_w,_h,_fx==1)if _fx==2 thensspr(_x,_y,_w,_h,sx-_ox+_w,sy-_oy,_w,_h,true)endif _nx thenmspr(_nx,sx,sy)end
endfunction cyc(age,arr,anis)local anis=anis or 1return arr[(age\anis-1)%#arr+1]
endfunction drawobj(obj)mspr(cyc(obj.age,obj.ani,obj.anis),obj.x,obj.y)--★if coldebug and obj.col thenmsprc(obj.col,obj.x,obj.y)end
endfunction onscreen(obj)if obj.x<-8 then return false endif obj.y<-8 then return false endif obj.x>136 then return false endif obj.y>136 then return false end return true
end
-->8
--i/o
function export()local s=arrname.."=split2d\""for i=1,#data doif i>1 thens..="|"endfor j=1,#data[i] doif j>1 thens..=","ends..=data[i][j]endends..="\""printh(s,file,true)local s="meta=split2d\""for i=1,#meta doif i>1 thens..="|"endfor j=1,#meta[i] doif j>1 thens..=","ends..=meta[i][j]endends..="\""printh(s,filem,true)add(msg,{txt="exported!",t=120})--debug[1]="exported!"
end
-->8
--uifunction refresh_setup()menu={}add(menu,{{txt="brain "..selbrain,w=" ",cmd="",x=3,y=3,c=13 }})local cap={"en:"," x:"," y:"}local selmeta=meta[selbrain]for i=1,3 dolocal lne={}add(lne,{txt=cap[i],w=" ",cmd="",x=3,y=3+i*6+2,c=13 })add(lne,{txt=selmeta[i],w=" ",cmd="meta",cmdy=i,x=3+12,y=3+i*6+2,c=13 })add(menu,lne)endendfunction refresh_brain()menu={}if selbrain>#data then--empty brain slotadd(menu,{{txt="< new brain ",w=" ",cmd="newbrain",x=3,y=3,c=13 }}) returnendadd(menu,{{txt="< brain "..selbrain.." >",w=" ",cmd="head",x=3,y=3,c=13 }}) add(menu,{{txt="◆setup",w=" ",cmd="setup",x=3,y=3+8,c=13 }})local mybra=brains[selbrain]local ly=19for i=1,#mybra,3 dolocal lne={}add(lne,{txt=mybra[i],w=" ",cmd="edit",cmdi=i,cmdb=selbrain,x=3,y=ly,c=13 })local lx=3+14 for j=1,2 dolocal mytxt=tostr(mybra[i+j])add(lne,{txt=mytxt,w=spacejam(#mytxt),cmd="edit",cmdi=i+j,cmdb=selbrain,x=lx,y=ly,c=13 })lx+=#mytxt*4+2endadd(menu,lne)ly+=8endadd(menu,{{txt="+",w=" ",cmd="newline",cmdb=selbrain,x=3,y=ly,c=13 }})endfunction refresh_table()menu={}for i=1,#data dolocal lne={}local linemax=#data[i]if i==cury thenlinemax+=1 endadd(lne,{txt=i,w=" ",cmd="",x=4,y=-4+8*i,c=2 })for j=1,linemax doif j==#data[i]+1 thenadd(lne,{txt="+",w=" ",cmd="newcell",cmdy=i,x=-10+14*(j+1),y=-4+8*i, })elseadd(lne,{txt=data[i][j],cmd="edit",cmdx=j,cmdy=i,x=-10+14*(j+1),y=-4+8*i,w=" "})endendadd(menu,lne)endadd(menu,{{txt=" + ",w=" ",cmd="newline",x=4,y=-4+8*(#data+1), }})
endfunction enter_table()local mymnu=menu[cury][curx]local typeval=typetxtif typeval==nil or typeval=="" thenif mymnu.cmdx==#data[mymnu.cmdy] and typetxt=="" then--delete celldeli(data[mymnu.cmdy],mymnu.cmdx)if mymnu.cmdx==1 thendeli(data,mymnu.cmdy)end_upd=update_tablereturnend typeval=0end data[mymnu.cmdy][mymnu.cmdx]=typeval_upd=update_table
endfunction enter_brain()_upd=update_brainlocal mymnu=menu[cury][curx]local typeval=typetxtenemies={}if mymnu.cmdi%3==1 then--editing command entryif typeval=="" thendeli(data[mymnu.cmdb],mymnu.cmdi)deli(data[mymnu.cmdb],mymnu.cmdi)deli(data[mymnu.cmdb],mymnu.cmdi)if #data[mymnu.cmdb]==0 thendeli(data,mymnu.cmdb)add(msg,{txt="brain deleted!",t=120})endreturnelse local found=false for c in all(cmdlist) doif typeval==c thenfound=trueend endif not found thentypeval="wai"endendelse--editing parameterstypeval=tonum(typetxt)if typeval==nil thentypeval=0endenddata[mymnu.cmdb][mymnu.cmdi]=typevalendfunction enter_meta()_upd=update_setuplocal mymnu=menu[cury][curx]local typeval=tonum(typetxt)enemies={}if typeval==nil thentypeval=0endmeta[selbrain][mymnu.cmdy]=typevalend
-->8
--enemyfunction dobrain(e)local mybra=brains[e.brain]local quit=falseif e.bri<#mybra thenlocal cmd=mybra[e.bri]local par1=mybra[e.bri+1]local par2=mybra[e.bri+2]if cmd=="hed" then--set heading / speede.ang=par1e.spd=par2 elseif cmd=="wai" then--wait x framese.wait=par1quit=trueende.bri+=3if quit then return enddobrain(e)end
endfunction doenemies()for e in all(enemies) doif e.wait>0 thene.wait-=1elsedobrain(e)ende.sx=sin(e.ang)*e.spde.sy=cos(e.ang)*e.spde.x+=e.sxe.y+=e.sye.age+=1if not onscreen(e) thendel(enemies,e)endendendfunction spawnen(eni,enx,eny)local en=enlib[eni]add(enemies,{x=enx,y=eny,ani=anilib[en[1]],anis=en[2],sx=0,sy=0,ang=0,spd=0,brain=selbrain,bri=1,age=0,flash=0,hp=en[4],col=en[5],wait=0})end
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__map__
0000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000