Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/MatthijsMud/Themaopd…
Browse files Browse the repository at this point in the history
…racht-Devices into development
  • Loading branch information
CasperTI committed Mar 22, 2018
2 parents 7168c9c + 0d4b952 commit c4be83c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 18 deletions.
61 changes: 47 additions & 14 deletions src/GameController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ GameController::GameController(GameParameterController & parameters, SendIRContr
rtos::task<>{ "GameController" },
parameters{parameters},
sender{sender},
countDownTime{ this, "countDownTime" },
gameTime{ this, "gameEnd" },
cooldownTime{ this, "cooldown" },
invulnerabilityTime{ this, "invulerability" },
countdownTime{ this, "countdownTime" },
// Make it so the game can be started out of the box. No particular reason.
gameTimeSetting{ 5 },
gameTime{ this, "gameTime" },
cooldownTime{ this, "cooldownTime" },
invulnerabilityTime{ this, "invulerabilityTime" },
messages{ this, "messages" },
keyPresses{ this, "keyPresses" }
{
Expand Down Expand Up @@ -40,10 +42,28 @@ void GameController::waitForStartCommand()
if (event == messages)
{
Message message = messages.read();
// Start command received.
if(message.isStartMessage())
{
break;
} else {
// The game master doesn't participate.
if (parameters.GetPlayer() != GAME_MASTER)
{
break;
}
else
{
hwlib::cout << "[" __FILE__ "]: Game master doesn't participate.\n";
}
}
// While waiting, the game master can configure the time.
// The game master is identified with playernumber 0.
else if (message.getPlayer() == GAME_MASTER)
{
hwlib::cout << "[" __FILE__ "]: " << "\n";
gameTimeSetting = message.getTime();
}
else
{
hwlib::cout << "[" __FILE__ "]: Unexpected message: " << message << "\n";
}
}
Expand All @@ -55,8 +75,8 @@ void GameController::waitForCountDownEnd()
{
hwlib::cout << "[" __FILE__ "]: Waiting for count down.\n";
// TODO: Make the time for countdown variable.
countDownTime.set(3 * 1'000 * 1'000);
while(wait() != countDownTime)
countdownTime.set(3 * 1'000 * 1'000);
while(wait() != countdownTime)
{
continue;
}
Expand All @@ -70,8 +90,9 @@ void GameController::startGame()
keyPresses.clear();
messages.clear();

// TODO: Make the time the game goes on for variable.
gameTime.set(5 * 60 * 1'000 * 1'000);
hwlib::cout << "[" __FILE__ "]: Playing " << gameTimeSetting << " minutes\n";
// gameDurationSetting is given in minutes, but a timer expects uS.
gameTime.set(gameTimeSetting * 60 * 1'000 * 1'000);

// Resetting th state so the player starts in a save way.
canShoot = true;
Expand Down Expand Up @@ -134,12 +155,24 @@ void GameController::shoot()

void GameController::handleHit(Message message)
{
uint16_t playerID = message.getPlayer();
if (isVulnerable)
{
hwlib::cout << "[" __FILE__ "]: Got hit " << message << ".\n";
isVulnerable = false;
// TODO:
invulnerabilityTime.set(1);
if (playerID == GAME_MASTER)
{
hwlib::cout << "[" __FILE__ "]: Ignore game master.\n";
}
else if (playerID == parameters.GetPlayer())
{
hwlib::cout << "[" __FILE__ "]: Ignore self.\n";
}
else
{
hwlib::cout << "[" __FILE__ "]: Got hit " << message << ".\n";
isVulnerable = false;
// TODO:
invulnerabilityTime.set(1);
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/GameController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class SendIRController;

class GameController : public rtos::task<>, public KeypadListener, public IRListener
{
private:
//! Player id for the person who sends out the start command (among others).
static constexpr uint16_t GAME_MASTER{0};

private:
GameParameterController & parameters;

Expand All @@ -21,7 +25,11 @@ class GameController : public rtos::task<>, public KeypadListener, public IRList
private:
//! Time to wait after receiving start signal to actually start.
//! Used to give the players time to separate.
rtos::timer countDownTime;
rtos::timer countdownTime;

private:
//! Setting for how long each game will take.
unsigned long int gameTimeSetting;

private:
//! Counts down once the game starts and will notify the game has ended.
Expand Down
19 changes: 16 additions & 3 deletions src/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ void Message::setData(uint16_t data){
internalMessage |= data << 5;
calculateChecksum();
}

uint16_t Message::getTime() const
{
return getData();
}

void Message::setTime(uint16_t time){
setData(time);
}
Expand All @@ -64,11 +70,18 @@ bool Message::isStartMessage() const
}

void Message::calculateChecksum(){

uint16_t checksum{0};
// Usage of (condition) ? 1 : 0 to make sure the bit is 1;
// true is defined as being anything but 0.
for (unsigned int i=0; i<5; ++i)
{
// Besides the start bit
checksum |= ((bit(i+1) != bit(i+6)) ? 1 : 0) << (4-i);
}
// Reset the checksum bits (X-XXXXX-XXXXX-00000).
internalMessage &= ~(31 << 0);
for(int i = 0; i < 5; i++){
internalMessage |= (( internalMessage >> (14-i % 16)) & 1) ^ ((internalMessage >> (9-i % 16)) & 1 ) << (4-i);
}
internalMessage |= checksum;
}

bool Message::bit(uint8_t position) const
Expand Down

0 comments on commit c4be83c

Please sign in to comment.