Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: src/ConfigState.cpp
- ===================================================================
- --- src/ConfigState.cpp (revision 596)
- +++ src/ConfigState.cpp (working copy)
- @@ -161,7 +161,7 @@
- {
- /* New value will be applied by PlayState on resume */
- GlobalConfig::getSingletonPtr()->setReal(setting, value.getFloat());
- - TimeManager::getSingleton().setDayScale(value.getFloat());
- + //TimeManager::getSingleton().setDayScale(value.getFloat());
- }
- else if (!setting.compare("gravity"))
- {
- Index: src/PlayState.cpp
- ===================================================================
- --- src/PlayState.cpp (revision 596)
- +++ src/PlayState.cpp (working copy)
- @@ -55,7 +55,7 @@
- //Read config settings
- cfg_fogThickness = config->getReal("fogThickness", 0.000022f);
- TimeManager::getSingleton().setTimeFactor(config->getReal("timeFactor", 1.0f));
- - TimeManager::getSingleton().setDayScale(config->getReal("dayScale", 0.8f));
- + //TimeManager::getSingleton().setDayScale(config->getReal("dayScale", 0.8f));
- timeToCheckUpdating = 3000; //How many ms that have to pass before we update the array of entities that are in range of the player
- lastCheckUpdatingTime = timeToCheckUpdating + 2;
- @@ -70,7 +70,7 @@
- this->m_WorldTerrain = 0; //In the begginning, there was nothing
- isOutdoor = true;
- - TimeManager::getSingletonPtr()->setTimeOfDay( (LENGTH_OF_MONTH * 10) + LENGTH_OF_DAY / 14.0 );
- + //TimeManager::getSingletonPtr()->setTimeOfDay( (LENGTH_OF_MONTH * 10) + LENGTH_OF_DAY / 14.0 );
- //Initialize noise generation
- ambientNoiseRandom = rand() % 300;
- @@ -564,12 +564,14 @@
- if(e.key == OIS::KC_F5)
- {
- - TimeManager::getSingleton().modDayScale(2);
- + float timeFactor = TimeManager::getSingleton().getTimeFactor();
- + TimeManager::getSingleton().setTimeFactor(timeFactor * 2);
- }
- if(e.key == OIS::KC_F6)
- {
- - TimeManager::getSingleton().modDayScale(-2);
- + float timeFactor = TimeManager::getSingleton().getTimeFactor();
- + TimeManager::getSingleton().setTimeFactor(timeFactor / 2);
- }
- if(e.key == OIS::KC_F9)
- @@ -629,6 +631,7 @@
- bool PlayState::frameStarted(const FrameEvent& evt)
- {
- + TimeManager::getSingleton().passTime(evt.timeSinceLastFrame * 1000);
- Real MoveFactor = 80.0 * evt.timeSinceLastFrame; //The time scaler to keep time constant
- timeScaler = MoveFactor * TimeManager::getSingleton().getTimeFactor();
- @@ -644,13 +647,13 @@
- mCamera->pitch(Degree(-mouse_state.Y.rel * mouse_speed_multiplier));
- }*/
- - float timeOfDay = TimeManager::getSingleton().update(MoveFactor);
- -
- if(isOutdoor)
- {
- if(m_sky != 0)
- {
- - m_sky->Update(m_Player->getPosition(),timeOfDay);
- + // HACK: the sun seems to be reversed
- + unsigned long t = TimeManager::getSingleton().getTime() / 1000;
- + m_sky->Update(m_Player->getPosition(), (t + 43200) % 86400);
- }
- //Play ambient outdoor sounds here
- @@ -659,7 +662,7 @@
- {
- curAmbientNoiseWait = 0;
- - int twentyfour_hour = TimeManager::getSingletonPtr()->get24FormatHour();
- + int twentyfour_hour = TimeManager::getSingletonPtr()->getHour();
- if( twentyfour_hour <= 6 || twentyfour_hour >= 18)
- {
- ambientNoiseRandom = rand() % 100;
- @@ -714,17 +717,11 @@
- }
- //Do time of day callbacks here
- - if (TimeManager::getSingleton().isEvening())
- - {
- + if(TimeManager::getSingleton().getHour() >= 18 ||
- + TimeManager::getSingleton().getHour() <= 6)
- this->startEvening();
- - }
- else
- - {
- - if (TimeManager::getSingleton().isMorning())
- - {
- - this->startMorning();
- - }
- - }
- + this->startMorning();
- // Update physics
- PhysicsManager::getSingletonPtr()->update();
- @@ -988,8 +985,7 @@
- sprintf(terrX, "%d", m_WorldTerrain->currentZoneX);
- sprintf(terrY, "%d", m_WorldTerrain->currentZoneY);
- - String theDate;
- - TimeManager::getSingleton().getDateStr(theDate);
- + String theDate = TimeManager::getSingleton().getDateString("%j%S of %F, %h:%i:%s %A\n");
- Location * thisLoc = GameWorld::getSingletonPtr()->getLocation(m_WorldTerrain->currentZoneX,m_WorldTerrain->currentZoneY);
- if(thisLoc != 0)
- @@ -1069,7 +1065,7 @@
- {
- // TODO: make music configurable, disable for now -- Archwyrm
- return;
- - int twentyfour_hour = TimeManager::getSingletonPtr()->get24FormatHour();
- + int twentyfour_hour = TimeManager::getSingletonPtr()->getHour();
- if(twentyfour_hour < 6 || twentyfour_hour >= 18) //If it's nighttime
- {
- Index: src/TimeManager.cpp
- ===================================================================
- --- src/TimeManager.cpp (revision 596)
- +++ src/TimeManager.cpp (working copy)
- @@ -3,159 +3,157 @@
- using namespace Ogre;
- -TimeManager TimeManager::m_instance;
- +template<> TimeManager* Singleton<TimeManager>::ms_Singleton = 0;
- -TimeManager::TimeManager() : m_timeOfDay(0), m_lastHour(0), m_timeFactor(1.0), m_dayScale(0.8)
- -{
- - m_isMorning = false;
- - m_isEvening = false;
- - m_dayPos = 0;
- +TimeManager::month TimeManager::m_monthInfo[12] = {
- + { "Morning Star", 30 },
- + { "Sun's Dawn", 30 },
- + { "First Seed", 30 },
- + { "Rain's Hand", 30 },
- + { "Second Seed", 30 },
- + { "Mid Year", 30 },
- + { "Sun's Height", 30 },
- + { "Last Seed", 30 },
- + { "Heartfire", 30 },
- + { "Frost Fall", 30 },
- + { "Sun's Dusk", 30 },
- + { "Evening Star", 30 }
- +};
- - //Setup months
- - gameMonths[0] = "Morning Star";
- - gameMonths[1] = "Sun's Dawn";
- - gameMonths[2] = "First Seed";
- - gameMonths[3] = "Rain's Hand";
- - gameMonths[4] = "Second Seed";
- - gameMonths[5] = "Mid Year";
- - gameMonths[6] = "Sun's Height";
- - gameMonths[7] = "Last Seed";
- - gameMonths[8] = "Heartfire";
- - gameMonths[9] = "Frost Fall";
- - gameMonths[10] = "Sun's Dusk";
- - gameMonths[11] = "Evening Star";
- -}
- -
- -
- -TimeManager::~TimeManager()
- +TimeManager::TimeManager()
- {
- + m_currentTime = 46800000; // default time of 1PM
- + m_currentSeconds = 0;
- + m_currentMinutes = 0;
- + m_currentHour = 0;
- + m_currentDay = 0;
- + m_currentMonth = 0;
- + m_currentYear = 0;
- + m_timeFactor = 1.0;
- }
- -
- -void TimeManager::setTimeFactor(float factor)
- +unsigned long TimeManager::passTime(const unsigned short milliseconds)
- {
- - if (factor >= 0.0)
- - {
- - m_timeFactor = factor;
- - ControllerManager::getSingleton().setTimeFactor(m_timeFactor);
- - //ParticleSystemManager::getSingleton().setTimeFactor(m_timeFactor);
- - }
- -}
- + unsigned long timePassed = milliseconds * m_timeFactor;
- + m_currentTime += timePassed;
- + unsigned long dayDuration = 86400000;
- -double TimeManager::update(Real moveFactor)
- -{
- - //86400 time units in a day
- - //7200 in an hour
- - //120 in a minute
- - //2 in a second
- -
- - m_timeOfDay += m_timeFactor * moveFactor * m_dayScale;
- - m_timeOfDay = (int)m_timeOfDay % LENGTH_OF_DAY;
- -
- - m_isMorning = false;
- - m_isEvening = false;
- - int twentyfour_hour = get24FormatHour();
- - if (twentyfour_hour <= 6 || twentyfour_hour >= 18) //If it's nighttime
- + if(m_currentTime >= dayDuration)
- {
- - if (m_lastHour >= 6 || m_lastHour <= 18)
- + m_currentTime -= dayDuration;
- + m_currentDay++;
- +
- + if(m_currentDay >= m_monthInfo[m_currentMonth].days)
- {
- - if (m_dayPos != 1)
- + m_currentDay -= m_monthInfo[m_currentMonth].days;
- + m_currentMonth++;
- +
- + short numMonths = sizeof(m_monthInfo) / sizeof(month);
- + if(m_currentMonth >= numMonths)
- {
- - m_isEvening = true;
- - m_dayPos = 1;
- + m_currentMonth -= numMonths;
- + m_currentYear++;
- }
- }
- }
- - else
- - {
- - if (m_lastHour <= 6 || m_lastHour >= 18)
- - {
- - if (m_dayPos != 2)
- - {
- - m_isMorning = true;
- - m_dayPos = 2;
- - }
- - }
- - }
- - m_lastHour = twentyfour_hour;
- - return m_timeOfDay;
- + unsigned long tmp = m_currentTime / 1000;
- + m_currentHour = tmp / 3600;
- + tmp %= 3600;
- + m_currentMinutes = tmp / 60;
- + tmp %= 60;
- + m_currentSeconds = tmp;
- +
- + return timePassed;
- }
- -
- -long TimeManager::getYear()
- +String TimeManager::getDateString(const String& format) const
- {
- - return (long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) / LENGTH_OF_YEAR;
- -};
- + String output = format;
- + size_t pos = output.find_first_of('%');
- -int TimeManager::getMonth()
- -{
- - return ((long)(m_timeOfDay + (LENGTH_OF_DAY / 2))% LENGTH_OF_YEAR) / LENGTH_OF_MONTH;
- -};
- + while(pos != String::npos && pos < output.length() - 1)
- + {
- + char c = output.at(pos + 1);
- + std::ostringstream ss;
- + ss << std::setfill('0');
- -int TimeManager::getDay()
- -{
- - return (((long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) % LENGTH_OF_MONTH) / LENGTH_OF_DAY) + 1;
- -};
- + switch(c)
- + {
- + // Day formatting
- + case 'D': // don't know the names of the days of the week
- + case 'l': // ^
- + case 'd':
- + ss << std::setw(2) << getDay() + 1;
- + break;
- + case 'j':
- + ss << getDay() + 1;
- + break;
- + case 'S':
- + switch(getDay() + 1)
- + {
- + case 1: ss << "st"; break;
- + case 2: ss << "nd"; break;
- + case 3: ss << "rd"; break;
- + default: ss << "th";
- + }
- + break;
- -int TimeManager::getHour()
- -{
- - int thisHour = ((long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) % LENGTH_OF_DAY / LENGTH_OF_HOUR);
- + // Month formatting
- + case 'M': // we have no short month names
- + case 'F':
- + ss << m_monthInfo[getMonth()].name;
- + break;
- + case 'm':
- + ss << std::setw(2) << getMonth() + 1;
- + break;
- + case 'n':
- + ss << getMonth() + 1;
- + break;
- - if(thisHour < 1)
- - {
- - thisHour = 12;
- - }
- - if(thisHour > 12)
- - {
- - thisHour -= 12;
- - }
- - return thisHour;
- -};
- + // Year formatting
- + case 'y': // 2-digit year but our years could be anything
- + case 'Y':
- + ss << getYear();
- + break;
- -int TimeManager::get24FormatHour()
- -{
- - int thisHour = ((long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) % LENGTH_OF_DAY / LENGTH_OF_HOUR);
- - return thisHour;
- -};
- + // Time formatting
- + case 'g':
- + ss << (getHour() % 12 == 0 ? 12 : getHour() % 12);
- + break;
- + case 'G':
- + ss << getHour();
- + break;
- + case 'h':
- + ss << std::setw(2)
- + << (getHour() % 12 == 0 ? 12 : getHour() % 12);
- + break;
- + case 'H':
- + ss << std::setw(2) << getHour();
- + break;
- + case 'i':
- + ss << std::setw(2) << getMinutes();
- + break;
- + case 's':
- + ss << std::setw(2) << getSeconds();
- + break;
- + case 'a':
- + ss << (getHour() < 12 ? "am" : "pm");
- + break;
- + case 'A':
- + ss << (getHour() < 12 ? "AM" : "PM");
- + break;
- -int TimeManager::getMinute()
- -{
- - int thisHour = ((long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) % LENGTH_OF_HOUR / LENGTH_OF_MINUTE);
- - return thisHour;
- -}
- + default: // add character as-is
- + ss << c;
- + }
- -String TimeManager::getAMorPM()
- -{
- - int thisHour = ((long)(m_timeOfDay + (LENGTH_OF_DAY / 2)) % LENGTH_OF_DAY / LENGTH_OF_HOUR);
- - if(thisHour < 12)
- - {
- - return String("AM");
- + output.erase(pos, 2);
- + output.insert(pos, ss.str());
- + pos = output.find_first_of('%');
- }
- - return String("PM");
- -}
- -void TimeManager::getDateStr(String& theDate)
- -{
- - char date[5];
- - char hour[5];
- - char minute[5];
- -
- - sprintf(date, "%d", TimeManager::getSingletonPtr()->getDay()); //%G for float, %d for int
- - sprintf(hour, "%d", TimeManager::getSingletonPtr()->getHour());
- - sprintf(minute, "%02d", TimeManager::getSingletonPtr()->getMinute());
- -
- - theDate += "Day ";
- - theDate += date;
- - theDate += " of ";
- - theDate += gameMonths[ getMonth() ];
- - theDate += ", ";
- - theDate += hour;
- - theDate += ":";
- - theDate += minute;
- - theDate += " ";
- - theDate += getAMorPM();
- - theDate += "\n";
- + return output;
- }
- Index: src/Main.cpp
- ===================================================================
- --- src/Main.cpp (revision 596)
- +++ src/Main.cpp (working copy)
- @@ -10,6 +10,8 @@
- #include "PlayState.h"
- #include "MainmenuState.h"
- +#include "TimeManager.h"
- +
- int checkWorkingDirectory();
- @@ -22,6 +24,7 @@
- GameWorld* gameWorld = new GameWorld();
- PhysicsManager* physics = new PhysicsManager();
- SoundManager* soundManager = new OpenALManager();
- + TimeManager* timeManager = new TimeManager();
- try
- {
- @@ -42,6 +45,7 @@
- return 1;
- }
- + delete timeManager;
- delete soundManager;
- delete physics;
- delete gameWorld;
- Index: src/TimeManager.h
- ===================================================================
- --- src/TimeManager.h (revision 596)
- +++ src/TimeManager.h (working copy)
- @@ -1,67 +1,97 @@
- #ifndef _TIME_MANAGER_H
- #define _TIME_MANAGER_H
- -#include <OgreString.h>
- -using namespace Ogre;
- +#include <OgreSingleton.h>
- -#define SUNRISE 64800
- -#define SUNSET 21600
- -#define LENGTH_OF_YEAR 31104000
- -#define LENGTH_OF_MONTH 2592000
- -#define LENGTH_OF_DAY 86400
- -#define LENGTH_OF_HOUR 3600
- -#define LENGTH_OF_MINUTE 60
- -#define LENGTH_OF_SECOND 1
- -
- -
- -/**
- - Time and Calendar functions
- -*/
- -class TimeManager
- +class TimeManager : public Ogre::Singleton<TimeManager>
- {
- public:
- - ~TimeManager();
- + TimeManager();
- + ~TimeManager() {}
- - static TimeManager& getSingleton() { return m_instance; };
- - static TimeManager* getSingletonPtr() { return &m_instance; }
- + static TimeManager& getSingleton()
- + {
- + assert(ms_Singleton);
- + return *ms_Singleton;
- + }
- + static TimeManager* getSingletonPtr() { return ms_Singleton; }
- - void setTimeOfDay(double val) { m_timeOfDay = val; }
- - double getTimeOfDay() { return m_timeOfDay; }
- - double update(Real moveFactor);
- + /**
- + * \brief Move forward in the day
- + * \param milliseconds Milliseconds to add to to the time of day
- + * \return The world time that actually passed (after multiplied
- + * by the time factor)
- + */
- + unsigned long passTime(unsigned short milliseconds);
- - void setTimeFactor(float factor);
- - float getTimeFactor() { return m_timeFactor; }
- - void setDayScale(float scale) { if (scale > 0) m_dayScale = scale; }
- - void modDayScale(float scale) { if (m_dayScale + scale > 0) m_dayScale += scale; }
- - float getDayScale() { return m_dayScale; }
- + /**
- + * \brief Set the time factor to multiply time passed by
- + * \param factor Time factor
- + */
- + void setTimeFactor(Ogre::Real factor) { m_timeFactor = factor; }
- + Ogre::Real getTimeFactor() const { return m_timeFactor; }
- - long getYear();
- - int getMonth();
- - int getDay();
- - int getHour();
- - int get24FormatHour();
- - int getMinute();
- - String getAMorPM();
- - void getDateStr(String& theDate);
- + /**
- + * \return Time of day in milliseconds
- + */
- + unsigned long getTime() const { return m_currentTime; }
- - bool isMorning() { return m_isMorning; }
- - bool isEvening() { return m_isEvening; }
- + unsigned short getSeconds() const { return m_currentSeconds; }
- + unsigned short getMinutes() const { return m_currentMinutes; }
- + unsigned short getHour() const { return m_currentHour; }
- + unsigned short getDay() const { return m_currentDay; }
- + unsigned short getMonth() const { return m_currentMonth; }
- + unsigned short getYear() const { return m_currentYear; }
- -protected:
- - TimeManager();
- - static TimeManager m_instance;
- + /**
- + * \brief Return the date and time as a formatted string
- + *
- + * This function creates a formatted string using embedded tags
- + * in the format string which are prefixed by '%'. It borrows its
- + * format characters from PHP's date() function, but only uses
- + * ones that are relavant to DungeonHack. Supported characters:
- + *
- + * d = day with leading zero
- + * j = day without leading zeros
- + * S = English ordinal suffix for day (st, nd, rd, th)
- + * F = full name of the month
- + * m = month with leading zero
- + * n = month without leading zero
- + * y = year
- + * g = hour, 12-hour format
- + * G = hour, 24-hour format
- + * h = hour, 12-hour format with leading zero
- + * H = hour, 24-hour format with leading zero
- + * i = minutes with leading zero
- + * s = seconds with leading zero
- + * a = am or pm
- + * A = AM or PM
- + *
- + * \param format Format string
- + * \return Formatted string
- + */
- + Ogre::String getDateString(const Ogre::String& format) const;
- - double m_timeOfDay;
- - String gameMonths[12];
- +private:
- + unsigned long m_currentTime;
- - int m_lastHour, m_dayPos;
- - bool m_isMorning;
- - bool m_isEvening;
- + // Keep track of these so we only have to calculate them once
- + unsigned short m_currentSeconds;
- + unsigned short m_currentMinutes;
- + unsigned short m_currentHour;
- + unsigned short m_currentDay;
- + unsigned short m_currentMonth;
- + unsigned short m_currentYear;
- - float m_timeFactor; //Factor to scale time by, speeds up and slows down the game
- - float m_dayScale;
- + Ogre::Real m_timeFactor;
- +
- + struct month
- + {
- + char name[16];
- + unsigned char days;
- + };
- + static month m_monthInfo[12];
- };
- -
- #endif // _TIME_MANAGER_H
Add Comment
Please, Sign In to add comment