Custom map loading technique

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
bobbymcbobface
Prole
Posts: 20
Joined: Tue Jun 04, 2019 8:31 pm

Re: Custom map loading technique

Post by bobbymcbobface » Sun Jul 14, 2019 8:31 am

sorry i didn't manage to get back to you so quickly sphyrth I'm been a bit busy

anyway i tried your code and it works perfectly thank you

ok next challenge, i need the character to move and not the map and then when he hits the boundaries I've set using this: https://github.com/adnzzzzZ/STALKER-X (topdown boundaries have been set with a lerp of 0.2 and a lead of 0)
the map will move and not the character, although i have made this partially work the map doesn't generate and black lines have re appeared again can anyone help me - thank you

edit: oh yeah here's the code

sprite.lua

Code: Select all

local themap = require 'map'

local sprite = {}
local allowmove = false
local player = {
        x = 400,
        y = 250,
        size = 25,
        image = love.graphics.newImage("sprites/jumphero1.png")
    }

function sprite:move_false()
    allowmove = false
end

function sprite:move_true()
    allowmove = true
end

function sprite:playery()
    return player.y
end

function sprite:playerx()
    return player.x
end

function sprite:load()
    Camera = require 'camera'
    camera = Camera()
    camera:setFollowLerp(0.2)
    camera:setFollowLead(0)
    camera:setFollowStyle('TOPDOWN')
    
    love.mouse.setCursor(love.mouse.getSystemCursor("crosshair"))
    
    TTimage = love.graphics.newImage("sprites/guy1.png")
    Bimage = love.graphics.newImage("Background.png")

    coins = {}
 
end

function sprite:update(dt)
    camera:follow(player.x, player.y, -0, 1, 1, player.image:getWidth()/2, player.image:getHeight()/2)
    camera:update(dt)
    
    if allowmove == true then
        if love.keyboard.isDown("s") then
            camera:shake(2, 1, 100)
        end
        
        if love.keyboard.isDown("left") then
            player.x = player.x - 80 * dt
           -- themap:moveMap(90  * dt, 0)
        elseif love.keyboard.isDown("right") then
            player.x = player.x + 80 * dt
           -- themap:moveMap(-90 * dt, 0)
        end
    
        if love.keyboard.isDown("up") then
            player.y = player.y - 80 * dt
           -- themap:moveMap(0,  20  * dt)
        elseif love.keyboard.isDown("down") then
            player.y = player.y + 80 * dt
            --themap:moveMap(0, -20  * dt)
        end
end

function sprite:draw()
    camera:attach()
    
    love.graphics.draw(tilesetBatch)
    love.graphics.print("FPS: "..love.timer.getFPS(), 10, 20)
    
    love.graphics.circle("line", player.x, player.y, player.size)
    love.graphics.draw(player.image, player.x, player.y,
    -0, 1, 1, player.image:getWidth()/2, player.image:getHeight()/2)
    
    camera:detach()
    camera:draw()
    
    end
end

return sprite
map.lua

Code: Select all

local tilesetImage
local tileSize = 32 -- size of tiles in pixels
local tileQuads = {} -- parts of the tileset used for different tiles
local tilesetSprite
local themap = {}
local map -- stores tiledata
local mapX, mapY -- view x,y in tiles. can be a fractional value like 3.25.
local mapWidth, mapHeight -- width and height in tiles

function themap:load()
  setupMap()
  setupTileset()
  updateTilesetBatch()
  themap:moveMap(-150, 0)
end

function setupMap()
  -- We only need a tiny map for this example
  mapWidth = 60
  mapHeight = 60
  
  --Leaving a space on the right and topsides so we know if the tiles are being "deleted"
  mapX = -20
  mapY = -20
  map = {}
  for x=0, mapWidth do
    map[x] = {}
    for y=0, mapHeight do
      map[x][y] = { --Let's turn this into a table that stores 3 things:
        tile = love.math.random(1, 100),
        x = x * tileSize,
        y = y * tileSize
      }
    end
  end
end

function tilequad(x, y, count)
   local start = #tileQuads + 1 -- The #<tablename> (in this case, the tablename is tileQuads) counts the number of max tiles.
                                           -- the +1 means we'll start at the number NEXT to the last tile.
                                           -- This means we won't override anything before the table.
   for i = start, start + count do
      tileQuads[i] = love.graphics.newQuad(x * tileSize, y * tileSize, tileSize, tileSize, tilesetImage:getWidth(), tilesetImage:getHeight())
   end
  return count
end

function setupTileset()
  tilesetImage = love.graphics.newImage( "mytileset.png" )
  --tilesetImage:setFilter("nearest", "linear")
  
  ---tileQuads[1] = love.graphics.newQuad(0 * tileSize, 1 * tileSize, tileSize, tileSize,
    ---tilesetImage:getWidth(), tilesetImage:getHeight())
  
  -- function tilequad has to add up to given number in map.tile
  
  tilequad(0, 0, 96)   --grass
  tilequad(0, 2, 4)    --rocks
  
  print(count)
  
  tilesetBatch = love.graphics.newSpriteBatch(tilesetImage, mapWidth * mapHeight)
end

function updateTilesetBatch()
  tilesetBatch:clear()
  for x=0, mapWidth do
    for y=0, mapHeight do
        tilesetBatch:add(tileQuads[map[x][y].tile], math.floor(map[x][y].x), math.
        floor(map[x][y].y))
    end
  end
  tilesetBatch:flush()
end

