Results 1 to 8 of 8

Thread: Objects on a tilemap

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Hi T-Bear,

    If you are storing an X,Y pair for each item in the Tiles and Objects array, then you can improve things quite a bit by moving to a two dimensional array.

    Code:
    Tiles : array[0..200,0..200] of TTile;
    Objects : array[0..200,0..200] of TObject;

    How would this work if the objects are larger than a single tile though? Basically on the map there are buildings which can span many tiles, and trees which are on a single tile.
    As you said this probably won't work with dynamic size of the objects.

    When I wrote a tile based game for one of our competitions, I used pointer based datastorage which is in fact much like the dynamic array with index calculations.
    How exactly would this work? Pointers from each tile object to the object on the tile? How would this work with objects larger than single tiles?

    Can you post some of the code where you are actually using the map?
    Anyway if you provide more information about your game and general map structure we would be able to offer you better advice particulary more suitable to your game.
    I am mostly just doing some information gathering for my new project, reading about stuff and so on, so I don't have any code sample for how the map structure is going to work exactly. I tried finding some old code illustrating how I did this, but couldn't find any.

  2. #2
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2
    The objects that I had that were bigger than a single tile were broken up into multiple tiles. By doing this I was able to create the illusion of depth by drawing them on different layers relative to the player so the 'front' were rendered behind the player while the 'back' was rendered in front of the player.

    But, back to your question... how could this work if the objects are bigger than a tile? Good question

    I'm not sure and it's not a solution I would pursue personally because of the overheads involved.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  3. #3
    Your original solution was almost perfect, but you are right, on larger maps it would get lower performance. Thus you can divide your map into chunks.

    Code:
    const CHUNKSIZE = 32;
    
    TChunk = class
    public
      posX, posY: integer;
      Tiles: array[0..CHUNKSIZE-1, 0..CHUNKSIZE] of TTile;
      Objects: array of TObject;
    end;
    
    TMap = class
    public
      Chunks: array of array of TChunk;
      function GetTile(x, y: integer): TTile;
    end;
    
    function TMap.GetTile(x, y: integer): TTile;
    var cx, cy: integer;
    procedure
      cx:=x div CHUNKSIZE;
      cy:=y div CHUNKSIZE;
      result:=Chunks[cx, cy].Tiles[x-cx, y-cy]
    end;
    Or something along these lines... Of course you could do range checking in GetTile() to avoid critical errors on negative values or out of boundaries.

  4. #4
    PGDCE Developer Carver413's Avatar
    Join Date
    Jun 2010
    Location
    Spokane,WA,Usa
    Posts
    206
    if I were trying to do someting like this I would use an 8bit index map and use index 0 as a null. the background would be for normal size tiles while the forground would be for larger tiles. then I would add a clip frame that reperesents the view able portion of the map. now all you have to do is move the clipframe around within the map to change the view. on the forground the indexed position would be the top/left and all indexes underneath it would be 0's. your clip frame would have to be larger then the actual view so if a larger tile were half on it would still be displayed.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •