Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package worldsimulation.map.characters;
- import java.util.Random;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import worldsimulation.map.roads.Road;
- import worldsimulation.Simulation;
- import worldsimulation.map.cities.City;
- import worldsimulation.map.cities.CityEntrance;
- import worldsimulation.map.cities.CityExit;
- import worldsimulation.map.roads.CrossRoad;
- public class Citizen extends Character implements Runnable {
- private static final int MOVEMENT_SPEED = 1;
- private String surname;
- private String hometown;
- private int movingDir = 1;
- private boolean modifyingList = true;
- private boolean halt = false;
- private boolean wait = false;
- private boolean inCity = false;
- private String cityName;
- private String nextCity;
- public int flagmovement = 1;
- /**
- *
- * @param positionX Set start position X.
- * @param positionY Set start position Y.
- * @param forename Set forename of the Citizen.
- * @param surname Set surname of the Citizen.
- * @param hometown Set hometown of the Citizen.
- */
- public Citizen(int positionX, int positionY, String forename, String surname, String hometown) {
- super(positionX, positionY, forename);
- this.surname = surname;
- this.hometown = hometown;
- goHometown();
- new Thread(this).start();
- }
- @Override
- public void move() {
- for (Citizen citizen : Simulation.citizenList) {
- switch (this.movingDir) {
- case 1:
- wait = this.positionX == citizen.getPositionX() && this.positionY - 32 == citizen.getPositionY();
- break;
- case 2:
- wait = this.positionX - 32 == citizen.getPositionX() && this.positionY == citizen.getPositionY();
- break;
- case 3:
- wait = this.positionX + 32 == citizen.getPositionX() && this.positionY == citizen.getPositionY();
- break;
- case 4:
- wait = this.positionX == citizen.getPositionX() && this.positionY + 32 == citizen.getPositionY();
- break;
- }
- if (wait) {
- break;
- }
- }
- for (Road road : Simulation.roadList) {
- if (positionX == road.getPositionX() && positionY == road.getPositionY()) {
- this.movingDir = road.getMovingDir();
- break;
- }
- }
- for (CrossRoad crossRoad : Simulation.crossRoadList) {
- switch (this.movingDir) {
- case 1:
- if (this.positionX == crossRoad.getPositionX() && this.positionY - 32 == crossRoad.getPositionY()) {
- try {
- crossRoad.sem.acquire();
- for (CrossRoad restCrossRoad : Simulation.crossRoadList) {
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() && restCrossRoad.getPositionY() == crossRoad.getPositionY() - 32) {
- restCrossRoad.sem.acquire();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() - 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY() - 32) {
- restCrossRoad.sem.acquire();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() - 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY()) {
- restCrossRoad.sem.acquire();
- }
- }
- } catch (InterruptedException ex) {
- Logger.getLogger(Citizen.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- moveThroughCrossRoad();
- } finally {
- crossRoad.sem.release();
- for (CrossRoad restCrossRoad : Simulation.crossRoadList) {
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() && restCrossRoad.getPositionY() == crossRoad.getPositionY() - 32) {
- restCrossRoad.sem.release();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() - 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY() - 32) {
- restCrossRoad.sem.release();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() - 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY()) {
- restCrossRoad.sem.release();
- }
- }
- }
- }
- break;
- case 2:
- if (this.positionX - 32 == crossRoad.getPositionX() && this.positionY == crossRoad.getPositionY()) {
- try {
- crossRoad.sem.acquire();
- for (CrossRoad restCrossRoad : Simulation.crossRoadList) {
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() - 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY()) {
- restCrossRoad.sem.acquire();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() - 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY() + 32) {
- restCrossRoad.sem.acquire();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() && restCrossRoad.getPositionY() == crossRoad.getPositionY() + 32) {
- restCrossRoad.sem.acquire();
- }
- }
- } catch (InterruptedException ex) {
- Logger.getLogger(Citizen.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- moveThroughCrossRoad();
- } finally {
- crossRoad.sem.release();
- for (CrossRoad restCrossRoad : Simulation.crossRoadList) {
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() - 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY()) {
- restCrossRoad.sem.release();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() - 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY() + 32) {
- restCrossRoad.sem.release();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() && restCrossRoad.getPositionY() == crossRoad.getPositionY() + 32) {
- restCrossRoad.sem.release();
- }
- }
- }
- }
- break;
- case 3:
- if (this.positionX + 32 == crossRoad.getPositionX() && this.positionY == crossRoad.getPositionY()) {
- try {
- crossRoad.sem.acquire();
- for (CrossRoad restCrossRoad : Simulation.crossRoadList) {
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() + 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY()) {
- restCrossRoad.sem.acquire();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() + 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY() - 32) {
- restCrossRoad.sem.acquire();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() && restCrossRoad.getPositionY() == crossRoad.getPositionY() - 32) {
- restCrossRoad.sem.acquire();
- }
- }
- } catch (InterruptedException ex) {
- Logger.getLogger(Citizen.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- moveThroughCrossRoad();
- } finally {
- crossRoad.sem.release();
- for (CrossRoad restCrossRoad : Simulation.crossRoadList) {
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() + 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY()) {
- restCrossRoad.sem.release();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() + 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY() - 32) {
- restCrossRoad.sem.release();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() && restCrossRoad.getPositionY() == crossRoad.getPositionY() - 32) {
- restCrossRoad.sem.release();
- }
- }
- }
- }
- break;
- case 4:
- if (this.positionX == crossRoad.getPositionX() && this.positionY + 32 == crossRoad.getPositionY()) {
- try {
- crossRoad.sem.acquire();
- for (CrossRoad restCrossRoad : Simulation.crossRoadList) {
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() && restCrossRoad.getPositionY() == crossRoad.getPositionY() + 32) {
- restCrossRoad.sem.acquire();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() + 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY() + 32) {
- restCrossRoad.sem.acquire();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() + 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY()) {
- restCrossRoad.sem.acquire();
- }
- }
- } catch (InterruptedException ex) {
- Logger.getLogger(Citizen.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- moveThroughCrossRoad();
- } finally {
- crossRoad.sem.release();
- for (CrossRoad restCrossRoad : Simulation.crossRoadList) {
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() && restCrossRoad.getPositionY() == crossRoad.getPositionY() + 32) {
- restCrossRoad.sem.release();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() + 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY() + 32) {
- restCrossRoad.sem.release();
- }
- if (restCrossRoad.getPositionX() == crossRoad.getPositionX() + 32 && restCrossRoad.getPositionY() == crossRoad.getPositionY()) {
- restCrossRoad.sem.release();
- }
- }
- }
- }
- break;
- }
- }
- for (CityEntrance cityEntrance : Simulation.citiesEntranceList) {
- if (positionX == cityEntrance.getPositionX() && positionY == cityEntrance.getPositionY()) {
- this.inCity = true;
- this.cityName = cityEntrance.getCityName();
- for (City city : Simulation.citiesList) {
- if (this.cityName == city.getName()) {
- city.setResidentsNumber(city.getResidentsNumber() + 1);
- System.out.println("#" + city.getName() + " POPULATION INCREASE: " + city.getResidentsNumber());
- }
- }
- }
- }
- if (!wait) {
- switch (this.movingDir) {
- case 1:
- this.setNewPosition(positionX, positionY - MOVEMENT_SPEED);
- break;
- case 2:
- this.setNewPosition(positionX - MOVEMENT_SPEED, positionY);
- break;
- case 3:
- this.setNewPosition(positionX + MOVEMENT_SPEED, positionY);
- break;
- case 4:
- this.setNewPosition(positionX, positionY + MOVEMENT_SPEED);
- break;
- }
- }
- }
- public void moveThroughCrossRoad() {
- boolean ready = false;
- while (!ready) {
- for (CrossRoad cross : Simulation.crossRoadList) {
- if (positionX == cross.getPositionX() && positionY == cross.getPositionY()) {
- String[] citiesList = cross.getCitiesList();
- for (int i = 0; i < citiesList.length; i++) {
- if (this.nextCity == citiesList[i]) {
- this.movingDir = cross.getMovingDir();
- ready = true;
- break;
- } else {
- this.movingDir = cross.getNextCrossRoad();
- }
- }
- break;
- }
- }
- switch (this.movingDir) {
- case 1:
- this.setNewPosition(positionX, positionY - MOVEMENT_SPEED);
- break;
- case 2:
- this.setNewPosition(positionX - MOVEMENT_SPEED, positionY);
- break;
- case 3:
- this.setNewPosition(positionX + MOVEMENT_SPEED, positionY);
- break;
- case 4:
- this.setNewPosition(positionX, positionY + MOVEMENT_SPEED);
- break;
- }
- try {
- Thread.sleep(10);
- } catch (InterruptedException ex) {
- Logger.getLogger(Citizen.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- private void inCity() {
- positionX = -32;
- positionY = -32;
- try {
- Random randomNumber = new Random();
- int randomTravelTime = randomNumber.nextInt(9) + 1;
- Thread.sleep(6000 * randomTravelTime);
- } catch (InterruptedException ex) {
- Logger.getLogger(Citizen.class.getName()).log(Level.SEVERE, null, ex);
- }
- for (CityExit cityExit : Simulation.citiesExitList) {
- if (this.cityName == cityExit.getCityName()) {
- this.inCity = false;
- for (City city : Simulation.citiesList) {
- if (this.cityName == city.getName()) {
- city.setResidentsNumber(city.getResidentsNumber() - 1);
- System.out.println("#" + city.getName() + " POPULATION DECREASE: " + city.getResidentsNumber());
- }
- }
- this.setNewPosition(cityExit.getPositionX(), cityExit.getPositionY());
- this.movingDir = cityExit.getMovingDir();
- if (this.cityName == this.hometown) {
- do {
- this.nextCity = randomCity();
- } while (this.cityName == this.nextCity);
- System.out.println("BOHATER: " + surname + " WYBIERA SIĘ NA WYCIECZKĘ DO: " + nextCity);
- } else {
- goHometown();
- System.out.println("BOHATER: " + surname + " IDZIE DO HOMETOWN!");
- }
- break;
- }
- }
- }
- public void changeHometown() {
- this.hometown = randomCity();
- }
- public static String randomCity() {
- Random randomNumber = new Random();
- int randomCityNumber = randomNumber.nextInt(9);
- int i = 0;
- String randomCityName = null;
- for (City city : Simulation.citiesList) {
- if (i == randomCityNumber) {
- if (!city.isCityDestroyed()) {
- randomCityName = city.getName();
- break;
- }
- }
- i++;
- }
- return randomCityName;
- }
- public void goHometown() {
- this.nextCity = this.hometown;
- }
- public void setModifyingList(boolean modifyingList) {
- this.modifyingList = modifyingList;
- }
- public void run() {
- while (true) {
- synchronized (this) {
- if (modifyingList) {
- } else if (inCity) {
- inCity();
- } else if (!halt) {
- move();
- }
- try {
- Thread.sleep(10);
- } catch (InterruptedException ex) {
- Logger.getLogger(Citizen.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement