NerfStatTrak/StateDefault.cpp
Mark van Renswoude 211740d51a Fixed #2 Persist counters
Added initialisation sketch and reset state
2016-12-10 12:01:43 +01:00

107 lines
2.2 KiB
C++

#include "StateDefault.h"
#include "NerfStatTrakConfig.h"
#include "Globals.h"
#include "StateLoseTheGame.h"
#include "StateUserUnknown.h"
#include "StateReset.h"
bool DefaultState::ShowHits = true;
void DefaultState::setup()
{
introStart = currentTime;
lastAction = currentTime;
}
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);
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);
lastAction = currentTime;
}
// B long
if (buttonB->pressed() && buttonB->pressedMillis(currentTime) > NerfButtonLongPressThreshold)
{
setCurrentState(new LoseTheGameState());
return;
}
// 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
{
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;
}