Advertisement
PifyZ

Prototype

Jul 4th, 2012
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var JSPif = {
  2.     /* Globals variables */
  3.         _canvas: null,
  4.         _context: null,
  5.  
  6.     /* Canvas element */
  7.         Canvas: function(id, width, height) {
  8.             this.resize = function(width, height) {
  9.                 this.width = width;
  10.                 this.height = height;
  11.  
  12.                 this.canvas.width = this.width;
  13.                 this.canvas.height = this.height;
  14.             }
  15.  
  16.             this.add = function(whos) {
  17.                 for (var i in whos) {
  18.                     this.members.push(whos[i]);
  19.                 }
  20.             }
  21.  
  22.             this.draw = function() {
  23.                 for (var i in this.members) {
  24.                     // alert(this.members[i].draw);
  25.                     this.members[i].draw();
  26.                 }
  27.             }
  28.  
  29.             this.clear = function() {
  30.                 this.canvas.width = this.canvas.width;
  31.             }
  32.  
  33.             this.mouse = function(e) {
  34.                 var r = {
  35.                     x: e.clientX - canvas.offsetLeft,
  36.                     y: e.clientY - canvas.offsetTop,
  37.                 }
  38.  
  39.                 return r;
  40.             }
  41.  
  42.             this.id = id;
  43.             this.width = width;
  44.             this.height = height;
  45.  
  46.             this.members = [];
  47.  
  48.             this.canvas = document.getElementById(id),
  49.             this.context = this.canvas.getContext('2d'),
  50.             this.div = '#' + id;
  51.  
  52.             JSPif._canvas = this.canvas;
  53.             JSPif._context = this.context;
  54.  
  55.             this.offsetLeft = this.canvas.offsetLeft;
  56.             this.offsetTop = this.canvas.offsetTop;
  57.  
  58.             this.resize(this.width, this.height);
  59.         },
  60.  
  61.     /* Object's mom */
  62.         Mom: function(x, y) {
  63.             this.x = x;
  64.             this.y = y;
  65.         },
  66.  
  67.     /* Group objects */
  68.         Group: function() {
  69.             this.add = function(what) {
  70.                 this.members.push(what);
  71.             }
  72.  
  73.             this.draw = function() {
  74.                 for (var i in this.members) {
  75.                     this.members[i].draw();
  76.                 }
  77.             }
  78.  
  79.             this.kill = function (member) {
  80.                 member.solid = false;
  81.  
  82.                 member.alpha = 0;
  83.  
  84.                 member.speed.x = 0;
  85.                 member.speed.y = 0;
  86.  
  87.                 member.maxspeed.x = 0;
  88.                 member.maxspeed.y = 0;
  89.  
  90.                 member.alive = false;
  91.             }
  92.  
  93.             /*this.overlap = function (whats) {
  94.                 for (var i in this.members) {
  95.                     var member = this.members[i];
  96.  
  97.                     if (whats.members.length > 0) {
  98.                         for (var j in whats.members) {
  99.                             var what = whats.members[j];
  100.  
  101.                             if (
  102.                                 (member.x > 0) &&
  103.                                 (member.x + member.width > what.x) &&
  104.                                 (member.x < what.x + what.width) &&
  105.                                 (member.y > 0) &&
  106.                                 (member.y + member.height > what.y) &&
  107.                                 (member.y < what.y + what.height) &&
  108.                                 (member.solid == true)
  109.                             ) {
  110.                                 alert('COLISION');
  111.                                 return true;
  112.                             }
  113.                         }
  114.                     }
  115.                 }
  116.  
  117.                 return false;
  118.             }*/
  119.  
  120.             this.clear = function() {
  121.                 for (var i in this.members) {
  122.                     this.members[i].solid = false;
  123.                     this.members[i].alpha = 0;
  124.                 }
  125.             }
  126.  
  127.             this.members = [];
  128.         },
  129.  
  130.     /* Rectangle */
  131.         Rect: function(x, y, width, height) {
  132.             JSPif.Mom.call(this, x, y);
  133.  
  134.             this.draw = function() {
  135.                 JSPif._context.beginPath();
  136.                 JSPif._context.rect(this.x, this.y, this.width, this.height);
  137.                 JSPif._context.fillStyle = this.color;
  138.                 JSPif._context.globalAlpha = this.alpha;
  139.                 //JSPif._context.rotate(this.rotate * Math.PI / 180);
  140.                 JSPif._context.fill();
  141.                 //JSPif._context.restore();
  142.                 //JSPif._context.rotate(-this.rotate * Math.PI / 180);
  143.                 //JSPif._context.save();
  144.             }
  145.  
  146.             this.overlap = function (what) {
  147.                 if (
  148.                     (this.x > 0) &&
  149.                     (this.x + this.width > what.x) &&
  150.                     (this.x < what.x + what.width) &&
  151.                     (this.y > 0) &&
  152.                     (this.y + this.height > what.y) &&
  153.                     (this.y < what.y + what.height) &&
  154.                     (this.alive == true) &&
  155.                     (what.alive == true)
  156.                 ) {
  157.                     return true;
  158.                 }
  159.  
  160.                 return false;
  161.             }
  162.  
  163.             /* this.overlapscreen = function() {
  164.                 if (
  165.                     this.x <= 0 ||
  166.                     this.x + this.width >= canvas.width ||
  167.                     this.y <= 0 ||
  168.                     this.y + this.height >= canvas.height
  169.                 ) {
  170.                     return true;
  171.                 }
  172.  
  173.                 return false;
  174.             } */
  175.  
  176.             this.kill = function() {
  177.                 this.solid = false;
  178.  
  179.                 this.alpha = 0;
  180.  
  181.                 this.speed.x = 0;
  182.                 this.speed.y = 0;
  183.  
  184.                 this.maxspeed.x = 0;
  185.                 this.maxspeed.y = 0;
  186.  
  187.                 this.alive = false;
  188.             }
  189.  
  190.             this.width = width;
  191.             this.height = height;
  192.  
  193.             this.color = '#000000';
  194.             this.alpha = 1;
  195.  
  196.             this.rotate = 0;
  197.  
  198.             this.speed = {
  199.                 x: 0,
  200.                 y: 0
  201.             };
  202.  
  203.             this.maxspeed = {
  204.                 x: 4,
  205.                 y: 4
  206.             };
  207.  
  208.             this.alive = true;
  209.         },
  210.  
  211.     /* Text */
  212.         Text: function(x, y, text) {
  213.             JSPif.Mom.call(this, x, y);
  214.  
  215.             this.draw = function() {
  216.                 JSPif._context.font = this.font;
  217.                 JSPif._context.fillStyle = this.color;
  218.                 JSPif._context.textAlign = this.align;
  219.                 JSPif._context.fillText(this.text, this.x, this.y);
  220.             }
  221.  
  222.             this.text = text;
  223.  
  224.             this.font = '12px Arial';
  225.             text.align = 'left';
  226.  
  227.             this.color = '#000000';
  228.         },
  229.  
  230.     /* Keyboard keys */
  231.         keys: {
  232.             BACKSPACE: 8,
  233.             TAB:       9,
  234.             ENTER:     13,
  235.             SHIFT:     16,
  236.             CTRL:      17,
  237.             ALT:       18,
  238.             //pause/break: 19,
  239.             CAPS_LOCK: 20,
  240.             ESCAPE:    27,
  241.             //page up: 33,
  242.             SPACE:     32,
  243.             //page down: 34,
  244.             END:       35,
  245.             HOME:      36,
  246.             LEFT:      37,
  247.             UP:        38,
  248.             RIGHT:     39,
  249.             DOWN:      40,
  250.             //print screen: 44,
  251.             INSERT:    45,
  252.             DELETE:    46,
  253.             NUMBER0: 48,
  254.             NUMBER1: 49,
  255.             NUMBER2: 50,
  256.             NUMBER3: 51,
  257.             NUMBER4: 52,
  258.             NUMBER5: 53,
  259.             NUMBER6: 54,
  260.             NUMBER7: 55,
  261.             NUMBER8: 56,
  262.             NUMBER9: 57,
  263.             A:         65,
  264.             B:         66,
  265.             C:         67,
  266.             D:         68,
  267.             E:         69,
  268.             F:         70,
  269.             G:         71,
  270.             H:         72,
  271.             I:         73,
  272.             J:         74,
  273.             K:         75,
  274.             L:         76,
  275.             M:         77,
  276.             N:         78,
  277.             O:         79,
  278.             P:         80,
  279.             Q:         81,
  280.             R:         82,
  281.             S:         83,
  282.             T:         84,
  283.             U:         85,
  284.             V:         86,
  285.             W:         87,
  286.             X:         88,
  287.             Y:         89,
  288.             Z:         90,
  289.             //left window key: 91,
  290.             //right window key: 92,
  291.             //select key: 93,
  292.             NUMPAD0:   96,
  293.             NUMPAD1:   97,
  294.             NUMPAD2:   98,
  295.             NUMPAD3:   99,
  296.             NUMPAD4:   100,
  297.             NUMPAD5:   101,
  298.             NUMPAD6:   102,
  299.             NUMPAD7:   103,
  300.             NUMPAD8:   104,
  301.             NUMPAD9:   105,
  302.             MULTIPLY:  106,
  303.             ADD:       107,
  304.             SUBSTRACT: 109,
  305.             //decimal point: 110,
  306.             DIVIDE:    111,
  307.             F1:        112,
  308.             F2:        113,
  309.             F3:        114,
  310.             F4:        115,
  311.             F5:        116,
  312.             F6:        117,
  313.             F7:        118,
  314.             F8:        119,
  315.             F9:        120,
  316.             F10:       121,
  317.             F11:       122,
  318.             F12:       123,
  319.             //num lock: 144,
  320.             //scroll lock: 145,
  321.             //semi-colon: 186,
  322.             //equal sign: 107,
  323.             //comma: 188,
  324.             //dash: 189,
  325.             //period: 190,
  326.             //forward slash: 191,
  327.             //open bracket: 219,
  328.             //back slash: 220,
  329.             //close bracket: 221,
  330.             //single quote: 222
  331.         }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement