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:
|
package main;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame{
private static final long serialVersionUID = 1L;
static int width = 300;
static int height = 300;
static int startx;
static int starty;
static int zielx;
static int ziely;
static int playerx;
static int playery;
static boolean[][] grid;
public static void main(String[] args) {
Display display = new Display();
Main m = new Main();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setSize(810,670);
m.setLocationRelativeTo(null);
m.add(display);
m.setVisible(true);
while(true){
long lastTime = System.nanoTime();
final double ns = 1000000000.0/60.0;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
int updates = 0;
while(true){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
display.update();
delta--;
updates++;
display.repaint();
frames++;
if((System.currentTimeMillis() - timer) >1000){
timer+=1000; m.setTitle("A-Stern v1.0" + "|" + updates + "ups," + frames + "fps");
updates = 0;
frames = 0; } } } }
public static class Display extends JLabel{
private static final long serialVersionUID = 1L;
Level level = new Level();
public Display(){
addMouseListener(new Mouse());
setFocusable(true);
}
public void update(){
zielx = Mouse.mousex;
ziely = Mouse.mousey;
grid = new boolean[width][height]; for(int y = 0;y<height;y++){
for(int x = 0;x<width;x++){
if(Level.levelMap[x/10][y/10]==1){ grid[x][y] = true; } } } }
protected void paintComponent(Graphics g){ super.paintComponents(g);
for(int x = 0;x<30;x++){
for(int y = 0;y<30;y++){
g.drawRect(x*10,y*10,10,10);
}
}
level.drawTiles(g);
ArrayList<Field> path =
AStar.AStaralgorithm(startx,starty,zielx,ziely,grid,width,height); if(path != null){
for(Fieldf:path){ g.fillRect(f.x,f.y,1,1);
System.out.println(f);
}
}
g.setColor(Color.BLUE);
g.fillRect(playerx,playery,10,10);
}
} } package main; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class AStar { public static ArrayList<Field> AStaralgorithm(int xFrom,int yFrom,int xTo,int yTo,boolean[][] grid,int width,int height){ Field[][] fields = new Field[width][height]; for(int x = 0;x<width;x+=1){ for(int y = 0;y<height;y+=1){ fields[x][y] = new Field(x,y,grid[x][y]); } } for(int x = 0;x<width;x+=1){ for(int y = 0;y<height;y+=1){ for(int dx = -1;dx<=1;dx+=1){ for(int dy = -1;dy<=1;dy+=1){ if(x+dx<0 || x+dx>=width || y+dy<0 || y+dy>=height){ continue; } fields[x][y].addNeighbor(fields[x+dx][y+dy]); } } } } Set<Field> openList = new HashSet<Field>(); Set<Field> closedList = new HashSet<Field>(); openList.add(fields[xFrom][yFrom]); Field target = fields[xTo][yTo]; while(!openList.isEmpty()){ Field current = null; for(Field f : openList){ if(current ==null || current.getF()>f.getF()){ current = f; } } openList.remove(current); if(current == target){ ArrayList<Field> result = new ArrayList<Field>(); Field tmp = target; while(tmp!=null){ result.add(0,tmp); tmp = tmp.getPredecessor(); } return result; } closedList.add(current); expandNode(current,target,openList,closedList); } return null; } public static void expandNode(Field current,Field target,Set<Field> openList,Set<Field> closedList){ HashMap<Field,Double> successors = current.getNeighbors(); Iterator<Entry<Field, Double>> it = successors.entrySet().iterator(); while(it.hasNext()){ Map.Entry<Field,Double> pair = it.next(); Field successor = pair.getKey(); if(closedList.contains(successor)){ continue; } double tentative_g = current.getG()+pair.getValue(); if(openList.contains(successor) && tentative_g >= successor.getG()){ continue; } successor.setPredecessor(current); successor.setG(tentative_g); double f = tentative_g + successor.calch(target); successor.setF(f); openList.add(successor); } } } package main; import java.util.HashMap; public class Field { public int x; public int y; private HashMap<Field,Double> neighbors = new HashMap<Field,Double>(); private double f; private double g; public boolean blocked; private Field predecessor; public Field(int x,int y,boolean blocked) { this.x = x; this.y = y; this.blocked = blocked; } public void addNeighbor(Field neighbor){ double delta = Math.sqrt(((this.x-neighbor.x)*(this.x-neighbor.x)) + ((this.y - neighbor.y)*(this.y-neighbor.y))); if(!neighbor.blocked&&this != neighbor){ neighbors.put(neighbor,delta); } } public HashMap<Field,Double> getNeighbors(){ return neighbors; } public double getF(){ return f; } public void setF(double f){ this.f = f; } public double getG(){ return g; } public void setG(double g){ this.g = g; } public Field getPredecessor(){ return predecessor; } public void setPredecessor(Field predecessor){ this.predecessor = predecessor; } public double calch(Field target){ double dx = Math.abs(this.x - target.x); double dy = Math.abs(this.y - target.y); return (Math.sqrt(2)-1)*Math.min(dx,dy) + Math.max(dx,dy); } @Override public String toString(){ return x +"/" +y; } } package main; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import javax.imageio.ImageIO; public class Level { public static int[][] levelMap; public int tilesw; public int tilesh; public int RASTER = 10; public int[] level; public BufferedImage tiles; public int tileNr; public Level() { try { tiles = ImageIO.read(new File("gui//spritesheet.png")); } catch (IOException e) {e.printStackTrace();} generateMap(1,"gui//map.txt"); } public void generateMap(int level_nr,String filename){ if(level_nr == 1){ tilesw=30; tilesh=30; } String stringMap = ""; try { BufferedReader reader = new BufferedReader(new FileReader(filename)); String line = null; while ((line = reader.readLine()) != null) { stringMap += line; } reader.close(); } catch (IOException e) {e.printStackTrace();} if(stringMap != ""){ String[] stringarray = stringMap.split(","); level = new int[stringarray.length]; for(int i = 0;i<stringarray.length;i++){ level[i] = Integer.parseInt(stringarray[i]); } int nr = 0; levelMap = new int[tilesw][tilesh]; for(int y=0;y<tilesh;y++){ for(int x=0;x<tilesw;x++){ levelMap[x][y]= level[nr]; nr++; } } } } public void drawTiles(Graphics g){ for(int x = 0; x<tilesw*RASTER; x+=RASTER){ for(int y = 0; y<tilesh*RASTER; y+=RASTER){ tileNr = levelMap[x/RASTER][y/RASTER]; if(tileNr==1){ g.fillRect(x,y,RASTER,RASTER); } } } } } package main; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class Mouse extends MouseAdapter{ public static int mousex,mousey; @Override public void mouseClicked(MouseEvent e) { mousex = e.getX(); mousey = e.getY(); } } //mapdata 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, 0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0, 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0, 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0, 0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0, 0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0, 0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0, 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0, 0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,1,0,0,0,0,0,0,0,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