Beginner qn.
i have a tilemap. And i use if loops to draw image/rectangle correspnding the table to render it on screen.
And currently i use a method that calculates the mouse's x-coordinate and dividing it by the individual tile width. The same process is repeated for the y-coordinate to determine the mouse hover on a specific tile.
Code: Select all
local tilemap = {
{1, 2, 3},
{4, 5, 6},
-- Add more rows as needed
}
-- Tile dimensions and tilemap dimensions
local tileWidth = 64
local tileHeight = 64
local numRows = #tilemap
local numCols = #tilemap[1]
-- Variable to store the previously hovered tile
local prevHoveredTile = nil
function love.load()
-- Set window dimensions
love.window.setMode(numCols * tileWidth, numRows * tileHeight)
end
function love.update(dt)
local mouseX, mouseY = love.mouse.getPosition()
local tileX = math.floor(mouseX / tileWidth) + 1
local tileY = math.floor(mouseY / tileHeight) + 1
local newHoveredTile = tilemap[tileY] and tilemap[tileY][tileX]
-- Check if the hover state has changed
if newHoveredTile ~= prevHoveredTile then
if newHoveredTile then
print("Entered tile: " .. newHoveredTile)
else
print("Left the tile")
end(
prevHoveredTile = newHoveredTile
end
end
function love.draw()
-- Draw the tilemap
for y = 1, numRows do
for x = 1, numCols do
love.graphics.rectangle("line", (x - 1) * tileWidth, (y - 1) * tileHeight, tileWidth, tileHeight)
love.graphics.print(tilemap[y][x], x * tileWidth - 20, y * tileHeight - 20)
end
end
end
2 Also is this how major maps and player position on maps work? (hearsay, grid based games)
Gratitude to the supportive community