script.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var stage, controller;
  2. var minimum_cell_size = 30;
  3. var maximum_grid_size = 20;
  4. function init() {
  5. // Do not use this for now...
  6. // createjs.MotionGuidePlugin.install();
  7. controller = new Controller;
  8. resize();
  9. controller.start_game();
  10. /*var p2 = new Particle({x: 3, y: 3});
  11. p2.start_time = createjs.Ticker.getTime();
  12. p2.target = {
  13. time: p2.start_time + 1000,
  14. x: 10,
  15. y: 7
  16. }
  17. var p2v = new ParticleView(p2);
  18. controller.add_view(p2v);*/
  19. }
  20. $(function(){
  21. // Bind the swipeHandler callback function to the swipe event on div.box
  22. $( "#demoCanvas" ).on( "swipe", swipeHandler );
  23. $.event.special.swipe.horizontalDistanceThreshold = 1;
  24. $.event.special.swipe.verticalDistanceThreshold = 1000;
  25. // Callback function references the event target and adds the 'swipe' class to it
  26. function swipeHandler( event ){
  27. event.preventDefault();
  28. // $( event.target ).addClass( "swipe" );
  29. var direction = {x: 0, y: 0};
  30. var start = event.swipestart.coords;
  31. var stop = event.swipestop.coords;
  32. var diff = {x: stop[0] - start[0], y: stop[1] - start[1]};
  33. direction.x = Math.abs(diff.x) > Math.abs(diff.y) ? diff.x > 0 ? 1 : -1 : 0;
  34. direction.y = Math.abs(diff.y) > Math.abs(diff.x) ? diff.y > 0 ? 1 : -1 : 0;
  35. console.log(direction);
  36. controller.turn_snake(direction);
  37. }
  38. });
  39. function resize() {
  40. var height = window.innerHeight - 50;
  41. var width = window.innerWidth;
  42. window.cell_size = 50;
  43. controller.grid_size.y = Math.floor(height / window.cell_size);
  44. controller.grid_size.x = Math.floor(width / window.cell_size);
  45. // if((height / minimum_cell_size) > maximum_grid_size){
  46. // cs = height / maximum_grid_size;
  47. // controller.grid_size.y = maximum_grid_size;
  48. // } else {
  49. // controller.grid_size.y = Math.floor(height / minimum_cell_size);
  50. // cs = minimum_cell_size;
  51. // }
  52. // if((width / minimum_cell_size) > maximum_grid_size){
  53. // window.cell_size = width / maximum_grid_size ;
  54. // controller.grid_size.x = maximum_grid_size;
  55. // } else {
  56. // controller.grid_size.x = Math.floor(width / minimum_cell_size);
  57. // window.cell_size = minimum_cell_size;
  58. // }
  59. controller.stage.canvas.width = width;
  60. controller.stage.canvas.height = height;
  61. }