Quintus.Platformer = function(Q) { Q.gravityY = 9.8*100; Q.gravityX = 0; Q.dx = 0.05; Q.register('2d',{ added: function() { var entity = this.entity; _(entity.p).defaults({ vx: 0, vy: 0, ax: 0, ay: 0, gravity: 1, collisionMask: 1 }); this.blocks = []; entity.bind('step',this,"step"); if(Q.debug) { entity.bind('draw',this,'debugDraw'); } }, extend: { collisionPoints: function(points) { var p = this.p, w = p.w, h = p.h; if(!points) { p.col = { top: [ [w/2, 0]], left: [ [0, h/3], [0, 2*h/3]], bottom:[ [w/2, h]], right: [ [w, h/3], [w, 2*h/3]] } } else { p.col = points; } } }, step: function(stepData) { var p = this.entity.p, dtStep = stepData.dt, dt = dtStep; while(dtStep > 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 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; }, load: function(dataAsset) { var data = _.isString(dataAsset) ? Q.asset(dataAsset) : dataAsset; this.p.tiles = dataAsset; this.p.rows = dataAsset.length; this.p.cols = dataAsset[0].length; this.p.w = this.p.rows * this.p.tileH; this.p.h = this.p.cols * this.p.tileW; }, 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