package com.lucasdnd.ags.system;
import java.util.Calendar;
/**
* Controls the time
* @author tulio
*
*/
public class TimeController {
private long time; // The time, in seconds, from 1990 to the current time
private long timeInDay; // The amount of time that passes inside one day
private int currentHour; // Current hour of day
private int currentHourCache; // Cache of the current day, to check the changes
private boolean hourChanged; // Indicates if the hour changed
private int currentDay; // Current day of month
private int currentDayCache; // Cache of the current hour, to check the changes
private boolean dayChanged; // Indicates if the day changed
private int currentMonth; // Current month of year
private int currentMonthCache; // Cache of the current month, to check the changes
private boolean monthChanged; // Indicates if the month changed
public TimeController() {
Calendar cal = Calendar.getInstance();
cal.set(1990, 0, 1, 9, 0, 0);
time = cal.getTimeInMillis();
timeInDay = 0;
// Time Changes
currentHourCache = 9;
currentDayCache = 1;
currentMonthCache = 0;
hourChanged = dayChanged = monthChanged = false;
}
/**
* Increase the simulation time
* @param delta
*/
public void passTime(int delta) {
// Each second in real life is 1 hour in game
// That means 10 seconds = 1 day (store is open 10 hours a day)
// That means 5 minutes = 30 days
// That means 1 hour = 1 year
// That means 20 hours = 20 years, which is the average gameplay time
long currentPass = (long)(delta * 60 * 60);
// Update the time
timeInDay += currentPass;
time += currentPass;
// Here we check if the time passed is larger than 10 hours. If so, we skip to the next day
if(timeInDay >= 10 * 60 * 60 * 1000) {
// Get the Overtime and subtract it later to prevent losing precision over time
long overtime = timeInDay - (10 * 60 * 60 * 1000);
timeInDay = 0;
time -= overtime;
time += 14 * 60 * 60 * 1000;
}
// Calendar to help checking for time changes
Calendar calCheck = Calendar.getInstance();
calCheck.setTimeInMillis(time);
// Check if the Month changed
currentMonth = calCheck.get(Calendar.MONTH);
if(currentMonthCache != currentMonth) {
currentMonthCache = currentMonth;
monthChanged = true;
} else {
monthChanged = false;
}
// Check if the Day changed
currentDay = calCheck.get(Calendar.DAY_OF_MONTH);
if(currentDayCache != currentDay) {
currentDayCache = currentDay;
dayChanged = true;
} else {
dayChanged = false;
}
// Check if the Hour changed
currentHour = calCheck.get(Calendar.HOUR_OF_DAY);
if(currentHourCache != currentHour) {
currentHourCache = currentHour;
hourChanged = true;
} else {
hourChanged = false;
}
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public long getTimeInDay() {
return timeInDay;
}
public void setTimeInDay(long timeInDay) {
this.timeInDay = timeInDay;
}
public int getCurrentDay() {
return currentDay;
}
public void setCurrentDay(int currentDay) {
this.currentDay = currentDay;
}
public int getCurrentHour() {
return currentHour;
}
public void setCurrentHour(int currentHour) {
this.currentHour = currentHour;
}
public boolean isHourChanged() {
return hourChanged;
}
public boolean isDayChanged() {
return dayChanged;
}
public boolean isMonthChanged() {
return monthChanged;
}
}