Intmaps inside of intmaps
Monkey Forums/Monkey Programming/Intmaps inside of intmaps
| ||
Hello, im trying to create an intmap stored inside of an intmap in order to store map information. I read the forums and i liked this idea but could not get it to work. Currently i am using arrays inside of an array. I can get Intmaps to work but not Intmaps within Intmaps... Any help or advice would be appreciated. |
| ||
http://monkeycoder.co.nz/Community/posts.php?topic=1165 |
| ||
Ah Ha. I did not search in the forums for just "Maps". My mistake thanks very much! |
| ||
So far i can only get it to store the same intmap over and over :S What do i change to make it makes a bran new intmap?Import mojo Class Game Extends App Field DirtImg:Image Field ActiveMap:Mapclass Method OnCreate () 'DirtImg = LoadImage ("Dirt_Block.png") ActiveMap = New Mapclass 'Makes a map object ActiveMap.Setsize(45,45) 'Make a 150 X 150 map ActiveMap.Fillborders(1) 'Fill in the borders of the map with 1's ActiveMap.PrintContent() SetUpdateRate 60 End End Class Mapclass Field MapsizeX:Int Field MapsizeY:Int Field Mapcontent : IntMap<IntMap<IntObject>> = New IntMap<IntMap<IntObject>> Field SubMapcontent : IntMap<IntObject> = New IntMap<IntObject> Method OnCreate() Mapcontent.Set(0,SubMapcontent) SubMapcontent.Set(0,0) End Method Setsize(imputx,imputy) MapsizeX = imputx MapsizeY = imputy End Method Fillborders(fill) 'Fill the boarder tiles of the map For Local x:Int = 0 To MapsizeX If x = 0 Or x = MapsizeX 'For the left side and the right side fill the entire SubMapcontent in For Local y:Int = 0 To MapsizeY SubMapcontent.Add(y,fill) Next Mapcontent.Add(x,SubMapcontent) Print("Adding x = 0 or x = MapsizeX") Else SubMapcontent.Clear 'Clear SubMapcontent to make sure there is no leftover data SubMapcontent.Add(0,fill) SubMapcontent.Add(MapsizeX,fill) Mapcontent.Add(x,SubMapcontent) Print("Adding at 0 and MapsizeX") End Next End Method PrintContent() For Local a:Int = 0 To MapsizeX Local this:IntMap<IntObject> = Mapcontent.Get(a) For Local b:Int = 0 To MapsizeY If this.Contains(b) = 1 'Check if each co-ordinate has an item Print("It does " + a + ", " + b) Endif Next Next End End Function Main() New Game End |
| ||
Dont Intmaps use Set to set their contents? (instead of Add) I could make a demo for you as I have used this technique before if you want. first create a mapsquare class Class mapsquare field x:Int field y:Int field tree_type:String end class I added tree_type just as an example then you create your overall IntMap: Global overall_intmap:IntMap<IntMap<mapsquare>>=new IntMap<IntMap<mapsquare>> then when you want to add..for example.. an oak tree to 3,16 you check if something is already at its x co-ordinate.(3) local i:IntMap<mapsquare>=overall_intmap.Get(3) if not then you create it: if i=null local the_intmap:IntMap<mapsquare>=new IntMap<mapsquare> endif then add it to the overall intmap overall_intmap.Set(3,the_intmap) you create a new mapsquare with an oak tree and insert at its y coordinate: local m:mapsquare=new mapsquare m.x=3 m.y=16 m.tree_type="oak" the_intmap.Set(16,m) when you want to know what type of tree is at...for example...15,32 you do this: local the_intmap:IntMap<mapsquare>=overall_intmap.Get(15) if the_intmap is null there is no tree there so: if the_intmap<>null local m:mapsquare=the_intmap.Get(32) if m<>null Print m.tree_type else Print "no tree here" endif else Print "no tree here" endif |
| ||
Sure. I would love to learn this method of storing data. Thanks. |
| ||
see above |
| ||
Thanks for taking the time to explain it so well! I appreciate it, this looks like a great way of doing things, much better than my array of arrays :) |