@@ -2,6 +2,7 @@ var CollisionBlock = function(args) {
2
2
this . width = ( args . width !== undefined ) ? args . width : 0 ;
3
3
this . height = ( args . height !== undefined ) ? args . height : 0 ;
4
4
this . bits = [ ] ;
5
+ this . image_data = undefined ;
5
6
6
7
var collision_array = args . matrix ;
7
8
if ( collision_array === undefined ) collision_array = [ [ 1 ] ] ;
@@ -30,13 +31,17 @@ var CollisionBlock = function(args) {
30
31
}
31
32
}
32
33
34
+ collision_array = pixels . slice ( 0 ) ;
35
+
33
36
if ( this . height > collision_array . length ) {
34
37
var clone = this . height % collision_array . length ;
35
38
var cloneindex = Math . floor ( collision_array . length / 2 ) - 1 ;
36
39
var multiply = Math . floor ( this . height / collision_array . length ) ;
37
40
var cloned = false ;
38
41
for ( var i = 0 ; i < collision_array . length ; i ++ ) {
39
- for ( var j = 0 ; j < multiply - 1 + ( i == cloneindex ? clone : 0 ) ; j ++ ) pixels . splice ( ( i * multiply ) + ( cloned ? clone : 0 ) , 0 , pixels [ i * multiply ] ) ;
42
+ for ( var j = 0 ; j < multiply - 1 + ( i == cloneindex ? clone : 0 ) ; j ++ ) {
43
+ pixels . splice ( ( i * multiply ) + ( cloned ? clone : 0 ) , 0 , collision_array [ i ] ) ;
44
+ }
40
45
}
41
46
}
42
47
@@ -46,8 +51,8 @@ var CollisionBlock = function(args) {
46
51
for ( var j = 0 ; j < len ; j ++ ) {
47
52
this . bits [ i ] [ j ] = 0 ;
48
53
for ( var k = 0 ; k < 32 ; k ++ ) {
49
- if ( pixels [ i ] [ k ] === undefined && j == len - 1 ) break ;
50
- this . bits [ i ] [ j ] += pixels [ i ] [ k ] == 0 ? 0 : Math . pow ( 2 , 32 - k - 1 ) ;
54
+ if ( pixels [ i ] [ ( j * 32 ) + k ] === undefined && j == len - 1 ) break ;
55
+ this . bits [ i ] [ j ] += pixels [ i ] [ ( j * 32 ) + k ] == 0 ? 0 : Math . pow ( 2 , 32 - k - 1 ) ;
51
56
}
52
57
}
53
58
}
@@ -113,4 +118,47 @@ CollisionBlock.prototype.isOverlapping = function(args) {
113
118
}
114
119
}
115
120
return false ;
121
+ }
122
+
123
+ CollisionBlock . prototype . draw = function ( args ) {
124
+ /* This should never be used outside of debugging. */
125
+ if ( args === undefined ) return ;
126
+ var context = args . context ;
127
+ var startX = ( args . x !== undefined ) ? args . x : 0 ;
128
+ var startY = ( args . y !== undefined ) ? args . y : 0 ;
129
+ var red = ( args . red !== undefined ) ? args . red : 255 ;
130
+ var green = ( args . green !== undefined ) ? args . green : 0 ;
131
+ var blue = ( args . blue !== undefined ) ? args . blue : 0 ;
132
+ var alpha = ( args . alpha !== undefined ) ? args . alpha : 128 ;
133
+ if ( context === undefined ) return ;
134
+ if ( this . image_data === undefined ) {
135
+ this . image_data = context . createImageData ( this . width , this . height ) ;
136
+
137
+ for ( var i = 0 ; i < this . bits . length ; i ++ ) {
138
+ for ( var j = 0 ; j < this . bits [ i ] . length ; j ++ ) {
139
+ for ( var k = 0 ; k < 32 ; k ++ ) {
140
+ if ( this . bits [ i ] [ j ] & Math . pow ( 2 , 31 - k ) ) {
141
+ //console.log('here!');
142
+ this . image_data . data [ ( i * this . width * 4 ) + ( j * 32 * 4 ) + ( k * 4 ) ] = red ;
143
+ this . image_data . data [ ( i * this . width * 4 ) + ( j * 32 * 4 ) + ( k * 4 ) + 1 ] = green ;
144
+ this . image_data . data [ ( i * this . width * 4 ) + ( j * 32 * 4 ) + ( k * 4 ) + 2 ] = blue ;
145
+ this . image_data . data [ ( i * this . width * 4 ) + ( j * 32 * 4 ) + ( k * 4 ) + 3 ] = alpha ;
146
+ }
147
+ else
148
+ {
149
+ this . image_data . data [ ( i * this . width * 4 ) + ( j * 32 * 4 ) + ( k * 4 ) + 3 ] = 0 ;
150
+ }
151
+ }
152
+ }
153
+ }
154
+ }
155
+ if ( this . alt_canvas === undefined ) {
156
+ this . alt_canvas = document . createElement ( "canvas" ) ;
157
+ this . alt_canvas . width = this . width ;
158
+ this . alt_canvas . height = this . height ;
159
+ var ctx = this . alt_canvas . getContext ( "2d" ) ;
160
+ ctx . putImageData ( this . image_data , 0 , 0 ) ;
161
+ }
162
+ //context.putImageData(this.image_data,startX,startY);
163
+ context . drawImage ( this . alt_canvas , startX , startY ) ;
116
164
}
0 commit comments