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

Posted on May 11, 2010 at 4:50 PM

Copy this code into an html file and then run it and you will get the saving on your computer. All this does is make it so that you get the saved cookies on your computer instead of the site which makes it so that they don’t get saved to this domain but yours which for some reason seems to work better.

<html>
<head>
    <title>Text Adventure</title>
</head>
<body onunload="save()">
    <p id="p0.3">
        You do not have javascript enabled currently. Please enable javascript to play my
        game.</p>
    <script type="text/javascript">
        "use strict";
        var versionNumber = 0.3;
        var paragraphElementID = "p" + versionNumber;
        var currentRoom;
        var rooms = {};
        var inventory = {};
        var output = "";
        var header = "<b>JSAdventure version " + versionNumber + "</b> <br/> <br/>";
        var setUp = false;
        var jsonizer = {};

        // implementation of JSON.stringify borrowed from http://www.sitepoint.com/blogs/2009/08/19/javascript-json-serialization/
        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 ? "]" : "}");
            }
        };

        // implementation JSON.parse borrowed from http://www.sitepoint.com/blogs/2009/08/19/javascript-json-serialization/
        jsonizer.parse = function (str)
        {
            if (str === "")
            {
                str = '""';
            }
            var func = new Function("var p =" + str + ";" +
    "return p;");
            return func();
        };

        function CreateCookie(name, value, days)
        {
            var expires = "";
            if (days)
            {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                expires = "; expires=" + date.toGMTString();
            }
            value = value.replace(/\;/g, "#");
            document.cookie = name + "=" + value + expires + "; path=/";
        }

        function ReadCookie(name)
        {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i = i + 1)
            {
                var c = ca[i];
                while (c.charAt(0) === ' ')
                {
                    c = c.substring(1, c.length);
                }
                if (c.indexOf(nameEQ) === 0)
                {
                    var string = c.substring(nameEQ.length, c.length);
                    string = string.replace(/#/g, ";");
                    return string;
                }
            }
            return null;
        }

        function AddToOutPut(text)
        {
            output += text;
        }

        function AddLinkToOutput(text, functionName, functionParams)
        {
            AddToOutPut("<b onclick=\"" + functionName + "(" + functionParams + ")\">" + text + "</b>");
        }

        function AddToCurrentText(text)
        {
            document.getElementById(paragraphElementID).innerHTML += "<br/" + text;
        }

        function WriteStoredLine()
        {
            document.getElementById(paragraphElementID).innerHTML = header + output;
            output = "";
        }

        function WriteDiscription()
        {
            AddToOutPut("You are in " + currentRoom.name + "<br/><br/>");
            AddToOutPut(currentRoom.discription + "<br/><br/>");
            for (var direction in currentRoom.exits)
            {
                if (currentRoom.exits.hasOwnProperty(direction))
                {
                    AddLinkToOutput(direction + " ", "Go", "'" + direction + "'");
                    if (currentRoom.exits[direction].key)
                    {
                        AddToOutPut("there is a locked exit.<br/>");
                    }
                    else
                    {
                        AddToOutPut("there is an exit.<br/>");
                    }
                }
            }
            for (var x in currentRoom.items)
            {
                if (currentRoom.items.hasOwnProperty(x))
                {
                    AddToOutPut("There is a ");
                    AddLinkToOutput(x, "GetOrDrop", "'" + x + "'");
                    AddToOutPut(" in the room.<br/>");
                }
            }
            AddToOutPut("<br/>");
            for (var itemName in inventory)
            {
                AddToOutPut("You have a ");
                AddLinkToOutput(itemName, "GetOrDrop", "'" + itemName + "'");
                AddToOutPut(" in your inventory.<br/>");
                if (inventory[itemName].listOfUses)
                {
                    AddToOutPut("With it you can:<br/>");
                    for (var useName in inventory[itemName].listOfUses)
                    {
                        if (inventory[itemName].listOfUses.hasOwnProperty(useName))
                        {
                            AddToOutPut("&nbsp&nbsp&nbsp&nbsp&nbsp");
                            AddLinkToOutput(useName, "Use", "'" + itemName + "','" + useName + "'");
                        }
                    }
                }
            }
            WriteStoredLine();
        }

        function Room(name, discription)
        {
            this.name = name;
            this.discription = discription;
            this.actors = {};
            this.items = {};
            this.exits = {};
            this.visits = 0;
            this.events = [];
            rooms[name] = this;
        }

        function Exit(direction, room, targetRoom, key)
        {
            this.direction = direction;
            room.exits[direction] = this;
            this.targetRoom = targetRoom;
            this.key = key;
        }

        function Item(name, container, carryable, listOfUses)
        {
            this.name = name;
            if (container)
            {
                container.items[name] = this;
            }
            else
            {
                inventory[name] = this;
            }
            this.carryable = carryable;
            this.listOfUses = listOfUses;
        }

        function Event(room, action, timesInRoom)
        {
            rooms[room].events.push(this);
            this.action = action;
            this.timesInRoom = timesInRoom;
        }

        function GetOrDrop(item)
        {
            var found = false;
            for (var name in currentRoom.items)
            {
                if (name === item)
                {
                    if (currentRoom.items[name].carryable === true)
                    {
                        inventory[name] = currentRoom.items[name];
                        delete currentRoom.items[name];
                        WriteDiscription();
                        found = true;
                    }
                    else
                    {
                        AddToCurrentText("You can't carry that! It's too big!");
                    }
                }
            }
            if (!found)
            {
                for (var itemName in inventory)
                {
                    if (itemName === item)
                    {
                        currentRoom.items[itemName] = inventory[itemName];
                        delete inventory[itemName];
                        WriteDiscription();
                    }
                }
            }
        }

        function Go(direction)
        {
            for (var i in currentRoom.exits)
            {
                if (currentRoom.exits[i].direction === direction)
                {
                    var door = currentRoom.exits[i];
                    if (!door.key)
                    {
                        if (rooms[door.targetRoom] === currentRoom)
                        {
                            AddToOutPut("You go through the exit and somehow come right back to the room you were just in... Weird...<br/><br/>");
                        }
                        currentRoom = rooms[door.targetRoom];
                        currentRoom.visits += 1;
                        WriteDiscription();
                    }
                    else
                    {
                        for (var x in inventory)
                        {
                            if (x == door.key.name)
                            {
                                door.key = null;
                                currentRoom = rooms[door.targetRoom];
                                currentRoom.visits += 1;
                                WriteDiscription();
                                AddToCurrentText("You unlocked the door with your " + x + " and step through.");
                                break;
                            }
                        }
                        if (door.key)
                        {
                            AddToCurrentText("You can't go that way because it's locked with a " + door.key.name);
                        }
                    }
                    break;
                }
            }
        }

        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();
                        }
                    }
                }
            }
        }

        function save()
        {
            if (setUp === true)
            {
                var inventoryJSON = jsonizer.stringify(inventory);
                var roomsJSON = jsonizer.stringify(rooms);
                CreateCookie("roomsJSON" + versionNumber, roomsJSON, 50);
                CreateCookie("inventoryJSON" + versionNumber, inventoryJSON, 50);
                CreateCookie("currentRoomName" + versionNumber, currentRoom.name, 50);
                CreateCookie("currentText" + versionNumber, document.getElementById(paragraphElementID).innerHTML, 50);
                CreateCookie("saved" + versionNumber, "saved", 50);
            }
        }

        if (ReadCookie("saved" + versionNumber))
        {
            rooms = jsonizer.parse(ReadCookie("roomsJSON" + versionNumber));
            inventory = jsonizer.parse(ReadCookie("inventoryJSON" + versionNumber));
            currentRoom = rooms[ReadCookie("currentRoomName" + versionNumber)];
            var text = ReadCookie("currentText" + versionNumber);
            text = text.replace(/$/g, "");
            document.getElementById(paragraphElementID).innerHTML = text;
            setUp = true;
        }
        else
        {
            var room1 = new Room("Room1", "A plain room... what else?");
            var room2 = new Room("Room2", "Another plain room... sigh...");
            var room3 = new Room("Room3", "Yet a third plain room... I'll give you a guess what the next one is!");
            var room4 = new Room("Room4", "Yep! Another plain one.");
            var room5 = new Room("Room5", "This is a secret room!!!!");
            var room6 = new Room("Room6", "Congrats! you just got past the first puzzle.");

            currentRoom = room1;
            currentRoom.visits += 1;

            var bouncyBall = new Item("small bouncy ball", room1, true, null);
            var bolder = new Item("large rock", room3, false, null);
            var key = new Item("key", room3, true, null);
            var bombuses = {};
            bombuses["blow up rock"] = "" +
                "for (var i in currentRoom.items)" +
                "{" +
                    "if (currentRoom.items[i].name == 'large rock')" +
                    "{" +
                        "delete currentRoom.items[i];" +
                        "for (var x in inventory) " +
                        "{" +
                            "if (inventory[x].name == 'bomb') " +
                            "{" +
                                "delete inventory[x];" +
                            "}" +
                        "}" +
                        "for(var room in rooms)" +
                        "{" +
                            "if(room == 'Room3')" +
                            "{" +
                                "new Exit('Down', rooms[room], 'Room6', null);" +
                            "}" +
                            "else if(room == 'Room6')" +
                            "{" +
                                "new Exit('Up', rooms[room], 'Room3', null);" +
                            "}" +
                        "}" +
                        "WriteDiscription();" +
                        "AddToOutPut('<br/>You use your bomb to blow up the rock<br/> and it leaves a small opening you can crawl through.');" +
                        "AddToCurrentText(output);" +
                        "output = '';" +
                        "return;" +
                    "}" +
                "}" +
                "AddToCurrentText('<br/>There is nothing to blow up in here...');";
            var bomb = new Item("bomb", room5, true, bombuses);

            var room1exit1 = new Exit("South", room1, "Room2", null);
            var room2exit1 = new Exit("North", room2, "Room1", null);
            var room2exit2 = new Exit("East", room2, "Room3", null);
            var room3exit1 = new Exit("West", room3, "Room2", null);
            var room3exit2 = new Exit("South", room3, "Room4", null);
            var room4exit1 = new Exit("North", room4, "Room3", null);
            var room4exit2 = new Exit("South", room4, "Room5", key);
            var room5exit = new Exit("North", room5, "Room4", null);

            WriteDiscription();
            setUp = true;
        }
    </script>
</body>
</html>

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading