添加转盘模块和实验性每日转盘小程序

This commit is contained in:
MrZ626
2021-11-15 15:47:05 +08:00
parent fa0bc3805f
commit ed45bebfa0
3 changed files with 165 additions and 0 deletions

View File

@@ -616,6 +616,11 @@ local commands={}do
scene='app_piano',
description="A simple keyboard piano"
},
{
code="spin",
scene='app_spin',
description="???"
},
}
commands.app={
code=function(name)

48
parts/scenes/app_spin.lua Normal file
View File

@@ -0,0 +1,48 @@
local spinner=require'parts/spinner'.new({
{font=45,disp=CHAR.icon.retry_spin,item='zTicket',amount=1,freq=30},
{font=60,disp=CHAR.mino.Z,item='Zfrag',amount=1,freq=10},
{font=60,disp=CHAR.mino.S,item='Sfrag',amount=1,freq=10},
{font=60,disp=CHAR.mino.J,item='Jfrag',amount=1,freq=10},
{font=60,disp=CHAR.mino.L,item='Lfrag',amount=1,freq=10},
{font=60,disp=CHAR.mino.T,item='Tfrag',amount=1,freq=10},
{font=60,disp=CHAR.mino.O,item='Ofrag',amount=1,freq=10},
{font=60,disp=CHAR.mino.I,item='Ifrag',amount=1,freq=10},
},function(item,amount)
getItem(item,amount)
saveStats()
end)
local scene={}
function scene.keyDown(key,isRep)
if isRep then return end
if key=='space'or key=='return'then
if STAT.item.zTicket>0 then
if spinner:start()then
STAT.item.zTicket=STAT.item.zTicket-1
saveStats()
end
else
MES.new('info',"Not enough zTicket")
end
elseif key=='escape'then
SCN.back()
end
end
function scene.update(dt)
spinner:update(dt)
end
function scene.draw()
setFont(40)
love.graphics.print("zTickets: "..STAT.item.zTicket,90,80)
spinner:draw(640,360)
end
scene.widgetList={
WIDGET.newButton{name="spin", x=1140,y=360,w=120,font=60,fText=CHAR.icon.retry_spin,code=pressKey"space"},
WIDGET.newButton{name="back", x=1140,y=640,w=170,h=80,font=60,fText=CHAR.icon.back,code=backScene},
}
return scene

112
parts/spinner.lua Normal file
View File

@@ -0,0 +1,112 @@
local gc=love.graphics
local Spinner={
data={},
totalFreq=0,
spinning=false,
angle=0,
angV=0,
chosen=false,
light=0,
}
Spinner.__index=Spinner
function Spinner:start()
if not self.spinning then
self.spinning=true
self.angle=MATH.tau*math.random()
self.angV=.62+.26*math.random()
return true
end
end
function Spinner:update(dt)
if self.spinning then
self.angle=(self.angle+self.angV*dt*60)%MATH.tau
local dV=
self.angV>.26 and .0026 or
self.angV>.12 and .0012 or
self.angV>.04 and .0004 or
.0001
self.angV=self.angV-dV*dt*60
if self.angV<=0 then
self.angV=0
self.spinning=false
local freq=self.angle%MATH.tau/MATH.tau*self.totalFreq
for i=1,#self.data do
local D=self.data[i]
freq=freq-D.freq
if freq<=0 then
self.chosen=D
self.light=1
if D.item then
self.hook_stop(D.item,D.amount)
end
break
end
end
end
else
if self.light>0 then
self.light=self.light-dt
end
end
end
function Spinner:draw(x,y)
gc.push('transform')
gc.translate(x,y)
--Draw circle
gc.setColor(1,1,1)
gc.setLineWidth(3)
gc.circle('line',0,0,300)
--Draw areas
gc.setLineWidth(1)
local freq=0
for i=1,#self.data do
gc.line(10,0,290,0)
local D=self.data[i]
freq=freq+D.freq
if D==self.chosen then
gc.setColor(1,1,1,.5*self.light)
gc.arc('fill','pie',0,0,300,0,-D.freq*MATH.tau/self.totalFreq)
gc.setColor(1,1,1)
end
local dAng=-MATH.tau*D.freq/self.totalFreq
gc.rotate(dAng*.5)
if D.amount>0 then
setFont(D.font)
if D.amount==1 then
mStr(D.disp,170,-D.font*.7)
else
mStr(D.disp.."x"..D.amount,170,-D.font*.7)
end
end
gc.rotate(dAng*.5)
end
gc.pop()
--Draw target pin
gc.setLineWidth(2)
gc.setColor(1,.3,.3)
gc.line(x,y,x+200*math.cos(self.angle),y-200*math.sin(self.angle))
end
function Spinner.new(data,hook_stop)
local self={}
setmetatable(self,Spinner)
local sum=0
for i=1,#data do
sum=sum+data[i].freq
if not data[i].font then data[i].font=30 end
end
self.data=data
self.totalFreq=sum
self.hook_stop=hook_stop
return self
end
return Spinner