2016-03-27 22:27:24 +00:00
|
|
|
package cicaprojekt;
|
|
|
|
|
2016-04-19 09:26:10 +00:00
|
|
|
import java.util.Stack;
|
|
|
|
|
2016-03-27 22:27:24 +00:00
|
|
|
public class Scale extends Field {
|
2016-04-24 21:01:34 +00:00
|
|
|
private Gate gateConnected;
|
|
|
|
private int threshold;
|
|
|
|
private int weight;
|
|
|
|
|
|
|
|
|
|
|
|
public Scale(Gate gate, int threshold) {
|
|
|
|
gateConnected = gate;
|
|
|
|
this.threshold = threshold;
|
|
|
|
boxStack = new Stack<Box>();
|
|
|
|
}
|
|
|
|
|
2016-05-13 15:59:14 +00:00
|
|
|
@Override
|
|
|
|
public boolean isSteppable()
|
|
|
|
{
|
|
|
|
if (boxStack.isEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-24 21:01:34 +00:00
|
|
|
@Override
|
|
|
|
public void onEntry(PlayerBase playerBase) {
|
2016-05-13 21:52:10 +00:00
|
|
|
super.onEntry(playerBase);
|
2016-04-24 21:01:34 +00:00
|
|
|
gateConnected.setOpen(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onExit(PlayerBase playerBase) {
|
|
|
|
gateConnected.setOpen(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Box getABox() {
|
|
|
|
if (boxStack.isEmpty())
|
|
|
|
return null;
|
|
|
|
weight -= boxStack.peek().weight();
|
|
|
|
stackChanged();
|
|
|
|
return boxStack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void putABox(Box box) {
|
|
|
|
if (box == null)
|
|
|
|
return;
|
|
|
|
boxStack.push(box);
|
|
|
|
weight += box.weight();
|
|
|
|
stackChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stackChanged() {
|
2016-05-13 16:47:26 +00:00
|
|
|
if (weight >= threshold)
|
2016-04-24 21:01:34 +00:00
|
|
|
gateConnected.setOpen(true);
|
|
|
|
else
|
|
|
|
gateConnected.setOpen(false);
|
|
|
|
}
|
2016-04-25 21:45:03 +00:00
|
|
|
|
|
|
|
public Gate getGateConnected() {
|
|
|
|
return gateConnected;
|
|
|
|
}
|
2016-03-27 22:27:24 +00:00
|
|
|
}
|