Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  A* Pathfinding Slick  (Read 448 times)
0 Members and 1 Guest are viewing this topic.
Offline tdegroot96

Junior Member


Projects: 1



« Posted 2013-08-30 16:58:27 »

Hey guys,

Currently I'm trying to implement some kind of ai to my mobs.
I'm trying to get this thing working with the A* Pathfinding utility of the Slick library.
I found out how to implement implement the TileBasedMap and the pathfinder.
The only thing that seems not too clear to me is how to seperate the path into steps every tick.
I think it's a pretty easy algorithm but I can't really find out how to do it.

Mob.java, where everything happens:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
package nl.tdegroot.games.nemesis.entity;

import nl.tdegroot.games.nemesis.Log;
import nl.tdegroot.games.nemesis.calc.GameMath;
import nl.tdegroot.games.nemesis.entity.projectile.Arrow;
import nl.tdegroot.games.nemesis.entity.projectile.Projectile;
import nl.tdegroot.games.nemesis.gfx.Screen;
import nl.tdegroot.games.nemesis.level.Level;
import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.util.pathfinding.AStarPathFinder;
import org.newdawn.slick.util.pathfinding.Mover;
import org.newdawn.slick.util.pathfinding.Path;

import java.util.Random;

public class Mob extends Entity implements Mover {

   protected SpriteSheet sheet;
   private AStarPathFinder pathFinder;
   private Mover mover;
   private Path destination;
   protected Rectangle vulnerability;
   private Player target;

   protected boolean isWalking = false;
   protected boolean wasWalking = false;
   protected boolean hasDestination = false;
   private int step = 0;

   protected double baseHealth;
   protected double health;

   protected int mobID = 0;
   protected int spawnerID = 0;
   protected int score = 0;

   public int dir = 0;
   private int lastDir = 0;
   protected int ld = 0;
   public int animIndex = 0;
   public int animCount = 0;
   public int animType = 0;

   public int frame = 0;
   protected int collisionMulX = 0;
   protected int collisionMulY = 0;
   protected int collisionAddX = 0;

   protected int collisionAddY = 0;
   protected float movementSpeed = 0;

   public Mob(Image image, float x, float y, int width, int height) {
      super(image, x, y, width, height);
      animCount = image.getWidth() / width;
      sheet = new SpriteSheet(image, width, height);
   }

   public Mob(Image image, float x, float y, int width, int height, int mobID, int spawnerID) {
      super(image, x, y, width, height);
      animCount = image.getWidth() / width;
      sheet = new SpriteSheet(image, width, height);
      vulnerability = new Rectangle(x + 10, y + 10, width - 20, height - 20);
      this.mobID = mobID;
      this.spawnerID = spawnerID;
   }

   public void init(Level level, Player target) {
      super.init(level);
      this.target = target;
      pathFinder = new AStarPathFinder(level, 50, true);
   }

   public void update(int delta) {
      Random random = new Random();

      if (!hasDestination)
         destination = getDestination(GameMath.random(0, 10), GameMath.random(0, 10));

      float xa = 0;
      float ya = 0;

      if (hasDestination && destination != null) {
         xa = destination.getX(step) - x;
         ya = destination.getY(step) - y;
         step++;
      }

      move(xa, ya, delta);


      if (isWalking) {
         if (frame % (102 / delta) == 0) {
            animIndex = ((animIndex + 1) % animCount);
         }
      }
      vulnerability.setLocation(x + 10, y + 10);
   }

   public void move(float xa, float ya, int delta) {
      if (xa != 0 && ya != 0) {
         move(xa, 0, delta);
         move(0, ya, delta);
         return;
      }

      if (ld > 0) {
         ld -= delta / 2;
      }

      if (ya < 0)
         dir = 0;
      if (xa > 0)
         dir = 1;
      if (ya > 0)
         dir = 2;
      if (xa < 0)
         dir = 3;

//      dir = getDirection(xa, ya);
     Log.log("Last dir: " + lastDir);
      Log.log("LD: " + ld);

      if (lastDir != dir && dir != 1 && dir != 3 && ld <= 0) {
         lastDir = dir;
         ld = 500;
      }

      if (x < 0) {
         x = 0;
      }

      if (y < 0) {
         y = 0;
      }

      if (!collision(xa, ya)) {
         x += xa;
         y += ya;
      }

   }

