Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

CityDirectory

Matthew Pohlmann edited this page Jan 1, 2014 · 1 revision

####Description An eager-instantiated singleton used to keep track of all buildings and people in SimCity201. Also takes care of keeping track of the time and updating every building/person with the new time. Contains many functions to add and search for different structures and people.

public class CityDirectory implements ActionListener {
	private static final CityDirectory INSTANCE = new CityDirectory();
	
	private CityDirectory() {};
	
	public static CityDirectory getInstance() {
		return INSTANCE;
	}

	Timer cityTimer = new Timer(INITIALTIMEROUT, this);
	CityTime time = new CityTime();
	List<PersonAgent> people = Collections.synchronizedList(new ArrayList<PersonAgent>());
	List<Restaurant> restaurants = Collections.synchronizedList(new ArrayList<Restaurant>());
	List<Bank> banks = Collections.synchronizedList(new ArrayList<Bank>());
	List<MarketStructure> markets = Collections.synchronizedList(new ArrayList<MarketStructure>());
	List<Residence> residences = Collections.synchronizedList(new ArrayList<Residence>());
	List<ApartmentComplex> apartments = Collections.synchronizedList(new ArrayList<ApartmentComplex>());
	
        public void resetCity() {
            cityTimer.stop();
	    time = new CityTime();
		
	    // Clear People
	    for (PersonAgent p : people) {
		p.stopGoOn();
		p.stateChanged();
		p.stateChanged();
		p.stateChanged();
	    }
	    this.people.clear();
		
	    // Clear buildings
	    restaurants.clear();
	    banks.clear();
	    markets.clear();
	    residences.clear();
    	    apartments.clear();
		
	    startTime();
        }
        
	// SimCity201 Time Stuff
	public void startTime() {
		cityTimer.setRepeats(true);
		cityTimer.start();
	}
	
	public void setStartTime(CityTime newTime) {
		time = newTime;
	}
	
	public void setTimerOut(int newTimerOut) {
		cityTimer.setDelay(newTimerOut);
		cityTimer.setInitialDelay(newTimerOut);
		cityTimer.restart();
	}
	
	public CityTime getTime() {
		return time;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		time.increment(TIMESTEP);
		
		synchronized(people) {
			for (PersonAgent p : people) {
				p.msgUpdateTime(time);
			}
		}
		
		synchronized(restaurants) {
			for (Restaurant r : restaurants) {
				r.updateTime(time);
			}
		}
		
		synchronized(banks) {
			for (Bank b : banks) {
				b.updateTime(time);
			}
		}
		
		synchronized(markets) {
			for (MarketStructure m : markets) {
				m.updateTime(time);
			}
		}
		
		synchronized(residences) {
			for (Residence r : residences) {
				r.updateTime(time);
			}
		}
		
		synchronized(apartments) {
			for (ApartmentComplex r : apartments) {
				r.updateTime(time);
			}
		}
	}
}

Wiki Home | Design Home

Clone this wiki locally