Advertisement
Guest User

Untitled

a guest
Dec 25th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. var grid = 64;
  2. var max = 50; //map is 50by50
  3. //set up camera
  4. camera = {};
  5. camera.centerX = canvas.width / 2;
  6. camera.centerY = canvas.height / 2;
  7. camera.x = toGrid(canvas.width/2,0,grid); //map grid x
  8. camera.y = toGrid(canvas.height/2,0,grid); //map grid y
  9. camera.offsetX = toWorld(camera.x,0,grid); //pixel x
  10. camera.offsetY = toWorld(camera.y,0,grid); //pixel y
  11.  
  12. function fixBoundries(x,y){
  13. if(x > 0 || y > 0){ //top left corner of the map works fine
  14. return false;
  15. }
  16.  
  17. var mapRightMax = (max * grid);//map is 50 by 50 grids of 64px
  18. //check right max to camera center some how
  19. return true;
  20. }
  21.  
  22. function update(e){ //called from a mouse event
  23. var new_mousePos = relMouseCoords(e,this);
  24. var difx = new_mousePos.x - mousePos.x,
  25. dify = new_mousePos.y - mousePos.y;
  26. if(fixBoundries(camera.offsetX+difx, camera.offsetY+dify)){
  27. camera.offsetX += difx;
  28. camera.offsetY += dify;
  29. }
  30. camera.x = world2Grid(canvas.centerX,camera.offsetX,grid);//get grid tile x
  31. camera.y = world2Grid(canvas.centerY,camera.offsetY,grid);//get grid tile y
  32. mousePos.x = new_mousePos.x;
  33. mousePos.y = new_mousePos.y;
  34. }
  35.  
  36. function toGrid(position,offset,grid){
  37. return (position*grid) + offset;
  38. }
  39. function toWorld(position,offset,grid){
  40. return Math.floor((position - offset) / grid);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement