-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMap.java
100 lines (76 loc) · 2.39 KB
/
Map.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
import java.util.*;
public abstract class Map {
public class Edge {
public Location l1, l2;
public String xfer_str;
public Edge(Location l1, Location l2, String xfer_str) {
this.l1=l1;
this.l2=l2;
this.xfer_str=xfer_str;
}
}
List<Edge> edges=new ArrayList<Edge>();
protected Location currentLocation;
Adventure adv;
protected abstract void buildEdges();
protected abstract void buildLocations();
protected List<Location> locations = new ArrayList<Location>();
public Map(Adventure adv) {
this.adv=adv;
buildLocations();
buildEdges();
}
public void resetActions() {
for(Location l : locations) {
l.actions.clear();
}
}
public Location getLocation() {
return currentLocation;
}
public void setLocation(Location l) {
currentLocation=l;
adv.tell(currentLocation.getDescription(), false);
for(Action a : currentLocation.actions) {
if(a.onEnter()) a.run();
}
}
public void addWay(Location l1, Location l2, String l1_to_l2, String l2_to_l1) {
edges.add(new Edge(l1, l2, l1_to_l2));
edges.add(new Edge(l2, l1, l2_to_l1));
}
public List<Edge> findWaysFrom(Location from) {
List<Edge> ret=new ArrayList<Edge>();
for(Edge e : edges) {
if(e.l1==from) ret.add(e);
}
return ret;
}
public void interact() {
interact(false);
}
/**
*
* @param pluralSubst Setze auf true, um die Action-Beschreibungen durch search&replace so anzupassen, dass sie Pluralformen enthalten.
*/
public void interact(boolean pluralSubst) {
adv.curScene.interact=true; // Flag is set to false by exiting location / scene
while(adv.curScene.interact) {
List<String> aList = new ArrayList<String>();
List<Action> whatCanWeDo=this.currentLocation.availActions();
for(Action a : whatCanWeDo) aList.add(pluralSubstOrNot(pluralSubst, a.getDescription()));
List<Edge> whereCanWeGo=findWaysFrom(currentLocation);
for(Edge e : whereCanWeGo) aList.add(pluralSubstOrNot(pluralSubst, e.xfer_str));
int answer=adv.ask(pluralSubst?Messages.getString("Map.0"):Messages.getString("Map.1"), aList); //$NON-NLS-1$ //$NON-NLS-2$
if(answer<whatCanWeDo.size()) {
// Entweder Action ausführen
whatCanWeDo.get(answer).run();
} else {
answer-=whatCanWeDo.size();
// Oder Ort wechseln
setLocation(whereCanWeGo.get(answer).l2);
}
}
}
protected abstract String pluralSubstOrNot(boolean pluralSubst, String in);
}