This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this demo uses a class called Vector2, which can be found here: https://gist.github.com/3929297 | |
// create a light source at position (9, 10) | |
var lightSource = new LightSource(new Vector2(9, 10), 10); | |
// move the light source to position (10, 10); | |
lightSource.update(new Vector2(10, 10)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// uses shadowcasting to calculate lighting at specified position | |
function LightSource(position, radius) { | |
this.tiles = []; | |
this.position = position; | |
this.radius = radius; | |
// multipliers for transforming coordinates into other octants. | |
this.mult = [ | |
[1, 0, 0, -1, -1, 0, 0, 1], | |
[0, 1, -1, 0, 0, -1, 1, 0], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this variable will be used to store the various unconnected caverns | |
var caverns = []; | |
// begin looping through the map | |
for(var x = 1; x<this.size.x; x++) { | |
for(var y = 1; y<this.size.y; y++) { | |
var cavern = []; | |
var totalCavern = []; | |
if(this.data[x][y].type != "dirty floor") { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Scatter wall blocks across the level at a 30-40% saturation level (I've found that 39% is ideal). | |
2. Iterate through all the tiles on the map four times: | |
- For each tile in each iteration, set the tile to be floor. | |
- Then, calculate the number of wall tiles within both one square of the original tile (these are neighbors) and two squares (these tiles are nearby). Store these numbers. | |
- If the number of neighbors for the tile is greater than or equal to 5, or there are less than two wall tiles nearby, set this tile to be a wall. | |
- If it is not a wall already, leave this tile as a floor tile. | |
3. Iterate through all the tiles on the map three more times: | |
- For each tile in each iteration, set the tile to be floor. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// basic position class, holds x and y coordinates and utility functions | |
function Vector2(x, y) { | |
this.x = x; | |
this.y = y; | |
this.add = function(other) { | |
return new Vector2(this.x + other.x, this.y + other.y); | |
}; | |
this.distance = function(pos) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Display(size, offset, openInNewWindow) { | |
var display = this; | |
if(offset) { | |
this.offset = offset; | |
} else { | |
this.offset = new Vector2(0, 0); | |
} | |
this.tileSize = new Vector2(10, 16); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a display for console-like rendering | |
display = new Display(new Vector2(80, 25), new Vector2(10, 35), true); | |
// let's draw the classic @ sign: | |
display.ch("@", new Vector2(10, 10)); | |
// maybe fake a few walls | |
for(var x = 0; x<10; x++) { | |
display.ch("#", new Vector2(x, 0)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a display for console-like rendering | |
display = new Display(new Vector2(80, 25), new Vector2(10, 35)); | |
// let's draw the classic @ sign: | |
display.ch("@", new Vector2(10, 10)); | |
// maybe fake a few walls | |
for(var x = 0; x<10; x++) { | |
display.ch("#", new Vector2(x, 0)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// recursive function to clone an object. If a non object parameter | |
// is passed in, that parameter is returned and no recursion occurs. | |
function cloneObject(obj) { | |
if (obj === null || typeof obj !== 'object') { | |
return obj; | |
} | |
var temp = obj.constructor(); // give temp the original obj's constructor | |
for (var key in obj) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bob = { | |
name: "Bob", | |
age: 32 | |
}; | |
var bill = Object.clone(bob); | |
bill.name = "Bill"; | |
console.log(bob); | |
console.log(bill); |
NewerOlder