Page 1 of 1

What is the best way?

Posted: Thu May 16, 2013 5:00 pm
by ellipticaldoor
I want to optimize this code but I don´t know very much lua, what I should do? :roll:

Code: Select all

function background.draw()
    if player.actual_level == 0 then
        love.graphics.setColor(52,73,94,255)
        love.graphics.rectangle("fill", 0, roof, 1280, 720)
        love.graphics.setColor(243, 156, 18, background.alpha)
        love.graphics.circle("fill", background.moon_x, 300 + roof, 300)
    end

    if player.actual_level == 1 then
        love.graphics.setColor(255,255,255,255)
        love.graphics.draw(bg, background.posx , roof)
        love.graphics.draw(bg, background.posx - 1280 , roof)
    end

    if player.actual_level == 2 then
        love.graphics.setColor(52,152,219,255)
        love.graphics.rectangle("fill", 0, roof, 1280, 720)
        love.graphics.setColor(230,126,34, background.alpha)
        love.graphics.rectangle("fill", 0, roof, 1280, 720)
        love.graphics.setColor(255,255,255,255)
        love.graphics.draw(bg_2a, 0 , roof)
        love.graphics.setColor(multiplyChannels(255, 255, 255, alphabg_2a))
        love.graphics.draw(bg_2b, 0, roof)
    end

    if player.actual_level == 3 then
        
    end
end

Re: What is the best way?

Posted: Thu May 16, 2013 7:48 pm
by Plu
What are you trying to optimise? Do you want it more readible, more easily expandable, or just more efficient? There's a different solution for each and I'm not really clear on what you're trying to accomplish.

Re: What is the best way?

Posted: Thu May 16, 2013 8:39 pm
by Kingdaro
If it's not lagging, don't try to optimize it. It's usually only good to start optimizing your code once things start slowing down.

In terms of shortness/readability, it looks fine. There's not much room for any repetitions or functions, since all of the drawing operations are pretty different from one another.

Re: What is the best way?

Posted: Thu May 16, 2013 9:15 pm
by ellipticaldoor
I want to reduce the number of needed lines and use a switch statement, I don't know how to make it in LUA... :o:

Re: What is the best way?

Posted: Thu May 16, 2013 9:19 pm
by markgo
Well you can try:

Code: Select all

switch = {...}

function blah(...)
 switch[player.actual_level](...)
end

Re: What is the best way?

Posted: Thu May 16, 2013 10:01 pm
by ellipticaldoor
thanks!

Re: What is the best way?

Posted: Thu May 16, 2013 10:11 pm
by Robin
You'll want to ask each level to draw themselves. So they each gets a function. It's basically what markgo suggested, but switch as a variable name is awfully generic.