Skip to content

Commit 1a62977

Browse files
committed
Fixed two bigs with CollisionBlock creation: row duplication when growing collision block vertically, and properly settings bits in j!=0 bits[i][j] array. Also improved debug tools, Actor and TileObject now draw their CollisionBlock in a partially transparent overlay (using new CollisionBlock.draw() method--expensive method, should only be used in debugging).
1 parent 98ce861 commit 1a62977

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

Actor.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ Actor.prototype.draw = function(args) {
5656
var y_offset = (args.yOffset !== undefined)? args.yOffset : 0;
5757
if(context === undefined) return;
5858

59-
if(args.debug) {
60-
context.fillStyle = "#00ff00";
61-
context.fillRect(this.x, this.y, this.collision_block.width, this.collision_block.height);
62-
}
6359
this.sprite.draw({'context':context,x:this.x-x_offset,y:this.y-y_offset});
60+
if(args.debug) this.collision_block.draw({context:context,x:this.x-x_offset,y:this.y-y_offset});
6461
}

CollisionBlock.js

+51-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var CollisionBlock = function(args) {
22
this.width = (args.width !== undefined)? args.width : 0;
33
this.height = (args.height !== undefined)? args.height : 0;
44
this.bits = [];
5+
this.image_data = undefined;
56

67
var collision_array = args.matrix;
78
if(collision_array === undefined) collision_array = [[1]];
@@ -30,13 +31,17 @@ var CollisionBlock = function(args) {
3031
}
3132
}
3233

34+
collision_array = pixels.slice(0);
35+
3336
if(this.height > collision_array.length) {
3437
var clone = this.height % collision_array.length;
3538
var cloneindex = Math.floor(collision_array.length / 2) - 1;
3639
var multiply = Math.floor(this.height / collision_array.length);
3740
var cloned = false;
3841
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+
}
4045
}
4146
}
4247

@@ -46,8 +51,8 @@ var CollisionBlock = function(args) {
4651
for(var j=0; j<len; j++) {
4752
this.bits[i][j] = 0;
4853
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);
5156
}
5257
}
5358
}
@@ -113,4 +118,47 @@ CollisionBlock.prototype.isOverlapping = function(args) {
113118
}
114119
}
115120
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);
116164
}

TileObject.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ TileObject.prototype.draw = function(args) {
1111
var context = args.context;
1212
var debug = args.debug;
1313
if(debug) {
14-
context.fillStyle = "#ff0000";
15-
context.fillRect(this.x, this.y, this.collision_block.width, this.collision_block.height);
14+
//context.fillStyle = "#ff0000";
15+
//context.fillRect(this.x, this.y, this.collision_block.width, this.collision_block.height);
16+
this.collision_block.draw({context:context,x:this.x,y:this.y,red:255,alpha:180});
1617
}
1718
}

side_scroller.html

-2
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,6 @@
267267
oso.registerObject(a);
268268
scene.addObserver(oso);
269269

270-
//scene.debug = true;
271-
272270
function draw() {
273271
scene.update({ms:33});
274272
scene.draw({context:context});

0 commit comments

Comments
 (0)