-- central function for moving the map
function themap:moveMap(dx, dy)
  for x = 0, mapWidth do
    for y = 0, mapHeight do
      map[x][y].x = map[x][y].x + dx
      map[x][y].y = map[x][y].y + dy
      
      -- Okay so I'm not "deleting" the tiles, but transferring their x and y coordinates
      
      --X
      if map[x][y].x < mapX - (tileSize / 2) then
         map[x][y].x = map[x][y].x + (tileSize * mapWidth)
      end
      if map[x][y].x > mapX + (mapWidth * tileSize) - (tileSize / 2) then
         map[x][y].x = map[x][y].x - (tileSize * mapWidth)
      end
      
      --Y
      if map[x][y].y < mapY - (tileSize / 2) then
         map[x][y].y = map[x][y].y + (tileSize * mapHeight)
      end
      if map[x][y].y > mapY + (mapHeight * tileSize) - (tileSize / 2) then
         map[x][y].y = map[x][y].y - (tileSize * mapHeight)
      end
    end
  end
  updateTilesetBatch()
end

return themap, tilesetBatch
:awesome: :awesome: Reply to me anytime :awesome: :awesome:

sphyrth
Citizen
Posts: 79
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Custom map loading technique

Post by sphyrth » Sun Jul 14, 2019 12:02 pm

lol - This time I'm out of that league. I can only help with the map-loading part. I think it's better that you create another thread concerning the Camera. You're on the right track anyway.

Happy coding!

User avatar
bobbymcbobface
Prole
Posts: 20
Joined: Tue Jun 04, 2019 8:31 pm

Re: Custom map loading technique

Post by bobbymcbobface » Sun Jul 14, 2019 1:57 pm

sphyrth wrote:
Sun Jul 14, 2019 12:02 pm
lol - This time I'm out of that league. I can only help with the map-loading part. I think it's better that you create another thread concerning the Camera. You're on the right track anyway.

Happy coding!
Well thankyou for the help 😀

edit: thankyou everyone for the help I'm creating a new post concerning camera setup for a topdown game i may come back to here later for more help but thanks for now :)

edit: here's the link to the new topic if anyone wants to see it https://love2d.org/forums/viewtopic.php?f=3&t=86895

edit: I'm back again! ok the newest problem is my 3rd function is not being called:

tilequad(a:0, B:0, c:98) --grass
tilequad(a:0, B:2, c:1) --rocks
tilequad(a:0, B:3, c:1) --trees

(note: a:, B:, c: are not in the actual code so need to be removed when in use)

and so i only end up with grass and rocks and also is there an equation to to add up all of C: and see it adds up to 100 since this would help with debugging a TON

also here's the tilequad() code:

Code: Select all

function tilequad(a, b, c)
   local start = #tileQuads + 1 -- The #<tablename> (in this case, the tablename is tileQuads) counts the number of max tiles.
                                           -- the +1 means we'll start at the number NEXT to the last tile.
                                           -- This means we won't override anything before the table.
   for i = start, start + count do
      tileQuads[i] = love.graphics.newQuad(x * tileSize, y * tileSize, tileSize, tileSize,
      tilesetImage:getWidth(), tilesetImage:getHeight())
   end
end
what's more is the values seem to be scewed and by scewed i mean that although my random.int is set to 1, 100 and normally throws an error if C: doesn't add up to 100 it now doesn't even recognize any errors and this seems to work fine

Code: Select all

tilequad(0, 0, 96)   --grass
tilequad(0, 2, 1)    --rocks
tilequad(0, 3, 1) 
the random.int code i was talking about:

Code: Select all

function setupMap()
  -- We only need a tiny map for this example
  mapWidth = 60
  mapHeight = 60
  
  --Leaving a space on the right and topsides so we know if the tiles are being "deleted"
  mapX = -20
  mapY = -20
  map = {}
  for x=0, mapWidth do
    map[x] = {}
    for y=0, mapHeight do
      map[x][y] = { --Let's turn this into a table that stores 3 things:
        tile = love.math.random(1, 101),                <------- here's the random.int bit 
        x = x * tileSize,
        y = y * tileSize
      }
    end
  end
end
:awesome: :awesome: Reply to me anytime :awesome: :awesome:

sphyrth
Citizen
Posts: 79
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Custom map loading technique

Post by sphyrth » Sun Jul 14, 2019 10:03 pm

Remember #tileQuads? That counts how many tiles you have. That means you don't have to manually set the 2nd argument of random to that:

Code: Select all

tile = love.math.random(1, #tileQuads) -- or tile = love.math.random(#tileQuads) since the default min is "1"
This way, you don't have to count how many tiles you have since #tileQuads does it for you.

User avatar
bobbymcbobface
Prole
Posts: 20
Joined: Tue Jun 04, 2019 8:31 pm

Re: Custom map loading technique

Post by bobbymcbobface » Mon Jul 15, 2019 6:49 am

I should of seen that sorry :roll: :P

ok after testing your code it seems now that not even the second function is called and no matter how much i increase the count on the others it doesn't seem to change one bit?
:awesome: :awesome: Reply to me anytime :awesome: :awesome:

sphyrth
Citizen
Posts: 79
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Custom map loading technique

Post by sphyrth » Mon Jul 15, 2019 10:03 pm

That's odd. May I see the main.lua?

User avatar
bobbymcbobface
Prole
Posts: 20
Joined: Tue Jun 04, 2019 8:31 pm

Re: Custom map loading technique

Post by bobbymcbobface » Tue Jul 16, 2019 7:09 am

I can't supply it at the moment but the only code in main.lua is my load functions update functions and draw functions
:awesome: :awesome: Reply to me anytime :awesome: :awesome:

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests