Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating from master #10

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/edu/nd/sarec/railwaycrossing/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collection;

import edu.nd.sarec.railwaycrossing.model.infrastructure.Direction;
import edu.nd.sarec.railwaycrossing.model.infrastructure.MapBuilder;
import edu.nd.sarec.railwaycrossing.model.infrastructure.RailwayTracks;
import edu.nd.sarec.railwaycrossing.model.infrastructure.Road;
Expand Down Expand Up @@ -40,13 +41,19 @@ public void start(Stage stage) throws Exception {
stage.setScene(scene);
stage.show();

// Train
RailwayTracks track = mapBuilder.getTrack("Royal");
Train train = new Train(track.getEndX()+100,track.getEndY()-25);
root.getChildren().add(train.getImageView());
// Trains
RailwayTracks track1 = mapBuilder.getTrack("Royal");
Train train1 = new Train(track1.getEndX()+100,track1.getEndY()-25,Direction.WEST);
root.getChildren().add(train1.getImageView());

for(CrossingGate gate: mapBuilder.getAllGates())
train.addObserver(gate);
RailwayTracks track2 = mapBuilder.getTrack("Other");
Train train2 = new Train(track2.getStartX()-100,track2.getStartY()-25,Direction.EAST);
root.getChildren().add(train2.getImageView());

for(CrossingGate gate: mapBuilder.getAllGates()){
train1.addObserver(gate);
train2.addObserver(gate);
}

// Sets up a repetitive loop i.e., in handle that runs the actual simulation
new AnimationTimer(){
Expand All @@ -55,13 +62,17 @@ public void start(Stage stage) throws Exception {
public void handle(long now) {

createCar();
train.move();
train1.move();
train2.move();

for(CrossingGate gate: mapBuilder.getAllGates())
gate.operateGate();

if (train.offScreen())
train.reset();
if (train1.offScreen())
train1.reset();

if (train2.offScreen())
train2.reset();

clearCars();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;

import edu.nd.sarec.railwaycrossing.model.infrastructure.gate.CrossingGate;
import edu.nd.sarec.railwaycrossing.model.vehicles.CarFactory;

/**
* Creates all infrastructure for the simulation
Expand All @@ -30,7 +31,7 @@ public MapBuilder(){
private void buildRoads(){
roads.put("Western Highway",new Road(new Point(800,0),new Point (800,1000),Direction.SOUTH,true,false));
roads.put("Skyway",new Road(new Point(400,0),new Point (400,1000),Direction.SOUTH,true,false));
roads.put("EastWest",new Road(new Point(415,800),new Point (785,800),Direction.EAST,true,true));
roads.put("EastWest",new Road(new Point(415,350),new Point (785,350),Direction.EAST,true,true));
}

private void buildCrossingGates(){
Expand All @@ -40,6 +41,7 @@ private void buildCrossingGates(){

private void buildTracks(){
tracks.put("Royal", new RailwayTracks(new Point(0,500),new Point(1200,500)));
tracks.put("Other", new RailwayTracks(new Point(0,550),new Point(1200,550)));
}

private void assignGatesToRoads(){
Expand All @@ -48,8 +50,10 @@ private void assignGatesToRoads(){
}

private void buildCarFactories(){
roads.get("Western Highway").addCarFactory();
roads.get("Skyway").addCarFactory();
CarFactory eastFactory = roads.get("Skyway").addCarFactory();
CarFactory westFactory = roads.get("Western Highway").addCarFactory();
eastFactory.setOtherCarFactory(westFactory);
westFactory.setOtherCarFactory(eastFactory);
}

public Collection<CrossingGate> getAllGates(){
Expand All @@ -65,6 +69,6 @@ public Collection<Road> getRoads(){
}

public RailwayTracks getTrack(String name){
return tracks.get("Royal");
return tracks.get(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Road {
private int endX;
private int startY;
private int endY;
private boolean hasCarFactory;
private CarFactory carFactory;
Direction direction;
Collection<CrossingGate> gates;
Expand All @@ -32,6 +33,7 @@ public Road(Point start, Point end, Direction direction, boolean buildCarFactory
endY = end.y;
roadSize = 18;

this.hasCarFactory = buildCarFactory;
this.direction = direction;
gates = new Vector<CrossingGate>();
this.clearEnds = clearEnds;
Expand All @@ -47,9 +49,10 @@ public void assignGate(CrossingGate gate){
carFactory = new CarFactory(direction, new Point(startX-roadSize/2,startY), gates); // allows additional gates. Needs fixing
}

public void addCarFactory(){
public CarFactory addCarFactory(){
if (carFactory == null) // We only allow one
carFactory = new CarFactory(direction, new Point(startX-roadSize/2,startY), gates);
return carFactory;
}

public CarFactory getCarFactory(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package edu.nd.sarec.railwaycrossing.model.infrastructure.gate;

import java.security.GeneralSecurityException;
import java.util.Observable;
import java.util.Observer;

import edu.nd.sarec.railwaycrossing.model.infrastructure.Direction;
import edu.nd.sarec.railwaycrossing.model.vehicles.Train;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
Expand All @@ -20,9 +22,9 @@ public class CrossingGate extends Observable implements Observer{
private int anchorY;
private int movingX;
private int movingY;
private int triggerPoint;
private int exitPoint;

private int rightTriggerPoint;
private int leftTriggerPoint;
private IGateState gateClosed;
private IGateState gateOpen;
private IGateState gateClosing;
Expand All @@ -40,8 +42,8 @@ public CrossingGate(int xPosition, int yPosition, String crossingGate){
anchorY = yPosition;
movingX = anchorX;
movingY = anchorY-60;
triggerPoint = anchorX+250;
exitPoint = anchorX-250;
rightTriggerPoint = anchorX+250;
leftTriggerPoint = anchorX-250;

// Gate elements
line = new Line(anchorX, anchorY,movingX,movingY);
Expand Down Expand Up @@ -119,11 +121,14 @@ public String getTrafficCommand(){
public void update(Observable o, Object arg) {
if (o instanceof Train){
Train train = (Train)o;
if (train.getVehicleX() < exitPoint)
if(train.getDirection() == Direction.WEST && train.getVehicleX() < leftTriggerPoint)
currentGateState.leaveStation();
if(train.getDirection() == Direction.EAST && train.getVehicleX() > rightTriggerPoint && gateName == "Gate1")
currentGateState.leaveStation();
else if(train.getVehicleX() < triggerPoint){

if(train.getVehicleX() < rightTriggerPoint && train.getVehicleX() > leftTriggerPoint)
currentGateState.approachStation();
}
}

}
}
}
90 changes: 76 additions & 14 deletions src/edu/nd/sarec/railwaycrossing/model/vehicles/Car.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@ public class Car extends Observable implements IVehicle, Observer{
private boolean gateDown = false;
private double leadCarY = -1; // Current Y position of car directly infront of this one
private double speed = 0.5;

private boolean inJunction = false;
private CarFactory factory;

/**
* Constructor
* @param x initial x coordinate of car
* @param y initial y coordinate of car
*/
public Car(int x, int y){
public Car(int x, int y, CarFactory eastFactory){
this.currentX = x;
this.currentY = y;
originalY = y;
ivCar = new ImageView(CarImageSelector.getImage());
ivCar.setX(getVehicleX());
ivCar.setY(getVehicleY());
this.factory = eastFactory;
}

@Override
Expand All @@ -53,22 +56,76 @@ public double getVehicleY(){
}

public void move(){
boolean canMove = true;
boolean canMoveY = true;
boolean canMoveX = false;

// First case. Car is at the front of the stopping line.
if (gateDown && getVehicleY() < 430 && getVehicleY()> 390)
canMove = false;
if(inJunction)
canMoveY = false;
else{
// First case. Car is at the front of the stopping line.
if (gateDown && getVehicleY() < 430 && getVehicleY()> 390)
canMoveY = false;

// Second case. Car is too close too other car.
if (leadCarY != -1 && getDistanceToLeadCar() < 50)
canMoveY = false;
}

// Second case. Car is too close too other car.
if (leadCarY != -1 && getDistanceToLeadCar() < 50)
canMove = false;
// Third case, car can chose to go into junction
if (!inJunction && currentX == 791.0 && currentY > 340 && currentY < 341.5 && Math.random() < 0.5){
inJunction = true;

// like in a linked list, remove car node and connect loose pointers
// tried to implement but didn't work
/*this.deleteObservers();

Car observed = null;
Car observer = null;
for(int i=0; i<factory.getCars().size(); i++){
Car c = factory.getCars().get(i);
if(c == this && i < factory.getCars().size()-1 && i > 0){
observed = factory.getCars().get(i+1);
observer = factory.getCars().get(i-1);
System.out.println("found");
break;
}
}
if(observed != null)
observed.addObserver(observer);
*/

// observe last car added to West road
factory.getOtherCarFactory().getPreviousCar().addObserver(this);
factory.getOtherCarFactory().setPreviousCar(this);
factory.getOtherCarFactory().addCarToList(this);
}

// Forth case. Car in junction
if (inJunction && currentX > 420)
canMoveX = true;

if (canMove){
// Fifth case. Move out of junction
if (inJunction && currentX < 420 && getDistanceToLeadCar() > 50){
currentX = 391;
ivCar.setX(currentX);
setChanged();
notifyObservers();
inJunction = false;
}

if (canMoveY){
currentY+=speed;
ivCar.setY(currentY);
setChanged();
notifyObservers();
}

if (canMoveX){
currentX-=speed;
ivCar.setX(currentX);
setChanged();
notifyObservers();
}
}

public void setSpeed(double speed){
Expand All @@ -91,7 +148,7 @@ public void reset(){
}

public double getDistanceToLeadCar(){
return Math.abs(leadCarY-getVehicleY());
return leadCarY-getVehicleY();
}

public void removeLeadCar(){
Expand All @@ -102,7 +159,8 @@ public void removeLeadCar(){
public void update(Observable o, Object arg1) {
if (o instanceof Car){
leadCarY = (((Car)o).getVehicleY());
if (leadCarY > 1020)

if (((Car)o).isCarInJunction() || leadCarY > 1020)
leadCarY = -1;
}

Expand All @@ -112,7 +170,11 @@ public void update(Observable o, Object arg1) {
gateDown = true;
else
gateDown = false;

}
}
}

public boolean isCarInJunction()
{
return inJunction;
}
}
32 changes: 29 additions & 3 deletions src/edu/nd/sarec/railwaycrossing/model/vehicles/CarFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class CarFactory {
Direction direction;
Point location;

CarFactory otherFactory;

public CarFactory(){}

public CarFactory(Direction direction, Point location, Collection<CrossingGate> gates){
Expand All @@ -33,9 +35,9 @@ public CarFactory(Direction direction, Point location, Collection<CrossingGate>
// Most code here is to create random speeds
public Car buildCar(){
if (previousCar == null || location.y < previousCar.getVehicleY()-100){
Car car = new Car(location.x,location.y);
Car car = new Car(location.x,location.y,this);
double speedVariable = (Math.random() * 10)/10;
car.setSpeed((2-speedVariable)*1.5);
car.setSpeed((2-speedVariable)*0.99);

// All cars created by this factory must be aware of crossing gates in the road
for(CrossingGate gate: gates){
Expand All @@ -44,7 +46,7 @@ public Car buildCar(){
car.setGateDownFlag(false);
}

// Each car must observe the car infront of it so it doesn't collide with it.
// Each car must observe the car in front of it so it doesn't collide with it.
if (previousCar != null)
previousCar.addObserver(car);
previousCar = car;
Expand Down Expand Up @@ -72,4 +74,28 @@ public ArrayList<Car> removeOffScreenCars() {
cars.remove(car);
return toDelete;
}

public Car getPreviousCar(){
return previousCar;
}

public void setPreviousCar(Car c){
previousCar = c;
}

public ArrayList<Car> getCars(){
return cars;
}

public void setOtherCarFactory(CarFactory cf){
otherFactory = cf;
}

public CarFactory getOtherCarFactory(){
return otherFactory;
}

public void addCarToList(Car c){
cars.add(c);
}
}
Loading