Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- getTiles: function(x,y,width,height)
- {
- var ret = [];
- this.level.layers.forEach(function(layer)
- {
- for(var mX = x; mX < width + x; mX++)
- {
- for(var mY = y; mY < height + y; mY++)
- {
- ret.push(layer.layer.data[mY][mX]);
- }
- }
- },this);
- return ret;
- },
- update: function()
- {
- var dt = this.game.time.elapsed / 1000;
- if( this.paused ) { return; }
- var pSpeed = 10;
- var move = [0,0];
- var moved = [0,0];
- var newDir = -1;
- var mSpeed = pSpeed * dt;
- var slideX = 0;
- var slideY = 0;
- var keys = [this.cursors.up,this.cursors.right,this.cursors.down,this.cursors.left];
- function vecx(dir) { if(dir === 1) { return 1; } else if(dir === 3) { return -1; } else { return 0; } }
- function vecy(dir) { if(dir === 0) { return -1; } else if(dir === 2) { return 1; } else { return 0; } }
- for(var key = 0; key < keys.length; key++)
- {
- if( keys[key].isDown )
- {
- move[0] += vecx(key) * mSpeed;
- move[1] += vecy(key) * mSpeed;
- }
- }
- var playerTileSize = [ this.player.worldWidth , this.player.worldHeight ];
- for(var m = 0; m < move.length; m++)
- {
- if( move[m] === 0 ) { continue; }
- // Mod for direction
- var mod = ( move[m] < 0 ? -1 : 1 );
- // nm is index of the other element in move array
- var nm = ( m === 0 ? 1 : 0 );
- // set which attribute of the tiles to check to see if they are collidable
- var collCheck = "";
- if( m === 0 ) { collCheck = (mod === -1 ? "collideRight" : "collideLeft"); }
- else { collCheck = (mod === -1 ? "collideDown" : "collideUp" ); }
- // help variable to know which axis to use
- var axis = ( m === 0 ? "x" : "y" );
- // more help variables, Tile = floored so its in tile coordinates
- var playerPos = [ this.player.worldX + moved[0], this.player.worldY + moved[1] ];
- var playerCenter = [this.player.worldX + moved[0] + this.player.worldWidth / 2, this.player.worldY + moved[1] + this.player.worldHeight / 2];
- var playerTile = [Math.floor(this.player.worldX + moved[0]), Math.floor(this.player.worldY + moved[1])];
- var playerEnd = [ this.player.worldX + moved[0] + playerTileSize[0] - ( (this.player.WorldX + moved[0]) % 1 === 0 ? 1 : 0 ),
- this.player.worldY + moved[1] + playerTileSize[1] - ( (this.player.WorldY + moved[1]) % 1 === 0 ? 1 : 0 ) ];
- var playerTileEnd = [ Math.floor(playerEnd[0]), Math.floor(playerEnd[1]) ];
- var movedPlayerTile = [ this.player.worldX + moved[0], this.player.worldY + moved[1] ];
- movedPlayerTile[m] += move[m];
- movedPlayerTile = [ ];
- var movedPlayerTileEnd = [ playerEnd[0] + moved[0], playerEnd[1] + moved[1] ];
- movedPlayerTileEnd[m] += move[m];
- movedPlayerTileEnd = [ Math.floor(movedPlayerTileEnd[0]), Math.floor(movedPlayerTileEnd[1]) ];
- // Our start is at the right side of the player, except if we move left, then it is the left side
- var start = playerEnd[m];
- if( mod === -1 ) { start = playerPos[m]; }
- // our end is the start with the movement added
- var end = start + move[m];
- // put start and end into tile coordinates
- var tileStart = Math.floor(start);
- var tileEnd = Math.floor(end);
- // measure distance in tiles
- var tileSteps = Math.abs(tileStart - tileEnd) + 1;
- var collided = false;
- for(var step = 1; step < tileSteps; step++)
- {
- // help variable, the tile we want to check is start + step tiles into the movement direction
- var helpStart = [ playerTile[0], playerTile[1] ];
- helpStart[m] = tileStart + mod * step;
- // set the size of the check on the direction we aren't moving in
- var checkSize = [ 1, 1 ];
- checkSize[nm] = playerTileEnd[nm] - playerTile[nm] + 1;
- var tiles = this.getTiles( helpStart[0], helpStart[1], checkSize[0], checkSize[1] );
- var collide = tiles.some(function(tile)
- {
- if(tile[collCheck])
- {
- move[m] = tile[axis] - playerPos[m];
- if( mod === 1 ) { move[m] -= playerTileSize[m]; }
- else { move[m]++; }
- return true;
- }
- return false;
- },this);
- if(collide)
- {
- collided = true;
- break;
- }
- }
- moved[m] = move[m];
- }
- if(moved[0] < 0)
- {
- newDir = 3;
- }
- else if(moved[0] > 0)
- {
- newDir = 1;
- }
- if(moved[1] < 0)
- {
- newDir = 0;
- }
- else if(moved[1] > 0)
- {
- newDir = 2;
- }
- this.player.worldX += moved[0];
- this.player.worldY += moved[1];
- if(newDir !== this.player.dir)
- {
- this.player.dir = newDir;
- }
- },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement