Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*
  3. CLASS FACTORY
  4. ============================================
  5. Put this in it's own file.
  6. Include the script in your HTML to define Classes.
  7. See testing examples below.
  8. =============================================
  9. */
  10. alert('test');
  11.                
  12. // The Class Factory
  13. function Class(  pSuper,pProps)
  14. {
  15.   function Class(){}
  16.  
  17.  
  18.   if(pSuper)
  19.   {
  20.      Class.prototype = new pSuper();
  21.      Class.prototype.super = new pSuper();
  22.   }
  23.   else
  24.   {
  25.      Class.prototype = new Object();
  26.      Class.prototype.super = new Object();  
  27.   }
  28.  
  29.   for(name in pProps)
  30.   {
  31.    Class.prototype[name] = pProps[name];  
  32.   }
  33.   Class.prototype['window'] = window;
  34.   Class.prototype['document'] = document;
  35.  
  36.  
  37.   return Class;
  38.  
  39. }
  40. //  END OF CLASS FACTORY ===============================
  41. //======================================================
  42.  
  43. // useful things
  44. var id = function (pId){
  45.                     return document.getElementById(pId);
  46.                 }
  47. /**
  48.  * Created by benjamin-rea on 28/08/2014.
  49.  */
  50.  
  51. Player = Class ( Object, {
  52.     model : undefined,
  53.     view : undefined
  54. });
  55.  
  56. PlayerModel = Class ( Player, {
  57.     name : "",
  58.     inventory : new Array(3)
  59. });
  60.  
  61. PlayerView = Class ( Player, {
  62.     canvas : id("textCanvas"),
  63.     ctx : id("textCanvas").getContext("2d"),
  64.     drawX : 0,
  65.     drawY : 0,
  66.     clear : function () {
  67.         this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
  68.     },
  69.     draw : function () {
  70.         this.clear();
  71.  
  72.         console.log("PlayerView.draw");
  73.  
  74.         console.log("Player " + this.model.name + " has " + this.model.inventory.length + " items.");
  75.     },
  76.      setModel : function (pModel) { this.model = pModel; }
  77. });
  78.  
  79. function createPlayer() {
  80.     var newPlayer = new Player();
  81.     newPlayer.model = new PlayerModel();
  82.     newPlayer.view = new PlayerView();
  83.     newPlayer.view.setModel(newPlayer.model);
  84.     return newPlayer;
  85. }
  86.  
  87.  
  88. var aPlayer = createPlayer();
  89.  
  90. aPlayer.view.draw();
  91. console.log(aPlayer.model.inventory.length);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement