c# - How do I access data in a .txt file to assign specific tiles with certain properties in XNA? -


okay, @ first may thinking; "oh great, question guy asking has not researched before..."

i assure have researched topic thoroughly, not luck specific problem. problem have read this tutorial allows me create simple .txt file handle layout of level, can later iterated through read integer values, , draw corresponding tile textures onto screen.

in tutorial linked above, happens map.cs class created, responsible loading in tile textures:

void loadtiletextures(contentmanager content) {       tiles = new list<texture2d>();       tiles.add(content.load<texture2d>("blocks/sky"));           // 0       tiles.add(content.load<texture2d>("blocks/grass_block"));   // 1       tiles.add(content.load<texture2d>("blocks/dirt_block"));    // 2       tiles.add(content.load<texture2d>("blocks/rock_block"));    // 3 } 

these textures presume assigned there numerical value in terms of order loaded in. after this, method created responsible loading, , guess assigning tile data:

void loadmapdata(string name) {       string path = name + ".txt";        // width , height of our tile array       int width = 0;       int height = file.readlines(path).count();        streamreader sreader = new streamreader(path);       string line = sreader.readline();       string[] tileno = line.split(',');        width = tileno.count();        // creating new instance of tile map       tilemap = new int[height, width];       sreader.close();        sreader = new streamreader(path);        (int y = 0; y < height; y++)       {              line = sreader.readline();              tileno = line.split(',');               (int x = 0; x < width; x++)              {                  tilemap[y, x] = convert.toint32(tileno[x]);              }          }           sreader.close(); } 

http://programming.nullanswer.com/forum/14791

the aforementioned link question posted same topic, same tutorial code. guy answered question little vague me personally. understand getting at, not experienced enough in own mind elaborate on suggesting.

just re-iterate, issue not sure how data in .txt file being assigned corresponding tile value. if knew how being done, able assign values these tiles within .txt file. specifically, know how assign value each tile in .txt file such boolean, whether solid or not, can go onto creating collision() method manipulates '.intersects' function.

sorry if has been loaded question, or come across confusing, have reached wall topic requires little assistance.

thank taking time read question,

kind regards, olly.

happy coding everyone! ^.^

it sounds you're on verge of outgrowing particular tile architecture.

in current code, tiles have 1 property: texture. therefore it's sufficient track tile definitions using list of textures. if want add additional properties tiles (e.g. solid flag), should start creating tile definition can expanded add features:

class tiledef {     public texture2d texture { get; set;}     public bool issolid { get; set; } } 

you may want load tile definitions file, can create them programmatically in same way loading textures before:

// replaces function loadtiletextures void loadtiledefinitions(contentmanager content) {     tiles = new list<tiledef>();     tiles.add(new tiledef() {         texture = content.load<texture2d>("blocks/sky"),         issolid = false }); // 0     tiles.add(new tiledef() {         texture = content.load<texture2d>("blocks/grass_block"),         issolid = true}); // 1     tiles.add(new tiledef() {         texture = content.load<texture2d>("blocks/dirt_block"),         issolid = true}); // 2     tiles.add(new tiledef() {         texture = content.load<texture2d>("blocks/rock_block"),         issolid = true}); // 3 } 

the logic loading tile map stays same, because tile numbers have not changed before. you'll have change drawing logic new texture property.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -