Android Open Source - ZorbsCity City






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;
//from  w  w w  . java 2  s .  co  m
import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Pixmap;

import jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Construction;
import jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Structure;
import jonathan.geoffroy.zorbscity.model.cityObjects.people.Person;
import jonathan.geoffroy.zorbscity.model.helpers.Coord2D;

/**
 * This class describe a City, i.e. a part of the Galaxy.
 * This is the main class of modeling, because it describe the City as it is managed by the player.
 * This city is composed by Structures (Building) & people (Person). Both is used by Simulator to reach the next step of simulation.
 * This city have a name and an amount of money.
 * @author Jonathan GEOFFROY
 * @version 0.1
 */
public class City {
  public final static int 
  UNREACHABLE_MAPPING = 0, WATER_MAPPING = 1, CONSTRUCTION_MAPPING = 2, DESTROYED_MAPPING = 3, ROAD_MAPPING = 4,
  GROUND_MAPPING = 100;

  private final static int GROUND_COLOR = 1717987071, WATER_COLOR = 65535;
  /**
   * The name of the City
   * shouldn't be null
   * Useful to load the City
   */
  private String name;

  /**
   * A byte-mapping of the City
   * This mapping is useful to describe each Terrain type: {water, unreachable, road, mountain, grassland ... } 
   */
  private int mapping[][];

  /**
   * A list of all buildings in the City.
   * Using by Simulator to compute the next step of simulation:
   * ForEach Building b in buildings -> b.simulationAct(simulator)
   */
  private List<Construction> constructions;

  /**
   * People who's walking down the street
   * Using by Simulator to compute the next step of simulation:
   * ForEach Person p in people -> p.simulationAct(simulator)
   */
  private List<Person> people;

  /**
   * Amount of city money 
   * can be negative
   */
  private int money;

  /**
   * Initialize the name of the city, & instantiate buildings & people. Don't add anything into Arrays
   * @param name
   */
  public City(String name) {
    super();
    this.name = name;
    constructions = new ArrayList<Construction>();
    people = new ArrayList<Person>();
  }

