Android Open Source - ZorbsCity House






From Project

Back to project page ZorbsCity.

License

The source code is released under:

GNU General Public License

If you think the Android project ZorbsCity listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jonathan.geoffroy.zorbscity.model.cityObjects.buildings;
/*from  w w  w  . ja v  a2  s.c o  m*/
import jonathan.geoffroy.zorbscity.model.Simulator;
import jonathan.geoffroy.zorbscity.model.SimulatorEvent;
import jonathan.geoffroy.zorbscity.model.TimeLine;
import jonathan.geoffroy.zorbscity.model.cityObjects.resources.Resource;
import jonathan.geoffroy.zorbscity.model.helpers.NeededList;
import jonathan.geoffroy.zorbscity.model.simulator.events.ConsumeNeededResources;

/**
 * House support inhabitants: it's used to have more inhabitants in a city.
 * @author Jonathan GEOFFROY
 * @version 0.1
 */
public abstract class House extends Building {

  /**
   * auto-generated serial (using eclipse)
   */
  private static final long serialVersionUID = -5852205493007290911L;

  public static final int TIME_TO_USE_RESOURCE = 12;

  /**
   * The list of previous resources, i.e. the resources that previous house needs
   */
  protected NeededList previousNeeded;

  public House() {
    super();
    previousNeeded = new NeededList();
    addPreviousNeededResources();
    addNeededResources();
  }

  public House(House house) {
    super(house);
    previousNeeded = new NeededList();

    addPreviousNeededResources();
    addNeededResources();

    this.numberPeople = house.numberPeople;
    this.resources = house.resources;
  }

  private void addPreviousNeededResources() {
    House previousHouse = getPreviousHouseType();
    if(previousHouse != null) {
      previousNeeded.addAllNeeded(previousHouse.previousNeeded);
      previousNeeded.addAllNeeded(previousHouse.needed);
    }
  }

  @Override
  public void addResource(Simulator sim, Resource r) {
    super.addResource(sim, r);

    House increasedThis = getNextHouseType();
    if(increasedThis != null && enoughResources()) {
      sim.transform(this, increasedThis);
      increasedThis.justIncreased(sim);
    }
  }

  @Override
  public void onAddedIntoCity(Simulator sim) {
    super.onAddedIntoCity(sim);
    beginToConsumeResources(sim);
  }

  @Override
  public boolean enoughResources() {
    return resources.enoughResources(needed) && resources.enoughResources(previousNeeded);
  }

  @Override
  public boolean enoughResources(int resourceType) {
    return resources.enoughResources(needed, resourceType) && resources.enoughResources(previousNeeded, resourceType);
  }

  /**
   * Check if the current house should increase
   * @return true if the current house should increase
   */
  public boolean shouldIncrease() {
    return resources.enoughResources(needed);
  }

  /**
   * Check if the current house should decrease
   * Ask the previous house type and check if it should increase
   * @return true if the current house should decrease
   */
  public boolean shouldDecrease() {
    return !resources.enoughResources(previousNeeded);
  }

  /**
   * Create a new instance of the next House (when the current House upgrade)
   * @return a new instance of the next House (or null if this is the last house type)
   */
  protected abstract House getNextHouseType();

  /**
   * Create a new instance of the previous House
   * @return a new instance of the previous House (or null if this is the first house type)
   */
  protected abstract House getPreviousHouseType();

  /**
   * Allows the needed resources exclusively for this house (and not previous houses) 
   */
  protected abstract void addNeededResources();

  public abstract int getMaxPeopleNumber();

  /**
   * 
   * @return true if this house cann't take an other inhabitant
   */
  public boolean isFull() {
    assert(numberPeople <= getMaxPeopleNumber());
    return numberPeople == getMaxPeopleNumber();
  }

  @Override
  public void onTriggeredEvent(SimulatorEvent event, Simulator sim) {
    super.onTriggeredEvent(event, sim);
    if(event instanceof ConsumeNeededResources) {
      onTriggeredEvent((ConsumeNeededResources) event, sim);
    }
  }

  public void onTriggeredEvent(ConsumeNeededResources event, Simulator sim) {
    int i = 0;
    int nb = event.getNumberConsumer();
    while (i < nb && resources.enoughResources(previousNeeded)) {
      resources.useNeeded(previousNeeded);
      i++;
    }

    if(!resources.enoughResources(previousNeeded)) {
      House decreasedThis = getPreviousHouseType();
      if(decreasedThis != null) {
        sim.transform(this, decreasedThis);
        decreasedThis.justDecreased(sim);
      }
    }
    else {
      beginToConsumeResources(sim);
    }

  }

  private void beginToConsumeResources(Simulator sim) {
    TimeLine timeLine = sim.getTimeLine();
    timeLine.add(new ConsumeNeededResources(this,timeLine.getTime() + TIME_TO_USE_RESOURCE, this.numberPeople));
  }

  private void justIncreased(Simulator sim) {}

  private void justDecreased(Simulator sim) {}
}




Java Source Code List

jonathan.geoffroy.zorbscity.MainActivity.java
jonathan.geoffroy.zorbscity.Main.java
jonathan.geoffroy.zorbscity.ZorbCityGame.java
jonathan.geoffroy.zorbscity.client.GwtLauncher.java
jonathan.geoffroy.zorbscity.model.City.java
jonathan.geoffroy.zorbscity.model.Galaxy.java
jonathan.geoffroy.zorbscity.model.SimulatorEvent.java
jonathan.geoffroy.zorbscity.model.Simulator.java
jonathan.geoffroy.zorbscity.model.TimeLine.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Building.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Construction.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Enterprise.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.House.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Road.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Storage.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Structure.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.enterprises.ClothesFactory.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.enterprises.PowerHouse.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.houses.FreeHouse.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.houses.SimpleHouse.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.houses.TentHouse.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.storages.FoodStorage.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.storages.SecondaryStorage.java
jonathan.geoffroy.zorbscity.model.cityObjects.people.DeliveryMan.java
jonathan.geoffroy.zorbscity.model.cityObjects.people.Person.java
jonathan.geoffroy.zorbscity.model.cityObjects.resources.Cloth.java
jonathan.geoffroy.zorbscity.model.cityObjects.resources.Power.java
jonathan.geoffroy.zorbscity.model.cityObjects.resources.Resource.java
jonathan.geoffroy.zorbscity.model.cityObjects.resources.Wheat.java
jonathan.geoffroy.zorbscity.model.cityObjects.resources.Wool.java
jonathan.geoffroy.zorbscity.model.helpers.Coord2D.java
jonathan.geoffroy.zorbscity.model.helpers.NeededList.java
jonathan.geoffroy.zorbscity.model.helpers.ResourcesList.java
jonathan.geoffroy.zorbscity.model.simulator.events.ConsumeNeededResources.java
jonathan.geoffroy.zorbscity.model.simulator.events.ObjectCreatedEvent.java
jonathan.geoffroy.zorbscity.model.simulator.events.WeaknessEvent.java
jonathan.geoffroy.zorbscity.view.CityView.java
jonathan.geoffroy.zorbscity.view.shell.CityShellView.java