Problems with camera on topdown map

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

Problems with camera on topdown map

Post by bobbymcbobface » Sun Jul 14, 2019 3:12 pm

Can anyone help me? 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 appeared can anyone help me - thank you

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
local dxx = 0
local dyy = 0

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
      --dxx = dxx + dx
      --dyy = dyy + 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
I make games that run on potatoes :P

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests