0

I'm trying to make a hexagon grid-based game. I've got some of the fundamentals down (implemented getting neighbors of a hexagon since the library didn't natively have it and also made it so I can recolor each individual hexagon), but I can't seem to do one thing:

When I hover over a hexagon, the neighbors surrounding it get changed to line fill mode (picture example) (assume my mouse is over the top left hexagon), but I can't seem to figure out how to change them back to 'fill' when I've stopped hovering over it.

Here's the relevant code (handles checking if I'm hovering over a hexagon and deals with the neighbors):

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
Ducktor
  • 337
  • 1
  • 9
  • 27

1 Answers1

0

There are a couple of ways that you could go about doing this, but the easiest (IMO) would be reset the previous tile's state at the end of every frame.

An updated loop would look something like this:

-- 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
DavisDude
  • 902
  • 12
  • 22
  • Not sure if you misunderstood my problem. The one I'm hovering over never changes (as shown in the example pic), only it's neighbors. I want a hexagon's **neighbors** to revert back to fill when I've stopped hovering over it. – Ducktor Oct 03 '17 at 21:11
  • @Ducktor So, to clarify, you want the hexagons to have fill mode unless the the mouse is over it, then it should be line? – DavisDude Oct 03 '17 at 21:44