Skip to content

Instantly share code, notes, and snippets.

View fov_demo.js
// 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));
View lightsource.js
// 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],
@ebonneville
ebonneville / floodfill.js
Created Oct 24, 2012
An algorithm used to handle unconnected regions of a map.
View floodfill.js
// 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") {
@ebonneville
ebonneville / gist:3949171
Created Oct 24, 2012
Basic cellular automata algorithm
View gist:3949171
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.
@ebonneville
ebonneville / vector2.js
Created Oct 22, 2012
Basic position holding class.
View vector2.js
// 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) {
@ebonneville
ebonneville / display.js
Created Oct 22, 2012
Simple console-like text rendering class.
View display.js
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);
View displaydemo.js
// 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));
}
View displaydemo.js
// 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));
}
@ebonneville
ebonneville / cloneObject.js
Created May 22, 2012
Clones an object and returns it.
View cloneObject.js
// 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) {
@ebonneville
ebonneville / cloneObject.js
Created May 22, 2012
Clone an object with Mootools
View cloneObject.js
var bob = {
name: "Bob",
age: 32
};
var bill = Object.clone(bob);
bill.name = "Bill";
console.log(bob);
console.log(bill);