Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. //animation class
  2. function tween(opts) {
  3. this.opts = opts;
  4. }
  5. function graphicsEngine(graphics) {
  6. this.graphics = graphics;
  7. this.w = graphics.getWidth();
  8. this.h = graphics.getHeight();
  9. this.centerX = this.w / 2;
  10. this.centerY = this.h / 2;
  11. this.stage = {};
  12. this.INTERVAL = 1000 / 10;
  13. }
  14.  
  15.  
  16. graphicsEngine.prototype.addToStage = function (id, element, tween) {
  17. if (tween) {
  18. element.tween = tween;
  19. }
  20. this.stage[id] = element;
  21. };
  22. graphicsEngine.prototype.addTweenToPlayer = function (id, tween) {
  23. this.stage[id].tween = tween;
  24. };
  25. graphicsEngine.prototype.removeFromStage = function (id) {
  26. delete this.stage[id];
  27. };
  28. graphicsEngine.prototype.run = function () {
  29. this.intervalID = setInterval(this.tick.bind(this), this.INTERVAL);
  30. };
  31. graphicsEngine.prototype.tick = function () {
  32. this.graphics.clear();
  33. this.update();
  34. this.paint();
  35. this.graphics.flip();
  36. };
  37. graphicsEngine.prototype.update = function () {
  38. //this.graphics.clear();
  39.  
  40. for (var name in this.stage) {
  41. if (this.stage.hasOwnProperty(name)) {
  42. // console.log(this.stage[name]);
  43. var player = this.stage[name];
  44. //player.x ++;
  45.  
  46. if (player.tween) {
  47. //there will be one prop per thing its tweening
  48. for (var tname in player.tween.opts) {
  49. var cur = player[tname];
  50.  
  51. if(!player.tween.opts[tname]['starttime']){
  52. player.tween.opts[tname]['starttime'] = Date.now();
  53. }
  54. var from = player.tween.opts[tname]['from'];
  55. var to = player.tween.opts[tname]['to'];
  56. var loop = player.tween.opts[tname]['loop'];
  57.  
  58.  
  59. if (cur <= to && from < to) {
  60.  
  61.  
  62. cur++;
  63. //cur = newcur;
  64. player[tname] = cur;
  65.  
  66. }
  67. else if (cur >= to && from < to) {
  68.  
  69. if (loop) {
  70. player.tween.opts[tname]['to'] = from;
  71. player.tween.opts[tname]['from'] = to;
  72. delete player.tween.opts[tname]['starttime'];
  73. } else {
  74. delete player.tween.opts[tname];
  75. }
  76.  
  77. }
  78.  
  79. else if (cur >= to && from > to) {
  80. cur = cur - 1;
  81. player[tname] = cur;
  82.  
  83. }
  84. else if (cur <=to && from > to) {
  85.  
  86. if (loop) {
  87. player.tween.opts[tname]['to'] = from;
  88. player.tween.opts[tname]['from'] = to;
  89. delete player.tween.opts[tname]['starttime'];
  90. } else {
  91. delete player.tween.opts[tname];
  92. }
  93.  
  94. }
  95.  
  96.  
  97.  
  98. }
  99. }
  100. //console.log(player);
  101. this.stage[name] = player;
  102. }
  103. }
  104.  
  105. };
  106. graphicsEngine.prototype.paint = function () {
  107. for (var name in this.stage) {
  108. if (this.stage.hasOwnProperty(name)) {
  109. // console.log(this.stage[name]);
  110. var player = this.stage[name];
  111. switch (player.type) {
  112. case 'circle':
  113. this.renderCircle(player.x, player.y, player.rad);
  114. break;
  115. case 'fillcircle':
  116. this.renderFillCircle(player.x, player.y, player.rad);
  117. break;
  118. case 'text':
  119. this.renderText(player.x, player.y, player.str);
  120. break;
  121. case 'rect':
  122. this.renderRect(player.x1, player.y1, player.x2, player.y2);
  123. break;
  124. case 'fillrect':
  125. this.renderFillRect(player.x1, player.y1, player.x2, player.y2);
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. }
  132.  
  133. };
  134. graphicsEngine.prototype.renderText = function (x, y, str) {
  135. this.graphics.drawString(str,x, y);
  136. };
  137. graphicsEngine.prototype.renderCircle = function (x, y, rad) {
  138. this.graphics.drawCircle(x, y, rad);
  139. };
  140. graphicsEngine.prototype.renderFillCircle = function (x, y, rad) {
  141. this.graphics.fillCircle(x, y, rad);
  142. };
  143. graphicsEngine.prototype.renderRect = function (x1, y1, x2,y2) {
  144. this.graphics.drawRect(x1, y1, x2, y2);
  145. };
  146. graphicsEngine.prototype.renderFillRect = function (x1, y1, x2,y2) {
  147. this.graphics.fillRect(x1, y1, x2, y2);
  148. };
  149.  
  150.  
  151. graphicsEngine.prototype.log = function (string) {
  152. console.log(string);
  153. };
  154. //
  155.  
  156.  
  157.  
  158. e = null;
  159. function start(){
  160. g.clear();
  161. e = new graphicsEngine(g);
  162. t3 = new tween({x2:{from: 20,to:50,loop:true}});
  163. e.addToStage("rect",{type:'fillrect',x1:10,y1:10,x2:20,y2:20},t3);
  164. e.run();
  165. }
  166.  
  167.  
  168.  
  169. // I2C
  170. I2C1.setup({scl:14,sda:12});
  171. var g = require("SSD1306").connect(I2C1, start);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement