Graphical Window Sprites

version 1 by Erik Temple

  • Home page
  • Beginning
  • Previous



  • Example: *** Castle of Sprites - An onscreen map of a multilevel space that reveals itself as the player moves through it.

    Let's say we want to create a graphical map of a multilevel space, and we want the different levels to appear onscreen at the same time while still remaining visually distinct for the player. This example takes one approach to this by modelling a multistory castle. As we move upward, rooms on the upper levels are drawn, while levels beneath are "grayed out" by a semitransparent sprite. As we move back down, the upper levels are successively peeled off--removed from the display--and the semitransparent occluding layer is moved downward appropriately. It may help to organize things beforehand using display layers. This example is organized using twelve layers, including a top-level display layer for a title that displays the name of the layer, and interstitial layers where the icons corresponding to the PC and his torch will be placed as we move upward:

        12 Titles (corresponding to the current region)
        11 Player's avatar (Tower level)
        10 Torch icon (Tower level)
        9 Tower level map
        8 Occluder (darkens the Palace and Dungeon maps when we're in the Tower)
        7 Player's avatar (Palace level)
        6 Torch icon (Palace level)
        5 Palace level map
        4 Occluder (darkens the Dungeon map when we're on higher levels)
        3 Player's avatar (Dungeon level)
        2 Torch icon (Dungeon level)
        1 Dungeon level map

    Note: Inform automatically inserts spaces around brackets, so that the coordinates in this example will appear with spaces around them when viewed in the list of Installed Extensions in the documentation pane. When writing, it is not necessary to include the padding between the braces and the numerals, though a space after the comma is required.

    We first the graphics window size and provide the necessary assignment of a background using the "when play begins" activity; in this case, we are simply specifying a background grid sans image.

        "Castle of Sprites"

        Include Graphical Window Sprites by Erik Temple.
        
        The graphics window pixel count is 350.

        The first when play begins rule:
            Unless glulx graphics is supported:
                say "This interpreter does not support graphics. Now exiting...";
                follow the immediately quit the game rule.

        When play begins:
            Now grid-background is true;
            change grid-x to 780;
            change grid-y to 555;
            change the currently shown picture to no-picture;
            now the current graphics drawing rule is the sprite-background scaled-centered display rule.


    Before defining sprite objects, we need to define the figures associated with them, and the files that each figure-name refers to:

        Figure of Tower Title is the file "Tower-Title.png". Figure of Palace Title is the file "Palace-Title.png". Figure of Dungeon Title is the file "Dungeon-Title.png". Figure of Occluder is the file "Occluder.png". Figure of Cell is the file "Cell.png". Figure of Central Hall is the file "Central Hall.png". Figure of Eastern Hall is the file "Eastern Hall.png". Figure of Western Hall is the file "Western Hall.png". Figure of Guardroom is the file "Guardroom.png". Figure of Torture Chamber is the file "Torture.png". Figure of Tower is the file "Tower.png". Figure of Palace is the file "Palace.png". Figure of Dungeon Stairs is the file "Dungeon Stairs.png". Figure of Palace Stairs is the file "Palace Stairs.png". Figure of Tower Stairs is the file "Tower Stairs.png". Figure of Player is the file "Player Icon.png". Figure of Torch is the file "Torch.png".

    The following code defines the display-layer for each of the regions of the map. The sprites associated with the rooms in the each region will be be displayed on the appropriate layer (see the final block of code below). We use a table to quickly define the sprites that will display the name of the region we're in.

        The Dungeon Area is a region. The Cell, the Central Hallway, the Hallway East, the Hallway West, the Guard Room, the Dungeon Stairs, and the Torture Chamber are in the Dungeon Area.

        The Palace Area is a region. The Palatial Hall and the Palace Stairs are in the Palace Area.

        The Tower Area is a region. The Tower Chamber and the Tower Stairs are in the Tower Area.

        A region has a number called the region-layer. The region-layer of the Dungeon Area is 1. The region-layer of the Palace Area is 5. The region-layer of the Tower Area is 9.

        A title is a kind of sprite. The display-layer of a title is usually 12. Some titles are defined by the Table of Title Sprites.

        A region has a sprite called the title-sprite. The title-sprite of the Tower Area is Tower-title. The title-sprite of the Palace Area is Palace-title. The title-sprite of the Dungeon Area is Dungeon-title.

        Table of Title Sprites
      title  sprite-identity  sprite-coordinates  
      Tower-title  Figure of Tower Title  {63, 59}  
      Palace-title  Figure of Palace Title  {63, 59}  
      Dungeon-title  Figure of Dungeon Title  {63, 59}  


    The following blocks of code define the sprites associated with things--the player's avatar, the icon for the torch, and the sprites that represent each of the rooms on the map. Note that the torch sprite has two pairs of coordinates (defined as "lists of numbers") that we can later use to relate the position of the torch icon to that of the player, allowing the player avatar to look as if it is carrying the torch. We will use the extension's "offset by" phrase in the final block of code (see below) to apply these offsets. The coordinates of the player-sprite are given as a property of the rooms, so that the avatar can be moved around the map as the player moves through the world (see below).

        The player-sprite is a sprite. The sprite-identity is Figure of Player. The display-layer is 3. The player-sprite is display-active. The sprite-scaling factor is 0.5200.

        The torch-sprite is a sprite. The sprite-identity is Figure of Torch. The display-layer is 2. The torch-sprite is display-inactive. The torch-sprite has a list of numbers called the held-offset. The held-offset of the torch-sprite is {-16, 2}. The torch-sprite has a list of numbers called the dropped-offset. The dropped-offset of the torch-sprite is {-30, 4}.

        A room-sprite is a kind of sprite. A room has a room-sprite called the location-sprite. Some room-sprites are defined by the Table of Location Sprites.

        The location-sprite of the Tower Chamber is Tower-sprite. The location-sprite of the Palatial Hall is Palace-sprite. The location-sprite of the Cell is Cell-sprite. The location-sprite of the Central Hallway is CentralHall-sprite. The location-sprite of the West Hallway is WestHall-sprite. The location-sprite of the East Hallway is EastHall-sprite. The location-sprite of the Guard Room is Guard-sprite. The location-sprite of the Torture Chamber is Torture-sprite. The location-sprite of the Dungeon Stairs is Dungeon-stair-sprite. The location-sprite of the Palace Stairs is Palace-stair-sprite. The location-sprite of the Tower Stairs is Tower-stair-sprite.

        Table of Location Sprites
      room-sprite  sprite-identity  sprite-coordinates  
      Tower-sprite  Figure of Tower  {586, 330}  
      Palace-sprite  Figure of Palace  {426, 20}  
      Cell-sprite  Figure of Cell  {265, 311}  
      CentralHall-sprite  Figure of Central Hall  {263, 396}  
      WestHall-sprite  Figure of Western Hall  {46, 397}  
      EastHall-sprite  Figure of Eastern Hall  {360, 397}  
      Guard-sprite  Figure of Guardroom  {576, 304}  
      Torture-sprite  Figure of Torture Chamber  {544, 60}  
      Dungeon-stair-sprite  Figure of Dungeon Stairs  {647, 440}  
      Palace-stair-sprite  Figure of Palace Stairs  {662, 428}  
      Tower-stair-sprite  Figure of Tower Stairs  {674, 416}  


        The occluder is a sprite. The sprite-identity is Figure of Occluder. The sprite-coordinates are {0, 0}. The occluder is display-inactive. The display-layer is 4.

    Finally, the lines below control the positioning of sprites on display-layers, which sprites should be displayed and when, and so on. The looking rules are used as the basic "hook" for most sprite updating, since our sprites generally change based on player movement and the looking rules are called each time the player character moves. Only the torch sprite provides a special case, since we display it differently depending on whether the PC holds it, is in the same room with but does not hold it, or whether the player is not in the same room with it. (The torch sprite is only visible when we are in the same room as the torch).

    Note the use of the phrase:
        
        {a set of coordinates} offset by {a set of coordinates}.

    This is defined by the extension and allows us to easily add one set of coordinates to another. In this case, one of two pairs of coordinates are chosen to offset the torch, based on whether it should be held by the player's avatar, or merely appear alongside it.

        The update map sprites rule is listed in the carry out looking rules.

        The uppermost map layer is a number that varies. The uppermost map layer is 1.

        This is the update map sprites rule:
            Repeat with item running through regions:
                now the title-sprite of the item is display-inactive;
            now the title-sprite of the map region of the location is display-active;
            let the current map layer be the the region-layer of the map region of the location;
            change the display-layer of the location-sprite of the location to the current map layer;
            change the display-layer of the player-sprite to (the current map layer + 2);
            change the display-layer of the torch-sprite to (the current map layer + 1);
            change the display-layer of the occluder to (the current map layer - 1);
            if the display-layer of the occluder < 1:
                now the occluder is display-inactive;
            otherwise:
                now the occluder is display-active;
            if the current map layer < the uppermost map layer:
                repeat with item running through regions:
                    if the region-layer of item is greater than the current map layer:
                        repeat with chamber running through the rooms in item:
                            now the location-sprite of chamber is display-inactive;
            if the current map layer > the uppermost map layer:
                now the uppermost map layer is the current map layer;
            if the current map layer > 1:
                repeat with space running through rooms in the map region of the location:
                    if space is visited:
                        now the location-sprite of space is display-active;
            if the location-sprite of the location is display-inactive:
                if in darkness:
                    do nothing;
                otherwise:
                    change the location-sprite of the location to display-active;
            follow the current graphics drawing rule.

        The torch-sprite activation rule is listed before the update map sprites rule in the carry out looking rules. The player-sprite activation rule is listed before the torch-sprite activation rule in the carry out looking rules.

        This is the player-sprite activation rule:
            Change the sprite-coordinates of the player-sprite to the player-coordinates of the location.
            
        This is the torch-sprite activation rule:
            if the player carries the torch:
                change the sprite-coordinates of the torch-sprite to the sprite-coordinates of the player-sprite offset by the held-offset of the torch-sprite;
            otherwise :
                change the sprite-coordinates of the torch-sprite to the sprite-coordinates of the player-sprite offset by the dropped-offset of the torch-sprite;
            if the player can see the torch:
                now the torch-sprite is display-active;
            otherwise:
                now the torch-sprite is display-inactive.
                
        Report taking the torch:
            follow the torch-sprite activation rule;
            follow the player-sprite activation rule;
            follow the update map sprites rule.
            
        Report dropping the torch:
            follow the torch-sprite activation rule;
            follow the player-sprite activation rule;
            follow the update map sprites rule.


    The geography and other elements of the game world follow. The room definitions include a pair of coordinates called the player-coordinates. These are the coordinates, given in terms of the background, where the icon representing the player's avatar should be placed so that it will appear properly within the graphic representation of the room.

        The description of the player is "Your matted hair rests heavy on your shoulders, your fingernails are long, yellowed, and splintered, your clothes threadbare and worn."

        Before taking inventory:
            Say "[the description of the player][line break]".

        A room has a list of numbers called the player-coordinates. The player-coordinates are usually {0, 0}. A room is usually dark.

        The Cell is a room. "The close, dank cell where you have spent so many days and nights. Very little light filters in from the hallway, but you known every chink in the stone walls and every loose bit of straw on the cobbled floor. The heavy wooden door in the south wall hangs open. Outside, silence." The player-coordinates are {304, 345}. The cell is lighted.

        Instead of going outside when in the Cell, try going south.

        The Central Hallway is south of the Cell. "A low hallway just wide enough for two overfed guards to drag a racked and bastinadoed prisoner between them. Soot darkens the ceiling at intervals along the hall, but most of the torches have gone out. The hall leads into darkness east and west." The player-coordinates are {305, 404}.

        Instead of going inside when in the Central Hallway, try going north.

        The torch is in the Central Hallway. "One torch burns valiantly in its sconce." The torch is lit. The description of the torch is "The torch's flame shows no sign of sputtering out." The printed name of the torch is "burning torch".

        Hallway East is east of the Central Hallway. "The hallway seems to open into a larger space to the east, and recedes into shadow back to the west." The printed name is "Hallway (east)". The player-coordinates are {478, 404}.

        Hallway West is west of the Central Hallway. "The hallway ends in a blank wall to the west, and recedes into shadow again to the east." The printed name is "Hallway (west)". The player-coordinates are {146, 404}.

        The Guard Room is east of Hallway East. "Quiet. Nothing disturbed. Where are they? Perhaps up the stairs in the alcove to the southeast, or in...that room to the north?" The player-coordinates are {628, 404}.

        The Torture Chamber is north of the Guard Room. "You shiver and focus your gaze resolutely at the center of the torch's pool of light. The room is empty but for the Implements. You want only to return to the south." The player-coordinates are {628, 160}.

        The Palatial Hall is a room. "The massive chamber where your imprisonment was pronounced. Shadows hang heavy among the columns. There was no exit then, and no exit now, apart from the stairs to the southeast." The player-coordinates are {558, 231}.

        The Tower Chamber is a room. "If it were daylight, you could glimpse your own castle from here--provided it still stands. As it is, the darkness outside this small chamber is total. There is no moon, no twinkling of stars. The stairwell is back to the southeast." The player-coordinates are {643, 376}.

        The Dungeon Stairs are southeast from the Guard Room. "Stairs spiral upward from this alcove." The player-coordinates are {683, 473}.

        The Palace Stairs are up from the Dungeon Stairs and southeast from the Palatial Hall. "A massive space opens to the northwest, and the stairs continue up and down." The player-coordinates are {695, 461}.

        The Tower Stairs are up from the Palace Stairs and southeast from the Tower Chamber. "You have reached the summit of the tower. The stairs wend steeply downward, and a chamber opens to the northwest." The player-coordinates are {707, 449}.

        Before going to the Tower Stairs:
            say "The stairs spiral upward interminably, but in the end you emerge at the top of the:[line break]".

            
    The "test me" command will show the full state of the map, with all sprites (except two of the three mutually exclusive titles) displayed:

        Test me with "s / get torch / w / e / e / e / n / s / se / u / nw / se / u / nw / drop torch".