Skip to content

Commit

Permalink
got collision events working properly
Browse files Browse the repository at this point in the history
  • Loading branch information
t3kt committed Dec 7, 2014
1 parent f274ce6 commit 91eb569
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
14 changes: 14 additions & 0 deletions src/GameEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@
//

#include "GameEvents.h"
#include <ofMain.h>


void RoundEventSender::notifyBallHitPaddle(Ball &ball, Paddle &paddle) {
ofLogVerbose() << "EVENT: ballHitPaddle: " << ball << " " << paddle;
BallHitPaddleEventArgs e(ball, paddle);
ofNotifyEvent(ballHitPaddleEvent, e);
}

void RoundEventSender::notifyBallHitBrick(Ball &ball, Brick &brick) {
ofLogVerbose() << "EVENT: ballHitBrick: " << ball << " " << brick;
BallHitBrickEventArgs e(ball, brick);
ofNotifyEvent(ballHitBrickEvent, e);
}
20 changes: 7 additions & 13 deletions src/GameEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
template<typename T>
class BallHitObjectEventArgs {
public:
BallHitObjectEventArgs(ofPtr<Ball> ball, ofPtr<T> object)
BallHitObjectEventArgs(Ball& ball, T& object)
: _ball(ball), _object(object) { }

ofPtr<Ball> ball() { return _ball; }
ofPtr<T> object() { return _object; }
Ball& ball() { return _ball; }
T& object() { return _object; }
private:
ofPtr<Ball> _ball;
ofPtr<T> _object;
Ball& _ball;
T& _object;
};

class PlayerEventArgs {
Expand All @@ -44,14 +44,8 @@ class RoundEventSender {
ofEvent<BallHitPaddleEventArgs> ballHitPaddleEvent;
ofEvent<BallHitBrickEventArgs> ballHitBrickEvent;
protected:
void notifyBallHitPaddle(ofPtr<Ball> ball, ofPtr<Paddle> paddle) {
BallHitPaddleEventArgs e(ball, paddle);
ofNotifyEvent(ballHitPaddleEvent, e);
}
void notifyBallHitBrick(ofPtr<Ball> ball, ofPtr<Brick> brick) {
BallHitBrickEventArgs e(ball, brick);
ofNotifyEvent(ballHitBrickEvent, e);
}
void notifyBallHitPaddle(Ball& ball, Paddle& paddle);
void notifyBallHitBrick(Ball& ball, Brick& brick);
};

#endif /* defined(__bleepout__GameEvents__) */
1 change: 1 addition & 0 deletions src/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <iostream>

enum GameObjectType {
GAME_OBJECT_OTHER,
GAME_OBJECT_BRICK,
GAME_OBJECT_PADDLE,
GAME_OBJECT_BALL,
Expand Down
23 changes: 14 additions & 9 deletions src/RoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void RoundController::setup() {
_box2d.createBounds(ofRectangle(5, 5, ofGetWidth() - 10, ofGetHeight() -10 ));
_box2d.setFPS(BleepoutConfig::getInstance().fps());
_box2d.registerGrabbing();
_box2d.enableEvents();
ofAddListener(_box2d.contactStartEvents, this, &RoundController::contactStart);
ofAddListener(_box2d.contactEndEvents, this, &RoundController::contactEnd);

Expand All @@ -76,7 +77,7 @@ void RoundController::addBrick(ofVec2f center) {
rect.setFromCenter(center, _config.brickSize().x, _config.brickSize().y);
ofPtr<Brick> brick(new Brick);
brick->setup(_box2d.getWorld(), rect);
brick->setData(&brick);
brick->setData(brick.get());
_bricks.push_back(brick);
}

Expand All @@ -85,7 +86,7 @@ void RoundController::addBall(ofVec2f center) {
ball->setPhysics(_config.ballDensity(), _config.ballBounce(), _config.ballFriction());
ball->setup(_box2d.getWorld(), center, _config.ballRadius());
ball->setVelocity(_config.ballInitialVelocity());
ball->setData(&ball);
ball->setData(ball.get());
_balls.push_back(ball);
}

Expand All @@ -96,7 +97,7 @@ void RoundController::addPaddle(ofVec2f center, ofPtr<Player> player) {
rect.setFromCenter(center, _config.paddleSize().x, _config.paddleSize().y);
paddle->setup(_box2d.getWorld(), rect);
paddle->setPhysics(_config.paddleBounce(), _config.paddleDensity(), _config.paddleFriction());
paddle->setData(&paddle);
paddle->setData(paddle.get());

_paddles.push_back(paddle);
}
Expand Down Expand Up @@ -155,9 +156,9 @@ void RoundController::setPaddlePosition(GameObjectId playerId, float xPercent) {
}

ofVec2f pos = paddle->getPosition();
ofLogVerbose() << "Paddle position was " << pos;
// ofLogVerbose() << "Paddle position was " << pos;
pos.x = xPercent * ofGetWidth();
ofLogVerbose() << "Setting paddle position to " << pos;
// ofLogVerbose() << "Setting paddle position to " << pos;
paddle->setPosition(pos);
//...
}
Expand All @@ -177,10 +178,12 @@ void RoundController::mouseDragged(int x, int y, int button) {
void RoundController::contactStart(ofxBox2dContactArgs &e) {
if (e.a == NULL || e.b == NULL)
return;
ofPtr<GameObject>& objA = *((ofPtr<GameObject>*)e.a->GetBody()->GetUserData());
ofPtr<GameObject>& objB = *((ofPtr<GameObject>*)e.b->GetBody()->GetUserData());
if (!objA || !objB)
GameObject* objA = (GameObject*)e.a->GetBody()->GetUserData();
GameObject* objB = (GameObject*)e.b->GetBody()->GetUserData();
if (!objA || !objB) {
ofLogVerbose() << "Unable to extra game object from b2d body";
return;
}
if (objA->type() == GAME_OBJECT_BALL) {
ballHitObject(static_cast<Ball&>(*objA), *objB);
} else if (objB->type() == GAME_OBJECT_BALL) {
Expand All @@ -193,6 +196,7 @@ void RoundController::contactEnd(ofxBox2dContactArgs &e) {
}

void RoundController::ballHitObject(Ball &ball, GameObject &obj) {
ofLogVerbose() << "pre-event: ball hit something: " << ball << " " << obj;
switch (obj.type()) {
case GAME_OBJECT_BRICK:
ballHitBrick(ball, static_cast<Brick&>(obj));
Expand All @@ -210,11 +214,12 @@ void RoundController::ballHitObject(Ball &ball, GameObject &obj) {

void RoundController::ballHitBrick(Ball &ball, Brick &brick) {
//...
//notifyBallHitBrick(
notifyBallHitBrick(ball, brick);
}

void RoundController::ballHitPaddle(Ball &ball, Paddle &paddle) {
ball.setLastPlayer(paddle.player());
notifyBallHitPaddle(ball, paddle);
}

void RoundController::dumpToLog() {
Expand Down

0 comments on commit 91eb569

Please sign in to comment.