Implemented mission timer
parent
61e06149c6
commit
6d2fd18b41
|
@ -26,6 +26,8 @@
|
|||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <time.h>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
|
||||
// Returns a non-accented letter.
|
||||
|
@ -232,6 +234,17 @@ void TimeToAsciiClean(time_t time, char *buffer)
|
|||
when.tm_hour, when.tm_min);
|
||||
}
|
||||
|
||||
std::string TimeFormat(float time)
|
||||
{
|
||||
int minutes = floor(time/60);
|
||||
double time2 = fmod(time, 60);
|
||||
double seconds;
|
||||
double fraction = modf(time2, &seconds)*100;
|
||||
std::ostringstream sstream;
|
||||
sstream << std::setfill('0') << std::setw(2) << minutes << ":" << std::setfill('0') << std::setw(2) << floor(seconds) << "." << std::setfill('0') << std::setw(2) << floor(fraction);
|
||||
return sstream.str();
|
||||
}
|
||||
|
||||
|
||||
// Adds an extension to file, if doesn't already one.
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
|
||||
#include <time.h>
|
||||
#include <string>
|
||||
|
||||
|
||||
// TODO: rewrite/refactor or remove
|
||||
|
@ -32,6 +33,7 @@ extern char GetToLower(char letter);
|
|||
|
||||
extern void TimeToAscii(time_t time, char *buffer);
|
||||
extern void TimeToAsciiClean(time_t time, char *buffer);
|
||||
extern std::string TimeFormat(float time);
|
||||
|
||||
extern void AddExt(char* filename, const char* ext);
|
||||
|
||||
|
|
|
@ -520,6 +520,11 @@ void CEngine::SetStatisticPos(Math::Vector pos)
|
|||
m_statisticPos = pos;
|
||||
}
|
||||
|
||||
void CEngine::SetTimerDisplay(const std::string& text)
|
||||
{
|
||||
m_timerText = text;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************
|
||||
|
@ -3510,6 +3515,7 @@ void CEngine::DrawInterface()
|
|||
DrawMouse();
|
||||
DrawHighlight();
|
||||
DrawStats();
|
||||
DrawTimer();
|
||||
}
|
||||
|
||||
void CEngine::UpdateGroundSpotTextures()
|
||||
|
@ -4479,6 +4485,14 @@ void CEngine::DrawStats()
|
|||
m_text->DrawText(str.str(), FONT_COLOBOT, 12.0f, pos, 1.0f, TEXT_ALIGN_LEFT, 0, Color(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
}
|
||||
|
||||
void CEngine::DrawTimer()
|
||||
{
|
||||
SetState(ENG_RSTATE_TEXT);
|
||||
|
||||
Math::Point pos(0.98f, 0.98f-m_text->GetAscent(FONT_COLOBOT, 15.0f));
|
||||
m_text->DrawText(m_timerText, FONT_COLOBOT, 15.0f, pos, 1.0f, TEXT_ALIGN_RIGHT, 0, Color(1.0f, 1.0f, 1.0f, 1.0f));
|
||||
}
|
||||
|
||||
|
||||
} // namespace Gfx
|
||||
|
||||
|
|
|
@ -780,6 +780,9 @@ public:
|
|||
|
||||
//! Sets the coordinates to display in stats window
|
||||
void SetStatisticPos(Math::Vector pos);
|
||||
|
||||
//! Sets text to display as mission timer
|
||||
void SetTimerDisplay(const std::string& text);
|
||||
|
||||
|
||||
/* *************** Object management *************** */
|
||||
|
@ -1236,6 +1239,8 @@ protected:
|
|||
void DrawMouseSprite(Math::Point pos, Math::Point dim, int icon);
|
||||
//! Draw statistic texts
|
||||
void DrawStats();
|
||||
//! Draw mission timer
|
||||
void DrawTimer();
|
||||
|
||||
//! Creates a new tier 2 object (texture)
|
||||
EngineBaseObjTexTier& AddLevel2(EngineBaseObject& p1, const std::string& tex1Name, const std::string& tex2Name);
|
||||
|
@ -1442,6 +1447,8 @@ protected:
|
|||
|
||||
bool m_debugLights;
|
||||
bool m_debugDumpLights;
|
||||
|
||||
std::string m_timerText;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -2546,6 +2546,8 @@ void CBrain::RunProgram(int rank)
|
|||
BlinkScript(true); // blink
|
||||
m_object->CreateSelectParticle();
|
||||
m_main->UpdateShortcuts();
|
||||
if(m_object->GetTrainer())
|
||||
m_main->StartMissionTimer();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -645,6 +645,10 @@ CRobotMain::CRobotMain(CApplication* app, bool loadProfile)
|
|||
m_time = 0.0f;
|
||||
m_gameTime = 0.0f;
|
||||
m_checkEndTime = 0.0f;
|
||||
|
||||
m_missionTimerEnabled = false;
|
||||
m_missionTimerStarted = false;
|
||||
m_missionTimer = 0.0f;
|
||||
|
||||
m_phase = PHASE_NAME;
|
||||
m_cameraRank = -1;
|
||||
|
@ -1087,6 +1091,9 @@ void CRobotMain::ResetKeyStates()
|
|||
//! Changes phase
|
||||
void CRobotMain::ChangePhase(Phase phase)
|
||||
{
|
||||
m_missionTimerEnabled = m_missionTimerStarted = false;
|
||||
m_missionTimer = 0.0f;
|
||||
|
||||
if (m_phase == PHASE_SIMUL) // ends a simulation?
|
||||
{
|
||||
SaveAllScript();
|
||||
|
@ -1417,10 +1424,7 @@ bool CRobotMain::ProcessEvent(Event &event)
|
|||
if (m_displayInfo != nullptr) // current edition?
|
||||
m_displayInfo->EventProcess(event);
|
||||
|
||||
if (m_phase == PHASE_SIMUL)
|
||||
{
|
||||
UpdateInfoText();
|
||||
}
|
||||
UpdateInfoText();
|
||||
|
||||
return EventFrame(event);
|
||||
}
|
||||
|
@ -1754,12 +1758,14 @@ bool CRobotMain::ProcessEvent(Event &event)
|
|||
break;
|
||||
|
||||
case EVENT_WIN:
|
||||
m_missionTimerEnabled = m_missionTimerStarted = false;
|
||||
ChangePhase(PHASE_WIN);
|
||||
if(m_exitAfterMission)
|
||||
m_eventQueue->AddEvent(Event(EVENT_QUIT));
|
||||
break;
|
||||
|
||||
case EVENT_LOST:
|
||||
m_missionTimerEnabled = m_missionTimerStarted = false;
|
||||
ChangePhase(PHASE_LOST);
|
||||
if(m_exitAfterMission)
|
||||
m_eventQueue->AddEvent(Event(EVENT_QUIT));
|
||||
|
@ -3365,12 +3371,16 @@ void CRobotMain::AbortMovie()
|
|||
//! Updates the text information
|
||||
void CRobotMain::UpdateInfoText()
|
||||
{
|
||||
CObject* obj = GetSelect();
|
||||
if (obj != nullptr)
|
||||
if (m_phase == PHASE_SIMUL)
|
||||
{
|
||||
Math::Vector pos = obj->GetPosition(0);
|
||||
m_engine->SetStatisticPos(pos);
|
||||
CObject* obj = GetSelect();
|
||||
if (obj != nullptr)
|
||||
{
|
||||
Math::Vector pos = obj->GetPosition(0);
|
||||
m_engine->SetStatisticPos(pos);
|
||||
}
|
||||
}
|
||||
m_engine->SetTimerDisplay(m_missionTimerEnabled && m_missionTimerStarted ? TimeFormat(m_missionTimer) : "");
|
||||
}
|
||||
|
||||
|
||||
|
@ -3394,6 +3404,9 @@ bool CRobotMain::EventFrame(const Event &event)
|
|||
m_displayText->DisplayError(INFO_BEGINSATCOM, Math::Vector(0.0f,0.0f,0.0f));
|
||||
m_beginSatCom = true; // message appears
|
||||
}
|
||||
|
||||
if(!m_movieLock && m_pause->GetPause() == PAUSE_NONE && m_missionTimerStarted)
|
||||
m_missionTimer += event.rTime;
|
||||
|
||||
m_water->EventProcess(event);
|
||||
m_cloud->EventProcess(event);
|
||||
|
@ -3760,6 +3773,11 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
|
|||
m_missionResult = ERR_MISSION_NOTERM;
|
||||
}
|
||||
|
||||
//NOTE: Reset timer always, even when only resetting object positions
|
||||
m_missionTimerEnabled = false;
|
||||
m_missionTimerStarted = false;
|
||||
m_missionTimer = 0.0f;
|
||||
|
||||
CLevelParser* level = new CLevelParser(base, rank/100, rank%100);
|
||||
level->Load();
|
||||
|
||||
|
@ -3853,6 +3871,15 @@ void CRobotMain::CreateScene(bool soluce, bool fixScene, bool resetObject)
|
|||
continue;
|
||||
}
|
||||
|
||||
if (line->GetCommand() == "MissionTimer")
|
||||
{
|
||||
m_missionTimerEnabled = line->GetParam("enabled")->AsBool();
|
||||
if(!line->GetParam("program")->AsBool(false)) {
|
||||
m_missionTimerStarted = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line->GetCommand() == "CacheAudio" && !resetObject && m_version >= 2)
|
||||
{
|
||||
m_sound->CacheMusic(std::string("../")+line->GetParam("filename")->AsPath("music"));
|
||||
|
@ -6342,12 +6369,14 @@ Error CRobotMain::CheckEndMission(bool frame)
|
|||
if (m_missionResult == INFO_LOST) //mission lost?
|
||||
{
|
||||
m_displayText->DisplayError(INFO_LOST, Math::Vector(0.0f,0.0f,0.0f));
|
||||
m_missionTimerEnabled = m_missionTimerStarted = false;
|
||||
m_winDelay = 0.0f;
|
||||
if (m_lostDelay == 0) m_lostDelay = m_endTakeLostDelay;
|
||||
m_displayText->SetEnable(false);
|
||||
}
|
||||
if (m_missionResult == INFO_LOSTq) //mission lost?
|
||||
{
|
||||
m_missionTimerEnabled = m_missionTimerStarted = false;
|
||||
m_winDelay = 0.0f;
|
||||
if (m_lostDelay == 0) m_lostDelay = 0.1f;
|
||||
m_displayText->SetEnable(false);
|
||||
|
@ -6355,6 +6384,11 @@ Error CRobotMain::CheckEndMission(bool frame)
|
|||
if (frame && m_base) return ERR_MISSION_NOTERM;
|
||||
if (m_missionResult == ERR_OK) { //mission win?
|
||||
m_displayText->DisplayError(INFO_WIN, Math::Vector(0.0f,0.0f,0.0f));
|
||||
if(m_missionTimerEnabled && m_missionTimerStarted) {
|
||||
CLogger::GetInstancePointer()->Info("Mission time: %s\n", TimeFormat(m_missionTimer).c_str());
|
||||
m_displayText->DisplayText(("Time: "+TimeFormat(m_missionTimer)).c_str(), Math::Vector(0.0f,0.0f,0.0f));
|
||||
}
|
||||
m_missionTimerEnabled = m_missionTimerStarted = false;
|
||||
if (m_winDelay == 0) m_winDelay = m_endTakeWinDelay;
|
||||
m_lostDelay = 0.0f;
|
||||
m_displayText->SetEnable(false);
|
||||
|
@ -6431,6 +6465,7 @@ Error CRobotMain::CheckEndMission(bool frame)
|
|||
m_lostDelay = 0.1f; // lost immediately
|
||||
m_winDelay = 0.0f;
|
||||
}
|
||||
m_missionTimerEnabled = m_missionTimerStarted = false;
|
||||
m_displayText->SetEnable(false);
|
||||
return INFO_LOSTq;
|
||||
}
|
||||
|
@ -6442,6 +6477,7 @@ Error CRobotMain::CheckEndMission(bool frame)
|
|||
m_lostDelay = m_endTakeLostDelay; // lost in 6 seconds
|
||||
m_winDelay = 0.0f;
|
||||
}
|
||||
m_missionTimerEnabled = m_missionTimerStarted = false;
|
||||
m_displayText->SetEnable(false);
|
||||
return INFO_LOST;
|
||||
}
|
||||
|
@ -6460,6 +6496,7 @@ Error CRobotMain::CheckEndMission(bool frame)
|
|||
m_winDelay = m_endTakeWinDelay; // wins in x seconds
|
||||
m_lostDelay = 0.0f;
|
||||
}
|
||||
m_missionTimerEnabled = m_missionTimerStarted = false;
|
||||
m_displayText->SetEnable(false);
|
||||
return ERR_OK; // mission ended
|
||||
}
|
||||
|
@ -6478,6 +6515,7 @@ Error CRobotMain::CheckEndMission(bool frame)
|
|||
{
|
||||
m_winDelay = 1.0f; // wins in one second
|
||||
m_lostDelay = 0.0f;
|
||||
m_missionTimerEnabled = m_missionTimerStarted = false;
|
||||
m_displayText->SetEnable(false);
|
||||
return ERR_OK; // mission ended
|
||||
}
|
||||
|
@ -6487,6 +6525,11 @@ Error CRobotMain::CheckEndMission(bool frame)
|
|||
if (m_winDelay == 0.0f)
|
||||
{
|
||||
m_displayText->DisplayError(INFO_WIN, Math::Vector(0.0f,0.0f,0.0f));
|
||||
if(m_missionTimerEnabled && m_missionTimerStarted) {
|
||||
CLogger::GetInstancePointer()->Info("Mission time: %s\n", TimeFormat(m_missionTimer).c_str());
|
||||
m_displayText->DisplayText(("Time: "+TimeFormat(m_missionTimer)).c_str(), Math::Vector(0.0f,0.0f,0.0f));
|
||||
}
|
||||
m_missionTimerEnabled = m_missionTimerStarted = false;
|
||||
m_winDelay = m_endTakeWinDelay; // wins in two seconds
|
||||
m_lostDelay = 0.0f;
|
||||
}
|
||||
|
@ -6960,3 +7003,11 @@ std::string& CRobotMain::GetUserLevelName(int id)
|
|||
{
|
||||
return m_dialog->GetUserLevelName(id);
|
||||
}
|
||||
|
||||
void CRobotMain::StartMissionTimer()
|
||||
{
|
||||
if(m_missionTimerEnabled && !m_missionTimerStarted) {
|
||||
CLogger::GetInstancePointer()->Info("Starting mission timer...\n");
|
||||
m_missionTimerStarted = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -397,6 +397,8 @@ public:
|
|||
void DisplayError(Error err, Math::Vector goal, float height=15.0f, float dist=60.0f, float time=10.0f);
|
||||
|
||||
std::string& GetUserLevelName(int id);
|
||||
|
||||
void StartMissionTimer();
|
||||
|
||||
protected:
|
||||
bool EventFrame(const Event &event);
|
||||
|
@ -589,5 +591,9 @@ protected:
|
|||
float m_colorShiftWater;
|
||||
|
||||
std::string m_oldLocale;
|
||||
|
||||
bool m_missionTimerEnabled;
|
||||
bool m_missionTimerStarted;
|
||||
float m_missionTimer;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue