Can't get auto tile to work

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
gubbengetze
Prole
Posts: 3
Joined: Thu May 10, 2018 9:06 pm

Can't get auto tile to work

Post by gubbengetze »

Hi!

I'm trying to write an extremely basic auto tile-system for my game as I have not found any libraries or such for it, but I can't seem to get it to work properly. Can someone give me a hint about what I am doing wrong or what I could be doing better? Please excuse my code as I am a beginner! :nyu:

Image

Code: Select all

blocksIndex = {}
Block = Object:extend()

function Block:new(x,y)
    
    self.sprite = groundMiddle2
    
    self.x = x
    self.y = y
    
    self.width = 32
    self.height = 32
    
    self.connectedLeft = false
    self.connectedRight = false
    self.connectedUp = false
    self.connectedDown = false
    
    self.connectedUpperLeft = false
    self.connectedUpperRight = false
    self.connectedLowerLeft = false
    self.connectedLowerRight = false
    
end

function Block:draw()

    love.graphics.draw(spritesheet, self.sprite, self.x, self.y) 

end

function updateBlockConnections()
    
    for i, v in ipairs(blocksIndex) do

        for o, b in ipairs(blocksIndex) do

            if v.x == b.x + b.width then v.connectedLeft = true end

            if v.x + v.width == b.x then v.connectedRight = true end

            if v.y == b.y + b.height then v.connectedUp = true end
            
            if v.y + v.height == b.y then v.connectedDown = true end

        end

    end
    
    for i, v in ipairs(blocksIndex) do

        --MIDDLE
        if  v.connectedLeft == true 
        and v.connectedRight == true 
        and v.connectedUp == true
        and v.connectedDown == true
        
        then v.sprite = groundMiddle 

        --LEFT
        elseif  v.connectedLeft == false 
        and v.connectedRight == true 
        and v.connectedUp == true
        and v.connectedDown == true
        
        then v.sprite = groundLeft 
        
        --RIGHT
        elseif  v.connectedLeft == true 
        and v.connectedRight == false 
        and v.connectedUp == true
        and v.connectedDown == true
        
        then v.sprite = groundRight

        --UP
        elseif  v.connectedLeft == true 
        and v.connectedRight == true 
        and v.connectedUp == false
        and v.connectedDown == true
        
        then v.sprite = groundUp

        --DOWN
        elseif  v.connectedLeft == true 
        and v.connectedRight == true 
        and v.connectedUp == true
        and v.connectedDown == false
        
        then v.sprite = groundDown

        --UPPER LEFT
        elseif  v.connectedLeft == false 
        and v.connectedRight == true 
        and v.connectedUp == false
        and v.connectedDown == true
        
        then v.sprite = groundUpperLeft

        --UPPER RIGHT
        elseif  v.connectedLeft == true 
        and v.connectedRight == false 
        and v.connectedUp == false
        and v.connectedDown == true
        
        then v.sprite = groundUpperRight

        --LOWER RIGHT
        elseif  v.connectedLeft == true 
        and v.connectedRight == false 
        and v.connectedUp == true
        and v.connectedDown == false
        
        then v.sprite = groundLowerRight 
            
        --LOWER LEFT
        elseif  v.connectedLeft == false 
        and v.connectedRight == true 
        and v.connectedUp == true
        and v.connectedDown == false
        
        then v.sprite = groundLowerLeft 
            
        end
    end
end


function insertBlock(x,y)
    
    table.insert(blocksIndex, Block(x,y))
    
end
Attachments
autotile-test.love.zip
(5.58 KiB) Downloaded 87 times
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Can't get auto tile to work

Post by raidho36 »

I suggest you rewrite it to fetch appropriate graphics using "search" keys instead of doing if-else comparisons. It's more robust and should work faster. Also it will be easier to pick up errors. Something like this:

Code: Select all

-- left = 8, top = 4, right = 2, bottom = 1
sprites[ 0 + 0 + 0 + 0 ] = sprite_island
sprites[ 8 + 4 + 2 + 1 ] = sprites_middle

sprites[ 8 + 0 + 0 + 0 ] = sprite_peninsula_right
sprites[ 0 + 4 + 0 + 0 ] = sprite_peninsula_bottom
sprites[ 0 + 0 + 2 + 0 ] = sprite_peninsula_top
sprites[ 0 + 0 + 0 + 1 ] = sprite_peninsula_left

sprites[ 8 + 4 + 0 + 0 ] = sprites_edge_right_bottom
sprites[ 8 + 0 + 0 + 1 ] = sprites_edge_right_top
sprites[ 0 + 4 + 2 + 0 ] = sprites_edge_left_bottom
sprites[ 0 + 0 + 2 + 1 ] = sprites_edge_left_top

sprites[ 8 + 0 + 2 + 0 ] = sprites_bridge_vertical
sprites[ 0 + 4 + 0 + 1 ] = sprites_bridge_horizontal

sprites[ 0 + 4 + 2 + 1 ] = sprites_edge_left
sprites[ 8 + 0 + 2 + 1 ] = sprites_edge_top
sprites[ 8 + 4 + 0 + 1 ] = sprites_edge_right
sprites[ 8 + 4 + 2 + 0 ] = sprites_edge_bottom

if tile.x == other.x + other.width  then tile.connectionCode = tile.connectionCode + 8 end
if tile.y == other.y + other.height then tile.connectionCode = tile.connectionCode + 4 end
if tile.x + tile.width  == other.x then tile.connectionCode = tile.connectionCode + 2 end
if tile.y + tile.height == other.y then tile.connectionCode = tile.connectionCode + 1 end

tile.sprite = sprites[ tile.connectionCode ]
Post Reply

Who is online

Users browsing this forum: No registered users and 171 guests