Unity5 Official tutorial Note (2D Rogue like) 01--boardmanager

Source: Internet
Author: User

As a rogue like game, the ability to randomly generate maps is essential. In the previous blog post (Unity5 official tutorial Note (2D Rogue like) 01--game Manager), we learned that the class responsible for this work is called "Boardmanager" in this project, and that it is also responsible for storing the map. The following is a description of the functions of this class.

Let's start by looking at the basic structure of the Code:

As you can see in this class, we:

    1. Specify the size of the map
    2. Sets the number of walls (map) units and food units generated (that is, the number of units in a particular level range)
    3. Preserves the type of picture (Sprite) that can be used by various units
    4. Enables the generated units to be arranged in a random order and can be displayed using a random picture

Here, the method that will be called directly by the external is only setupscene (), so take a look at it first:

In this method, the walls and the ground (Boardsetup) are generated in the map, and then the arrays (initializelist) are initialized to randomly select the positions, and then the wall units, food units, and enemy units are randomly placed on the map. Finally, the exit is placed in the upper right corner of the map.

Boardsetup () Method:

Here, there is a method called instantiate (), the personal understanding of this thing is essentially the functionality provided in C # assembly: A dynamic instantiation of an object. The parameters used are: the object that needs to be instantiated, the coordinates after the instantiation, and the angle after the instantiation. In order to increase the randomness of the game, we select the objects that need to be instantiated, which are randomly selected from a predetermined range. In this example, a picture is randomly selected from the candidate array outerwalltiles of the Outerwall (outer wall) as the display.

At the same time, each created instance is marked as a sub-object of Boardholder, which allows them to share a coordinate system and is easy to manage, avoiding confusion.

The Initiallizelist () method, which is simple, is used to initialize a candidate array that will then be used to randomly pick a position.

This method of selecting a cell in this array is more efficient than a direct random one, and avoids the possibility of two units being set in a cell at the same time. This can be seen from the Randomposition () method:

After each selection of a new location, the position is removed from the gridpositions array.

Finally, the Layoutobjectatrandom () method:

This method selects the contents of the given range (Tilearray) in a random random position placed on the map, and the number placed is a random number in the specified range (minimum to maximum).

Because the "Board" Object (The Boardsetup () method) is created directly in the code, and all newly added content is a child of the object, the entire map is added to the game scene when the creation is complete.

As you can see, all of the object coordinates used are relative coordinates (that is, coordinates that are not labeled with absolute criteria such as pixels or feet), and all of the image resources are of the same size, so you can tell that this method will be more useful for matching different screen sizes and should be more consistent with unity's usage habits. However, due to the author of the relative coordinates and the size of the image of the matching method is not known, so for the moment there are still some do not understand the place, to be in the future after the study will be here to fill in or a single article to explain.

Unity5 Official tutorial Note (2D Rogue like) 01--boardmanager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.