模式地图坐标机制重写,动画升级

This commit is contained in:
MrZ626
2020-11-13 20:45:11 +08:00
parent 621d753a5f
commit 7bd4580b81

View File

@@ -1,16 +1,16 @@
local mt=love.math
local gc=love.graphics local gc=love.graphics
local ms,kb,tc=love.mouse,love.keyboard,love.touch local ms,kb,tc=love.mouse,love.keyboard,love.touch
local Timer=love.timer.getTime local Timer=love.timer.getTime
local max,min=math.max,math.min
local int,abs=math.floor,math.abs local int,abs=math.floor,math.abs
local sin=math.sin local sin=math.sin
mapCam={ local mapCam={
sel=nil,--Selected mode ID sel=nil,--Selected mode ID
--Basic paragrams xOy=mt.newTransform(0,0,0,1),
x=0,y=0,k=1,--Camera pos/k
x1=0,y1=0,k1=1,--Camera pos/k shown
--If controlling with key --If controlling with key
keyCtrl=false, keyCtrl=false,
@@ -19,24 +19,25 @@ mapCam={
zoomMethod=nil, zoomMethod=nil,
zoomK=nil, zoomK=nil,
} }
local mapCam=mapCam
local touchDist=nil local touchDist=nil
function sceneInit.mode(org) function sceneInit.mode(org)
BG.set("space") BG.set("space")
destroyPlayers() destroyPlayers()
local cam=mapCam local cam=mapCam
cam.zoomK=org=="main"and 5 or 1 cam.zoomK=org=="main"and 5 or 1
if cam.sel then end
local M=MODES[cam.sel]
cam.x,cam.y=M.x*cam.k+180,M.y*cam.k local function getK()
cam.x1,cam.y1=cam.x,cam.y return abs(mapCam.xOy:transformPoint(1,0)-mapCam.xOy:transformPoint(0,0))
end end
local function getPos()
return mapCam.xOy:inverseTransformPoint(0,0)
end end
local function onMode(x,y) local function onMode(x,y)
local cam=mapCam x,y=x-640,y-360
x=(cam.x1-640+x)/cam.k1 x,y=mapCam.xOy:inverseTransformPoint(x,y)
y=(cam.y1-360+y)/cam.k1
for name,M in next,MODES do for name,M in next,MODES do
if RANKS[name]and M.x then if RANKS[name]and M.x then
local s=M.size local s=M.size
@@ -50,52 +51,46 @@ local function onMode(x,y)
end end
end end
end end
function wheelMoved.mode(_,y) local function moveMap(dx,dy)
local cam=mapCam local k=getK()
local t=cam.k local x,y=getPos()
local k=t+y*.1 if x>1300 and dx<0 or x<-1200 and dx>0 then dx=0 end
if k>1.5 then k=1.5 if y>420 and dy<0 or y<-1900 and dy>0 then dy=0 end
elseif k<.3 then k=.3 mapCam.xOy:translate(dx/k,dy/k)
end end
t=k/t function wheelMoved.mode(_,dy)
if cam.sel then mapCam.keyCtrl=false
cam.x=(cam.x-180)*t+180 local k=getK()
cam.y=cam.y*t k=min(max(k+dy*.1,.3),1.6)/k
else mapCam.xOy:scale(k)
cam.x=cam.x*t
cam.y=cam.y*t local x,y=getPos()
end mapCam.xOy:translate(x*(1-k),y*(1-k))
cam.k=k
cam.keyCtrl=false
end end
function mouseMove.mode(_,_,dx,dy) function mouseMove.mode(_,_,dx,dy)
if ms.isDown(1)then if ms.isDown(1)then
mapCam.x,mapCam.y=mapCam.x-dx,mapCam.y-dy moveMap(dx,dy)
end end
mapCam.keyCtrl=false mapCam.keyCtrl=false
end end
function mouseClick.mode(x,y) function mouseClick.mode(x,y)
local cam=mapCam local _=mapCam.sel
local _=cam.sel
if not _ or x<920 then if not _ or x<920 then
local SEL=onMode(x,y) local SEL=onMode(x,y)
if _~=SEL then if _~=SEL then
if SEL then if SEL then
cam.moving=true mapCam.moving=true
_=MODES[SEL] _=MODES[SEL]
cam.x=_.x*cam.k+180 mapCam.sel=SEL
cam.y=_.y*cam.k
cam.sel=SEL
SFX.play("click") SFX.play("click")
else else
cam.sel=nil mapCam.sel=nil
cam.x=cam.x-180
end end
elseif _ then elseif _ then
keyDown.mode("return") keyDown.mode("return")
end end
end end
cam.keyCtrl=false mapCam.keyCtrl=false
end end
function touchDown.mode() function touchDown.mode()
touchDist=nil touchDist=nil
@@ -103,7 +98,7 @@ end
function touchMove.mode(_,x,y,dx,dy) function touchMove.mode(_,x,y,dx,dy)
local L=tc.getTouches() local L=tc.getTouches()
if not L[2]then if not L[2]then
mapCam.x,mapCam.y=mapCam.x-dx,mapCam.y-dy moveMap(dx,dy)
elseif not L[3]then elseif not L[3]then
x,y=SCR.xOy:inverseTransformPoint(tc.getPosition(L[1])) x,y=SCR.xOy:inverseTransformPoint(tc.getPosition(L[1]))
dx,dy=SCR.xOy:inverseTransformPoint(tc.getPosition(L[2]))--Not delta!!! dx,dy=SCR.xOy:inverseTransformPoint(tc.getPosition(L[2]))--Not delta!!!
@@ -138,88 +133,70 @@ function keyDown.mode(key)
end end
function Tmr.mode() function Tmr.mode()
local cam=mapCam local dx,dy=0,0
local x,y,k=cam.x,cam.y,cam.k
local F local F
if not SCN.swapping then if not SCN.swapping then
if kb.isDown("up", "w")then y=y-10*k F=true end if kb.isDown("up", "w")then dy=dy+10 F=true end
if kb.isDown("down","s")then y=y+10*k F=true end if kb.isDown("down","s")then dy=dy-10 F=true end
if kb.isDown("left","a")then x=x-10*k F=true end if kb.isDown("left","a")then dx=dx+10 F=true end
if kb.isDown("right","d")then x=x+10*k F=true end if kb.isDown("right","d")then dx=dx-10 F=true end
local js1=joysticks[1] local js1=joysticks[1]
if js1 then if js1 then
local dir=js1:getAxis(1) local dir=js1:getAxis(1)
if dir~="c"then if dir~="c"then
if dir=="u"or dir=="ul"or dir=="ur"then y=y-10*k F=true end if dir=="u"or dir=="ul"or dir=="ur"then dy=dy+10 F=true end
if dir=="d"or dir=="dl"or dir=="dl"then y=y+10*k F=true end if dir=="d"or dir=="dl"or dir=="dl"then dy=dy-10 F=true end
if dir=="l"or dir=="ul"or dir=="dl"then x=x-10*k F=true end if dir=="l"or dir=="ul"or dir=="dl"then dx=dx+10 F=true end
if dir=="r"or dir=="ur"or dir=="dr"then x=x+10*k F=true end if dir=="r"or dir=="ur"or dir=="dr"then dx=dx-10 F=true end
end end
end end
end end
if F or cam.keyCtrl and(x-cam.x1)^2+(y-cam.y1)^2>2.6 then
if F then if F then
cam.keyCtrl=true mapCam.keyCtrl=true
end moveMap(dx,dy)
local x1,y1=(cam.x1-180)/cam.k1,cam.y1/cam.k1 local x,y=getPos()
for name,M in next,MODES do for name,M in next,MODES do
if RANKS[name]and M.x then if RANKS[name]and M.x then
local SEL local SEL
local s=M.size local s=M.size
if M.shape==1 then if M.shape==1 then
if x1>M.x-s and x1<M.x+s and y1>M.y-s and y1<M.y+s then SEL=name end if x>M.x-s and x<M.x+s and y>M.y-s and y<M.y+s then SEL=name end
elseif M.shape==2 then elseif M.shape==2 then
if abs(x1-M.x)+abs(y1-M.y)<s then SEL=name end if abs(x-M.x)+abs(y-M.y)<s then SEL=name end
elseif M.shape==3 then elseif M.shape==3 then
if(x1-M.x)^2+(y1-M.y)^2<s^2 then SEL=name end if(x-M.x)^2+(y-M.y)^2<s^2 then SEL=name end
end end
if SEL and cam.sel~=SEL then if SEL and mapCam.sel~=SEL then
cam.sel=SEL mapCam.sel=SEL
SFX.play("click") SFX.play("click")
end end
end end
end end
end end
if x>1850*k then x=1850*k
elseif x<-1000*k then x=-1000*k
end
if y>500*k then y=500*k
elseif y<-1900*k then y=-1900*k
end
cam.x,cam.y=x,y
--Keyboard controlling
cam.x1=cam.x1*.85+x*.15
cam.y1=cam.y1*.85+y*.15
cam.k1=cam.k1*.85+k*.15
local _=SCN.stat.tar local _=SCN.stat.tar
cam.zoomMethod=_=="play"and 1 or _=="mode"and 2 mapCam.zoomMethod=_=="play"and 1 or _=="mode"and 2
if cam.zoomMethod==1 then if mapCam.zoomMethod==1 then
if cam.sel then _=mapCam.zoomK
local M=MODES[cam.sel]
cam.x=cam.x*.8+M.x*cam.k*.2
cam.y=cam.y*.8+M.y*cam.k*.2
end
_=cam.zoomK
if _<.8 then _=_*1.05 end if _<.8 then _=_*1.05 end
if _<1.1 then _=_*1.05 end if _<1.1 then _=_*1.05 end
cam.zoomK=_*1.05 mapCam.zoomK=_*1.05
elseif cam.zoomMethod==2 then elseif mapCam.zoomMethod==2 then
cam.zoomK=cam.zoomK^.9 mapCam.zoomK=mapCam.zoomK^.9
end end
end end
function Pnt.mode() function Pnt.mode()
local _ local _
local cam=mapCam
gc.push("transform") gc.push("transform")
gc.translate(640,360) gc.translate(640,360)
gc.scale(cam.zoomK) gc.scale(mapCam.zoomK)
gc.translate(-cam.x1,-cam.y1) gc.rotate((mapCam.zoomK^.8-1)*.6)
gc.scale(cam.k1) gc.shear((mapCam.zoomK-1)*.0626,0)
gc.applyTransform(mapCam.xOy);
local R=RANKS local R=RANKS
local sel=cam.sel local sel=mapCam.sel
--Draw lines connecting modes --Draw lines connecting modes
gc.setLineWidth(8) gc.setLineWidth(8)
@@ -236,17 +213,15 @@ function Pnt.mode()
setFont(60) setFont(60)
for name,M in next,MODES do for name,M in next,MODES do
if R[name]then if R[name]then
local S=M.size
local d=((M.x-(cam.x1+(sel and -180 or 0))/cam.k1)^2+(M.y-cam.y1/cam.k1)^2)^.55
if d<500 then S=S*(1.25-d*0.0005) end
local c=rankColor[R[M.name]] local c=rankColor[R[M.name]]
if c then if c then
gc.setColor(c) gc.setColor(c)
else else
c=.5+sin(Timer()*6.26)*.2 c=.5+sin(Timer()*6.26)*.2
S=S*(.9+c*.4)
gc.setColor(c,c,c) gc.setColor(c,c,c)
end end
local S=M.size
if M.shape==1 then--Rectangle if M.shape==1 then--Rectangle
gc.rectangle("fill",M.x-S,M.y-S,2*S,2*S) gc.rectangle("fill",M.x-S,M.y-S,2*S,2*S)
if sel==name then if sel==name then
@@ -291,6 +266,7 @@ function Pnt.mode()
end end
end end
gc.pop() gc.pop()
if sel then if sel then
local M=MODES[sel] local M=MODES[sel]
gc.setColor(.7,.7,.7,.5) gc.setColor(.7,.7,.7,.5)
@@ -329,13 +305,13 @@ function Pnt.mode()
end end
end end
end end
if cam.keyCtrl then if mapCam.keyCtrl then
gc.setColor(1,1,1) gc.setColor(1,1,1)
gc.draw(TEXTURE.mapCross,460-20,360-20) gc.draw(TEXTURE.mapCross,640-20,360-20)
end end
end end
WIDGET.init("mode",{ WIDGET.init("mode",{
WIDGET.newButton({name="start", x=1040, y=655,w=180,h=80, font=40,code=WIDGET.lnk.pressKey("return"),hide=function()return not mapCam.sel end}), WIDGET.newButton({name="start", x=1040, y=655,w=180,h=80, font=40,code=WIDGET.lnk_pressKey("return"),hide=function()return not mapCam.sel end}),
WIDGET.newButton({name="back", x=1200, y=655,w=120,h=80, font=40,code=WIDGET.lnk.BACK}), WIDGET.newButton({name="back", x=1200, y=655,w=120,h=80, font=40,code=WIDGET.lnk_BACK}),
}) })