Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Tutorial How to create an area for the player to move too!

Joined
Oct 24, 2015
Messages
82
In this tutorial i will show you how too:
  1. Create an Area
  2. Select random Coordinate in area
  3. Create a path to Coordinate
  4. Walk there!
This came around as i wanted to find a random coordinate in the bank for my player to move to and then interact with the bank to make it more realistic than going to the same point every time!

1) Create an Area
I'm going to do it in terms of a bank but you can change it to your needs!
To create and area, in a way just copy me:
Code:
final Area bank = new Area.Rectangular(new Coordinate(xxxx, xxxx, 0), new Coordinate(xxxx, xxxx, 0)); // Bank area
To find the Coordinates use the Area maker or this one!

2.) Select random Coordinate in area
Ok so this is easy once you know what you are doing!
Code:
final Coordinate destination = bank.getRandomCoordinate();
Simple as that, just make sure "bank." is the same as what you defined the Area to be.

3.) Create a path
Next you need to create a path you can use what you want, a Bresenham Path or a Region Path i prefer a region path as my bank is close by!
As "Baddest Man on Earth" pointed out:
  • RegionaPath can only be generated if the destination is in the current loaded Region. You should always have a failsafe BreshnamPath or WebPath
So make sure you take this into acount!
Code:
final RegionPath pathtobank = RegionPath.buildTo(destination); // Sets path
For a Bresenham Path just replace "RegionPath" with "BresenhamPath"

4.) Walk There!
This is the easiest part!
Code:
if (pathtobank != null)
                pathtobank.step(true); // goes to the bank
Just a null check to see if it fails and then it walks there!

All the code together looks like this:

Code:
final Area bank = new Area.Rectangular(new Coordinate(xxxx, xxxx, x), new Coordinate(xxxx, xxxx, x)); // Bank area

        if (!Bank.isOpen()) { // Check if bank is open
     
            final Coordinate destination = bank.getRandomCoordinate(); // Gets random Coordinate in area
            final RegionPath pathtobank = RegionPath.buildTo(destination); // Sets path
            if (pathtobank != null)
                pathtobank.step(true); // goes to the bank
        
        }

Hope this has helped anybody that needed it! If you have any questions ask away!
 
Last edited:
Joined
Nov 26, 2014
Messages
616
Good for beginners.

But two problems:
  • RegionaPath can only be generated if the destination is in the current loaded Region. You use always have a failsafe BreshnamPath or WebPath
  • The random coordinate may not always be reachable, so it may not be able to generate a RegionPath
 
Joined
Oct 24, 2015
Messages
82
Good for beginners.

But two problems:
  • RegionaPath can only be generated if the destination is in the current loaded Region. You use always have a failsafe BreshnamPath or WebPath
  • The random coordinate may not always be reachable, so it may not be able to generate a RegionPath

Added the thing about the Region Path, in the main Post, But realistically you should make sure it is all reachable!
 
Would love to se a video version! :)
I May see what i can do!
 
Top