mercredi 11 novembre 2015

How to keep track of what object belongs to which object?

Disclaimer: This is a school project but Prof said it's ok to ask for help.

The project is to simulate a traffic grid. The part I'm working on is keeping track of what car is on what road. A road is an array list containing cars. I've made a road handler (an array list) that contains all roads.

public void checkPositionOnRoad(){
    if(this.getPosition() > RoadHandler.currentRoad.getLength()-this.getCarLength()){
        RoadHandler.currentRoad.remove(this);
        Road nextRoad = moveToNextRoad(RoadHandler.currentRoad);
        nextRoad.accept(this);
    }
}

The problem is that I don't know how to keep track of "currentRoad". Getting the next road is easy enough because I can just say

nextroad(Road currentRoad); 

where I just grab the currentRoad's index+1.

The end goal is for the car to finish it's road and move on to the next one. So while the car runs it checks to see where its on in the road.

public void run(double time) {
    checkPositionOnRoad();
    position += velocity;
}

Also I want to reset the car's position on the new road.

Aucun commentaire:

Enregistrer un commentaire