  /**
   * Load the City cityName
   * @param cityName the name of loaded file
   * @return the loaded City, or null if the city don't exist
   */
  public static City load(String cityName) {
    City c = new City(cityName);
    try {
      FileHandle file = Gdx.files.internal("data/cities/" + cityName + ".bmp");
      Pixmap img = new Pixmap(file);
      int width = img.getWidth();
      int height = img.getHeight();

      c.mapping = new int[height][width];
      for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
          switch(img.getPixel(j, i)) {
          case GROUND_COLOR:
            c.mapping[i][j] = GROUND_MAPPING; 
            break;
          case WATER_COLOR:
            c.mapping[i][j] = WATER_MAPPING;
            break;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
    return c;
  }

  /**
   * Add resources on the city, no matter what Storage
   * If the free space < amount, so all free space is used, & the rest is definitively lost
   * @param resType the type of the resource
   * @param amount the amount of resources (should be > 0)
   * @return true only if all resources have been added 
   */
  public boolean addResources(int resType, int amount) {
    assert(amount > 0) : "the amount of ressources should be positive";
    //TODO add resources on City's storages.
    return haveAtLeast(resType, amount);
  }

  /**
   * Remove resources on the city, if it is possible, or do nothing if there is no enough free space on the city.
   *  no matter what Storage is used.
   * @param resType the type of the resource
   * @param amount the amount of resources (should be > 0)
   * @return true only if all resources have been added
   */
  public boolean rmResources(int resType, int amount) {
    // TODO rm ressources from City's storages
    return haveAtLeast(resType, amount);
  }

  /**
   * 
   * @param resType
   * @param amount
   * @return true if there is at least amount piece of resType
   */
  private boolean haveAtLeast(int resType, int amount) {
    // TODO
    return false;
  }

  /**
   * add the structure s into the City, at coords coordinates.
   * @param s the structure to add
   * @param coords the coordinates of the new structure s
   */
  public void add(Structure s, Coord2D coords) {
    assert(canBeAdded(s, coords)) : "this Structure cannot be placed here!";
    s.addInto(this, coords);
  }

  /**
   * add the building b into the City, at coords coordinates.
   * @param c the building to add
   * @param coords the coordinates of the new building b
   */
  public void add(Construction c, Coord2D coords) {
    assert(canBeAdded(c, coords)) : "this Structure cannot be placed here!";
    c.addInto(this, coords);
    constructions.add(c);
  }

  /**
   * Add a person on the city
   * Delegate method for people.add(Person)
   * @param p
   */
  public void add(Person p) {
    people.add(p);
  }

  /**
   * Remove the Structure at the coords coordinates. Replace this Structure by GROUND_MAPPING if destroyed == false, by DESTROYED_MAPPING otherwise
   * @param coords
   * @param destroyed true if the structure to destroy is destroyed and not just removed
   * @return true if there is anything removed
   */
  public boolean rm(Coord2D coords, boolean destroyed) {
    int replacedBy;
    if(destroyed) {
      replacedBy = DESTROYED_MAPPING;
    }
    else {
      replacedBy = GROUND_MAPPING;
    }
    
    if(canBeRemoved(coords)) {
      switch(mapping[coords.y][coords.x]) {
      case CONSTRUCTION_MAPPING:
        assert(constructions.contains(coords));
        Construction removed = constructions.get(constructions.indexOf(coords));
        assert(removed != null);

        constructions.remove(coords);
        for(int i = 0, size = removed.getMappingSize(); i < size; i++) {
          for(int j = 0; j < size; j++) {
            assert(mapping[coords.y + i][coords.x + j] == CONSTRUCTION_MAPPING) : "we're trying to remove something else that a construction ?!";
            mapping[coords.y + i][coords.x + j] = replacedBy;
          }
        }
        break;
      case ROAD_MAPPING:
        mapping[coords.y][coords.x] = replacedBy;
        break;
      }
      return true;
    }
    return false;
  }

  /**
   * 
   * @param s the structure to test
   * @param coords the coordinates of top-left corner of structure s
   * @return true if the structure s can be placed on the coords coordinates
   */
  public boolean canBeAdded(Structure s, Coord2D coords) {
    int structSize = s.getMappingSize();
    int endX = coords.x + structSize -1, endY = coords.y + structSize -1;
    Coord2D testCoords = new Coord2D();

    // No enough space in the map 
    if(endY >= getHeight() || endX >= getWidth()) {
      return false;
    }

    // Any unreachable space in the map into coordinates
    for(testCoords.y = coords.y; testCoords.y <= endY; testCoords.y++) {
      for(testCoords.x = coords.x; testCoords.x <= endX; testCoords.x++) {
        if(!isConstructibleMapping(testCoords)) {
          return false;
        }
      }
    }

    return true;
  }

  /**
   * 
   * @param coords the coordinates of structure to test
   * @return true if there is anything to remove
   */
  public boolean canBeRemoved(Coord2D coords) {
    // outside the map
    if(coords.y >= getHeight() || coords.x >= getWidth()) {
      return false;
    }

    switch(mapping[coords.y][coords.x]) {
    case CONSTRUCTION_MAPPING:
    case ROAD_MAPPING:
      return true;
    default:
      return false;
    }
  }

  /**
   * 
   * @param c
   * @return true if the building <b> have at least a road on one of its edges
   */
  public boolean haveRoad(Construction c) {
    Coord2D bCoords = c.getCoords();
    for(int i = 0, size = c.getMappingSize(); i < size; i++) {
      if(   (bCoords.y > 0 && mapping[bCoords.y - 1][bCoords.x + i] == ROAD_MAPPING) ||             // check the row, on the column before the building 
          (bCoords.y + size < mapping.length && mapping[bCoords.y + size][bCoords.x + i] == ROAD_MAPPING) ||   // check the row, on the column after  the building
          (bCoords.x > 0 &&mapping[bCoords.y + i][bCoords.x - 1] == ROAD_MAPPING) ||               // check the column, on the row before the building
          (bCoords.y + size < mapping[0].length && mapping[bCoords.y + i][bCoords.x + size] == ROAD_MAPPING)) {// check the column, on the row after  the building
        return true;
      }
    }
    return false;
  }


  /* ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *****
   * 
   * ***** ***** ***** ***** ***** Getters & Setters ***** ***** ***** ***** *****
   * 
   * ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *****
   */
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getMoney() {
    return money;
  }

  public void setMoney(int money) {
    this.money = money;
  }

  public void addMoney(int amount) {
    money += amount;
  }

  public void removeMoney(int amount) {
    money -= amount;
  }

  public List<Construction> getBuildings() {
    return constructions;
  }

  public List<Person> getPeople() {
    return people;
  }

  public int getMapping(Coord2D coords) {
    return mapping[coords.y][coords.x];
  }

  public boolean isConstructibleMapping(Coord2D coords) {
    assert(coords.y < getHeight() && coords.x < getWidth());
    return mapping[coords.y][coords.x] >= 100;
  }

  public void setMapping(int mapType, Coord2D coords) {
    mapping[coords.y][coords.x] = mapType;
  }

  public int getWidth() {
    return mapping[0].length;
  }

  public int getHeight() {
    return mapping.length;
  }
}




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