Page 1 of 1

How do I replace isometric tiles

Posted: Mon Apr 04, 2022 9:32 am
by jmk
Hello, I've been working on some isometric game

i want to make some tiles will replace if the mouse click on it
here's the code;

Code: Select all

function love.load()
  love.graphics.setDefaultFilter("nearest", "nearest")

  require 'src.zooming'

  camera = require 'lib.camera'
  cam = camera()

  x = 350
  y = 200

  my = love.mouse.getY()
  mx = love.mouse.getX()


  -- Load image in love.load()
  tileHeight = 32
  tileWidth = 32
  tilesheet = love.graphics.newImage('tilesheets.png')
  tiles = fromImageToQuads(tilesheet, tileWidth, tileHeight)
  -- Draw the map from top point 200, 100
  mapX, mapY = 350, 200 -- Used in love.draw()
  map = { -- The map to draw
    {1, 2, 1},
    {4, 3, 4},
    {1, 2, 1},
  }
end
  
function love.update(dt)


  if love.keyboard.isDown("left") then
    x = x - 1

  end

  if love.keyboard.isDown("right") then
    x = x + 1

  end

  if love.keyboard.isDown("up") then
    y = y - 1

  end

  if love.keyboard.isDown("down") then
    y = y + 1
    

  end

  cam:lookAt(x, y)
  cam:zoomTo(zooms)
  
end


-- Where tilesheet is an image and tilewidth is the width
-- and height of your textures (32x32 in this case)
function fromImageToQuads(tilesheet, tileWidth, tileHeight)
  local tiles = {} -- A table containing the quads to return
  local imageWidth = tilesheet:getWidth()
  local imageHeight = tilesheet:getHeight()
  -- Loop trough the image and extract the quads
  for i = 0, imageHeight - 1, tileHeight do
    for j = 0, imageWidth - 1, tileWidth do
      table.insert(tiles, love.graphics.newQuad( i, j, tileWidth, tileHeight, imageWidth, imageHeight ) )
    end
  end
  -- Return the table of quads
  return tiles
end

function love.mousepressed(x, y, button)

end

function love.draw()
  cam:attach()
  for i = 1, #map do -- Loop trough rows
    for j = 1, #map[i] do -- Loop through cols in the rows
      if map[i][j] ~= 0 then -- If there is a tile to draw

        local x =
          mapX -- Starting point
          + (j * (tileWidth / 2)) -- The width on rows
          - (i * (tileWidth / 2)) -- The width on cols
        local y =
          mapY
          + (i * (tileHeight / 4)) -- The height on rows
          + (j * (tileHeight / 4)) -- The width on cols
        -- Draw the tiles

        love.graphics.draw(tilesheet, tiles[map[i][j]], x, y)
      end
    end
  end

  cam:detach()

  love.graphics.rectangle("fill", x, y, 0, 0)

end

Re: How do I replace isometric tiles

Posted: Mon Apr 04, 2022 5:45 pm
by BrotSagtMist
Yea but what is your question?

Re: How do I replace isometric tiles

Posted: Mon Apr 04, 2022 7:50 pm
by dusoft
You should overwrite the array / structure:

Code: Select all

map[i][j]
with the new tile type.
Use

Code: Select all

love.mousepressed
to detect clicks:
https://love2d.org/wiki/love.mousepressed

Re: How do I replace isometric tiles

Posted: Mon Apr 04, 2022 7:50 pm
by dusoft
Also, your question belongs to Support forum. Not to General!

Re: How do I replace isometric tiles

Posted: Fri Apr 22, 2022 2:21 am
by Urada
Maybe you want to convert your mouse coord to tile coord? you can use matrices to convert, there are some great videos on youtube.