Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Tileset management class
- By liekzomg Date 2009-06-23 08:45
Making a 2d top down game with a friend that will use a lot tilesets. He's going to be doing a lot of the art. Ideally I want him to be able to edit tilesets to his hearts content, moving things around, redoing them etc. So I don't want to hardcode any particular tiles (like 3,4 is player.run_left). I think I can achieve this by having a 2nd file alongside the png with names and positions. Eg. player.png and player.ts in the /media file with player.png having a bunch of tiles and player.ts having 16x16 on first line for tile size followed by run_left,run_up,...,walk_left etc in matching positions to that of the png. When I call Tileset.load("player") it splits up all the tiles into single tiles and their corresponding name so I can call something like player.tile(:run_up) and get the tile for player running up.

This is my first ever try at creating a game so any advice you can give me on whether this is a bad idea or any pitfalls I'll run into would be appreciated.
- By jlnr (dev) Date 2009-06-23 13:58
Hmm, if you use YAML it should be very straightforward to construct such a class :)

# player.yml
foo: { x: 5, y: 6 }
bar: { x: 6, y: 10 }


# .rb
require 'yaml'

tiles = YAML.load_file("player.yml")
p tiles['foo']['x']


Now all the class would need is a hash to remember available images.
- By philomory Date 2009-06-23 16:05 Edited 2009-06-23 16:13
Well, you might want to take a look at my ImageManager, Tileset and Tile classes from Operation Lambda, though in my game I didn't pack sprites for different poses or orientations into single files. The only time I load multiple tiles from a single png is when loading animation frames. None-the-less, some of it could be helpful.

ImageManager, Tileset, Tile, my main tileset metadata file.

Here's an example usage, the main draw method in my GameplayMap class:


    def draw
      @background_image.draw(0,0,ZOrder::Stars)
      self.each_with_coords do |obj,x,y|
        xpos = x * Sizes::TileWidth
        ypos = y * Sizes::TileHeight
        @floor_image.draw(xpos,ypos,ZOrder::Floor) unless (obj.noun == 'Space')
        ImageManager.tile(obj.key).frame(obj.frame_fraction,true).draw(xpos,ypos,ZOrder::Things)
        obj.draw_beams(xpos,ypos)
      end
    end


Edit: By all means, please ignore all the font-related code in ImageManager, by the way. Though functional, it is hideous and not a starting point for your own work. I need to change it. The actual image-related code is pretty good, though. At least, it's served me very well.

I should also note (since this hasn't made it into source comments yet) that in the context of Operation Lambda, a 'Tileset' is a complete set of every tile image that is used by the game. This differs from what is probably the normal use of the term, under which you would have a tileset for each entity, such as a player tileset, a tileset for each enemy, etc. In other words, my Tileset class might be more appropriately called a 'theme' or 'skin'.

Anyway, hope this helps.

Edit again: Spelling.
- By liekzomg Date 2009-06-25 23:35
Thankyou jlnr and philomory. YAML seems like a really good fit and I'm reading through your code now, don't know how I'm going to handle animations so your code is definitely helping.

P.S. sorry for somehow putting this post in the showcase forum... really not sure how I managed to do that.
Up Topic Gosu / Gosu Exchange / Tileset management class

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill