-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonster.java
140 lines (127 loc) · 4.65 KB
/
Monster.java
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* Write a description of class Monster here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Monster extends Actor
{
int zielX;
int zielY;
boolean changed=false;
private final int clock = 5;
private final int maxSearch = 30;
private int tick;
public Monster(){
super();
getImage().scale(50,50);
}
public void zielSetzen(int x, int y){
zielX = x;
zielY = y;
changed = true;
}
/**
* Act - do whatever the Monster wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
if(Greenfoot.mouseClicked(getWorld())){
MouseInfo mouseInfo = Greenfoot.getMouseInfo();
int mx = mouseInfo.getX();
int my = mouseInfo.getY();
System.out.println("Gesuchter Weg: " + mx + " " + my);
zielSetzen(mx,my);
}
// MyWorld welt = (MyWorld) getWorld();
// zielX = welt.spielfigurGeben().getX();
// zielY = welt.spielfigurGeben().getY();
ArrayList<Knoten> warteschlange = new ArrayList<Knoten>();
int startX = getX();
int startY = getY();
Knoten startknoten = new Knoten(startX, startY);
warteschlange.add(startknoten);
int breite = getWorld().getWidth();
int hoehe = getWorld().getHeight();
boolean[][] besucht = new boolean[breite][hoehe];
Knoten[][] vorgänger = new Knoten[breite][hoehe];
boolean zielErreicht = false;
if(changed==true && tick > clock) {
tick = 0;
for(int x = 0; x<breite; x++){
for(int y= 0; y<hoehe; y++){
if(!getWorld().getObjectsAt(x, y, Mauer.class).isEmpty()){
besucht[x][y] = true;
}
}
}
while(!warteschlange.isEmpty() && !zielErreicht){
Knoten k = warteschlange.remove(0);
// falls rechter Nachbar noch nicht besucht, diesen zu Warteschlange hinzuzufügen
int x = k.getX();
int y = k.getY();
if( x+1 < getWorld().getWidth() && !besucht[x+1][y]){
Knoten nachbarRechts = new Knoten (x+1, y);
warteschlange.add(nachbarRechts);
besucht[x+1][y]= true;
vorgänger[x+1][y] = k;
if(x+1==zielX && y==zielY){
zielErreicht = true;
System.out.println("Ziel erreicht :)");
}
}
if( x-1 >= 0 && !besucht[x-1][y]){
Knoten nachbarLinks = new Knoten (x-1, y);
warteschlange.add(nachbarLinks);
besucht[x-1][y]= true;
vorgänger[x-1][y] = k;
if(x-1==zielX && y==zielY){
zielErreicht = true;
System.out.println("Ziel erreicht :)");
}
}
if( y+1 < hoehe && !besucht[x][y+1]){
Knoten nachbarOben = new Knoten (x, y+1);
warteschlange.add(nachbarOben);
besucht[x][y+1]= true;
vorgänger[x][y+1] = k;
if(x==zielX && y+1==zielY){
zielErreicht = true;
System.out.println("Ziel erreicht :)");
}
}
if( y-1 >=0 && !besucht[x][y-1]){
Knoten nachbarUnten = new Knoten (x, y-1);
warteschlange.add(nachbarUnten);
besucht[x][y-1]= true;
vorgänger[x][y-1] = k;
if(x==zielX && y-1==zielY){
zielErreicht = true;
System.out.println("Ziel erreicht :)");
}
}
}
if(zielErreicht){
int x = zielX;
int y = zielY;
int letztesX = x;
int letztesY = y;
while(x != startX || y != startY){
Knoten k = vorgänger[x][y];
letztesX = x;
letztesY = y;
x = k.getX();
y = k.getY();
}
setLocation(letztesX, letztesY);
}
}
else{
tick++;
}
}
}