Here is my main.lua
Code: Select all
function love.load()
-- Grab window size
windowWidth = love.graphics.getWidth()
windowHeight = love.graphics.getHeight()
-- Set world meter size (in pixels)
love.physics.setMeter(32)
-- Load a map exported to Lua from Tiled
map = sti(mapinfo[("map" .. mapnum)].mapdir, {"Box2D"})
-- Prepare physics world with horizontal and vertical gravity
world = love.physics.newWorld(0, 0)
-- Prepare collision objects
map:box2d_init(world)
-- Create a Custom Layer
map:addCustomLayer("Sprite Layer", 3)
-- Add data to Custom Layer
local spriteLayer = map.layers["Sprite Layer"]
spriteLayer.sprites = {
player = {
image = love.graphics.newImage("assets/player.png"),
x = 200,
y = 200,
r = 0,
}
}
spriteLayer.sprites.player.body = love.physics.newBody(world, spriteLayer.sprites.player.x/2, spriteLayer.sprites.player.y/2, "dynamic")
spriteLayer.sprites.player.body:setLinearDamping(10)
spriteLayer.sprites.player.body:setFixedRotation(true)
spriteLayer.sprites.player.shape = love.physics.newRectangleShape(32, 20)
spriteLayer.sprites.player.fixture = love.physics.newFixture(spriteLayer.sprites.player.body, spriteLayer.sprites.player.shape)
-- Update callback for Custom Layer (movement for sprite happens here)
function spriteLayer:update(dt)
end
-- Draw callback for Custom Layer
function spriteLayer:draw()
local image = self.sprites.player.image
local x = math.floor(self.sprites.player.x)
local y = math.floor(self.sprites.player.y)
love.graphics.draw(image, x, y, r, 1, 1, 14, 25)
end
end
function love.update(dt)
--Update map
world:update(dt)
-- put the layout origin at position (100,100)
--the layout will grow down and to the right from this point
gui.layout:reset(100,100)
--Move the sprite
local sprite = map.layers["Sprite Layer"].sprites
local down = love.keyboard.isDown
-- Went through your code, I really liked how neat you made the movement code, nice :)
local x, y = 0, 0
if down("w") or down("up") then y = y - 4000 end
if down("s") or down("down") then y = y + 4000 end
if down("a") or down("left") then x = x - 4000 end
if down("d") or down("right") then x = x + 4000 end
sprite.player.body:applyForce(x, y)
sprite.player.x, sprite.player.y = sprite.player.body:getWorldCenter()
map:update(dt)
end
function love.draw()
-- camera movement stuff/translation
local sprite = map.layers["Sprite Layer"].sprites
local ww = love.graphics.getWidth()
local wh = love.graphics.getHeight()
local tx = math.floor(-sprite.player.x + ww / 2 - 16)
local ty = math.floor(-sprite.player.y + wh / 2 - 16)
-- Draw sprite in centre of screen
love.graphics.push()
love.graphics.translate(tx, ty)
map:setDrawRange(-tx, -ty, ww, wh)
-- Draw the map and all objects within
map:draw()
-- Draw Collision Map (useful for debugging)
love.graphics.setColor(255, 0, 0, 255)
map:box2d_draw()
-- Draw character lines
love.graphics.polygon("line", sprite.player.body:getWorldPoints(sprite.player.shape:getPoints()))
love.graphics.pop()
--Font Size.
local size = 12
-- Reset color
love.graphics.setColor(255, 255, 255, 255)
--Sets font size.
font = love.graphics.newFont(size)
-- Sets the font.
love.graphics.setFont(font)
end
function love.resize(w, h)
map:resize(w, h)
end
This is what it looks like:
This is what the map looks like in tiled(I made the other layers more opaque to show you which were the solid objects): Let me know if you need anything else, any help is appreciated!