4

So, I'm building myself a browser based rpg using Javascript. Originally, my level had a single layer and was loaded from a javascript 2d map array. However, I'm changing my code to allow support for multiple layers loaded from a file.

I can retrieve the file data without too many problems, however, I have no idea how to parse the information into useable arrays.

The contents of my text file are as follows;

LAYER
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
LAYER
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0

My Ajax and PHP for retrieving the level;

// JAVASCRIPT
    $.ajax({
            type: 'POST',
            url: 'lib/ajax.php',
            data: {method: 'getLevel'},
            success: function(data){

            },
            error: function(x, h, r){
                console.log(x, h, r);
            }
        })

// PHP FILE 2

public function getLevel(){
   $file = file_get_contents('../levels/level1.txt');
   echo $file;
}

There is an intermediate file handling all of my ajax requests, passing them to a functions class.

I can get my level data fine, I just don't know what to do with it once I have it.

I know I can get someway towards achieving this by adding newline characters at the end of each group, and parsing them that way. However, this will become a nightmare when implementing a tile editor in the future. Do you guys have any suggestions how to go about this? Would you suggest parsing at the php or javascript level, or both?

  • Can you use json to store your data? It would make your life a lot easier. – Mike Sep 29 '15 at 13:45
  • Sure, I'm up for using anything at this point. Would it still be within the realms of possibility of converting the map above, to say, a 2d array that I could reference like map[x][y] using json? – Lewis Sep 29 '15 at 13:46
  • 2
    Yup, best part is json is already a JavaScript object, so you don't have any further work to do. You could access a point with data.layer1[2][4] in your Ajax callback. I'll post the json version of your input file when I'm at a computer (too hard on my phone) – Mike Sep 29 '15 at 13:58
  • Brilliant, thanks for your speedy answer! I'll be sure to upvote you later! – Lewis Sep 29 '15 at 13:59
  • I have updates my answer for @Mike's surgestion so you can see it in JSON if you going with that option wait for Mike to post his answer and mark that one as correct i have just added his JSON implementation so you can see it – Martin Barker Sep 29 '15 at 14:43
1

If you data is following this layout

LAYER
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
LAYER2
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0
0, 0, 0, 0, 0

You can just do it like this

function parseLayer($text){
    $layers = array();
    $lines = explode("\n", $text);
    $lastLayer;

    $currArray = array();

    foreach($lines as $line){
        if(strpos($line, ",") === false){
            if(!empty($lastLayer)){
                $layers[$lastLayer] = $currArray;
                $currArray = array();
            }
            $lastLayer = trim($line);
        }else{
            $nodes = explode(",", $line);
            $nodeList = array();
            foreach($nodes as $node){
                $nodeList[] = trim($node);
            }
            $currArray[] = $nodeList;
        }
        $layers[$lastLayer] = $currArray;
    }
    return $layers;
}

Then to pass it to Javascript you can use JSON for php json_encode

Since @Mike is on his phone here is the code for you:

{"LAYER":[
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
],
"LAYER2":[
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
]
}
  • This is a good answer, thank you! (upvoted) - I'll see how @mike's solution turns out before accepting. – Lewis Sep 29 '15 at 14:37
  • Sorry i had a bug in it i have fixed it for you gives you $layers["layer_name"][x][y] – Martin Barker Sep 29 '15 at 14:38
  • 1
    The bottom part is the exact structure I was suggesting, only store your data in that format to take out the processing overhead. – Mike Sep 29 '15 at 14:45
  • Thanks guys. @Mike I've upvoted your comments to offset the accept. – Lewis Sep 29 '15 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.