Skip to content

Commit

Permalink
implementing game logic events (#12) and basic scoring
Browse files Browse the repository at this point in the history
Also, switch a bunch of ofPtrs to raw pointers. They don’t need to have
shared ownership since GameState is the owner of all game objects.
  • Loading branch information
t3kt committed Dec 8, 2014
1 parent 4c37ab1 commit 4129818
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/Ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Ball::~Ball() {
void Ball::output(std::ostream &os) const {
auto pos = body->GetPosition();
os << "Ball{id:" << id() << ", pos:(" << pos.x << "," << pos.y << ")";
os << ", curPlayer:";
if (_lastPlayer)
os << _lastPlayer->id();
os << ", player:";
if (_player)
os << _player->id();
else
os << "(none)";
os << "}";
Expand Down
9 changes: 4 additions & 5 deletions src/Ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ class Ball : public GameObject, public ofxBox2dCircle {
Ball();
~Ball() override;

ofPtr<Player> lastPlayer() { return _lastPlayer; }
void setLastPlayer(ofPtr<Player> player) {
_lastPlayer = player;
}
Player* player() { return _player; }
void setPlayer(Player* player) { _player = player; }

void output(std::ostream& os) const override;
private:
ofPtr<Player> _lastPlayer;
Player* _player;
};

#endif /* defined(__bleepout__Ball__) */
6 changes: 5 additions & 1 deletion src/Brick.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@

class Brick : public GameObject, public ofxBox2dRect {
public:
Brick() : GameObject(GAME_OBJECT_BRICK) {}
Brick() : GameObject(GAME_OBJECT_BRICK), _value(1) {}

void output(std::ostream& os) const override;

int value() const { return _value; }
void setValue(int value) { _value = value; }
private:
int _value;
};

#endif /* defined(__bleepout__Brick__) */
27 changes: 17 additions & 10 deletions src/LogicController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,35 @@ void LogicController::update() {
}

void LogicController::onBallHitPaddle(BallHitPaddleEventArgs &e) {
Player* previousPlayer = e.ball()->lastPlayer().get();
ofPtr<Player> player = e.object()->player();
e.ball()->setLastPlayer(player);
notifyBallOwnerChanged(e.ball(), player.get(), previousPlayer);
Player* previousPlayer = e.ball()->player();
Player* player = e.object()->player();
e.ball()->setPlayer(player);
notifyBallOwnerChanged(e.ball(), player, previousPlayer);
}

void LogicController::onBallHitBrick(BallHitBrickEventArgs &e) {
Ball* ball = e.ball();
Brick* brick = e.object();
Player* player = ball->player();

brick->kill();
notifyBrickDestroyed(brick, ball);

if (player) {
player->addScore(brick->value());
notifyPlayerScoreChanged(player);
}
//...
}

void LogicController::onBallHitWall(BallHitWallEventArgs &e) {
Ball* ball = e.ball();
Wall* wall = e.object();
if (wall->isExit()) {
Ball* ball = e.ball();
Player* player = ball->player();
}
}

void LogicController::onBallHitBall(BallHitBallEventArgs &e) {
//...
}

void LogicController::playerBallOut(Player &player, Ball &ball) {
// ball destroyed!!!
//???
}
4 changes: 0 additions & 4 deletions src/LogicController.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ class LogicController : public RoundStateEventSource {
void onBallHitWall(BallHitWallEventArgs& e);
void onBallHitBall(BallHitBallEventArgs& e);

private:
void playerBallOut(Player& player, Ball& ball);
void playerTakeBallOwnership(Player& player, Ball& ball);

private:
RoundState& _state;
RoundConfig& _config;
Expand Down
3 changes: 2 additions & 1 deletion src/Paddle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "Paddle.h"

Paddle::Paddle(ofPtr<Player> player)
Paddle::Paddle(Player* player)
: GameObject(GAME_OBJECT_PADDLE), _player(player) {
ofLogVerbose() << "Create Paddle";
}
Expand All @@ -19,6 +19,7 @@ Paddle::~Paddle() {
}

void Paddle::output(std::ostream &os) const {
// this shouldn't be necessary. getPosition should really be marked as const
auto pos = const_cast<Paddle*>(this)->getPosition();
os << "Paddle{id:" << id() << ", pos:(" << pos.x << "," << pos.y << ")";
if (_player)
Expand Down
6 changes: 3 additions & 3 deletions src/Paddle.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

class Paddle : public GameObject, public ofxBox2dRect {
public:
Paddle(ofPtr<Player> player);
Paddle(Player* player);
~Paddle() override;

ofPtr<Player> player() { return _player; }
Player* player() { return _player; }

void output(std::ostream& os) const override;
private:
ofPtr<Player> _player;
Player* _player;
};


Expand Down
5 changes: 0 additions & 5 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
#include <ofMain.h>

Player::Player() : GameObject(GAME_OBJECT_PLAYER) {
ofLogVerbose() << "Create Player";
}

Player::~Player() {
ofLogVerbose() << "Destroy Player";
}

void Player::output(std::ostream &os) const {
Expand Down
17 changes: 13 additions & 4 deletions src/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ class Paddle;
class Player : public GameObject {
public:
Player();
~Player() override;
void setPaddle(ofPtr<Paddle> paddle) { _paddle = paddle; }
ofPtr<Paddle> paddle() { return _paddle; }

void setPaddle(Paddle* paddle) { _paddle = paddle; }
Paddle* paddle() { return _paddle; }

int score() const { return _score; }

int addScore(int add) {
_score += add;
return _score;
}

private:
void output(std::ostream& os) const override;

ofPtr<Paddle> _paddle;
Paddle* _paddle;
int _score;
};

#endif /* defined(__bleepout__Player__) */
2 changes: 1 addition & 1 deletion src/RoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void RoundController::setPaddlePosition(GameObjectId playerId, float xPercent) {
return;
}

ofPtr<Paddle> paddle = player->paddle();
Paddle* paddle = player->paddle();
if (!paddle) {
ofLogError() << "Unable to set paddle position for player: " << playerId;
return;
Expand Down
6 changes: 3 additions & 3 deletions src/SpaceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void SpaceController::setup() {
for (int i = 0; i < numPlayers; i++) {
ofPtr<Player> player = _state.players()[i];
ofVec2f paddleCenter = getPaddleStartPosition(i, numPlayers, _config);
addPaddle(paddleCenter, player);
addPaddle(paddleCenter, player.get());
ofVec2f ballCenter = getBallStartPosition(i, numPlayers, _config);
addBall(ballCenter);
}
Expand Down Expand Up @@ -82,10 +82,10 @@ void SpaceController::addBall(ofVec2f center) {
_state.balls().push_back(ball);
}

void SpaceController::addPaddle(ofVec2f center, ofPtr<Player> player) {
void SpaceController::addPaddle(ofVec2f center, Player* player) {
ofPtr<Paddle> paddle(new Paddle(player));
ofRectangle rect;
player->setPaddle(paddle);
player->setPaddle(paddle.get());
rect.setFromCenter(center, _config.paddleSize().x, _config.paddleSize().y);
setObjPhysics(paddle.get(), _config.paddlePhysics());
paddle->setup(_box2d.getWorld(), rect);
Expand Down
2 changes: 1 addition & 1 deletion src/SpaceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SpaceController : public CollisionEventSource {

void addBrick(ofVec2f center);
void addBall(ofVec2f center);
void addPaddle(ofVec2f center, ofPtr<Player> player);
void addPaddle(ofVec2f center, Player* player);

void contactStart(ofxBox2dContactArgs& e);
void contactEnd(ofxBox2dContactArgs& e);
Expand Down

0 comments on commit 4129818

Please sign in to comment.