1     /**
2         A class to represent the level
3         @author <a href="mailto:matthewcasperson@gmail.com">Matthew Casperson</a>
4         @class
5     */
6     function Level()
7 {
8 this.blocks = new Array();
9 this.powerups = new Object;
10 this.blockWidth = 64;
11 this.blockHeight = 48;
12 13 /** 14 Initialises this object 15 */ 16 this.startupLevel = function(canvasWidth, canvasHeight)
17 {
18 this.blocks[0] = 3;
19 this.blocks[1] = 2;
20 this.blocks[2] = 1;
21 this.blocks[3] = 1;
22 this.blocks[4] = 1;
23 this.blocks[5] = 1;
24 this.blocks[6] = 2;
25 this.blocks[7] = 3;
26 this.blocks[8] = 2;
27 this.blocks[9] = 1;
28 this.blocks[10] = 2;
29 this.blocks[11] = 3;
30 this.blocks[12] = 4;
31 this.blocks[13] = 5;
32 this.blocks[14] = 4;
33 this.blocks[15] = 3;
34 35 this.powerups['1'] = 'Gem';
36 this.powerups['6'] = 'Gem';
37 this.powerups['10'] = 'Gem';
38 this.powerups['14'] = 'Gem';
39 40 this.addBlocks(canvasWidth, canvasHeight);
41 this.addPowerups(canvasWidth, canvasHeight);
42 43 return this;
44 }
45 46 /** 47 Adds the blocks to the screen by creating VisualGameObjects 48 */ 49 this.addBlocks = function(canvasWidth, canvasHeight)
50 {
51 for (var x = 0; x < this.blocks.length; ++x)
52 {
53 for (var y = 0; y < this.blocks[x]; ++y)
54 {
55 new VisualGameObject().startupVisualGameObject(g_block, x * this.blockWidth, canvasHeight - (y + 1) * this.blockHeight, 4);
56 }
57 }
58 }
59 60 this.addPowerups = function(canvasWidth, canvasHeight)
61 {
62 for (var x = 0; x < this.blocks.length; ++x)
63 {
64 if (this.powerups[x])
65 {
66 var xPosition = x * this.blockWidth + this.blockWidth / 2;
67 var yPosition = canvasHeight - this.groundHeight(x);
68 69 switch(this.powerups[x])
70 {
71 case 'Gem':
72 new Powerup().startupPowerup(10, g_gem, xPosition - g_gem.width / 2, yPosition - g_gem.height, 4, 1, 1);
73 break;
74 }
75 }
76 }
77 }
78 79 /** 80 @return The block under the specified x position 81 @param x The x position to test 82 */ 83 this.currentBlock = function(x)
84 {
85 return parseInt( x / this.blockWidth);
86 }
87 88 /** 89 @return The hieght of the ground under the specified block 90 @param blockIndex The block number 91 */ 92 this.groundHeight = function(blockIndex)
93 {
94 if (blockIndex < 0 || blockIndex > this.blocks.length) return 0;
95 96 return this.blocks[blockIndex] * this.blockHeight;
97 }
98 }
Top