ARRAYCEPTION

Posted on February 23, 2012 | 6 minute read

ARRAYCEPTIONUpdate - Feb 23, 2012, 12:50pm

In my previous post I talked about how I know that I should be sticking away from global arrays…in this post I’m going to talk about how I now have a global array of global arrays. Yes, I am facepalming right now.

I mean, it’s working. It’s doing what I need it to do (as far as what I’ve built so far - it actually needs to do much, much more), but it feels extremely messy. I have no doubt that I’m probably making this more convoluted than it has to be.

So here’s what I have:

    • Each ‘base’ entity in the game creates its own path array when clicked.
    • There is a controller class that holds an (empty by default) base array when the game is launched. This is a global array.

The rules

  • The player selects a base by clicking on it. There will be multiple base entities with different attributes (so base1.js, base2.js, except with more descriptive names), and sometimes multiple instances of the same bases in each level

  • They then draw a path by clicking on a series of nodes from the base to a set destination

  • When the path is complete, they can click “Go” to spawn entities from the base and have them move along the path to the destination

If the player wants to delete part of a path They have to click on the nodes in backwards order (so first in last out) to deselect the parts they want to change.

If the player wants to delete the entire path They can click on their selected base, which both deselects the base, removes it from the base array, and clears its entire path array.

How I’m currently doing it

Clicking on the base

When the player clicks on a base entity, the empty path array gets created in that base entity. At the same time, that base is pushed to the base array, which is a global array in the controller class. This is done like this:

[crayon title="base.js”] clicked: function () { if (this.clickcount == 0 ) { this.arrayname = [this]; // Create path array ig.game.base_array.push(this); // Add this base to the base array this.selected = true; console.log(ig.game.base_array); this.clickcount++; }

else if (this.clickcount == 1 ){
    // Remove coordinates of clicked base from path array
    // Iterate through base array
    for(var i = ig.game.base_array.length-1; i >= 0; i--){
        // Test base for match
        if(ig.game.base_array[i] == this){
            // Splice base from array when match is found
            ig.game.base_array.splice(i,1);
            this.selected = false;
            this.clickcount = 0;
        }
    }
    // Clear entire path array (base is required for the path to remain)
    this.arrayname = []; // NOTE: Remember to try clearing this properly by changing to this.arrayname.length = 0; instead.
}

}, [/crayon]

Selecting the path

When a base is selected, the player can draw a path from that base along existing nodes in node.js:

[crayon title="node.js”] clicked: function() { // Get name of first path array from array of selected bases (base array) var path_array = eval(ig.game.base_array[0].arrayname); // if at least one base is selected and the current node is inactive, do this: if (this.clickcount == 0 && ig.game.base_array.length > 0) { // Add this node to the path array path_array.push(this); this.currentAnim = this.anims.selected; this.clickcount++; }

// If this node is already in the path array, do this:
else if (this.clickcount == 1) {
    // If this is the last node in the path array, do this:
    if (this == path_array[path_array.length-1]) {
        // Remove clicked node from path array
        path_array.pop();
        this.currentAnim = this.anims.deselected;
        console.log ('Time to pop!');
        // Reset click count to 0 so that node can be selected again if re-clicked
        this.clickcount = 0;
    }
}
console.log(path_array);

}, [/crayon]

The player can then select the destination in the same way as above, except the destination node is its own entity (in destination.js) and there is only one instance of it on the screen.

Moving along the path

When the player has a complete path that starts from a base entity and finishes at the destination entity in the path array, they can click Go in Gobutton.js:

[crayon title="Gobutton.js”] clicked: function() { var path_array = eval(ig.game.base_array[0].arrayname); if (path_array[path_array.length-1] == ig.game.destination) { // Call the ‘spawn’ function of the first item in the path array, which is always the base path_array[0].spawn(); } }, [/crayon]

The first item (the base element) is then shifted off of the path array, followed by the entities being spawned moving through the remaining nodes.

Array naming and stuff…

The problem here is that I’m doing this dodgy roundabout way of defining the path array. In the base.js init function:

[crayon] this.name = ‘base_50px’ + this.id; // Set what the name of this array is going to be var arrayname = ‘ig.game.path_array’ + this.name; [/crayon]

So I’m kind of naming it like I would define a global array, and then using eval to actually let myself refer to it from other entities…

So I’m calling this thing ‘arrayname’ in base.js, but in other entities I then have to use arrayname along with the global base array to set a new name for it:

[crayon]this.path_array = eval(ig.game.base_array[0].arrayname)[/crayon]

I can then push(), pop(), etc as required by referring to the array as this.path_array. However, this means I now have two different names for the same array in different places - in base.js where the array is set it’s “arrayname”. Everywhere else it turns into “path_array”. This also means I have to define path_array from scratch each time I want to use it in a new entity. It works in the sense that it’s functional, but it seems all kinds of screwed up.

In addition

In addition, James and Simon suggested some different ways to move the entities from node to node when Go is clicked, which I’m also itching to try out. I’ve been holding off for now as I want to get the rest of this path follow stuff done and the range checking I’m doing now is working for the time being, but it’s something I have to remember to go back to and try.

Update:

Feb 23, 2012 12:50pm

I modified this somewhat at lunch. I am now doing this in the Controller class:

[crayon]ig.game.path_array = eval(ig.game.base_array[0].arrayname)[/crayon]

And then simply referring to ig.game.path_array whenever I need to use the path array. I think originally I had this screwed up attempt to not use a global array for this, but it ended up being a roundabout way to refer to a global array anyway…this is just easier. I’m hoping I’ll still find a way around having to do this in the future, but now I can just refer to ig.game.base_array and ig.game.path_array when needed.

I’ve also created a new function in node.js to clear all nodes when the base is deselected. I’m having some weird problems with the animation not changing over for this, but otherwise it’s working. Will have a proper look at the animation problem tonight.




Categories:game dev dev games
comments powered by Disqus