Importing the map is done via STI. It imports a tilemap done in Tiled. Very simple right? Which is then being updated and drawn to the screen.
Code: Select all
function love.load()
map = sti("assets/maps/testmapbig.lua", {"box2d"})
end
function love.update(dt)
map:update(dt)
end
function love.draw()
map:draw(-dx, -dy, mapScale, mapScale)
end
Code: Select all
player = {}
player.width = 16
player.height = 23
player.ismoving = false
player.collider = world:newCircleCollider(width/2, height/2, 15)
--Creates a grid table and places spreadsheet into it
player.grids = {}
player.grids.walk = anim8.newGrid(player.width, player.height, sprites.linkSpriteSheet:getDimensions())
--Creates animations out of these grids
player.animations = {}
player.animations.walkDown = anim8.newAnimation(player.grids.walk('1-8', 1), 0.1)
player.animations.walkUp = anim8.newAnimation(player.grids.walk('1-8', 2), 0.1)
player.animations.walkRight = anim8.newAnimation(player.grids.walk('1-8', 3), 0.1)
player.animations.walkLeft = anim8.newAnimation(player.grids.walk('1-8', 4), 0.1)
--Stores players current animation
player.anim = player.animations.walkDown
function player:update(dt)
--Freeze animation if player isn't moving
if player.isMoving then
player.anim:update(dt)
end
local vectorX = 0
local vectorY = 0
playerX = player.collider:getX()
playerY = player.collider:getY()
--Keyboard direction checks for movement and changes animation
if love.keyboard.isDown("a", "left") then
vectorX = -1
player.anim = player.animations.walkLeft
end
if love.keyboard.isDown("d", "right") then
vectorX = 1
player.anim = player.animations.walkRight
end
if love.keyboard.isDown("w", "up") then
vectorY = -1
player.anim = player.animations.walkUp
end
if love.keyboard.isDown("s", "down") then
vectorY = 1
player.anim = player.animations.walkDown
end
player.collider:setLinearVelocity(vectorX * 100, vectorY* 100)
--Checks if player is moving
--(if players previous position if different from current position)
if vectorX == 0 and vectorY == 0 then
player.isMoving = false
player.anim:gotoFrame(1) -- Go to standing frame in current grid
else
player.isMoving = true
end
end
function player:draw()
local px = player.collider:getX()
local py = player.collider:getY()
--Sets stylesheet filter so scaling doesn't blur
sprites.linkSpriteSheet:setFilter('nearest', 'nearest')
--Draws current animation frame
player.anim:draw(sprites.linkSpriteSheet, px, py, nil, 2, 2, player.width/2, player.height/1.3)
end
The problem comes when I try to implement collisionboxes for my map. The way I've figured out to do this is to loop through every object in my map.lua file an retrieve it's x and y cords. Then the loop also creates a new collision rectangle at those specific coordinates.
Code: Select all
function love.load()
--Gets number of tables(objects) in currently loaded map
colliderLayer = map.layers[2].objects
--Loops through all objects and gets their respective x, y values
--this then creates a new collider rectangle at respective coordinates
for i, v in pairs(colliderLayer) do
local colliderObjectX = map.layers[2].objects[i].x
local colliderObjectY = map.layers[2].objects[i].y
colliderBox = world:newRectangleCollider(
(colliderObjectX*2+6), --Multiplies current cordinate by 2 too make up for map scaling in draw()
(colliderObjectY*2-28), --then adds specific numbers to allign them just perfect
20,
25)
colliderBox:setType('static')
end
end
Code: Select all
function love.draw()
dx = player.collider:getX() - (love.graphics.getWidth()/2)
dy = player.collider:getY() - (love.graphics.getHeight()/2)
love.graphics.scale(scale)
love.graphics.translate(-dx, -dy)
--Scales map to double size to fit screen
--Draws map and sets new scale
map:draw(-dx, -dy, mapScale, mapScale)
player:draw()
world:draw()
end