Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. //
  2.  
  3. // Manages the visible state of a level.
  4.  
  5. //
  6.  
  7. function LevelState(levmgr)
  8.  
  9. {
  10.  
  11. this.cachedLevMgr = levmgr;
  12.  
  13.  
  14.  
  15. // Array of images to load for the level
  16.  
  17. this.imageFiles = [ "blank", "dirt", "bedrock", "ice", "stone" ];
  18.  
  19.  
  20.  
  21. // No currently loaded level yet.
  22.  
  23. this.layout = null;
  24.  
  25.  
  26.  
  27. // Load graphics
  28.  
  29. this.imageCache = new Array;
  30.  
  31. for(var i = 0; i < this.imageFiles.length; ++i)
  32.  
  33. {
  34.  
  35. this.imageCache[i] = new Image;
  36.  
  37. this.imageCache[i].src = "images/" + this.imageFiles[i] + ".png";
  38.  
  39. }
  40.  
  41.  
  42.  
  43. // Initialise cells for level layout.
  44.  
  45. this.levelStyleCache = new Array;
  46.  
  47. for(var y = 0; y < this.cachedLevMgr.numYCells; ++y)
  48.  
  49. {
  50.  
  51. this.levelStyleCache[y] = new Array;
  52.  
  53. for(var x = 0; x < this.cachedLevMgr.numXCells; ++x)
  54.  
  55. this.levelStyleCache[y][x] = null;
  56.  
  57. }
  58.  
  59.  
  60.  
  61. this.ClearLevel = function()
  62.  
  63. {
  64.  
  65. var topLevelDiv = document.getElementById("maindiv");
  66.  
  67. var levDiv = document.getElementById("levelDiv");
  68.  
  69. if(levDiv != null)
  70.  
  71. topLevelDiv.removeChild(levDiv);
  72.  
  73. }
  74.  
  75.  
  76.  
  77. this.Reset = function(layoutState)
  78.  
  79. {
  80.  
  81. // Does not do an array copy, just makes a reference to the player's level layout.
  82.  
  83. this.layout = layoutState;
  84.  
  85.  
  86.  
  87. // Clear the existing layout.
  88.  
  89. this.ClearLevel();
  90.  
  91.  
  92.  
  93. //
  94.  
  95. // Set positions of the eggs and grain for the current player.
  96.  
  97. //
  98.  
  99. var topLevelDiv = document.getElementById("maindiv");
  100.  
  101. var levDiv = document.createElement("DIV");
  102.  
  103. levDiv.id = "levelDiv";
  104.  
  105. levDiv.style.position = "absolute";
  106.  
  107. levDiv.style.width = this.cachedLevMgr.levelXMax<<1;
  108.  
  109. levDiv.style.height = this.cachedLevMgr.levelYMax;
  110.  
  111. levDiv.style.backgroundColor = "#000000";
  112.  
  113. topLevelDiv.appendChild(levDiv);
  114.  
  115.  
  116.  
  117. for(var y = 0; y < this.cachedLevMgr.numYCells; ++y)
  118.  
  119. {
  120.  
  121. var row = this.levelStyleCache[y];
  122.  
  123. var imageRow = layoutState[y];
  124.  
  125. for(var x = 0; x < this.cachedLevMgr.numXCells; ++x)
  126.  
  127. {
  128.  
  129. if(imageRow[x] == this.cachedLevMgr.blank)
  130.  
  131. continue;
  132.  
  133.  
  134.  
  135. var newElem = document.createElement("IMG");
  136.  
  137. newElem.src = this.imageCache[imageRow[x]].src;
  138.  
  139. newElem.style.position = "absolute";
  140.  
  141. newElem.style.left = x<<4;
  142.  
  143. newElem.style.top = y<<4;
  144.  
  145. levDiv.appendChild(newElem);
  146.  
  147. row[x] = newElem.style;
  148.  
  149. }
  150.  
  151. }
  152.  
  153. }
  154.  
  155.  
  156.  
  157. // Used to erase an egg or grain when collected by the farmer
  158.  
  159. this.ClearCellAt = function(x, y)
  160.  
  161. {
  162.  
  163. this.layout[y][x] = 0;
  164.  
  165. this.levelStyleCache[y][x].visibility = "hidden";
  166.  
  167. }
  168.  
  169.  
  170.  
  171. } // end of Level object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement