SoKoBan

Member
Posts: 102
Joined: 2005.01
Post: #1
MM i think thats what its called, anyway i have the basc thing build the little guy runs around and pushes boxes correctly (although he jumps from square to square no animation) the process of hardcoding in all the levels would suck, so i want to make it read a text file that has 20 rows and 20 columns

the first level looks like this
Code:
####################
#####+++############
#####$++############
#####++$############
###++$+$+###########
###+#+##+#####++&&##
#+$++$++++++++++&&##
#####+###+#@##++&&##
#####+++++##########
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################


# = wall
@ = hero
& = goal pad
+ = ground
$ = movable object


how can i get a loop to read through this and draw the correctly

:edit: i drew the level wrong...... but yea
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #2
How is it being stored? In a string, char array, NSString, random something else?
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #3
if you mean how is the game board being stored, i have 3 20x20 arrays, objects, background, and the accual rects themselves, i just need to know how to extract the character from a rtf file... i can do the rest myself im pretty sure
Quote this message in a reply
Member
Posts: 86
Joined: 2005.01
Post: #4
Just load it with fstream and read it with getline statements, then go through the array line and break it up character by character.

http://rav.realitybytes.tk/oldies/Map.cpp
http://rav.realitybytes.tk/oldies/Map.h

Is my current map class (Yeah, it's ugly as hell and doesn't work very nicely) but it will load a map from a text file.

Newest game: Glow, a sci-fi RPG with lots of zombie bashing. Get it: OS X
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #5
ive never heard of either of those commands, so
here is my guess

Code:
- (void)loadMapFromFile:(NSString *)map
{
    ifstream TheFile(map);
    //if that is right is TheFile made and defined? or what do i have to pre-define that as?
    if (!TheFile)    {
        NSRunAlertPanel(@"Map Missing!",@"You are missing map %@", @"OK", nil, nil, map);
    }
    else {
        int x, y;
        for (x = 0 ; x < 20 ; x++)    {
            for (y = 0 ; y < 2 ; y++)    {
                TheFile.getline
                //now im confused
            }
        }
    }
}
//and can obj-c/cocoa do this?
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #6
Coin Wrote://and can obj-c/cocoa do this?

NSString *text = [NSString stringWithContentsOfFile:@"myfile.txt"];
char ch = [text characterAtIndex:index];

NSString
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #7
NSRunAlertPanel(@"level one", @"%@", @"OK", nil, nil, [NSString stringWithContentsOfFile:@"lvl1.rtf"]);


displays NULL

where does the text file need to be?

lvl1.rtf is in the base level of my files/groups

yes... i spelled it right

:edit: do i have to use a path that points the bundle?
how do you show the bundle in a path

NSMainBundle/Contents/Resources/lvl1.txt

?
Quote this message in a reply
Member
Posts: 40
Joined: 2004.12
Post: #8
I believe the parent directory of the app is the the execution directory, so the path to your resource map is "MyApp.app/Contents/Resources/myResource.txt" You may want to save your files as plaintext because .rtf is going to have a lot of extra stuff in there you don't want to parse through!

Jericho
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #9
Well,

I have some code for Carbon that loads levels for my game....
And instead of storing them in .rtf store them in .txt.......

http://binary.macintoshdevelopers.net/Shards.html

anyway, If it's a certian size....
Load it into an array (MyArray[20][20])

Take the array and set your level class or struct
against a costant....

Code:
enum {
wall = #,
hero = @,
goal pad = &,
ground = +,
movable object = $
};

/// later in your life
Init(x,y)
{
Level[x][y].type = WhatType(MyArray[x][y]);

}

WhatType(x,y)
{
Switch (MyArray[x][y])
case wall:
return kwallFormatedForClass/Struct; // whatever you use for the class/struct storage...
break;

// mostly the same as the first one...
case hero:
case goal pad:
case ground:
case movable object:

}





Then basicly you need a draw function Grin

Hope that helps,
BinarySpike Cool
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #10
Oh,

You might want to add a "nothing" object so that you have better proformance...
Grin
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #11
I'm using cocoa, heres what i have

Code:
- (IBAction)open:(id)sender
{
    NSString *lvl1 = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"lvl1" ofType:@"txt"]];
    int a, b;
    int index = 0;
    for (a = 0 ; a < 20 ; a++)    {
        for (b = 0 ; b < 20 ; b++)    {
            switch ([lvl1 characterAtIndex:index])    {
                case '#':
                    enviro[a][b] = WALL;
                    objects[a][b] = WALL;
                    break;
                case '@':
                    objects[a][b] = HERO;
                    xpoh = a;
                    ypoh = b;
                    break;
                case '+':
                    enviro[a][b] = GRASS;
                    objects[a][b] = NONE;
                    break;
                case '$':
                    objects[a][b] = BLOCK;
                    break;
                default:
                    objects[a][b] = NONE;
                    enviro[a][b] = GRASS;
                    break;
            }
            index++;    
        }
        index++;
    }
    [self setNeedsDisplay:YES];
}


that loads the level ALMOST correctly, it loads the top of the map on the left and so on...no idea why
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #12
Well,

That has something to do with a and b....

try switching them...
(instead of [a][b] try [b][a])

This will flip it somehow...
if this doesn't work then it has something to do with the loading...

Oh, along with that "nothing" object you might want to add a
multiple HERO stopper...
(so people can't have more than one Hero)
(unless you want to be able to do that...)

Global warming is caused by hobos and mooses
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #13
thats all handled elsewhere

also i already reversed a and b and

blah[b][a] makes it upside down...... really great

im going to be in vegas for a while, stupid other life thing...

anyway here

http://www.nomorepasting.com/paste.php?pasteID=30895

its all there tell me how to be more effient/make it work/anything i should fix
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #14
Coin Wrote:blah[b][a] makes it upside down...... really great

Ok, then blah[a][b] should be right-side up, instead of...
Coin Wrote:the top of the map on the left and so on

Coin Wrote:its all there tell me how to be more effient/make it work/anything i should fix

Well, everything looks good Grin

Global warming is caused by hobos and mooses
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #15
Coin Wrote:im going to be in vegas for a while, stupid other life thing...
LOL

Well, don't lose your shirt Wink, Smile, Grin, LOL.

Global warming is caused by hobos and mooses
Quote this message in a reply
Post Reply