script.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. (function(){
  2. var cellSize = 1;
  3. var app = angular.module("app", ["views"]);
  4. var views = angular.module("views", []);
  5. views.factory("Particle", function(){
  6. function Particle(modelObject){
  7. this.particle = new createjs.Shape();
  8. this.particle.graphics.beginFill("#FFE989").drawCircle(0, 0, modelObject.radius);
  9. this.particle.animationTarget = modelObject.target;
  10. this.particle.gridPosition = modelObject.position;
  11. this.animate();
  12. }
  13. Particle.prototype = {
  14. addToStage: function(stage){
  15. stage.addChild(this.particle);
  16. },
  17. removeFromStage: function(stage){
  18. stage.removeChild(this.particle);
  19. },
  20. get x () {
  21. return this.particle.gridPosition.x;
  22. },
  23. set x (val) {
  24. this.particle.x = cellSize * val + cellSize / 2;
  25. },
  26. get y () {
  27. return this.particle.gridPosition.y;
  28. },
  29. set y (val) {
  30. this.particle.y = cellSize * val + cellSize / 2;
  31. },
  32. animate: function(){
  33. if(this.particle.animationTarget){
  34. console.log("Will animate");
  35. createjs.Tween.get(this.particle, {loop: true}).to({
  36. x: this.particle.animationTarget.x * cellSize + cellSize / 2,
  37. y: this.particle.animationTarget.y * cellSize + cellSize / 2
  38. }, this.particle.animationDuration);
  39. }
  40. }
  41. }
  42. return (Particle);
  43. })
  44. app.service('modelService', function(){
  45. this.prop = function(){
  46. return 100;
  47. }();
  48. });
  49. app.directive('gameControl', ['modelService', function(modelService){
  50. return {
  51. restrict: 'E',
  52. templateUrl: 'templates/game-control.html',
  53. controller: ['$scope', function($scope){
  54. $scope.score = modelService.prop;
  55. }],
  56. controllerAs: 'control'
  57. }
  58. }]);
  59. app.directive('screen', ['modelService', 'Particle', function(modelService, Particle){
  60. return {
  61. restrict: 'E',
  62. replace: true,
  63. scope: {},
  64. template: '<canvas id="screen" width="100" height="100"></canvas>',
  65. link: function(scope, element, attribute){
  66. var manifest, loader, particle;
  67. drawGame();
  68. function drawGame(){
  69. if(scope.stage){
  70. scope.stage.autoClear = true;
  71. scope.stage.removeAllChildren();
  72. scope.stage.update();
  73. } else {
  74. scope.stage = new createjs.Stage(element[0]);
  75. }
  76. manifest = [{src: 'README.md', id: 'readme'}];
  77. loader = new createjs.LoadQueue(false);
  78. loader.addEventListener("complete", handleComplete);
  79. loader.loadManifest(manifest, true, "/");
  80. }
  81. function handleComplete(){
  82. particle = new Particle({
  83. radius: 10,
  84. target: {x: 5, y: 1},
  85. duration: 200
  86. });
  87. particle.addToStage(scope.stage);
  88. createjs.Ticker.timingMode = createjs.Ticker.RAF;
  89. createjs.Ticker.addEventListener("tick", scope.stage);
  90. }
  91. }
  92. }
  93. }])
  94. }());