How do I replace isometric tiles

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
jmk
Prole
Posts: 1
Joined: Mon Apr 04, 2022 9:23 am

How do I replace isometric tiles

Post 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
User avatar
BrotSagtMist
Party member
Posts: 657
Joined: Fri Aug 06, 2021 10:30 pm

Re: How do I replace isometric tiles

Post by BrotSagtMist »

Yea but what is your question?
obey
User avatar
dusoft
Party member
Posts: 635
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: How do I replace isometric tiles

Post 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
User avatar
dusoft
Party member
Posts: 635
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: How do I replace isometric tiles

Post by dusoft »

Also, your question belongs to Support forum. Not to General!
Urada
Prole
Posts: 2
Joined: Sat Oct 09, 2021 1:16 pm

Re: How do I replace isometric tiles

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests