之前寫的 BubbleBall Level One 是錯誤示範,兒子超過爸爸了。改寫後如下:
class Box2dLevelOne {
public:
Box2dLevelOne() {
physics_.initWorld();
physics_.setEdgeBackground(...);
physics_.setPlayer(...);
physics_.setGoal(...);
}
~Box2dLevelOne() {
physics_.deinitWorld();
}
void setTriangle(...) {
physics_.setTriangle(...);
}
bool isGoaledAfter(int seconds) {
float time_step = 1.0f/60.0f;
for (int i = 0; i < seconds; ++i) {
physics_.simulate(time_step);
if (physics_.isGoaled()) {
return true;
}
}
return false;
}
private:
Box2dPhysics physics_;
};
TEST(Box2dLevelOne, StressTest) {
const TestItem item[] = { ... };
const int size = arraysize(item);
for (int i = 0; i != size; ++i) {
Box2dLevelOne level;
level.setTriangle(...);
EXPECT_EQ(item[i].is_goaled, level.isGoaledAfter(60 * 10));
}
}
以上。當然我還犯其他錯誤 (過早最佳化,沒有利用 assert 來說明內部假設與不變性),接下來會修掉這些錯誤。繼續加油吧!
physics == baseLevel?
回覆刪除Yes.
回覆刪除In the old design, BaseLevel class has two responsibilities: level and physics. So I want to make BaseLevel class simpler.