Chapter 6. The Big Wild World

As we start considering the wider bounds of our RPG world, we need to look at alternate views for the game. It's important to keep the player engaged and make them feel that they are entering a vast arena with lots of places to explore, especially when you initially release your game and you only have a few towns to visit.

Another thing to consider is whether you want fixed maps in your game or you want to venture down the rabbit hole of procedural generation. Both are valid routes and there's nothing to say that you only have to use one. In this chapter, we'll cover all the options and then implement a nice and simple system to walk through the basics.

The following is a list of topics that will be covered in this chapter:

  • Resources to build a map
  • Structuring and adding points of interest
  • Working with prefabs
  • Transitioning between views
  • Marshaling input

The larger view

Our budding hero is now ready to pack his bags and leave the shelter of his hometown for the wider world. So, we need to widen the scope of what the player can see and build a large map with places of interest to visit.

This usually opens the floodgates for just how big your game will be. Planning can decide whether your game will be a hit or feel just too short.

Maps in RPGs certainly aren't mandatory; several hit games just go from place to place with maybe an animation or cut scene to show movement. However, in the best cases, a map just opens up the scope of the game and gives the player an understanding of the world they are travelling in.

Types of maps

When looking at what kind of map or world you are going to choose to connect the dots between places to visit or secret hideaways, there are a few paths you can take. Generally, there are two options:

  • Fixed: In this option, images are usually drawn by an artist and have extensive detail of the world surrounding the player or are blank, exposing places as the player travels to or discovers them.
  • Generated: In this option, each run of the game completely randomizes the places to go or events that will take place, with the focus being on unpredictability.

Both the preceding options are perfectly valid and there's nothing to say you need to focus on just one or the other; mix it up if you wish. Generally, the better the variety, the better the chances of the player being engaged in your game—it will entice them to explore and play more.

Another keen element is that it should support repeatability and replayability—let players return to existing locations and discover new things, and reuse what you have to the fullest.

Fixed maps

There are many resources to get maps for your title if you don't have a dedicated artist and, in some ways, these also provide insight or creative juice for how you want your maps to look. The following map image is a good example of a high-quality paid map:

Fixed maps

The first site to mention, primarily because it is also completely free, is http://freefantasymaps.org/. The preceding screenshot is a prime example of the level of quality you can get from the site. Although the images on the site are free to use for any purpose, I would recommend you to use their images to donate to the running of the site so that they remain free.

Another useful site is the Cartographers' Guild at http://www.cartographersguild.com/. This is a veritable map paradise with lots of content available for use in most projects. Unlike the preceding site, all works are protected and you will need to purchase them or gain rights to use them. However, they are of a very high quality.

Tip

When browsing maps and art, always be sure to check the license and usage policies of the images you download. This should be done whenever you acquire art, even from Google. Be safe, check, and get permission.

The same can be said for code and any other kinds of assets. If in doubt, check the license and even if it's free, just check with the author whether they feel it's ok to use in your project.

Always check the license on anything you download and use.

Another good feature of the Cartographers' Guild is that it allows you to request a map. They have an awesome request system and hundreds of artists are ready to commission anything your heart wants (see http://www.cartographersguild.com/mapmaking-requests/).

Generated maps

There are also many other resources out there to get maps for your game. Another method is to use an online map generation system. Now, to be fair, most are aimed at tabletop gamers and most are of a low quality, but there are a few gems to be found.

The one I used for the game we are building is from http://donjon.bin.sh/fantasy/world/, because I like it and it provides fairly high-resolution generated images and includes world features and places. It looks like a nice place to have a holiday (if you are an Orc). The following image is the map I've used in the sample project with lots of interesting places to see and explore:

Generated maps

As you can see, there is a lot of detail here and a very large world for the player to explore.

DonJon's site also offers a vast array of other generators to build maps and other RPG elements. There are even name and game dialogue generators, so it really does meet most RPG needs.

In-game generated maps

Now, if you want more control over what the player sees or will have in their game, you can go to all the way and start building maps and more through either Unity's asset pipeline or in-code within the game.

