@Override
  public void onScroll(int scrolledY) {
    super.onScroll(scrolledY);

    mBoundedTranslatedRatio = clamp(getTranslationRatio(), 0f, 1f);

    if (hasAnimatorBundles) {
      mAnimatorBuilder.animateOnScroll(mBoundedTranslatedRatio, getHeader().getTranslationY());
    }
  }
Пример #2
0
  /** Run the simulation for <code>duration</code> model seconds. */
  public void run(int duration) {
    _agents = new ArrayList<Agent>();
    setup(builder, rows, columns);
    _animator = builder.getAnimator();
    super.addObserver(_animator);
    if (_disposed) throw new IllegalStateException();

    // print out lights and roads.
    for (int i = 0; i < duration; i++) {
      _time++;

      // go through road sources and see if a car needs to be added
      for (int k = 0; k < _sources.size(); k++) {
        if (_time % _sources.get(k).getGeneration() == 0) {
          if (_sources.get(k).forGridLock() > 0) {
            Car car = new Car();
            car.setRoad(_sources.get(k));
            _cars.add(car);
            _agents.add(car);
            _sources.get(k).accept(car);
          }
        }
      }

      Agent[] agents_copy = _agents.toArray(new Agent[0]);
      for (Agent a : agents_copy) {
        a.run(_time);

        if (a instanceof Car) {

          // MOVE THE CARS IF THEY'RE OFF ROADS
          _index = _roads.indexOf((((Car) a).getRoad()));

          // if its on a sink
          if (((Car) a).getKill() == true) {
            _agents.remove(a);
            _roads.get(_index).removeCar((Car) a);
          }
          // if its gone off a road, but its not a sink
          else if (((Car) a).getPosition() < 0) {

            // can't move if the light is red
            Road road = _roads.get(_index);

            // all about stopping for the lights
            if (road.getPattern().equals("RH")
                && intersections[road.getX()][road.getY()].getColorEW().equals(Color.RED)) {
              ((Car) a).setPosition((int) (MP.roadLength - ((Car) a).getLength()));
              ((Car) a).stop(true);
            } else if (road.getPattern().equals("RV")
                && intersections[road.getX()][road.getY()].getColorNS().equals(Color.RED)) {
              ((Car) a).setPosition((int) (MP.roadLength - ((Car) a).getLength()));
              ((Car) a).stop(true);
            } else if (road.getPattern().equals("AH")
                && intersections[road.getX()][road.getY() - 1].getColorEW().equals(Color.RED)) {
              ((Car) a).setPosition((int) (MP.roadLength - ((Car) a).getLength()));
              ((Car) a).stop(true);
            } else if (road.getPattern().equals("AV")
                && intersections[road.getX() - 1][road.getY()].getColorNS().equals(Color.RED)) {
              ((Car) a).setPosition((int) (MP.roadLength - ((Car) a).getLength()));
              ((Car) a).stop(true);
            } else {
              // check for road block
              // don't go over the array size
              if ((_index + 1) < _roads.size()) {
                if (_roads.get(_index + 1).forGridLock() > 0) {
                  _roads.get(_index).removeCar(a);
                  ((Car) a).setPosition(0);
                  _index++;

                  _roads.get(_index).accept((Car) a);
                  ((Car) a).setRoad(_roads.get(_index));
                }
              }
            }
          }
        }
      }
      super.setChanged();
      super.notifyObservers();
    }
  }
 @Override
 protected void onAnimatorReady() {
   super.onAnimatorReady();
   mAnimatorBuilder = getAnimatorBuilder();
   hasAnimatorBundles = (mAnimatorBuilder != null) && (mAnimatorBuilder.hasAnimatorBundles());
 }
Пример #4
0
  /** Construct the model, establishing correspondences with the visualizer. */
  private void setup(AnimatorBuilder builder, int rows, int columns) {
    _roads = new ArrayList<Road>();
    _cars = new ArrayList<Car>();
    _sources = new ArrayList<Road>();
    intersections = new Light[rows][columns];

    // Add Lights
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < columns; j++) {
        intersections[i][j] = new Light();
        builder.addLight(intersections[i][j], i, j);
        _agents.add(intersections[i][j]);
      }
    }

    // Add Horizontal Roads
    boolean eastToWest = false;
    Road l;
    ArrayList<Road> temp;

    for (int i = 0; i < rows; i++) {
      temp = new ArrayList<Road>();
      for (int j = 0; j <= columns; j++) {

        if (!eastToWest) {
          if (j == 0) {
            l = new Road("source", i, j, "RH");
          } else if (j == columns) {
            l = new Road("sink", i, j, "RH");
          } else {
            l = new Road("regular", i, j, "RH");
          }

          builder.addHorizontalRoad(l, i, j, eastToWest);
          _roads.add(l);
        } else {
          if (j == 0) {
            temp.add(new Road("sink", i, j, "AH"));
          } else if (j == columns) {
            temp.add(new Road("source", i, j, "AH"));
          } else {
            temp.add(new Road("regular", i, j, "AH"));
          }
        }
      }
      if (eastToWest) {
        for (int k = temp.size() - 1; k >= 0; k--) {
          l = temp.get(k);
          builder.addHorizontalRoad(l, i, k, eastToWest);
          _roads.add(l);
        }
      }
      if (alternating) eastToWest = !eastToWest;
    }

    // Add Vertical Roads
    boolean southToNorth = false;
    for (int j = 0; j < columns; j++) {
      temp = new ArrayList<Road>();
      for (int i = 0; i <= rows; i++) {
        if (!southToNorth) {
          if (i == 0) {
            l = new Road("source", i, j, "RV");
          } else if (i == rows) {
            l = new Road("sink", i, j, "RV");
          } else {
            l = new Road("regular", i, j, "RV");
          }

          builder.addVerticalRoad(l, i, j, southToNorth);
          _roads.add(l);
        } else {
          if (i == 0) {
            temp.add(new Road("sink", i, j, "AV"));
          } else if (i == rows) {
            temp.add(new Road("source", i, j, "AV"));
          } else {
            temp.add(new Road("regular", i, j, "AV"));
          }
        }
      }
      if (southToNorth) {
        for (int k = temp.size() - 1; k >= 0; k--) {
          l = temp.get(k);

          builder.addVerticalRoad(l, k, j, southToNorth);
          _roads.add(l);
        }
      }
      if (alternating) southToNorth = !southToNorth;
    }

    // Add Road Sources
    for (Road l1 : _roads) {
      if (l1.getRoadType().equals("source")) {
        _sources.add(l1);
      }
    }
  }