Skip to content

Commit

Permalink
switching to object keys stored in the box2d objects instead of direc…
Browse files Browse the repository at this point in the history
…t pointers

see VideoBleep#12 (and eventually VideoBleep#6)
  • Loading branch information
t3kt committed Dec 5, 2014
1 parent 20c12a8 commit bc142c1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ static GameObjectId nextId() {
}

GameObject::GameObject(GameObjectType t)
: _type(t), _id(nextId()) {}
: _key(t, nextId()) {}
15 changes: 11 additions & 4 deletions src/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,26 @@ enum GameObjectType {
};
typedef int GameObjectId;

struct GameObjectKey {
const GameObjectType type;
const GameObjectId id;
GameObjectKey(GameObjectType t, GameObjectId i) : type(t), id(i) {}
};

class GameObject {
public:
GameObject(GameObjectType t);

GameObjectType type() const { return _type; }
GameObjectId id() const { return _id; }
GameObjectType type() const { return _key.type; }
GameObjectId id() const { return _key.id; }
const GameObjectKey& key() const { return _key; }
GameObjectKey& key() { return _key; }
bool alive() const { return _alive; }

void kill() { _alive = false; }
void revive() { _alive = true; }
private:
const GameObjectType _type;
const GameObjectId _id;
GameObjectKey _key;
bool _alive;
};

Expand Down
35 changes: 27 additions & 8 deletions src/RoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ void RoundController::addBrick(ofVec2f center) {
rect.setFromCenter(center, _config.brickSize().x, _config.brickSize().y);
ofPtr<Brick> brick(new Brick);
brick->rect().setup(_box2d.getWorld(), rect);
brick->rect().setData(brick.get());
brick->rect().setData(&(brick->key()));
_bricks.add(brick);
}

void RoundController::addBall(ofVec2f center) {
ofPtr<Ball> ball(new Ball);
ball->circle().setup(_box2d.getWorld(), center, _config.ballRadius());
ball->circle().setPhysics(_config.ballDensity(), _config.ballBounce(), _config.ballFriction());
ball->circle().setData(ball.get());
ball->circle().setData(&(ball->key()));
_balls.add(ball);
}

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

_paddles.add(paddle);
}

ofPtr<GameObject> RoundController::getObject(const GameObjectKey &key) {
switch (key.type) {
case GAME_OBJECT_BALL:
return ofPtr<GameObject>(_balls.getById(key.id));
case GAME_OBJECT_BRICK:
return ofPtr<GameObject>(_bricks.getById(key.id));
case GAME_OBJECT_PADDLE:
return ofPtr<GameObject>(_paddles.getById(key.id));
case GAME_OBJECT_PLAYER:
return ofPtr<GameObject>(_playerManager.players().getById(key.id));
default:
return ofPtr<GameObject>();
}
}

void RoundController::draw() {
_renderer.draw(*this);
//...
Expand All @@ -104,13 +119,17 @@ void RoundController::update() {
void RoundController::contactStart(ofxBox2dContactArgs &e) {
if (e.a == NULL || e.b == NULL)
return;
GameObject* objA = static_cast<GameObject*>(e.a->GetBody()->GetUserData());
GameObject* objB = static_cast<GameObject*>(e.b->GetBody()->GetUserData());
if (objA == NULL || objB == NULL)
GameObjectKey* keyA = static_cast<GameObjectKey*>(e.a->GetBody()->GetUserData());
GameObjectKey* keyB = static_cast<GameObjectKey*>(e.a->GetBody()->GetUserData());
if (keyA == NULL || keyB == NULL)
return;
ofPtr<GameObject> objA = getObject(*keyA);
ofPtr<GameObject> objB = getObject(*keyB);
if (!objA || !objB)
return;
if (objA->type() == GAME_OBJECT_BALL) {
if (keyA->type == GAME_OBJECT_BALL) {
ballHitObject(static_cast<Ball&>(*objA), *objB);
} else if (objB->type() == GAME_OBJECT_BALL) {
} else if (keyB->type == GAME_OBJECT_BALL) {
ballHitObject(static_cast<Ball&>(*objB), *objA);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/RoundManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class RoundController : public RoundEventSender {
void ballHitObject(Ball& ball, GameObject& obj);
void ballHitBrick(Ball& ball, Brick& brick);
void ballHitPaddle(Ball& ball, Paddle& paddle);

ofPtr<GameObject> getObject(const GameObjectKey& key);
private:
GameObjectCollection<Paddle> _paddles;
GameObjectCollection<Ball> _balls;
Expand Down

0 comments on commit bc142c1

Please sign in to comment.