Files
2026-10-05 11:27:21 +02:00

94 lines
3.5 KiB
C++

#include <iostream>
#include "Pelican.h"
#include "Pole.h"
#include "PoleVisitor.h"
#include "Screen.h"
void Pole::accept(PoleVisitor &pv) {
for (int i = RED_LIGHT; i <= RED_MAN; i++) {
lights[i]->accept(pv);
}
waitLight->accept(pv);
}
Pole::Pole(int x, int y, Pelican &pelican)
: waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), myPelican(pelican) {
using namespace Screen;
lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED);
lights[YELLOW_LIGHT] = new Light("*", x + 11, y + 5, YELLOW);
lights[GREEN_LIGHT] = new Light("*", x + 11, y + 7, GREEN);
lights[GREEN_MAN] = new Light("*|-<", x + 6, y + 7, GREEN);
write(" _____ ", x, ++y, GRAY);
write(" /___ /| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" | |/| ", x, ++y, GRAY);
write(" |____| | ", x, ++y, GRAY);
write(" | |/| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" |____|/ ", x, ++y, GRAY);
write(" _||__ ", x, ++y, GRAY);
write(" /___ /| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" |____|/ ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write("========||===========", x, ++y, GRAY);
write(" / || / ", x, ++y, GRAY);
write("_ /_________ /_ ", x, ++y, GRAY);
write("/ / ", x, ++y, GRAY);
// the only time we need to ask the Pelican its state is at start up
update(Hint());
}
Pole::~Pole() {
for (int i = 0; i < sizeof lights / sizeof(lights[0]); ++i)
delete lights[i];
delete waitLight;
}
void Pole::update(const Hint &h) {
if (typeid(h) == typeid(WaitHint)) {
const WaitHint *whPtr = dynamic_cast<const WaitHint *>(&h);
setWaitLight(whPtr->pressed);
} else if (typeid(h) == typeid(LightHint)) {
const LightHint &lh = dynamic_cast<const LightHint &>(h);
setTrafficLights(lh.state);
} else {
// get the state and update everything
setWaitLight(myPelican.isPressed());
setTrafficLights(myPelican.getState());
notify(Hint());
}
notify(h);
}
void Pole::setTrafficLights(Pelican::PelicanState state) {
static const Light::LightState LightStates[Pelican::YELLOW_STATE + 1][RED_MAN + 1] = {
// red yellow green green man red man
{Light::ON, Light::OFF, Light::OFF, Light::ON, Light::OFF}, // red
{Light::OFF, Light::FLASH, Light::OFF, Light::FLASH, Light::OFF}, // y flash
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g time
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g idle
{Light::OFF, Light::ON, Light::OFF, Light::OFF, Light::ON} // yellow
};
for (int i = RED_LIGHT; i <= RED_MAN; i++) {
lights[i]->set(LightStates[state][i]);
}
}
void Pole::setWaitLight(bool state) { waitLight->set(state ? Light::ON : Light::OFF); }