script.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. $( document ).on( "mobileinit", function() {
  26. $.mobile.loader.prototype.options.disabled = true;
  27. });
  28. // Callback function references the event target and adds the 'swipe' class to it
  29. function swipeHandler( event ){
  30. // $( event.target ).addClass( "swipe" );
  31. var direction = {x: 0, y: 0};
  32. var start = event.swipestart.coords;
  33. var stop = event.swipestop.coords;
  34. var diff = {x: stop[0] - start[0], y: stop[1] - start[1]};
  35. direction.x = Math.abs(diff.x) > Math.abs(diff.y) ? diff.x > 0 ? 1 : -1 : 0;
  36. direction.y = Math.abs(diff.y) > Math.abs(diff.x) ? diff.y > 0 ? 1 : -1 : 0;
  37. console.log(direction);
  38. controller.turn_snake(direction);
  39. }
  40. });
  41. function resize() {
  42. var height = window.innerHeight - 50;
  43. var width = window.innerWidth;
  44. window.cell_size = 50;
  45. controller.grid_size.y = Math.floor(height / window.cell_size);
  46. controller.grid_size.x = Math.floor(width / window.cell_size);
  47. // if((height / minimum_cell_size) > maximum_grid_size){
  48. // cs = height / maximum_grid_size;
  49. // controller.grid_size.y = maximum_grid_size;
  50. // } else {
  51. // controller.grid_size.y = Math.floor(height / minimum_cell_size);
  52. // cs = minimum_cell_size;
  53. // }
  54. // if((width / minimum_cell_size) > maximum_grid_size){
  55. // window.cell_size = width / maximum_grid_size ;
  56. // controller.grid_size.x = maximum_grid_size;
  57. // } else {
  58. // controller.grid_size.x = Math.floor(width / minimum_cell_size);
  59. // window.cell_size = minimum_cell_size;
  60. // }
  61. controller.stage.canvas.width = width;
  62. controller.stage.canvas.height = height;
  63. }