buenzliai/src/states.cpp
2024-05-28 14:09:39 +02:00

280 lines
5.4 KiB
C++

#include "states.h"
#include <sequences.h>
#include "state.h"
#include "station.h"
#include "sounds.h"
Startup::Startup(): State(nullptr)
{
}
State* Startup::putDown(const Context context, Station* newStation)
{
const int newStationIndex = context.getStationIndex(newStation);
if (newStationIndex == 0)
{
return new WaitingForGameStart();
}
return this;
}
State* Startup::update(const Context context)
{
updateSeqence(context);
return this;
}
String Startup::updateDisplay()
{
return "Initializing:Put"
"on first station";
}
void Startup::activated(Context context)
{
}
//------------------------------------
WaitingForGameStart::WaitingForGameStart() : State(&ON_MAIN_STATION)
{
}
State* WaitingForGameStart::pickedUp(const Context context)
{
return new OnTheMove(context.stations + 1, &AFTER_MAIN_STATION); // first element of the array
}
State* WaitingForGameStart::update(const Context context)
{
// Serial.println("update wait");
updateSeqence(context);
// Serial.println("after");
return this;
}
String WaitingForGameStart::updateDisplay()
{
return "Waiting for Game"
"Start...";
}
void WaitingForGameStart::activated(const Context context)
{
context.dfPlayer->play(IDLE);
}
//------------------------------------
OnTheMove::OnTheMove(Station* targetStation, Sequence* sequence): State(sequence)
{
this->targetStation = targetStation;
}
State* OnTheMove::putDown(const Context context, Station* newStation)
{
if (newStation == this->targetStation)
{
if (targetStation == context.stations) // first station
{
return new End();
}
return new Hacking(this->targetStation, nullptr);
}
return new IncorrectStation(targetStation);
}
State* OnTheMove::update(const Context context)
{
updateSeqence(context);
return this;
}
String OnTheMove::updateDisplay()
{
return "On the Move... ";
}
void OnTheMove::activated(const Context context)
{
const int index = context.getStationIndex(this->targetStation);
if (index == 1)
{
context.dfPlayer->play(REMOVED_FROM_MAIN_STATION);
}
else
{
context.dfPlayer->play(REMOVED_FROM_SIDE_STATION);
}
}
//------------------------------------
IncorrectStation::IncorrectStation(Station* targetStation): State(nullptr) // todo remove nullptr
{
this->targetStation = targetStation;
}
State* IncorrectStation::pickedUp(const Context context)
{
return new OnTheMove(this->targetStation, nullptr); // todo fix nullptr
}
State* IncorrectStation::update(const Context context)
{
updateSeqence(context);
return this;
}
String IncorrectStation::updateDisplay()
{
return "Wrong Station! ";
}
void IncorrectStation::activated(Context context)
{
}
//------------------------------------
Hacking::Hacking(Station* currentStation, Sequence* sequence): State(sequence), timer(5000, false)
{
this->currentStation = currentStation;
this->timer.start();
}
State* Hacking::pickedUp(const Context context)
{
return new Complain(currentStation);
}
State* Hacking::update(const Context context)
{
updateSeqence(context);
Serial.println(String(context.delta));
const bool done = timer.update(context.delta);
if (done)
{
const int currentStationIndex = context.getStationIndex(currentStation);
const int nextStationIndex = (currentStationIndex + 1) % context.stationCount;
Station* nextStation = context.stations + nextStationIndex;
return new WaitingForPickup(currentStation, nextStation, nullptr);
}
return this;
}
String Hacking::updateDisplay()
{
return "Hacking B)";
}
void Hacking::activated(const Context context)
{
context.dfPlayer->play(OBJECT_PLACED_ON_MAIN_STATION);
}
//------------------------------------
Complain::Complain(Station* currentStation): State(nullptr)
{
this->currentStation = currentStation;
}
State* Complain::putDown(const Context context, Station* newStation)
{
if (this->currentStation == newStation) // was put back on correct station
{
return new Hacking(this->currentStation, nullptr);
}
return this; // was not put back on correct station (just keeps complaining)
}
State* Complain::update(Context context)
{
return this;
}
String Complain::updateDisplay()
{
return "Put me back!!";
}
void Complain::activated(Context context)
{
}
//------------------------------------
WaitingForPickup::WaitingForPickup(Station* currentStation, Station* targetStation, Sequence* sequence): State(sequence)
{
this->currentStation = currentStation;
this->targetStation = targetStation;
}
State* WaitingForPickup::pickedUp(Context context)
{
return new OnTheMove(targetStation, nullptr);
}
State* WaitingForPickup::update(Context context)
{
return this;
}
String WaitingForPickup::updateDisplay()
{
return "Pick me up ";
}
void WaitingForPickup::activated(Context context)
{
}
//------------------------------------
End::End():State(nullptr)
{
}
State* End::pickedUp(const Context context)
{
// todo what happens when we pick up here?...
return this;
}
State* End::update(const Context context)
{
// todo after some time return waiting for game start...
return this;
}
String End::updateDisplay()
{
return "END...";
}
void End::activated(const Context context)
{
context.dfPlayer->play(GAME_WON);
}