startshape realStart rule realStart { start { x 2 } } background { b -1 } rule start { terminal4 { r 12 } } rule start { terminal1 { r 12 } } rule start { terminal4 { r 12 } } rule start { terminal4 { r 12 } } rule start .05 { terminal2 { r 12 } } rule terminal4 { SQUARE { b 0.50 s .055 sat 0.40 hue 95 } SQUARE { b 1 s .055 a -.8 } train { x .06 } train { r 90 y .06 } train { r 180 x -.06 } train { r 270 y -.06 } } rule terminal2 2 { SQUARE { b 1 s .055 a -.85 } train { r 90 y .06 } train { r 270 y -.06 } } rule terminal2 1 { SQUARE { b 1 s .055 a -.8 } train { x .06 } train { r 90 y .06 } } rule terminal2 1 { SQUARE { b 1 s .055 a -.85 } train { x.06 } train { r 270 y -.06 } } rule terminal1 { CIRCLE { b 1 s .055 a -.9 } train { r 90 y .06 } } rule terminal1 { CIRCLE { b 1 s .055 a -.9 } train { r -90 y -.06 } } rule train 1 { CIRCLE { b 1 s .045 a -.9 } train { x .06 } } rule train 2 { terminal1 { } } rule train .02 { terminal2 { } } rule train .005 { terminal4 { } } rule train .03 { CIRCLE { b 1 s .03 a -.9 r -95 } }

Keith's Blog

A teen's .net musings

JSAdventure Item Uses and Saving

Posted on May 11, 2010 at 1:30 PM

In this version I have added two major features. First is the ability to create uses for the items. For instance I made a boulder in the previous version that was not carryable. Now with the bomb in the new locked room you can blow up the boulder and reveal an exit leading down to a new room. This was all possible through JavaScript's eval. I take the string which is defined in creation of the item and call eval on it which will parse the string and execute it as though it were just plain JavaScript. I have found this somewhat useful and effective only because it allows my program to be easily extended, though there are some things that you will need to think about before using it every where. First is the fact that all eval code should only be taken from your own program and nowhere else. This is because if you were to gather it from some other source it might be liable to tampering and could cause data loss or some other hap hazard effect. The only places I have found that this was useful was in this case and in json which I have used in this update as well. Here is the code for the uses.

function Use(nameofitem, nameofuse)
{
    for (var i in inventory)
    {
        if (i === nameofitem)
        {
            var tool = inventory[i];
            for (var z in tool.listOfUses)
            {
                if (z === nameofuse)
                {
                    var func = new Function(tool.listOfUses[z]);
                    func();
                }
            }
        }
    }
}

As you can see I try not to have actual references to any of the objects and so I keep the names instead. And using the objects in JavaScript (or associative arrays, they are one in the same) I can access the item or exit or whatever that I need just from the name of the object and it’s container. Then once I have the item I iterate through the objects list of uses to find the one I want and then I execute it. It’s as simple as that, but now if I wanted I could create an admin console or something in c# to generate whatever I’d like and just convert it to json to be used as the map or objects that I can create.

The last thing that I added into this version was the use of cookies for saving state. I searched and searched all over the internet for a good serializer when I realized that it was all right under my nose! json when trying to serialize straight to and from JavaScript on the same client is amazing and it was quite straight forward too. I just take some code to serialize ( borrowed from here ) into json and then just call eval to bring it back.

jsonizer.stringify = function (obj)
{
    var type = typeof (obj);
    if (type !== "object" || obj === null)
    {
        // simple data type
        if (type === "string")
        {
            obj = '"' + obj + '"';
        }
        return String(obj);
    }
    else
    {
        // recurse array or object
        var nameOfElement;
        var v;
        var json = [];
        var isArray = (obj && obj.constructor === Array);
        for (nameOfElement in obj)
        {
            if (obj.hasOwnProperty(nameOfElement))
            {
                v = obj[nameOfElement];
                type = typeof (v);
                if (type === "string")
                {
                    v = '"' + v + '"';
                }
                else if (type === "object" && v !== null)
                {
                    v = jsonizer.stringify(v);
                }
                json.push((isArray ? "" : '"' + nameOfElement + '":') + String(v));
            }
        }
        return (isArray ? "[" : "{") + String(json) + (isArray ? "]" : "}");
    }
};

The code is a little complicated ( I still don’t exactly know all that v does, but I the basic idea is to iterate over all of the properties of the object (or associative array which allows us to for loop over them) and either return a hard coded value, or rinse and repeat on the new object. Then what you get back is all of the literals of the object that you wanted serialized. Then when I want any of it back I just eval the string and I get back the object with all of it’s literals and everything still intact.

Ok, so I lied about that being the very last thing but this one is minor. I was looking for a good source analysis program for JavaScript and stumbled over jslint. At first the site looks like some small side project that somebody just works on in their spare time, but don’t be fooled, it can pack a powerful punch! The premise is that you take your crappy JavaScript code and put it all into jslint and it will yell at you telling you that it’s crappy. It actually found over 1000 problems with my code and I fixed every single one. Now I'm not going to say that my program is perfect ( you have to realize that I started coding in JavaScript last week ) but it sure is a lot better than it was.

So if anyone cares… Here it is!

You do not have javascript enabled, please enable it and try again.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading