Example #1
0
  @Override
  public void update(int delta) {
    if (viewingCompletedDay) {
      viewingDayTime += delta;
      if (viewingDayTime >= 4000) {
        advanceDay();
      }
      return;
    }
    tickProgress += delta;
    if (tickProgress >= ticksPerHour) {
      advanceHour();
      tickProgress = 0;
    }
    // 1. update office world
    officeWorld.updateEntities(delta);
    // update paperwork
    paperworkLeft -= (getNumWorkerAlive() * EMPLOYEE_WORK_RATE);
    if (paperworkLeft < 0) {
      paperworkLeft = 0;
    }

    // imp spawning
    currImpAttackTime += delta;
    if (currImpAttackTime > impWaitTimeCurrent
        && getNumWorkerAlive() != 0) { // there must be workers alive to spawn an imp on one
      // make an imp randomly attack
      // make a random worker fall asleep
      if (Math.random() > 0.5) { // 50% chance to spawn at time limit
        // randomly chose a non-dead worker
        int whichOne = 0;
        int iterations = 0; // prevent infinite loop
        do {
          whichOne = (int) (Math.random() * workers.length);
          iterations++;
        } while (iterations >= 50
            && // safe lock to stop the loop after 100 times.
            workers[whichOne].getState() != Employee.WORKING
            && !workers[whichOne].isBeingAttackedByImp());
        if (!workers[whichOne].isBeingAttackedByImp()
            && workers[whichOne].getState() == Employee.WORKING
            && iterations < 50) { // only spawn an imp if iterations did not go out too far
          // define the task and add it to the list of tasks to be updated
          HitImpTask impTask = new HitImpTask(this, workers[whichOne]);
          ImpAttackTaskObject impTaskObject =
              new ImpAttackTaskObject(
                  officeWorld,
                  workers[whichOne].getX(),
                  getAssetManager().getImage("emptyImage"),
                  camera,
                  impTask);
          impTaskObjects.add(impTaskObject); // add to the list to keep track of it
          // spawn a "wake up" task object near them
          officeWorld.addEntity(impTaskObject);
          SoundPlayer.getSoundPlayer().playImpApproaching();
          // System.out.println("An imp started to attack an employee: whichOne: " + whichOne
          //     + ", worker being attacked already: " + workers[whichOne].isBeingAttackedByImp());
          workers[whichOne].setBeingAttackedByImp(true);
        }
      }
      currImpAttackTime = 0;
    }

    if (nextTaskIndex < toDoList.size() && toDoList.get(nextTaskIndex).isComplete()) {
      nextTaskIndex++;
    }
    for (int j = 0; j < toDoList.size(); j++) {
      toDoList.get(j).update(delta);
    }
    // 3. ?? update the input ??
    // Input does not need to be updated

    // Update the camera
    // The camera sets its position itself
    camera.setXLocation((int) (demonPlayer.getX() + (demonPlayer.getWidth() / 2) - 400));
    // Cool parallax scrolling
    parallaxOffset =
        (int)
            ((float)
                    (demonPlayer.getX()
                        / ((float) (Camera.getCameraMax() - demonPlayer.getWidth())
                            - Camera.getCameraMin()))
                * 800);

    // Update flying imp
    if (showImp) {
      impXLoc += 30;
      if (impXLoc > 850) {
        showImp = false;
      }
    }

    // startShowingImp();

    OfficeTaskObject intersects = null;
    for (int i = 0; i < officeWorld.getEntities().size(); i++) {
      OfficeObject obj = officeWorld.getEntities().get(i);
      if (obj instanceof OfficeTaskObject) {
        OfficeTaskObject task = (OfficeTaskObject) obj;
        if (task instanceof ChangeEmployeeStateTaskObject || task instanceof ImpAttackTaskObject) {
          // if a wake employee, interview, or kill imp task is completed, then its deleted on
          // completion.
          if (task.getAssociatedTask().isComplete()) {
            // remove object
            if (activeTask == task.getAssociatedTask()) {
              activeTask = null;
            }
            obj.setParent(null);
            officeWorld.getEntities().remove(obj);
            // remove the imp task from the list of imp tasks.
            if (task instanceof ImpAttackTaskObject) {
              ImpAttackTaskObject iat = (ImpAttackTaskObject) task;
              if (impTaskObjects.contains(iat)) {
                impTaskObjects.remove(iat);
              }
            }
            continue; // go to the next iteration.
          }
        }
        if (task.collidesWith(demonPlayer)) {
          intersects = task;
        }
      }
    }
    if (intersects != null) {
      activeTask = intersects.getAssociatedTask();
    } else {
      activeTask = null;
    }
    gui.update(-1, -1);
  }