• Scenario

    Many times we want to build more complicated games. But to build a labyrinth game, for example, with what you've read so far, would not be an easy task. For this we use a scenario.

    In the Jplay there is a class called Scene that is used to construct scenarios.

    1 - Before start building the code

    Before writing any line of code, think about how your scenario is, which are the static images (not animated), which are the dynamic images as sprites and animations. The static images will be those that are included in the file that compose the scene.

    Keep in mind that all the scenario images must have the same dimension, if you have images that are rectangles, then all must be rectangles.

    After that, you are ready to start building the file of your scenario. The sections 2 and 3 take care of this building.

    2 - Scenario file organization

    In Jplay, information for the construction of the scenario is stored in a file with the extension '.scn'.

    The file is organized as follows:

    Line 1 - number of images used to construct the scenario (the static images).
    Line 2 - path of the first image and its extension
    Line 3 - path of the second image and its extension
    Line 4 - path of the third image and its extension
    Line n+1 - path of the n-th image and its extension
    Line n+2 - start of the construction of the matrix
    Line m - end of the construction of the matrix that is representedby the '%' character.
    Line m+1 - path of the backdrop and its extension.

    Obs.: Do not leave any blank line, beacuse this will generate a runtime error.

    Example of a '.scn' file.

    3 - Building the matrix

    The matrix consists of integers separated by commas without spaces between them.

    The number 0 (zero) always correspond to the backdrop (the image that will be drawn under the scenario).

    Yet the other numbers follow this logic::
    1 - represents the wall.jpg image
    2 - represents the flor.jpg image
    3 - represents the star.jpg image

    We put the number 0 (zero) in the edges suggesting that this is a backdrop area.

    Number 1 is used as 'wall' and delimits passages.
    Number 2 is used as traversable places.
    Number 3 is used as final point of arrival.

    4 - Building the code to show a scenario

    To build the code we will use the followinf methods of the Scene class:

    Example: Showing a scenario on the screen.

    5 - Adding an object to the scenario.

    To add an object to be controlled by us to the scenario we use the method below:

    With the above method we can add a GameImage, an Animation or a Sprite to the scenario.

    6 - The auxiliar class TileInfo.

    When Jplay reads the scenario input file, it creates, for each integer value of the matrix stored in the '.scn' file, a TileInfo.

    The information stored in TileInfo are: 'id' - numeric value that represents the image and a reference to the image.

    All TileInfos are stored in a matrix used internally by the Scene class.

    Example:

    The image 'wall.jpg' is represented by number 1.
    The Jplay creates a TileInfo with ID = 1 and associates this TileInfo to the image 'wall.jpg' that is stored in another internal structure in the Scene class.

    It is through the TileInfo that we can discover where a particular overlay is located and which are the images overlayed by it or around it.

    7 - Defining traversable areas.

    Scene class method used in this section:

    If we do not define areas for which the GameObject (added with the addOverlay() method) can traverse, it will move across all the scenario.

    To make the GameObject move only by a few areas, we follow this steps:

    1 - We capture the minimum and maximum values for the coordinates of the GameObject.

    2 - We capture the images that are in the same area of the GameObject.

    3 - For each tile returned, we verify which tile it is from its id, and if the player collided with it.

    After the collision test and the id verification, what will be done is a choise of the programmer.

    In the section below the GameObject is repositioned only a few pixels. Since it was meant to have a collision, some pixels need to be overlayed. If it is not done, the GameObject will collide infinitely with the TileInfo.

    Example:

    8 - All the Scene class methods.

    UFF - Universidade Federal Fluminense - Institudo de Computação - Ciência da Computação