Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var grid = 64;
- var max = 50; //map is 50by50
- //set up camera
- camera = {};
- camera.centerX = canvas.width / 2;
- camera.centerY = canvas.height / 2;
- camera.x = toGrid(canvas.width/2,0,grid); //map grid x
- camera.y = toGrid(canvas.height/2,0,grid); //map grid y
- camera.offsetX = toWorld(camera.x,0,grid); //pixel x
- camera.offsetY = toWorld(camera.y,0,grid); //pixel y
- function fixBoundries(x,y){
- if(x > 0 || y > 0){ //top left corner of the map works fine
- return false;
- }
- var mapRightMax = (max * grid);//map is 50 by 50 grids of 64px
- //check right max to camera center some how
- return true;
- }
- function update(e){ //called from a mouse event
- var new_mousePos = relMouseCoords(e,this);
- var difx = new_mousePos.x - mousePos.x,
- dify = new_mousePos.y - mousePos.y;
- if(fixBoundries(camera.offsetX+difx, camera.offsetY+dify)){
- camera.offsetX += difx;
- camera.offsetY += dify;
- }
- camera.x = world2Grid(canvas.centerX,camera.offsetX,grid);//get grid tile x
- camera.y = world2Grid(canvas.centerY,camera.offsetY,grid);//get grid tile y
- mousePos.x = new_mousePos.x;
- mousePos.y = new_mousePos.y;
- }
- function toGrid(position,offset,grid){
- return (position*grid) + offset;
- }
- function toWorld(position,offset,grid){
- return Math.floor((position - offset) / grid);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement