First off to spare anyone not very interested in reading this whole novel, there is no code in this question it is purely theoretical. I am on my phone and this is a question about which choice is best fitting and easiest to work with in the situation.
So to the question, I am working on what I believe should be a basic Tilemap engine* and I'm in doubt about my approach. I have already rewritten it several times. Not that it have taken too long, the project is still in its diaper to say the least.
Basically, I have a Sprite class with this constructor:string name, int Id, texture2d texture.
And then there is my Tile class that derives from the Sprite class, with these added to the constructor : Int X, int Y, bool walkable.
At my first attempt I used a 2D integer array and assigned the ID value of the desired tile to the position on the array. And, then when drawing the tilemap I would double loop** the entire array and find the tile with the Same ID as the array position currently at in the loop with a Tile function, which would return a new Tile, that I would then continue to draw with the loops current X and Y(multiplied with texture size, for obvious reasons). Because at this point, my Tile class didn't have X and Y values. Which seemed like an awful lot of throwing forth and back, and for some reason I felt it was a sort of primitive approach.
But then I went with another approach, I made a 2D Tile array and instead of having to assign each of the arrays positions an ID value, and then matching it, making a new instance of it, and then drawing it I could just loop the array and make the array position a new instance of the desired tile with the current X and Y in the constructor, and then draw it with the Tile.X and Tile.Y. Seemed more fitting.
At least for about a second before I ran the code, it did. See, now I needed to fully populate the whole array with tiles before I tried to draw it, or else it would return a null object reference( of course ), because previously I had a tile with the ID of 0, which it would just draw as the default if nothing else had been assigned. Somewhere in all the mess I had before, I also had a simple little "if" that just told it not to draw if the ID was 0, but it was later on I added that. I realize now that I could do the same with the Tile array, just instead of checking if 0, then checking if null I guess. But, I don't care much for checking if null. Again, that seems primitive in some way to me, as If the code is not fully thought through and doing something it's not supposed to. Also, I would now be in sort of a pickle if I tried to parse a simple xml file as tilemap, because I wouldn't know how to match the xml with a tile, where I previously just could've used the ID's. Btw, I've never worked with xml before, so I can only guess if that would be a problem.
*although if anyone would clarify exactly when or why a "project" could be considered an engine, however basic it may be, then that'd be cool too. Because I don't think I understand that fully yet.
** I realized that it would be more fitting to use foreach in this situation, but that would only work with the Tile array, afaik.
TL;DR So to end my question.. what is most fitting? The int[,] or the Tile[,] for a tilemap? And, should I instead of using 2 unrelated X and Y ints use vector2 in the Tile class for positioning?
Note - I am a computer science student, and we have only had the basic "hello world" training in so far, the rest I've learned is from personal interest. Also, please do correct me if there is any flaws, or terms used wrongly. I'm asking here because I want help, and I'll take any help offered, whether it's on- or off-topic. However, I would appreciate if it's given in a properly and constructive manner, please.
Edit: holy sht, I didn't even realize I had written that much. To whoever reads all of this, you're the real MVP!
TileStack
. WithTileStack
possessing some metadata about the coords, and aList
ofTile
.Tile
will then contain the Tile's id, and additional information about the tile instance, such as color, offsets, rotation, etc. – Bradley Uffner