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

Discussion in 'Tutorials & Resources' started by youramazingames, Oct 28, 2015.

  1. 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 (Text):
    1. 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 (Text):
    1. 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 (Text):
    1. 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 (Text):
    1. if (pathtobank != null)
    2.                 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 (Text):
    1. final Area bank = new Area.Rectangular(new Coordinate(xxxx, xxxx, x), new Coordinate(xxxx, xxxx, x)); // Bank area
    2.  
    3.         if (!Bank.isOpen()) { // Check if bank is open
    4.      
    5.             final Coordinate destination = bank.getRandomCoordinate(); // Gets random Coordinate in area
    6.             final RegionPath pathtobank = RegionPath.buildTo(destination); // Sets path
    7.             if (pathtobank != null)
    8.                 pathtobank.step(true); // goes to the bank
    9.        
    10.         }
    Hope this has helped anybody that needed it! If you have any questions ask away!
     
    #1 youramazingames, Oct 28, 2015
    Last edited: Oct 28, 2015
    LucasSousa, Who USAF and Clery like this.
  2. Would love to se a video version! :)
     
  3. 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
     
  4. Added the thing about the Region Path, in the main Post, But realistically you should make sure it is all reachable!
    --- Double Post Merged, Oct 28, 2015, Original Post Date: Oct 28, 2015 ---
    I May see what i can do!
     

Share This Page

Loading...