I'm really stuggling with tilemaps. I have two problems, but first some context. I've fumbled my way around enough to get some on screen. I have a sprite in too.
problem 1: Collision
I'm using addWallSprites, when I do though, it only adds one big block around my whole tilemap. I wanted it to be 16x16 blocks.
problem2: tilemap movement with collisions
I'm able to move my sprite no problem using moveWithCollisions. But tilemap doesn't have that. In the above screenshot. I am able to move the tileset where ever I want, but the collision stays in place making it usless.
I've seen some demos around that show it's possible to do this, but it's not clear from the documentation as to how.
Demo Code here, if anyone has any advice that would be awesome.
import "CoreLibs/sprites"
gfx = playdate.graphics
-- playdate.display.setScale(2)
tiles = gfx.imagetable.new('assets/link/rprite')
map = gfx.tilemap.new()
map:setImageTable(tiles)
map:setSize(16,16)
-- map:setTiles({0,0,1,1}, 16)
-- map:setZIndex(100)
s = gfx.sprite.new(gfx.image.new('assets/link/s1'))
s:add()
s:moveTo(15, 15)
s:setCollideRect(0, 0, s:getSize())
-- s:setZIndex(300)
for y = 5,10 do
for x = 1, 16 do
map:setTileAtPosition(x, y, 1)
end
end
-- s.addEmptyCollisionSprite(22, 22,16,16)
print(map:getTileAtPosition(1, 1))
function s:collisions()
local collisions = self:overlappingSprites()
-- for c = 1, #collisions do
-- local collisionPair = collisions[c]
-- local sprite1 = collisionPair[1]
-- local sprite2 = collisionPair[2]
-- print("sprite1: ",sprite1)
-- print("sprite2: ", sprite2)
-- print("collisions",collisionPair)
-- collisionPair:remove()
-- if sprite1 == 1 then
-- sprite1 = 0
-- end
-- if sprite2 == 1 then
-- print(sprite2)
-- sprite2:remove()
-- end
-- --print(c)
-- end
end
s.addWallSprites(map, {2, 3},16,0)
x = 0
y = 0
leftdown = false
rightdown = false
updown = false
downdown = false
-- gfx.sprite.addWallSprites()
a = 0
b = 0
function playdate.update()
-- if leftdown then x = x + 3 end
-- if rightdown then x = x - 3 end
-- if updown then y = y + 3 end
-- if downdown then y = y - 3 end
if leftdown then a = -3 b = 0 end
if rightdown then a = 3 b = 0 end
if updown then a = 0 b = -3 end
if downdown then a = 0 b = 3 end
-- gfx.setColor(gfx.kColorWhite)
-- gfx.fillRect(0,0,50,40)
gfx.sprite.update()
s:moveWithCollisions(s.x,s.y)
s:moveBy(a, b)
map:draw(15,1)
s:collisions()
-- c = [x , y]
-- gfx.sprite.addWallSprites(map, 2, {x, y})
-- map:moveWithCollisions(map.x,map.y)
end
function playdate.leftButtonDown() leftdown = true end
function playdate.leftButtonUp() leftdown = false end
function playdate.rightButtonDown() rightdown = true end
function playdate.rightButtonUp() rightdown = false end
function playdate.upButtonDown() updown = true end
function playdate.upButtonUp() updown = false end
function playdate.downButtonDown() downdown = true end
function playdate.downButtonUp() downdown = false end