There was a period of time without writing this banner game demo tutorial. Now come on.
The range acquisition of the banner game is not complicated, it is mainly the traversal of the nodes and the comparison of the weights.
As you know, in the A*star shortest path finding algorithm, the weight value is a G value and H value, the G value is the starting point to the current point of movement (usually adjacent to the two-cell movement of 1), H value is the current point to the target point of the movement of the estimated value.
Of course, for the SLG game to find the moving range, and not so complex, we here only need a G value, to represent the amount of movement.
Main algorithm principle:
1. There are two list----openlist,closelist.
2. The role locations to be moved are added to Openlist and closelist.
3. Remove the first position point in the openlist and get the top and bottom four points of the point.
4. Calculate weights for these points based on the weights of the four map squares up or down.
5. If these points are not duplicated in the linked list, and the weights are less than the moving forces of the character (where we use the moving force of the character as the maximum weight), then add these points to the two linked lists. Also in this step, we can determine whether the point can be moved (for example, the point has other characters, the point is not movable stone walls, etc.).
Loop repeats 3-5 of the operation until Openlist is empty, Closelist is the range of points that our character can move.
The main logic code is as follows:
Public linkedlist<wnode> Searchmovescan (point2d, 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); linkedlist<wnode> nearlist = Near.getnearnodelist (); Get 4 nodes near the start node Addmovewnode (nearlist, start, close, step); }return Closelist;} public void Addmovewnode (linkedlist<wnode> near, Wnode start, wnode before, int. step) {for (int i = 0; i < NEAR.S Ize (); i++) {Wnode Nearwnode = near.get (i);d ouble x = Nearwnode.getpoint (). GetX ();d ouble y = Nearwnode.getpoint (). GetY (); try { Switch (map[(int) y][(int) x]) {case 0:nearwnode.setvalu EG (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 ((i NT) Nearwnode.getpoint (). GetX (), (int) nearwnode.getpoint (). GetY ()); if (!isopen &&!isclose &&!ishit & amp;& Nearwnode.getvalueg () <= step) {LEVELLIST.ADDM (Nearwnode); Closelist.addfirst (Nearwnode);}}} public boolean ishit (int x, int y) {//Determines whether the current location can move if (map_sprite! = null) {if (map_sprite[y][x]! = 0) return true;} return false;}
The main results are as follows:
Suddenly feel that all the code is a comparison of the trouble, here is the first principle, we can also try to write their own. In the last lesson I will download the entire project.
So this is the lesson, and in the next lesson we'll talk about the turn logic in the JavaFX banner game.
This article is personal original, all rights reserved, reprint please indicate source:http://blog.csdn.net/ml3947. In addition to my personal blog:http://www.wjfxgame.com.
JavaFX Banner Game development Lesson six move range acquisition