1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 03:12:36 +01:00

Implemented method to modify game time properly

This commit is contained in:
Timmy Sjöstedt 2016-08-10 17:22:47 +02:00
parent f1391aa7b6
commit 03d27a0e80
3 changed files with 79 additions and 0 deletions

View File

@ -678,6 +678,26 @@ int GameWorld::getMinute()
return state->basic.gameMinute;
}
void GameWorld::offsetGameTime(int minutes)
{
int gameMinute = state->basic.gameMinute;
int gameHour = state->basic.gameHour;
gameMinute += minutes;
bool backwards = gameMinute < 0;
gameHour += gameMinute / 60 - (backwards ? 1 : 0);
// Black mathgic? No. If the value is negative we want to wrap it around
// to the other side, e.g. minute -7 should be 53.
// The equivalent would be doing "while (gameMinute < 0) { gameMinute += 60 }"
gameMinute = (gameMinute % 60 + 60) % 60;
gameHour = (gameHour % 24 + 24) % 24;
state->basic.gameMinute = gameMinute;
state->basic.gameHour = gameHour;
}
glm::vec3 GameWorld::getGroundAtPosition(const glm::vec3 &pos) const
{
btVector3 rayFrom(pos.x, pos.y, 100.f);

View File

@ -165,6 +165,12 @@ public:
*/
int getMinute();
/**
* Modifies the game time and handles the circular nature of clock numbers
* Supports negative numbers
*/
void offsetGameTime(int minutes);
glm::vec3 getGroundAtPosition(const glm::vec3& pos) const;
float getGameTime() const;

View File

@ -16,6 +16,59 @@ BOOST_AUTO_TEST_CASE(test_gameobject_id)
BOOST_CHECK_NE( object1->getGameObjectID(), object2->getGameObjectID() );
}
BOOST_AUTO_TEST_CASE(test_offsetgametime)
{
GameWorld gw(&Global::get().log, &Global::get().work, Global::get().d);
gw.state = new GameState();
BOOST_CHECK_EQUAL(0, gw.getHour());
BOOST_CHECK_EQUAL(0, gw.getMinute());
gw.offsetGameTime(30);
BOOST_CHECK_EQUAL(0, gw.getHour());
BOOST_CHECK_EQUAL(30, gw.getMinute());
gw.offsetGameTime(30);
BOOST_CHECK_EQUAL(1, gw.getHour());
BOOST_CHECK_EQUAL(0, gw.getMinute());
gw.offsetGameTime(-30);
BOOST_CHECK_EQUAL(0, gw.getHour());
BOOST_CHECK_EQUAL(30, gw.getMinute());
gw.offsetGameTime(-60);
BOOST_CHECK_EQUAL(23, gw.getHour());
BOOST_CHECK_EQUAL(30, gw.getMinute());
gw.offsetGameTime(30);
BOOST_CHECK_EQUAL(0, gw.getHour());
BOOST_CHECK_EQUAL(0, gw.getMinute());
gw.offsetGameTime(24*60);
BOOST_CHECK_EQUAL(0, gw.getHour());
BOOST_CHECK_EQUAL(0, gw.getMinute());
gw.offsetGameTime(8*60 + 25);
BOOST_CHECK_EQUAL(8, gw.getHour());
BOOST_CHECK_EQUAL(25, gw.getMinute());
gw.offsetGameTime(-30);
BOOST_CHECK_EQUAL(7, gw.getHour());
BOOST_CHECK_EQUAL(55, gw.getMinute());
gw.offsetGameTime(-24*60);
BOOST_CHECK_EQUAL(7, gw.getHour());
BOOST_CHECK_EQUAL(55, gw.getMinute());
gw.offsetGameTime(0);
BOOST_CHECK_EQUAL(7, gw.getHour());
BOOST_CHECK_EQUAL(55, gw.getMinute());
gw.offsetGameTime(30*24*60 + 90);
BOOST_CHECK_EQUAL(9, gw.getHour());
BOOST_CHECK_EQUAL(25, gw.getMinute());
}
#endif
BOOST_AUTO_TEST_SUITE_END()