我正在尝试制作一个基于六边形网格的游戏。我已经有一些基本原理(实施了六角形的邻居,因为图书馆没有原住民,也可以做到它,所以我可以重新涂上每个六角形),但是我似乎不能做一件事:
当我悬停在六角形上时,周围的邻居将更改为线填充模式(图片示例)(假设我的鼠标在左上角六角形上),但是我似乎无法弄清楚如何将它们重新更改为当我停止徘徊时"填充"。
这是相关的代码(负责检查我是否悬停在六角形上并与邻居打交道):
function love.update()
mx, my = love.mouse.getPosition()
if hexGrid:containingHex(mx, my)~=nil then --am I hovering over a hexagon?
local q,r=hexGrid:containingHex(mx, my).q,hexGrid:containingHex(mx, my).r -- get the position on the grid for the hexagon
local neighbors=hexGrid:neighbors(q,r) -- get neighbors of hexagon I'm hovering over
for dir, neighbor in pairs(neighbors) do -- loop through neighbor table
if neighbor ~=nil and hexes.getHexagon(hexGrid:getHex(neighbor.q,neighbor.r))["fill"]=="fill" then -- does neighbor exist and is their fill mode "fill"?
local neighborHexagon=hexGrid:getHex(neighbor.q,neighbor.r) -- get the hexagon from the grid
local neighborData=hexes.getHexagon(neighborHexagon) -- get it's data from a seperate dictionary (stores color data and fill mode data)
neighborData["fill"]="line" -- set fill mode to line
end
end
end
end
有几种方法可以做到这一点,但是最简单的(IMO)将在每个帧的末端重置上一个瓷砖的状态。
更新的循环看起来像这样:
-- Reference to the old tile position
local oldhexagon
function love.update()
mx, my = love.mouse.getPosition()
if hexGrid:containingHex(mx, my)~=nil then --am I hovering over a hexagon?
-- Check if you were previously over a tile
if oldhexagon then
-- Set old hexagon's fill mode to line
end
-- Get rid of the reference for next frame
oldhexagon = nil
-- etc.
oldhexagon = neighborData
end
end