Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Check string in text document and set correct texture accordingly XNA

We're currently working on a school project where we are going to make a very simple little game, but here is my problem:

I currently have a text document in which I write coordinates where I want to place a certain sprite using "streamreader" formatted like this: CoordinateX:CoordinateY:texturename.
I have made another classfile called "level" with methods with get and set functions for each value.

Code
while ((k = blockposition.ReadLine()) != null)
            {
                string[] database = k.Split(':');
                tilestemppos.X = float.Parse(database[1]);
                tilestemppos.Y = float.Parse(database[0]);
                tilestexturess = database[2];
                tilesposlist.Add(new level(tilestemppos.X, tilestemppos.Y,tilestexturess));
            }

Here it adds the strings and floats from the text document into the list "List<level> tilesposlist = new List<level>();".

Code
for (int i = 0; i < tilesposlist.Count; i++)
            {
                tiles_pos.X = tilesposlist.ElementAt(i).blockpositionX;
                tiles_pos.Y = tilesposlist.ElementAt(i).blockpositionY;
                spriteBatch.Draw(tiles, tiles_pos, Color.White);
            }

Here it draws the "tiles" sprite which is a Texture2d using the correct coordinates.

Now I want the program to check the "tilestexturess" in the text document and change the "tiles" texture to the correct texture from all the textures I have.

I was thinking of having another list where I put all textures and then make it check if the names match. How do I do this in the most simplest form?
I would probably be looking to create struct and slap a dictionary of the struct against a texture name. I am presuming that the same x and y would never map to different textures in the same context?

So end up with:

var levelTextures = new Dictionary<myStruct, textureName>();

With a large amount of cross referencing, dictionary are probably going to be your best bet. If you need to update these on a high ratio you'd probably want to investigate some SlimLock functions to prevent reading out of date info. If you have a pretty finite list of textures, consider using an Enum to map the textures. It's just much more elegant to reference than direct string comparisons.

While this might not give you a copy and paste answer, I figure this would point you in the right direction.
You seem to have two posts? I think you've made a mistake where tiletemppos.X is taking the Y value and vice versa.
Thank you guys. Solved it by doing two different text documents, one for coordinates and one for sprites. It all works fine now :D


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by MordiciaNov 15, 2019 07:21 PM 3 209 Nov 15, 2019 07:21 PM
Last Post: Mordicia
[Python] by Sparkling JackAug 9, 2018 01:02 AM 2 166 Aug 9, 2018 01:02 AM
Last Post: Sparkling Jack
[.NET] by SuliumJul 29, 2018 09:33 PM 1 171 Jul 29, 2018 09:33 PM
Last Post: SpookedOnion