11 Replies - 2263 Views - Last Post: 23 February 2011 - 12:53 PM Rate Topic: -----

#1 Shane Hudson   User is offline

  • D.I.C Technophile
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Map Array

Posted 12 February 2011 - 11:44 AM

Hi,

Just a quick question... most maps seem to use 2D arrays with classes for layers.

Can 3D arrays not be used to achieve layers easier? Why is this not more common? Should I not be attempting it?

Thanks,

Shane
Is This A Good Question/Topic? 0
  • +

Replies To: Map Array

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14929
  • View blog
  • Posts: 59,607
  • Joined: 12-June 08

Re: Map Array

Posted 12 February 2011 - 12:04 PM

I would use arrays of classes because I could pack more in to each node of the array...
Was This Post Helpful? 0
  • +
  • -

#3 Shane Hudson   User is offline

  • D.I.C Technophile
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Re: Map Array

Posted 12 February 2011 - 12:14 PM

Could you please explain what you mean by "pack it in"?

I am struggling to get my head around the tilemap.. which is not good!
Was This Post Helpful? 0
  • +
  • -

#4 Antiokus   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 192
  • Joined: 06-September 06

Re: Map Array

Posted 14 February 2011 - 02:01 AM

I don't see why a 3-d wouldn't work - just might be more work depending on what all you want to do. Though using classes would offer more flexibility if there was going to be anything you needed to do during an update or the draw on a layer basis. I'm fond of having an array/list of Layers. Each layer has a 2d array of the map and some other data (name, ID, etc).
Was This Post Helpful? 0
  • +
  • -

#5 chinchang   User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 727
  • Joined: 22-December 08

Re: Map Array

Posted 14 February 2011 - 02:46 AM

View PostShane Hudson, on 13 February 2011 - 12:44 AM, said:

Could you please explain what you mean by "pack it in"?

I am struggling to get my head around the tilemap.. which is not good!



If you use a 3D array, you achieve only 3 layers for your game. But like modi123_1 said, if you classes for each cell of 2D array, you could easily insert and delete layers as per you game.
Was This Post Helpful? 0
  • +
  • -

#6 Shane Hudson   User is offline

  • D.I.C Technophile
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Re: Map Array

Posted 14 February 2011 - 05:39 AM

Hmm my code has gotten fairly confusing... going to need to rewrite. Would I be allowed to post my code and see if anyone can make sense of it and convert it to classes like you say?
Was This Post Helpful? 0
  • +
  • -

#7 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: Map Array

Posted 14 February 2011 - 05:46 AM

We could give you pointers :)
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14929
  • View blog
  • Posts: 59,607
  • Joined: 12-June 08

Re: Map Array

Posted 14 February 2011 - 08:07 AM

... but if he is using C++ he probably has enough pointers! Zing!

What I believe I meant when I said "pack it all in" was if your tiles have specific properties, right? Type, arrangement, boolean 'can move through', and so forth. That information is all packed into that class. What would be gained from, conceptually, stacking more of that class information on top of each other? Change the class, change the tile!

Maybe you want to add some sort of masking or overlay. Well alter your class to have one and there you go. No mess no fuss. No worrying about getting the top most tile information.

What do you think you might gain from "vertically stacking" your tiles?
Was This Post Helpful? 0
  • +
  • -

#9 Shane Hudson   User is offline

  • D.I.C Technophile
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Re: Map Array

Posted 14 February 2011 - 09:43 AM

View Postmodi123_1, on 14 February 2011 - 03:07 PM, said:

What do you think you might gain from "vertically stacking" your tiles?


I suppose I thought it would be easier... its not!

http://www.2shared.c...ame_Engine.html

That is the link to what I have at the moment. I am thinking about converting it to XNA, but I did not much like XNA last time I tried it.

If you look at my code, ignore the game engine.. that is currently being converted from another thing I made.

The map editor is currently rather confusing, so I am definitely going to need to make a Tile class and do it the normal way... but if anybody can help me get back to sanity, it is rather messy at the moment, that would be so very helpful!

Thanks
Shane
Was This Post Helpful? 0
  • +
  • -

#10 stayscrisp   User is offline

  • フカユ
  • member icon

Reputation: 1040
  • View blog
  • Posts: 4,325
  • Joined: 14-February 08

Re: Map Array

Posted 17 February 2011 - 05:13 AM

Ah, you are using C#. I thought you were using C++, I am not going to be of much help here.
Was This Post Helpful? 0
  • +
  • -

#11 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Map Array

Posted 23 February 2011 - 04:27 AM

Basic tile map is a 2D array of indexes to the tile to be drawn.

int[,] map = new map[100, 100];



When you are tiling with C# use the first index as y and the second as x.

public void SetTile(int x, int y, int index)
{
    map[y, x] = index;
}



I would say go 3D array for your map but not 3D array. Rather use the idea of layers. The first layer will be your ground layer that has ground related tiles, the next layer will be drawn over the first and could be a environment layer with trees and such. Add another layer for buildings. So what you will have is a map layer and a map class. The map will have a number of layers. Here's a bit of a skeleton that you can consider for your maps.

class MapLayer
{
    int[,] layer;

    public void SetTile(int x, int y, int index)
    {
        layer[y, x] = index;
    }

    public int GetTile(int x, int y)
    {
        return layer[y, x];
    }

    public void Draw()
    {
        for (int y = 0; y < layer.GetLength(0); y++)
        {
            for (int x = 0; x < layer.GetLength(1); x++)
            {
                DrawTileAt(x * tileWidthOnScreen, y * tileHeightOnScreen, layer[y, x]);
            }
        }
    }
}

class Map
{
    readonly List<MapLayer> layers = new List<MapLayer>();

    public List<MapLayer> MapLayers
    {
        get { return layers; }
    }

    public void Draw()
    {
        foreach (MapLayer layer in layers)
        {
            layer.Draw();
        }
    }
}



You can easily replace int with a class Tile that has more information associated with it. That is a very basic multilayer tile engine and it can be expanded quite a bit.
Was This Post Helpful? 1
  • +
  • -

#12 Shane Hudson   User is offline

  • D.I.C Technophile
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Re: Map Array

Posted 23 February 2011 - 12:53 PM

SixOfEleven - That is just what I needed!! Thank you very much. My code was trying to achieve the same outcome but via very twisted logic. I was able to work out how to get the map on its own but not add layers. Looking at your examples, it is actually very easy!

Thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1