Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Quellcode |
|
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 |
public void update(int xoffset,int yoffset,AffineTransform scale) { timer++; if(timer>100){ timer=0; } peasantBox.x = (int) x; peasantBox.y = (int) y; mx = Mouse.mousex; my = Mouse.mousey; in = new Point(mx, my); out = null; try { out = scale.inverseTransform(in,out); } catch (NoninvertibleTransformException e) {e.printStackTrace();} if(Mouse.right&&marked){ endx = (int) out.getX()+(xoffset/width); endy = (int) out.getY()+(yoffset/height); } if(marked==false||marked){ path = Level.findPath((int)startx,(int)starty,(int)endx,(int)endy); } if (path!=null) { Tile node = path.get(path.size() - 1); //Rechts if (node.x * width > x && node.y * height == y) { x+=speed; figure = getFrame(figure,10,14,5); } //Links if (node.x * width < x && node.y * height == y) { x-=speed; figure = getFrame(figure,15,19,5); } //Unten if (node.x * width== x && node.y * height > y) { y+=speed; figure = getFrame(figure,5,9,5); } //Oben if (node.x * width == x && node.y * height < y) { y-=speed; figure = getFrame(figure,0,4,5); } //Rechts unten if (node.x * width > x && node.y * height > y) { x+=speed; y+=speed; figure = getFrame(figure,30,34,5); } //Links unten if (node.x * width < x && node.y * height > y) { x-=speed; y+=speed; figure = getFrame(figure,35,39,5); } //Rechts oben if (node.x * width > x && node.y * height < y) { x+=speed; y-=speed; figure = getFrame(figure,20,24,5); } //Links oben if (node.x * width < x && node.y * height < y) { x-=speed; y-=speed; figure = getFrame(figure,25,29,5); } if (x == node.x * width) { startx = node.x; } if (y == node.y * height) { starty = node.y; } } } |
Quellcode |
|
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 |
public static List<Tile> findPath(int startx,int starty,int endx,int endy){ for(int i = 0; i < width; i++){ for(int j = 0;j < height;j++){ tiles[i][j] = new Tile(i,j,size,size,obstacles[i][j],false); } } for(int i = 0; i < width; i++){ for(int j = 0;j < height;j++){ tiles[i][j].addNeighbours(tiles,width,height); } } List<Tile> openList = new ArrayList<Tile>(); // Here i want a TreeSet HashSet<Tile> closedList = new HashSet<Tile>(); List<Tile> path = null; Tile start = tiles[startx][starty]; Tile end = tiles[endx][endy]; Tile closest = start; closest.h = heuristic(closest,end); openList.add(start); while(!openList.isEmpty()) { int winner = 0; for (int i = 0; i < openList.size(); i++) { if (openList.get(i).f < openList.get(winner).f) { winner = i; } } Tile current = openList.get(winner); openList.remove(current); if (current == end) { path = new ArrayList<Tile>(); Tile tmp = current; path.add(tmp); while (tmp.previous != null) { path.add(tmp); tmp = tmp.previous; } return path; } closedList.add(current); List<Tile> neighbours = current.neighbours; for (int i = 0; i < neighbours.size(); i++) { Tile neighbour = neighbours.get(i); int cost = current.g + heuristic(current,neighbour); if (openList.contains(neighbour) && cost < neighbour.g) { openList.remove(neighbour); } if (closedList.contains(neighbour) && cost < neighbour.g) { closedList.remove(neighbour); } int newcost = heuristic(neighbour, end); if (!openList.contains(neighbour) && !closedList.contains(neighbour) && !neighbour.obstacle) { neighbour.h = newcost; if (neighbour.h < closest.h) { closest = neighbour; } } if (!openList.contains(neighbour) && !closedList.contains(neighbour) && !neighbour.obstacle) { neighbour.g = cost; openList.add(neighbour); neighbour.f = neighbour.g + neighbour.h; neighbour.previous = current; } } } Tile tmp = closest; path = new ArrayList<Tile>(); path.add(tmp); while (tmp.previous != null) { path.add(tmp); tmp = tmp.previous; } return path; } public static int heuristic(Tile A,Tile B) { int dx = Math.abs(A.x - B.x); int dy = Math.abs(A.y - B.y); return 1 * (dx + dy) + (1 - 2 * 1) * Math.min(dx,dy); } |
Quellcode |
|
1 2 3 |
0 1 2 3 4 5 6 7 8 |
Community-Fossil
Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer
Werbeanzeige