NerfStatTrak/StateDefault.cpp

107 lines
2.2 KiB
C++
Raw Permalink Normal View History

#include "StateDefault.h"
2016-12-09 20:28:11 +00:00
#include "NerfStatTrakConfig.h"
#include "Globals.h"
#include "StateLoseTheGame.h"
#include "StateUserUnknown.h"
#include "StateReset.h"
bool DefaultState::ShowHits = true;
void DefaultState::setup()
{
introStart = currentTime;
2016-12-09 23:00:42 +00:00
lastAction = currentTime;
}
2016-12-09 23:00:42 +00:00
void DefaultState::loop()
{
if (buttonA->pressed() && buttonB->pressed())
{
// A + B
setCurrentState(new UserUnknownState());
return;
}
// A short (triggered on release)
if (buttonA->changed() && !buttonA->pressed() && buttonA->pressedMillis(currentTime) <= NerfButtonLongPressThreshold)
{
addHit();
if (!DefaultState::ShowHits)
setShowHits(true);
2016-12-09 23:00:42 +00:00
lastAction = currentTime;
}
// A long
if (buttonA->pressed() && buttonA->pressedMillis(currentTime) > NerfButtonLongPressThreshold)
{
setCurrentState(new ResetState());
}
// B short (triggered on release)
if (buttonB->changed() && !buttonB->pressed() && buttonB->pressedMillis(currentTime) <= NerfButtonLongPressThreshold)
{
setShowHits(!DefaultState::ShowHits);
2016-12-09 23:00:42 +00:00
lastAction = currentTime;
}
// B long
if (buttonB->pressed() && buttonB->pressedMillis(currentTime) > NerfButtonLongPressThreshold)
{
setCurrentState(new LoseTheGameState());
return;
}
2016-12-09 23:00:42 +00:00
// Shot
if (buttonArmed->changed() && !buttonArmed->pressed())
{
// The shot has already been counted in Globals.cpp
lastAction = currentTime;
}
if (showIntro)
{
if (currentTime - introStart >= NerfDefaultIntroTime)
showIntro = false;
if (DefaultState::ShowHits)
{
// Prevents warning: deprecated conversion from string constant to 'char*'
char text[] = "Hits";
display->writeTextLeft(text);
}
else
{
char text[] = "Shots";
display->writeTextLeft(text);
}
}
else
{
2016-12-09 23:00:42 +00:00
if (currentTime - lastAction >= NerfDefaultSleepTime)
{
sleep();
lastAction = currentTime;
}
if (DefaultState::ShowHits)
display->writeNumber(hits);
else
display->writeNumber(shots);
}
}
void DefaultState::setShowHits(bool value)
{
showIntro = true;
introStart = currentTime;
DefaultState::ShowHits = value;
}