Skip to content

Commit

Permalink
added countdown timer tick event
Browse files Browse the repository at this point in the history
  • Loading branch information
t3kt committed Dec 26, 2014
1 parent 2f61fd3 commit 73516ff
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/config/BleepoutConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ _modifierFadeTime(0.2f),
_domeRadius(150.0f),
_domeMargin(20.0f),
_name(name),
_startDelay(0) { }
_startDelay(0),
countdownTimerPeriod(10) { }

void RoundConfig::loadJsonFile(std::string path) {
Json::Value obj;
Expand Down
2 changes: 2 additions & 0 deletions src/config/BleepoutConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ class RoundConfig {
const GameRules& rules() const { return _rules; }
GameRules& rules() { return _rules; }

float countdownTimerPeriod;

Json::Value toJsonVal() const;
private:
std::string _name;
Expand Down
8 changes: 8 additions & 0 deletions src/core/AudioManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ void AudioManager::attachTo(RoundController &roundController) {
this, &AudioManager::onPlayerLivesChanged);
ofAddListener(roundController.logicController().playerLostEvent,
this, &AudioManager::onPlayerLost);
ofAddListener(roundController.logicController().countdownTickEvent,
this, &AudioManager::onCountdownTick);
}

void AudioManager::detachFrom(RoundController &roundController) {
Expand All @@ -96,6 +98,8 @@ void AudioManager::detachFrom(RoundController &roundController) {
this, &AudioManager::onPlayerLivesChanged);
ofRemoveListener(roundController.logicController().playerLostEvent,
this, &AudioManager::onPlayerLost);
ofRemoveListener(roundController.logicController().countdownTickEvent,
this, &AudioManager::onCountdownTick);
}

void AudioManager::onRoundStarted(RoundStateEventArgs &e) {
Expand Down Expand Up @@ -133,3 +137,7 @@ void AudioManager::onPlayerLivesChanged(PlayerStateEventArgs &e) {
void AudioManager::onPlayerLost(PlayerStateEventArgs &e) {
_playerLostSound.play();
}

void AudioManager::onCountdownTick(TimerEventArgs &e) {
_countdownTimerTickSound.play();
}
2 changes: 1 addition & 1 deletion src/core/AudioManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AudioManager {
void onBallDestroyed(BallStateEventArgs& e);
void onPlayerLivesChanged(PlayerStateEventArgs& e);
void onPlayerLost(PlayerStateEventArgs& e);
// void onCountdownTimerT
void onCountdownTick(TimerEventArgs& e);

const BleepoutParameters& _appParams;
ofSoundPlayer _roundStartedSound;
Expand Down
2 changes: 1 addition & 1 deletion src/core/BleepoutApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void BleepoutApp::endRound() {
_playerManager->setIsInRound(false);
_audioManager->detachFrom(*_roundController);
_roundController.reset();
_endingRound = true;
_endingRound = false;
notifyRoundEnded();
}

Expand Down
7 changes: 7 additions & 0 deletions src/core/GameEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ void ModifierRemovedEventArgs::output(std::ostream &os) const {
os << ")";
}

void TimerEventArgs::output(std::ostream& os) const {
os << "(";
os << "current:" << _currentTime << ", ";
os << "remaining:" << _remainingTime;
os << ")";
}

void EventSource::logEvent(const char *name,
const Outputable &event) const {
if (loggingEnabled()) {
Expand Down
12 changes: 12 additions & 0 deletions src/core/GameEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ class PlayerYawPitchRollEventArgs {
float _roll;
};

class TimerEventArgs : public Outputable {
public:
TimerEventArgs(float currentTime, float remainingTime)
: _currentTime(currentTime), _remainingTime(remainingTime) { }
float currentTime() const { return _currentTime; }
float remainingTime() const { return _remainingTime; }
void output(std::ostream& os) const override;
private:
float _currentTime;
float _remainingTime;
};

class EventSource {
public:
EventSource() : _logLevel(OF_LOG_SILENT) {}
Expand Down
18 changes: 14 additions & 4 deletions src/core/LogicController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LogicController::LogicController(RoundState& state,
RoundConfig& config,
BleepoutParameters& appParams)
:_state(state), _config(config), _appParams(appParams)
, _lastSpecifiedTimeLimitOffset(-1)
, _lastSpecifiedTimeLimitOffset(-1), _countdownTickPulser(1)
, EventSource() { }

void LogicController::setup() {
Expand All @@ -40,9 +40,14 @@ void LogicController::update() {
}
_lastSpecifiedTimeLimitOffset = limit;
}
if (_state.endTime > 0 && _state.remainingTime() <= 0) {
notifyTryEndRound();
return;
if (_state.endTime > 0) {
if (_state.remainingTime() <= _config.countdownTimerPeriod &&
_countdownTickPulser.update(_state.time))
notifyCountdownTick();
if (_state.remainingTime() <= 0) {
notifyTryEndRound();
return;
}
}
for (auto& obj : _state.paddles()) {
if (obj && obj->alive()) {
Expand Down Expand Up @@ -256,3 +261,8 @@ void LogicController::notifyModifierRemoved(RoundState& state, const ModifierSpe
ofNotifyEvent(modifierRemovedEvent, e);
logEvent("ModifierRemoved", e);
}
void LogicController::notifyCountdownTick() {
TimerEventArgs e(_state.time, _state.remainingTime());
ofNotifyEvent(countdownTickEvent, e);
logEvent("CountdownTick", e);
}
4 changes: 4 additions & 0 deletions src/core/LogicController.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "GameEvents.h"
#include "BleepoutConfig.h"
#include "BleepoutParameters.h"
#include "Timing.h"

class SpaceController;

Expand All @@ -34,6 +35,7 @@ class LogicController : public EventSource {
ofEvent<ModifierEventArgs> modifierDestroyedEvent;
ofEvent<ModifierEventArgs> modifierAppliedEvent;
ofEvent<ModifierRemovedEventArgs> modifierRemovedEvent;
ofEvent<TimerEventArgs> countdownTickEvent;

void setup();
void update();
Expand All @@ -57,6 +59,7 @@ class LogicController : public EventSource {
void notifyModifierDestroyed(RoundState& state, Modifier* modifier);
void notifyModifierApplied(RoundState& state, Modifier* modifier, GameObject* target);
void notifyModifierRemoved(RoundState& state, const ModifierSpec &modifierSpec, GameObject* target);
void notifyCountdownTick();

void onCollision(CollisionEventArgs& e);

Expand All @@ -73,6 +76,7 @@ class LogicController : public EventSource {
RoundConfig& _config;
BleepoutParameters& _appParams;
float _lastSpecifiedTimeLimitOffset;
Pulser _countdownTickPulser;
};

#endif /* defined(__bleepout__LogicController__) */
1 change: 1 addition & 0 deletions src/core/RoundManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class RoundController : public EventSource
bool notifyTryEndRound(EndRoundEventArgs &e);

void onModifierAppeared(ModifierEventArgs& e);
void onCountdownTick(TimerEventArgs& e);

bool _paused;
float _startTime;
Expand Down
13 changes: 12 additions & 1 deletion src/rendering/Animations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MessageAnimation : public AnimationObject {
virtual void draw(const RoundConfig& config) override;
virtual void output(std::ostream& os) const override;
private:
const MessageSpec& _message;
MessageSpec _message;
ofTrueTypeFont& _font;
};

Expand Down Expand Up @@ -191,13 +191,22 @@ void AnimationManager::onModifierRemoved(ModifierRemovedEventArgs &e) {
addAnimation(anim);
}

void AnimationManager::onCountdownTick(TimerEventArgs &e) {
int time = static_cast<int>(e.remainingTime());
addMessage(MessageSpec("Time: " + ofToString(time), ofColor(255, 0, 0))
.setSize(10)
.setTiming(0, 0.9));
}

void AnimationManager::attachTo(LogicController &roundEvents) {
ofAddListener(roundEvents.brickDestroyedEvent, this,
&AnimationManager::onBrickDestroyed);
ofAddListener(roundEvents.modifierAppliedEvent, this,
&AnimationManager::onModifierApplied);
ofAddListener(roundEvents.modifierRemovedEvent, this,
&AnimationManager::onModifierRemoved);
ofAddListener(roundEvents.countdownTickEvent, this,
&AnimationManager::onCountdownTick);
}

void AnimationManager::detachFrom(LogicController &roundEvents) {
Expand All @@ -207,4 +216,6 @@ void AnimationManager::detachFrom(LogicController &roundEvents) {
&AnimationManager::onModifierApplied);
ofRemoveListener(roundEvents.modifierRemovedEvent, this,
&AnimationManager::onModifierRemoved);
ofRemoveListener(roundEvents.countdownTickEvent, this,
&AnimationManager::onCountdownTick);
}
1 change: 1 addition & 0 deletions src/rendering/Animations.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AnimationManager {
void onBrickDestroyed(BrickDestroyedEventArgs& e);
void onModifierApplied(ModifierEventArgs& e);
void onModifierRemoved(ModifierRemovedEventArgs& e);
void onCountdownTick(TimerEventArgs& e);
private:
RoundController& _roundController;
ofTrueTypeFont _messageFont;
Expand Down

1 comment on commit 73516ff

@t3kt
Copy link
Contributor Author

@t3kt t3kt commented on 73516ff Dec 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closes #66,
see #41

Please sign in to comment.