Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //animation class
- function tween(opts) {
- this.opts = opts;
- }
- function graphicsEngine(graphics) {
- this.graphics = graphics;
- this.w = graphics.getWidth();
- this.h = graphics.getHeight();
- this.centerX = this.w / 2;
- this.centerY = this.h / 2;
- this.stage = {};
- this.INTERVAL = 1000 / 10;
- }
- graphicsEngine.prototype.addToStage = function (id, element, tween) {
- if (tween) {
- element.tween = tween;
- }
- this.stage[id] = element;
- };
- graphicsEngine.prototype.addTweenToPlayer = function (id, tween) {
- this.stage[id].tween = tween;
- };
- graphicsEngine.prototype.removeFromStage = function (id) {
- delete this.stage[id];
- };
- graphicsEngine.prototype.run = function () {
- this.intervalID = setInterval(this.tick.bind(this), this.INTERVAL);
- };
- graphicsEngine.prototype.tick = function () {
- this.graphics.clear();
- this.update();
- this.paint();
- this.graphics.flip();
- };
- graphicsEngine.prototype.update = function () {
- //this.graphics.clear();
- for (var name in this.stage) {
- if (this.stage.hasOwnProperty(name)) {
- // console.log(this.stage[name]);
- var player = this.stage[name];
- //player.x ++;
- if (player.tween) {
- //there will be one prop per thing its tweening
- for (var tname in player.tween.opts) {
- var cur = player[tname];
- if(!player.tween.opts[tname]['starttime']){
- player.tween.opts[tname]['starttime'] = Date.now();
- }
- var from = player.tween.opts[tname]['from'];
- var to = player.tween.opts[tname]['to'];
- var loop = player.tween.opts[tname]['loop'];
- if (cur <= to && from < to) {
- cur++;
- //cur = newcur;
- player[tname] = cur;
- }
- else if (cur >= to && from < to) {
- if (loop) {
- player.tween.opts[tname]['to'] = from;
- player.tween.opts[tname]['from'] = to;
- delete player.tween.opts[tname]['starttime'];
- } else {
- delete player.tween.opts[tname];
- }
- }
- else if (cur >= to && from > to) {
- cur = cur - 1;
- player[tname] = cur;
- }
- else if (cur <=to && from > to) {
- if (loop) {
- player.tween.opts[tname]['to'] = from;
- player.tween.opts[tname]['from'] = to;
- delete player.tween.opts[tname]['starttime'];
- } else {
- delete player.tween.opts[tname];
- }
- }
- }
- }
- //console.log(player);
- this.stage[name] = player;
- }
- }
- };
- graphicsEngine.prototype.paint = function () {
- for (var name in this.stage) {
- if (this.stage.hasOwnProperty(name)) {
- // console.log(this.stage[name]);
- var player = this.stage[name];
- switch (player.type) {
- case 'circle':
- this.renderCircle(player.x, player.y, player.rad);
- break;
- case 'fillcircle':
- this.renderFillCircle(player.x, player.y, player.rad);
- break;
- case 'text':
- this.renderText(player.x, player.y, player.str);
- break;
- case 'rect':
- this.renderRect(player.x1, player.y1, player.x2, player.y2);
- break;
- case 'fillrect':
- this.renderFillRect(player.x1, player.y1, player.x2, player.y2);
- break;
- default:
- break;
- }
- }
- }
- };
- graphicsEngine.prototype.renderText = function (x, y, str) {
- this.graphics.drawString(str,x, y);
- };
- graphicsEngine.prototype.renderCircle = function (x, y, rad) {
- this.graphics.drawCircle(x, y, rad);
- };
- graphicsEngine.prototype.renderFillCircle = function (x, y, rad) {
- this.graphics.fillCircle(x, y, rad);
- };
- graphicsEngine.prototype.renderRect = function (x1, y1, x2,y2) {
- this.graphics.drawRect(x1, y1, x2, y2);
- };
- graphicsEngine.prototype.renderFillRect = function (x1, y1, x2,y2) {
- this.graphics.fillRect(x1, y1, x2, y2);
- };
- graphicsEngine.prototype.log = function (string) {
- console.log(string);
- };
- //
- e = null;
- function start(){
- g.clear();
- e = new graphicsEngine(g);
- t3 = new tween({x2:{from: 20,to:50,loop:true}});
- e.addToStage("rect",{type:'fillrect',x1:10,y1:10,x2:20,y2:20},t3);
- e.run();
- }
- // I2C
- I2C1.setup({scl:14,sda:12});
- var g = require("SSD1306").connect(I2C1, start);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement