Skip to content

Loading Levels From Files

noooway edited this page Jul 10, 2017 · 11 revisions

While we are talking about splitting the code, I also want to move level definitions into separate files. This is not immediately necessary, but will become convenient later, when the amount of levels will grow.

The files with the levels descriptions will be kept at the levels folder. It is possible to write each level-file as a separate Lua module. However, it is considered a good practice to avoid any code in level files, preferably making them data-only. Therefore, the only thing that remains from the Lua module structure is the return statement. Here is an example of hey.lua:

return {
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
   { 1, 0, 1, 0, 2, 2, 2, 0, 3, 0, 3 },
   { 1, 0, 1, 0, 2, 0, 0, 0, 3, 0, 3 },
   { 1, 1, 1, 0, 2, 2, 0, 0, 0, 3, 0 },
   { 1, 0, 1, 0, 2, 0, 0, 0, 0, 3, 0 },
   { 1, 0, 1, 0, 2, 2, 2, 0, 0, 3, 0 },
   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
}

Previously the order of the levels was maintained by the levels.sequence array. I also want to move it into a separate file sequence.lua. It is going to store an array of level filenames.

return {
   "hey",
   "bye"
}

The levels.sequence has to be required inside the levels module:

.....
levels.sequence = require "levels/sequence"

To load the contents of the current level file, a separate function is defined:

function levels.require_current_level()
   local level_filename = "levels/" .. levels.sequence[ levels.current_level ] --(*1)
   local level = require( level_filename )
   return level
end

(*1): current level filename is appended to the "levels/" folder.

love.load and levels.switch_to_next_level( bricks, ball ) have to be update to use this function.

function love.load()
   level = levels.require_current_level()
   bricks.construct_level( level )
   .....
end

function levels.switch_to_next_level( bricks, ball )
    .....
         levels.current_level = levels.current_level + 1
         level = levels.require_current_level()
         bricks.construct_level( level )
    .....
end

Apart from these modifications, there are a couple of other things I want to introduce in this part. First, to facilitate the bricks destruction process, I want to define a function bricks.clear_current_level_bricks(), that is going to clear all the remaining bricks on the level. It is going to be called when the c key is pressed.

function love.keyreleased( key, code )
   if key == 'c' then
      bricks.clear_current_level_bricks()
   .....   
end

function bricks.clear_current_level_bricks()
   for i in pairs( bricks.current_level_bricks ) do
      bricks.current_level_bricks[i] = nil
   end
end

The main point, of course, is to automatically achieve the condition of switching to the next level.

The second thing is bricks coloring, depending on the brick's type. To implement this, in bricks.draw_brick it is necessary to set the value of the fill color using this property. Bricks of type 1 will be in red, 2 - green, and 3 - blue.

function bricks.draw_brick( single_brick )
   love.graphics.rectangle( 'line',
                            single_brick.position.x,
                            single_brick.position.y,
                            single_brick.width,
                            single_brick.height )
   local r, g, b, a = love.graphics.getColor( )
   if single_brick.bricktype == 1 then                 --(*1)
      love.graphics.setColor( 255, 0, 0, 100 )
   elseif single_brick.bricktype == 2 then
      love.graphics.setColor( 0, 255, 0, 100 )
   elseif single_brick.bricktype == 3 then
      love.graphics.setColor( 0, 0, 255, 100 )
   end
   love.graphics.rectangle( 'fill',                    --(*2)
                            single_brick.position.x,
                            single_brick.position.y,
                            single_brick.width,
                            single_brick.height )
   love.graphics.setColor( r, g, b, a )                --(*3)
end

(*1): choose drawing color for the brick.
(*2): add fill representation.
(*3): restore the default colorscheme.

    Home
    Acknowledgements
    Todo

Chapter 1: Prototype

  1. The Ball, The Brick, The Platform
  2. Game Objects as Lua Tables
  3. Bricks and Walls
  4. Detecting Collisions
  5. Resolving Collisions
  6. Levels

    Appendix A: Storing Levels as Strings
    Appendix B: Optimized Collision Detection (draft)

Chapter 2: General Code Structure

  1. Splitting Code into Several Files
  2. Loading Levels from Files
  3. Straightforward Gamestates
  4. Advanced Gamestates
  5. Basic Tiles
  6. Different Brick Types
  7. Basic Sound
  8. Game Over

    Appendix C: Stricter Modules (draft)
    Appendix D-1: Intro to Classes (draft)
    Appendix D-2: Chapter 2 Using Classes.

Chapter 3 (deprecated): Details

  1. Improved Ball Rebounds
  2. Ball Launch From Platform (Two Objects Moving Together)
  3. Mouse Controls
  4. Spawning Bonuses
  5. Bonus Effects
  6. Glue Bonus
  7. Add New Ball Bonus
  8. Life and Next Level Bonuses
  9. Random Bonuses
  10. Menu Buttons
  11. Wall Tiles
  12. Side Panel
  13. Score
  14. Fonts
  15. More Sounds
  16. Final Screen
  17. Packaging

    Appendix D: GUI Layouts
    Appendix E: Love-release and Love.js

Beyond Programming:

  1. Game Design
  2. Minimal Marketing (draft)
  3. Finding a Team (draft)

Archive

Clone this wiki locally