   protected void shoot(float x, float y, double dir, Player player) {
      Projectile projectile = new Arrow(x, y, dir, player);
      level.addProjectile(projectile);
   }

   protected boolean collision(float xa, float ya) {
      boolean solid = false;

      for (int c = 0; c < 4; c++) {
         int xt = (int) ((x + xa) + c % 2 * collisionMulX - collisionAddX);
         int yt = (int) ((y + ya) + c / 2 * collisionMulY + collisionAddY);

         try {
            if (level.isCollision(xt, yt))
               solid = true;
         } catch (ArrayIndexOutOfBoundsException e) {
            Log.exception(e.getMessage());
         }
      }

      return solid;
   }

   private Path getDestination(int destX, int destY) {
      Path path = null;
      int sx = (int) x / level.tileSize;
      int sy = (int) y / level.tileSize;
      path = pathFinder.findPath(this, 7, 8, 5, 5);
      hasDestination = true;
      step = 0;
      if (path == null) {
         Log.log("path is null");
         hasDestination = false;
      } else {
         Log.log("found a path");
         hasDestination = true;
      }

      return path;
   }

   public void hit(Projectile p) {
      health -= p.getDamage();
      if (health <= 0.0) {
         remove();
      }
   }

   protected int getDirection(float xa, float ya) {
      boolean xPositive = true, yPositive = true;

      if (xa < 0)
         xPositive = false;

      if (ya < 0)
         yPositive = false;

      if (xPositive && yPositive) {
         if (xa > ya) {
            return 1;
         } else if (ya > xa) {
            return 2;
         }
      } else if (!xPositive && !yPositive) {
         if (xa < ya) {
            return 3;
         } else if (ya > xa) {
            return 0;
         }
      } else if (xPositive && !yPositive) {
         float yy = ya * -1;
         if (xa > yy) {
            return 1;
         } else if (yy > xa) {
            return 0;
         }
      } else if (!xPositive && yPositive) {
         float xx = xa * -1;
         if (xx > ya) {
            return 3;
         } else if (ya > xx) {
            return 2;
         }
      }

      return -1;
   }

   public void render(Screen screen) {
      screen.renderMob(this);
      if (destination != null)
         screen.renderPath(destination);
   }

   public Rectangle getVulnerability() {
      return vulnerability;
   }

   public SpriteSheet getSheet() {
      return sheet;
   }

   public int getID() {
      return mobID;
   }

   public void setScore(int score) {
      this.score = score;
   }

   public int getScore() {
      return score;
   }

   public void setHealth(double health) {
      this.health = health;
   }

   public float getMovementSpeed() {
      return movementSpeed;
   }

   public void setMovementSpeed(float movementSpeed) {
      this.movementSpeed = movementSpeed;
   }

   public boolean isMoving() {
      return isWalking;
   }

   public int getDir() {
      return dir;
   }

   public int getLastDir() {
      return lastDir;
   }

   public int getAnimIndex() {
      return animIndex;
   }

   public int getAnimType() {
      return animType;
   }

   public double getHealth() {
      return health;
   }

   public int getSpawnerID() {
      return spawnerID;
   }
}
Offline tdegroot96

Junior Member


Projects: 1



« Reply #1 - Posted 2013-09-01 22:09:57 »

Someone?
Offline lcass
« Reply #2 - Posted 2013-09-03 11:02:47 »

Thats quite a chunk of code, maybe focus in a bit. Make a seperate function called move that is called every tick then put the methods you need to move in there.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #3 - Posted 2013-09-03 11:07:01 »

I usually have a move(param)-method to move around mobs. When I've calculated a mobs path, I would convert it into a Queue<Step>, and then execute one step each "tick" (assuming you know about delta timing for ticks).

I would also advise against storing the PathFinder in each entity, as well as a the SpriteSheet.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (36 views)
2014-07-23 21:16:32

Riven (24 views)
2014-07-23 21:07:15

Riven (26 views)
2014-07-23 20:56:16

ctomni231 (56 views)
2014-07-18 06:55:21

Zero Volt (48 views)
2014-07-17 23:47:54

danieldean (39 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!