Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Manages the visible state of a level.
- //
- function LevelState(levmgr)
- {
- this.cachedLevMgr = levmgr;
- // Array of images to load for the level
- this.imageFiles = [ "blank", "dirt", "bedrock", "ice", "stone" ];
- // No currently loaded level yet.
- this.layout = null;
- // Load graphics
- this.imageCache = new Array;
- for(var i = 0; i < this.imageFiles.length; ++i)
- {
- this.imageCache[i] = new Image;
- this.imageCache[i].src = "images/" + this.imageFiles[i] + ".png";
- }
- // Initialise cells for level layout.
- this.levelStyleCache = new Array;
- for(var y = 0; y < this.cachedLevMgr.numYCells; ++y)
- {
- this.levelStyleCache[y] = new Array;
- for(var x = 0; x < this.cachedLevMgr.numXCells; ++x)
- this.levelStyleCache[y][x] = null;
- }
- this.ClearLevel = function()
- {
- var topLevelDiv = document.getElementById("maindiv");
- var levDiv = document.getElementById("levelDiv");
- if(levDiv != null)
- topLevelDiv.removeChild(levDiv);
- }
- this.Reset = function(layoutState)
- {
- // Does not do an array copy, just makes a reference to the player's level layout.
- this.layout = layoutState;
- // Clear the existing layout.
- this.ClearLevel();
- //
- // Set positions of the eggs and grain for the current player.
- //
- var topLevelDiv = document.getElementById("maindiv");
- var levDiv = document.createElement("DIV");
- levDiv.id = "levelDiv";
- levDiv.style.position = "absolute";
- levDiv.style.width = this.cachedLevMgr.levelXMax<<1;
- levDiv.style.height = this.cachedLevMgr.levelYMax;
- levDiv.style.backgroundColor = "#000000";
- topLevelDiv.appendChild(levDiv);
- for(var y = 0; y < this.cachedLevMgr.numYCells; ++y)
- {
- var row = this.levelStyleCache[y];
- var imageRow = layoutState[y];
- for(var x = 0; x < this.cachedLevMgr.numXCells; ++x)
- {
- if(imageRow[x] == this.cachedLevMgr.blank)
- continue;
- var newElem = document.createElement("IMG");
- newElem.src = this.imageCache[imageRow[x]].src;
- newElem.style.position = "absolute";
- newElem.style.left = x<<4;
- newElem.style.top = y<<4;
- levDiv.appendChild(newElem);
- row[x] = newElem.style;
- }
- }
- }
- // Used to erase an egg or grain when collected by the farmer
- this.ClearCellAt = function(x, y)
- {
- this.layout[y][x] = 0;
- this.levelStyleCache[y][x].visibility = "hidden";
- }
- } // end of Level object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement