Thursday, February 20, 2014

Loading levels

This week I have been working on several different things. I've implemented a state manager, texture manager and created the class game object and seen to it that all classes that should now inherit from it. I have moved code from engine to the game state and changed some of it to make better sense and one of these changes is what I'm going to explain into detail about today.

In our game state we load the level from a text file. From the start there was only the tile map to load from the file but as we added more objects to the game our level file grew. Now it includes everything: tile map, player starting position, guards and their patterns, all props, evidence location and more. As we added more and more to the file we simply added more and more to our method that loads it, this made our method pretty big and clumsy.
When elevator doesn't have a rotation
   The main problem with the method was that each object had to be listed after a specific other object. For example: The elevator position had to be directly after the player position, otherwise the program would load everything wrongly and the game would either crash or look really weird. Another problem with the method was that it couldn't load anything but rectangular levels.

So when I created the load method again I though I'd rewrite the whole thing and solve the existing problems.
   The first and most important problem was fairly easy to solve. After the file was opened I created a while loop that continues until the file stream reads "End". The first thing that happens in the while loop is it reads one word from the file, it then checks to see if it was a keyword like "Player:". If it is a keyword a few lines specific to that keyword is run, for example if the keyword "Player" is found; it creates a sprite from a read texture and sets it to a read position. This way any object can be written anywhere in the file also we can write anything like explanatory text without the load method reading anything wrong.
   The second problem with the loading of empty tile was solved by skipping one tile if the stream reads two 9's. 9,9 means to take the tile from the image file with the x and y position at 9 which will only happen if we manage to get 81 tiles. I believe this was a easy and good solution and we will most probably not need to change it.

Well, that was it! Thanks for reading and stay awesome!

3 comments:

  1. It's me again,

    I find it hard to believe that your level class was really that hard, how could you not have any trouble creating a statemanager, a gamestate and a texture manager ?
    I would have loved to see the thought process there.
    What kind of methods did you use?
    What fundamentally different was it compared to SDL?
    Which methods was inheriting from SFML own classes ?

    I would have loved to see more about your thought process during these classes than the problem to recreate a method obsolete in sfml just because you wanted to make your program similar to SDL.

    though I did like your solution to your problem, it was simple and a quick shortcut. Just a simple note, your language in this post seems stressed so in the future please put more effort into these posts are they train you to better document your efforts and problemsolving skills.

    Shamefrul disprace.
    Much love,
    Ladbon

    ReplyDelete
    Replies
    1. Hello again Ladbon!

      You are correct again, the load level method wasn't hard at all. After your comment I realize I maybe should have chosen another artifact to write about, such as state manager or texture manager, but at the time I wrote this post I felt happier with the loading of levels method.

      Thank you for commenting and stay awesome!

      Delete
  2. Hello community! I have read you blog with great interest. I feel like the concept of using text files for texture is really good, I should probably check them out more to get a better understanding of how to set them up. There comes as little to no surprise that you felt like you wanted to redo the solution for loading them when it got too messy. Firstly you don't want to wander around, lost in your code later if it gets out of control and secondly there are not to many solutions that you will be happy with after 1 month from what I have seen =) I would have seen some more about how you used that while loop to act as a "empty" condition as it sounds really neat(yea neat , best word EU:P). The keyword seems really smart when you first read it. But I am unsure of what it exactly changed when all is said and done. Is it just that you go through what is supposed to happen when you load a file from a text document; it creates a sprite from a read texture and sets it to a read position, or did miss something new there? I will give you props for the layout though, I would probably want to see some more code in the picture but that would not look as good. I guess you might have to paint the code before ^^

    Sleep tight and I'll see you tomorrow =)

    / Hampus Lyman

    ReplyDelete