所以我一直在制作游戏,直到我想尝试 TDD,所以我的大部分工作代码都没有任何测试,但我想为每个新功能尝试 TDD。
我的问题是我的游戏由大量相互依赖的系统组成(有点像没有关卡就无法使用相机,对象保留大量引用并且初始化事物将其他事物作为参数)。所以只是为了测试雾系统,我需要初始化关卡、物理、相机、碰撞(因为它们在某种程度上都相互依赖),这会产生很多重复。这是代码:
test( "shadow test", function() {
var b2world=new b2World(new b2Vec2(0, 0), false);
var contactListener = new collisionHandler.CollisionHandler(MASK_BITS);
b2world.SetContactListener(contactListener);
var map = gamejs.http.load('images/prot8.json');
var level = new Level.Level({
map: map,
size: 0.5,
nMaskBits: MASK_BITS.node,
nCategoryBits: MASK_BITS.player | MASK_BITS.birdy | MASK_BITS.innerBody,
world: b2world,
scale: SCALE});
var cam = new Camera.Camera({
lvlWid: this.level.width*SCALE*this.level.blockSize,
lvlHei: this.level.height*SCALE*this.level.blockSize,
yBand: 2,
maxSpeed: 20,
peerWindow: new b2Vec2(350, 300),
scrWid: scrWid,
scrHei: scrHei});
var shadow = new Shadow.Shadow({
width : 300,
height : 300,
level : level,
eye : new b2Vec2(600, 600),
});
ok( shadow.blit, "Shadow is extended from surface" );
ok( shadow.level, "Shadow has reference to the level" );
ok( shadow.eye, "Shadow has reference to player's eye" );
ok( (function() {
for (var i = 0; i < shadow.onScreenBlocks.length; i++) {
var rect = level.boxes[ shadow.onScreenBlocks[i] ];
//this is half finished
}
return true;
}), "Shadow do picks the blocks that are visible on screen" );
ok( (function() {
for (var i = 0; i < level.boxes.length; i++) if ( shadow.notProcessBlock(i) ) {
var rect = level.boxes[i];
if (rect.left < cam.offsetX //at this point I just realized that camera need to be setup in a more complex way...
}
return true;
}), "Shadow only process those blocks that are visible on screen" );
});
总的来说,它的氛围很差。我的想法更难绕开,也更难维持。