External tools such as Tiled (http://www.mapeditor.org/), which is shown in the following image, can be used to build and design maps. It has many interesting features and can even output several layers. You can also use either top-down, 2D side scrolling, or even isometric maps.

In-game generated maps

A great Unity example of this is a project called uTiled (https://bitbucket.org/vinull/utiled), which provides a Unity asset that can read maps from Tiled. Here, the author has customized a map generation system that uses an external tool to build maps according to a predefined set of map tiles, which are just small textures used when building the map.

The following image is a simple example of what can be achieved with Tiled editor that generates the maps:

In-game generated maps

The project is a great example of not just how to import a 2D-based tile map system into Unity, but also how to construct and run these 2D maps in your game.

Using a system like uTiled, you can either build a selection of maps in Tiled for use as world maps in your game, or even full 2D scenes.

Alternatively, you could use procedural techniques (see the Going procedural section) to build the map while the game is running, using the framework to knit individual segments of your map together. We will discuss this more later.

2D doesn't mean you have to use only 2D

Just because the rest of your game is 2D, it doesn't means you have to use 2D. Some of the best games I've seen use a mixture of 2D and 3D, and Unity will help you there because it's not limited.

Whether you have 2D play areas and 3D terrain for maps or 3D villages and a 2D map, it doesn't really matter. Just go with whatever you are comfortable with or what fits with the aesthetic of your game.

Thanks to the way 2D is implemented in Unity, you can even mix it up by having 3D elements placed and animated in your 2D scene either as background elements or interactive components.

Note

A word to the wise

Although you can mix the 2D and 3D rendering in a single scene, you cannot mix the 2D and 3D physics. This is simply because they are separate engines and have no connection to each other. Keep this in mind depending on what you are trying to build in your game.

Going procedural

If you are the bold or adventurous sort, another route to flesh out your world is to procedurally generate it. What you usually see when you look for procedural generation in Unity are dungeon generators. In fact, some of the best examples I've seen out there involve randomly generated dungeons where every run of the game is different from the last. Other examples are usually found in endless running games where a style of procedural selection is done to choose the next running area or to put random scene items in.

When we try to apply this to RPG games, we want to balance the fixed part of the world/story we are looking to convey with a more random placement of towns/villages or places of interest. This will make the world we see different for every player but still convey the background of the theme.

Now, the whole subject of procedural generation is far too large to go into for this book, but I can give you a few points for where to look.

The best place to start is the Procedural Content Generation Wiki available at http://pcg.wikidot.com/; it's the go-to place to start learning the following general techniques:

  • Iterated function systems: These are fractals to create land masses or structured areas
  • L-systems: These are used for roads or path generation
  • Diamond-square and midpoint displacement algorithms: These are used to create random height terrains
  • Perlin and simplex noise systems: These are used to add further randomness to the generation

Reddit.com is also a great place for examples, questions, and queries on procedural generation. Two of the best reddits are as follows:

  • http://www.reddit.com/r/proceduralgeneration (this is the main procedural generation reddit. Although it's mostly promotional these days, there are still a lot of older posts with samples and information. It's also a good place to start with questions.)
  • http://www.reddit.com/r/worldbuilding (this is a good source of information for this section on maps for your game. It is mainly related to D&D, but it does contain a lot of useful information.)

Note

A word to the wise

Procedural generation is not for the faint of heart, there is a lot of math involved and a lot of trial and error. However, if you can master small parts, you can achieve a truly wondrous game with lots of replayability.

Procedural generation is too large a subject for this book. Hopefully, I've given you a few tips and tidbits to get you going, so you know what to look for should you want to venture down this road.

Screen space and world space

When dealing with either touch or mouse input, we have to recognize that coordinates managed by Unity are in screen space and not world space. Both of these terms are defined as follows:

  • Screen space: This refers to coordinates relative to the screen / display area, starting at the top-left corner of the screen
  • World space: This refers to the coordinates that are used inside the Unity engine

Tip

The starting position, (0, 0) 2D coordinate, for the mouse can be different depending on how you access it:

  • If you use Input.mousePosition, then (0, 0) is in the top-left corner of the screen
  • If, however, you use Event.current.mousePosition, then (0, 0) is in the bottom-left corner of the screen

For touch, however, it is always in the the top-left corner of the screen. This is something to keep in mind when you access input—always check.

When you poll for the mouse position (which can only be one mouse), use the following line of code:

Input.mousePosition

When we are looking at touch points (which can be many), we use the following line of code:

Input.GetTouch(<touch index>).position

However, because the position we get back is always in screen space, we need to convert it to world space when checking it against objects in the game world. Thankfully, this is very simple to do using the following built-in Unity function:

Camera.main.ScreenToWorldPoint(<screenCoordinate>)

Tip

The preceding default function uses the main camera to translate the coordinate from screen space to world space. If you are taking input from an alternate view (like a mini-map), be sure to use the specific camera for that view to convert the value, or else you will get incorrect results.

Note

Remember that any coordinate you get back from the input system or used in world coordinates are always Vector3 coordinates. However, because this is a 2D game, we need coordinates in Vector2 coordinates, so you will need to convert the value you get back for any 2D functions such as colliders or 2D distance checking.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.157.6