script.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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.target ).addClass( "swipe" );
  28. var direction = {x: 0, y: 0};
  29. var start = event.swipestart.coords;
  30. var stop = event.swipestop.coords;
  31. var diff = {x: stop[0] - start[0], y: stop[1] - start[1]};
  32. direction.x = Math.abs(diff.x) > Math.abs(diff.y) ? diff.x > 0 ? 1 : -1 : 0;
  33. direction.y = Math.abs(diff.y) > Math.abs(diff.x) ? diff.y > 0 ? 1 : -1 : 0;
  34. console.log(direction);
  35. controller.turn_snake(direction);
  36. }
  37. });
  38. function resize() {
  39. var height = window.innerHeight - 50;
  40. var width = window.innerWidth;
  41. window.cell_size = 50;
  42. controller.grid_size.y = Math.floor(height / window.cell_size);
  43. controller.grid_size.x = Math.floor(width / window.cell_size);
  44. // if((height / minimum_cell_size) > maximum_grid_size){
  45. // cs = height / maximum_grid_size;
  46. // controller.grid_size.y = maximum_grid_size;
  47. // } else {
  48. // controller.grid_size.y = Math.floor(height / minimum_cell_size);
  49. // cs = minimum_cell_size;
  50. // }
  51. // if((width / minimum_cell_size) > maximum_grid_size){
  52. // window.cell_size = width / maximum_grid_size ;
  53. // controller.grid_size.x = maximum_grid_size;
  54. // } else {
  55. // controller.grid_size.x = Math.floor(width / minimum_cell_size);
  56. // window.cell_size = minimum_cell_size;
  57. // }
  58. controller.stage.canvas.width = width;
  59. controller.stage.canvas.height = height;
  60. }