Quintus.Platformer = function(Q) { Q.TileLayer = Q.Sprite.extend({ init: function(props) { this._super(_(props).defaults({ tileW: 32, tileH: 32, blockTileW: 10, blockTileH: 10, type: 1 })); if(this.p.dataAsset) { this.load(this.p.dataAsset); } this.blocks = []; this.p.blockW = this.p.tileW * this.p.blockTileW; this.p.blockH = this.p.tileH * this.p.blockTileH; this.colBounds = {}; this.directions = [ 'top','left','right','bottom']; }, load: function(dataAsset) { var data = _.isString(dataAsset) ? Q.asset(dataAsset) : dataAsset; this.p.tiles = data; this.p.rows = data.length; this.p.cols = data[0].length; this.p.w = this.p.rows * this.p.tileH; this.p.h = this.p.cols * this.p.tileW; }, setTile: function(x,y,tile) { var p = this.p, blockX = Math.floor(x/p.blockTileW), blockY = Math.floor(y/p.blockTileH); if(blockX >= 0 && blockY >= 0 && blockX < this.p.cols && blockY < this.p.cols) { this.p.tiles[y][x] = tile; if(this.blocks[blockY]) { this.blocks[blockY][blockX] = null; } } }, checkBounds: function(pos,col,start) { start = start || 0; for(var i=0;i<4;i++) { var dir = this.directions[(i+start)%4]; var result = this.checkPoints(pos,col[dir],dir); if(result) { result.start = i+1; return result; } } return false; }, checkPoints: function(pos,pts,which) { for(var i=0,len=pts.length;i 0) { this.colBounds.tile = p.tiles[tileY][tileX]; this.colBounds.direction = which; switch(which) { case 'top': this.colBounds.destX = x; this.colBounds.destY = (tileY+1)*p.tileH + p.y + Q.dx; break; case 'bottom': this.colBounds.destX = x; this.colBounds.destY = tileY*p.tileH + p.y - Q.dx; break; case 'left': this.colBounds.destX = (tileX+1)*p.tileW + p.x + Q.dx; this.colBounds.destY = y; break; case 'right': this.colBounds.destX = tileX*p.tileW + p.x - Q.dx; this.colBounds.destY = y; break; } return this.colBounds; } return false; }, prerenderBlock: function(blockX,blockY) { var p = this.p, tiles = p.tiles, sheet = this.sheet(), blockOffsetX = blockX*p.blockTileW, blockOffsetY = blockY*p.blockTileH; if(blockOffsetX < 0 || blockOffsetX >= this.p.cols || blockOffsetY < 0 || blockOffsetY >= this.p.rows) { return; } var canvas = document.createElement('canvas'), ctx = canvas.getContext('2d'); canvas.width = p.blockW; canvas.height= p.blockH; this.blocks[blockY] = this.blocks[blockY] || {}; this.blocks[blockY][blockX] = canvas; for(var y=0;y 0) { dt = Math.min(1/30,dtStep); // Updated based on the velocity and acceleration p.vx += p.ax * dt + Q.gravityX * dt * p.gravity; p.vy += p.ay * dt + Q.gravityY * dt * p.gravity; p.x += p.vx * dt; p.y += p.vy * dt; this.entity.parent.collide(this.entity); dtStep -= 1/30; } }, debugDraw: function(ctx) { var p = this.entity.p; ctx.save(); ctx.fillStyle = "black"; if(p.col) { _.each(p.col,function(points,dir) { for(var i=0;i