Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Tiny level editor

edited March 2015 in Code Sharing Posts: 175

Hello guys, today i want share to community my first and very tiny level editor
Code:

displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
    lvl = {1,2,3,0,
           0,2,0,0,
           0,2,0,0}
    pos = {}
    local nrow = 0
    for k,v in pairs(lvl) do
        if math.fmod(k,4) == 0 then
        nrow = nrow + 1
        end
        if v == 0 then
        table.insert(pos,vec2(-200,-200))
        else    
        table.insert(pos,vec2(WIDTH/4+(v)*100,HEIGHT/2-nrow*100))
        end
    end

end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    for k,v in pairs(pos) do
    --rect(v.x,v.y,512/4,512/4)
    sprite("Platformer Art:Crate",v.x,v.y,512/4,512/4)
    end
    -- Do your drawing here
    strokeWidth(5)
end

function touched(t)

end

Little explaining :
You have table "lvl", which store information, where is your ground
Table "pos" store converted numbers to coordinates from table "lvl"

In this code you can make 3x3 field, if you want make 4x4 or 5x5 (and so on) you must fill last column with 0 , so you 4x4 field
would be

         lvl = {1,2,3,4,0,
                1,2,3,4,0,
                1,2,3,4,0}

And you must change number on this method to amount numbers in your table lvl , including column filled with zeros

math.fmod(k,4) == 0

How can you see on the top, i formatted form of lvl table . In my example code you would see T-map , so imagine this numbers are blocks.

P.s sorry for my bad explanation, if something is not clear for you, ask me in this thread or pm

Upd:

Added demo project


--# Main displayMode(FULLSCREEN) -- Use this function to perform your initial setup function setup() lvl1 = Lvl1() lvl2 = Lvl2() lvl3 = Lvl3() lvl4 = Lvl4() lvl5 = Lvl5() timer = 0 end -- This function gets called once every frame function draw() -- This sets a dark background color background(40, 40, 50) timer = timer + DeltaTime if timer <= 2 then lvl1:draw() elseif timer > 2 and timer < 4 then lvl2:draw() elseif timer > 4 and timer < 6 then lvl3:draw() elseif timer > 6 and timer < 8 then lvl4:draw() elseif timer > 8 and timer < 10 then lvl5:draw() elseif timer > 10 then timer = 0 end -- Do your drawing here end function touched(t) end --# Lvl1 Lvl1 = class() function Lvl1:init() -- you can accept and set parameters here self.lvl = {0,2,0,0, 0,2,0,0, 0,2,0,0, 0,2,0,0, 0,2,0,0, 0,2,0,0} self.pos = {} local nrow = 0 for k,v in pairs(self.lvl) do if math.fmod(k,4) == 0 then nrow = nrow + 1 end if v == 0 then table.insert(self.pos,vec2(-100,-100)) else table.insert(self.pos,vec2((v)*100,HEIGHT-100-nrow*100)) end end end function Lvl1:draw() -- Codea does not automatically call this method for k,v in pairs(self.pos) do sprite("Platformer Art:Block Grass",v.x,v.y,512/5,512/5) end end function Lvl1:touched(touch) -- Codea does not automatically call this method end --# Lvl2 Lvl2 = class() function Lvl2:init(x) -- you can accept and set parameters here self.lvl = {0,2,3,4,0, 0,2,3,4,0, 0,2,0,4,0, 0,2,3,4,0, 0,2,0,4,0, 0,2,0,4,0} self.pos = {} local nrow = 0 for k,v in pairs(self.lvl) do if math.fmod(k,6) == 0 then nrow = nrow + 1 end if v == 0 then table.insert(self.pos,vec2(-100,-100)) else table.insert(self.pos,vec2((v)*100,HEIGHT-100-nrow*100)) end end end function Lvl2:draw() -- Codea does not automatically call this method for k,v in pairs(self.pos) do sprite("Platformer Art:Block Grass",v.x,v.y,512/5,512/5) end end function Lvl2:touched(touch) -- Codea does not automatically call this method end --# Lvl3 Lvl3 = class() function Lvl3:init(x) -- you can accept and set parameters here self.lvl = {1,2,3,4,5,0, 1,0,3,0,5,0, 1,0,3,4,5,0, 1,2,3,0,5,0, 1,0,3,0,5,0, 1,2,3,4,5,0} self.pos = {} local nrow = 0 for k,v in pairs(self.lvl) do if math.fmod(k,6) == 0 then nrow = nrow + 1 end if v == 0 then table.insert(self.pos,vec2(-100,-100)) else table.insert(self.pos,vec2((v)*100,HEIGHT-100-nrow*100)) end end end function Lvl3:draw() -- Codea does not automatically call this method for k,v in pairs(self.pos) do sprite("Platformer Art:Block Grass",v.x,v.y,512/5,512/5) end end function Lvl3:touched(touch) -- Codea does not automatically call this method end --# Lvl4 Lvl4 = class() function Lvl4:init(x) -- you can accept and set parameters here self.lvl = {1,2,3,4,5,6,0, 0,2,0,0,5,6,0, 0,0,3,4,0,6,0, 0,0,3,4,0,6,0, 0,2,0,0,5,6,0, 1,2,3,4,5,6,0} self.pos = {} local nrow = 0 for k,v in pairs(self.lvl) do if math.fmod(k,7) == 0 then nrow = nrow + 1 end if v == 0 then table.insert(self.pos,vec2(-100,-100)) else table.insert(self.pos,vec2((v)*100,HEIGHT-100-nrow*100)) end end end function Lvl4:draw() -- Codea does not automatically call this method for k,v in pairs(self.pos) do sprite("Platformer Art:Block Grass",v.x,v.y,512/5,512/5) end end function Lvl4:touched(touch) -- Codea does not automatically call this method end --# Lvl5 Lvl5 = class() function Lvl5:init(x) -- you can accept and set parameters here self.lvl = {1,2,3,4,5,0, 1,0,0,0,5,0, 1,0,0,0,5,0, 1,0,3,4,5,0, 1,0,3,0,5,0, 1,2,3,4,5,0} self.pos = {} local nrow = 0 for k,v in pairs(self.lvl) do if math.fmod(k,6) == 0 then nrow = nrow + 1 end if v == 0 then table.insert(self.pos,vec2(-100,-100)) else table.insert(self.pos,vec2((v)*100,HEIGHT-100-nrow*100)) end end end function Lvl5:draw() -- Codea does not automatically call this method for k,v in pairs(self.pos) do sprite("Platformer Art:Block Grass",v.x,v.y,512/5,512/5) end end function Lvl5:touched(touch) -- Codea does not automatically call this method end

Comments

Sign In or Register to comment.