JavaFX battle flag game development Lesson 6 mobile scope acquisition

Source: Internet
Author: User

JavaFX battle flag game development Lesson 6 mobile scope acquisition

For a while, I haven't written a tutorial on this banner game Demo. Continue now.

In fact, the range acquisition of War flag games is not complex, mainly because of node traversal and weight comparison.

As you know, in the * Star shortest path algorithm, the weight value has a g value and an H value, the G value is the movement from the starting point to the current point (usually the movement of two adjacent cells is equal to 1), and the H value is the estimation of the movement from the current point to the target point.

Of course, it is not that complicated to search for a mobile range in SLG games. Here we only need a G value to indicate the mobile volume.

Main algorithm principles:

1. There are two lists ---- OpenList and CloseList.

2. Add the location of the role to be moved to OpenList and CloseList.

3. Retrieve the first position in OpenList and obtain the top, bottom, and left points of the position.

4. Calculate the weights for these points based on the weights of the four Upper and Lower map blocks.

5. if these points are not repeated in the linked list and the weight is smaller than the moving force of the role (here we will set the moving force of the role as the maximum weight ), add these points to the two linked lists. In addition, in this step, we can also determine whether the point can be moved (for example, the point has other roles, the point is a non-movable stone wall, and so on ).

Repeat 3-5 operations until the OpenList is empty, and CloseList is the range point that our role can move.

 

The main logic code is as follows:

 

Public writable list
 
  
SearchMoveScan (Point2D location, int step) {WNode start = new WNode (location); start. setValueG (0); start. setParent (null); levelList. add (start); closeList. addFirst (start); while (levelList. size ()> 0) {WNode near = (WNode) levelList. toArray () [0]; levelList. remove (0); Revoke list
  
   
NearList = near. getnew.delist (); // obtain the addMoveWNode (nearList, start, near, step) of the four nodes near the start Node;} return closeList;} public void addMoveWNode (writable list
   
    
Near, WNode start, WNode before, int step) {for (int I = 0; I <near. size (); I ++) {WNode nearWNode = near. get (I); double x = nearWNode. getPoint (). getX (); double y = nearWNode. getPoint (). getY (); try {switch (map [(int) y] [(int) x]) {case 0: nearWNode. setValueG (before. getValueG () + 1); break; case 7: nearWNode. setValueG (before. getValueG () + 4); break; case 18: nearWNode. setValueG (before. getValueG () + 2 ); Break;} catch (Exception e) {nearWNode. setValueG (before. getValueG () + 1);} boolean isOpen = contains (levelList, nearWNode); boolean isClose = contains (closeList, nearWNode); boolean isHit = isHit (int) nearWNode. getPoint (). getX (), (int) nearWNode. getPoint (). getY (); if (! IsOpen &&! IsClose &&! IsHit & nearWNode. getValueG () <= step) {levelList. addM (nearWNode); closeList. addFirst (nearWNode) ;}} public boolean isHit (int x, int y) {// determines whether the current location can be moved if (map_sprite! = Null) {if (map_sprite [y] [x]! = 0) return true;} return false ;}
   
  
 

 

The main result is as follows:

 

I suddenly felt that it was quite troublesome to paste all the code. Here we will talk about the principle first. You can try to write it yourself. In the last lesson, I will download the entire project.

This lesson is now available. The next lesson will introduce the round logic in the JavaFX flag game.

 

Related Article

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.