-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathCrossingGate.java
More file actions
146 lines (127 loc) · 3.38 KB
/
CrossingGate.java
File metadata and controls
146 lines (127 loc) · 3.38 KB
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
package edu.nd.sarec.railwaycrossing.model.infrastructure.gate;
import java.util.Observable;
import java.util.Observer;
import edu.nd.sarec.railwaycrossing.model.vehicles.Train;
import edu.nd.sarec.railwaycrossing.model.vehicles.Train2;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
/**
* Context class for Crossing Gate
* @author jane
*
*/
public class CrossingGate extends Observable implements Observer{
// Crossing Gate location and its trigger & exit points
private int anchorX;
private int anchorY;
private int movingX;
private int movingY;
private int triggerPoint;
private int triggerPoint2;
private int exitPoint;
private int exitPoint2;
private IGateState gateClosed;
private IGateState gateOpen;
private IGateState gateClosing;
private IGateState gateOpening;
private IGateState currentGateState;
private Line line;
private Pane root;
String gateName;
public CrossingGate(){}
public CrossingGate(int xPosition, int yPosition, String crossingGate){
anchorX = xPosition;
anchorY = yPosition;
movingX = anchorX;
movingY = anchorY-60;
triggerPoint = anchorX+250;
triggerPoint2 = anchorX-250;
exitPoint = anchorX-250;
exitPoint2 = anchorX+250;
// Gate elements
line = new Line(anchorX, anchorY,movingX,movingY);
line.setStroke(Color.RED);
line.setStrokeWidth(10);
// Gate States
gateClosed = new GateClosed(this);
gateOpen = new GateOpen(this);
gateOpening = new GateOpening(this);
gateClosing = new GateClosing(this);
currentGateState = gateOpen;
gateName = crossingGate;
}
public Line getGateLine(){
return line;
}
public void operateGate(){
currentGateState.operate();
}
public void close(){
if (movingY<anchorY){
movingX+=1;
movingY+=1;
line.setStartX(anchorX);
line.setStartY(anchorY);
line.setEndX(movingX);
line.setEndY(movingY);
} else {
currentGateState.gateFinishedOpening();
}
}
public void open(){
if (movingX>anchorX){
movingX-=1;
movingY-=1;
line.setStartX(anchorX);
line.setStartY(anchorY);
line.setEndX(movingX);
line.setEndY(movingY);
} else {
currentGateState.gateFinishedOpening();
}
}
// State getters and setters
public IGateState getGateClosedState(){
return gateClosed;
}
public IGateState getGateOpenState(){
return gateOpen;
}
public IGateState getGateClosingState(){
return gateClosing;
}
public IGateState getGateOpeningState(){
return gateOpening;
}
public void setGateState(IGateState newState){
currentGateState = newState;
setChanged();
notifyObservers();
}
public int getAnchorY() {
return anchorY;
}
public String getTrafficCommand(){
return currentGateState.getTrafficAction();
}
@Override
public void update(Observable o, Object arg) {
if (o instanceof Train){
Train train = (Train)o;
if (train.getVehicleX() < exitPoint)
currentGateState.leaveStation();
else if(train.getVehicleX() < triggerPoint){
currentGateState.approachStation();
}
}
if (o instanceof Train2){
Train2 train2 = (Train2)o;
if (train2.getVehicleX() > exitPoint2)
currentGateState.leaveStation();
else if(train2.getVehicleX() > triggerPoint2){
currentGateState.approachStation();
}
}
}
}