Skip to content

Commit

Permalink
added ball spawned event with animation
Browse files Browse the repository at this point in the history
  • Loading branch information
t3kt committed Dec 27, 2014
1 parent ebd2f42 commit 5e852f6
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/config/BleepoutConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ _ballRadius(8.0f),
_modifierRadius(9.0f),
_brickFadeTime(0.4f),
_modifierFadeTime(0.2f),
_ballSpawnedFadeTime(0.2f),
_domeRadius(150.0f),
_domeMargin(20.0f),
_name(name),
Expand Down
2 changes: 2 additions & 0 deletions src/config/BleepoutConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class RoundConfig {
float brickFadeTime() const { return _brickFadeTime; }
float modifierRadius() const { return _modifierRadius; }
float modifierFadeTime() const { return _modifierFadeTime; }
float ballSpawnedFadeTime() const { return _ballSpawnedFadeTime; }

float domeRadius() const { return _domeRadius; }
float domeMargin() const { return _domeMargin; }
Expand Down Expand Up @@ -169,6 +170,7 @@ class RoundConfig {
float _brickFadeTime;
float _modifierRadius;
float _modifierFadeTime;
float _ballSpawnedFadeTime;
float _domeRadius;
float _domeMargin;
GameRules _rules;
Expand Down
9 changes: 8 additions & 1 deletion src/core/RoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ RoundResults RoundController::buildRoundResults(RoundEndReason reason) {
}

void RoundController::onTrySpawnBall(SpawnBallEventArgs &e) {
_spaceController->addBall(e.ballSpec());
Ball& ball = _spaceController->addBall(e.ballSpec());
notifyBallSpawned(_state, &ball);
}

void RoundController::notifyBallSpawned(RoundState &state, Ball *ball) {
BallStateEventArgs e(state, ball);
logEvent("BallSpawned", e);
ofNotifyEvent(ballSpawnedEvent, e);
}

void RoundController::onModifierAppeared(ModifierEventArgs& e) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/RoundManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class RoundController : public EventSource
ofEvent<RoundStateEventArgs> roundQueueEvent;
ofEvent<RoundStateEventArgs> roundPlayEvent;
ofEvent<RoundEndedEventArgs> roundEndedEvent;
ofEvent<BallStateEventArgs> ballSpawnedEvent;

RoundState& state() { return _state; }
const RoundState& state() const { return _state; }
Expand Down Expand Up @@ -80,6 +81,7 @@ class RoundController : public EventSource
void onModifierAppeared(ModifierEventArgs& e);
void onCountdownTick(TimerEventArgs& e);
void onTrySpawnBall(SpawnBallEventArgs& e);
void notifyBallSpawned(RoundState& state, Ball* ball);

void endRound();
void notifyRoundEnded(RoundResults& results);
Expand Down
3 changes: 2 additions & 1 deletion src/core/SpaceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ void SpaceController::addBrick(const BrickSpec &brickSpec) {
_world.addObject(&brick);
}

void SpaceController::addBall(const BallSpec &ballSpec) {
Ball& SpaceController::addBall(const BallSpec &ballSpec) {
Ball& ball = _state.addBall(ballSpec);
_world.addObject(&ball);
return ball;
}

void SpaceController::addPaddle(float heading, Player* player) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/SpaceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SpaceController : public EventSource, public RoundComponent {

void addInitialPaddles();

void addBall(const BallSpec& ballSpec);
Ball& addBall(const BallSpec& ballSpec);
void addPaddle(float heading, Player* player);

void setUpModifier(Modifier& modifier,
Expand Down
45 changes: 45 additions & 0 deletions src/rendering/Animations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,50 @@ void ModifierAnimation::output(std::ostream &os) const {
<< "}";
}

class BallSpawnedAnimation : public AnimationObject {
public:
BallSpawnedAnimation(const Ball& ball, const RoundConfig& config)
: AnimationObject(0, config.ballSpawnedFadeTime())
, _ball(ball)
, _ballRadius(config.ballRadius()) { }

void draw(const RoundConfig& config) override;
void output(std::ostream& os) const override;
private:
const Ball& _ball;
float _ballRadius;
};

void BallSpawnedAnimation::draw(const RoundConfig &config) {
ofPushMatrix();
ofPushStyle();
ofEnableAlphaBlending();
ofFill();
float radius = _ballRadius;
radius *= ofMap(percentage(), 0, 1, 1.5, 1);
ofColor color(192, 192, 192);
color.a = (unsigned char)std::floor(ofMap(percentage(), 0, 1, 127, 0));
ofSetColor(color);
ofDrawSphere(_ball.getPosition(), radius);
ofPopStyle();
ofPopMatrix();
}

void BallSpawnedAnimation::output(std::ostream &os) const {
os << "BallSpawnedAnimation{id:" << id()
<< ", ball: " << _ball.id()
<< "}";
}

AnimationManager::AnimationManager(RoundController& roundController)
: _roundController(roundController)
, _messageFont(){
_messageFont.loadFont("PixelSplitter-Bold.ttf", 50, false, false, true);
ofAddListener(_roundController.ballSpawnedEvent, this, &AnimationManager::onBallSpawned);
}

AnimationManager::~AnimationManager() {
ofRemoveListener(_roundController.ballSpawnedEvent, this, &AnimationManager::onBallSpawned);
}

void AnimationManager::addAnimation(AnimationObject *animation) {
Expand Down Expand Up @@ -193,6 +233,11 @@ void AnimationManager::onModifierRemoved(ModifierRemovedEventArgs &e) {
addAnimation(anim);
}

void AnimationManager::onBallSpawned(BallStateEventArgs &e) {
auto anim = new BallSpawnedAnimation(*e.ball(), _roundController.config());
addAnimation(anim);
}

void AnimationManager::onCountdownTick(TimerEventArgs &e) {
int time = static_cast<int>(e.remainingTime());
addMessage(MessageSpec("Time: " + ofToString(time), ofColor(255, 0, 0))
Expand Down
2 changes: 2 additions & 0 deletions src/rendering/Animations.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class LogicController;
class AnimationManager {
public:
AnimationManager(RoundController& roundController);
~AnimationManager();

void attachTo(LogicController& roundEvents);
void detachFrom(LogicController& roundEvents);
Expand All @@ -33,6 +34,7 @@ class AnimationManager {
void onModifierApplied(ModifierEventArgs& e);
void onModifierRemoved(ModifierRemovedEventArgs& e);
void onCountdownTick(TimerEventArgs& e);
void onBallSpawned(BallStateEventArgs& e);
private:
RoundController& _roundController;
ofTrueTypeFont _messageFont;
Expand Down

1 comment on commit 5e852f6

@t3kt
Copy link
Contributor Author

@t3kt t3kt commented on 5e852f6 Dec 27, 2014

Choose a reason for hiding this comment

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

see #69

Please sign in to comment.