JS Metrix Life

random array play ground

by kirrrusha

SCSS

//plan - map
//vector
//grid
//Directions
//	for
// Critter
//elementFromChar
//charFromElement

//World
//	legend
//		{'#' 'o'}
//	map
//
//		world.toString
//		world.turn // grid.forEach
//		world.letAct
// 		world.checkDestination


//

JavaScript

var plan =
 ["############################",
  "#      #    #      o      ##",
  "#                          #",
  "#          #####           #",
  "##         #   #    ##     #",
  "###           ##     #     #",
  "#           ###      #     #",
  "#   ####                   #",
  "#   ##       o             #",
  "# o  #         o       ### #",
  "#    #                     #",
  "############################"
];

//vector
function Vector(x,y) {
	this.x = x;
	this.y = y;
}
Vector.prototype.plus = function(other){
	return new Vector(this.x + other.x, this.y + other.y);
};

//grids
function Grid(width, height){
	this.space = new Array(width * height);
	this.width = width;
	this.height = height;
}

Grid.prototype.set = function(vector, char){
	this.space[vector.x + vector.y * this.width] = char;
	//=> this.space[22] = "#"
};
Grid.prototype.get = function(vector){
	return this.space[vector.x + vector.y * this.width];
};
Grid.prototype.isInside = function(vector){
	return vector.x < this.width && vector.x >= 0 &&
				vector.y < this.height && vector.y >= 0;
};
Grid.prototype.forEach = function(fn, context){
	var critter = null;
	for(var y = 0; y < this.height; y++) {
		for(var x = 0; x < this.width; x++) {
			critter = this.space[x + y * this.width];
			if(critter !== null) {
				fn.call(context, critter, new Vector(x, y));
			}
		}
	}
};


//directions
var directions = {
  "n":  new Vector( 0, -1),
  "ne": new Vector( 1, -1),
  "e":  new Vector( 1,  0),
  "se": new Vector( 1,  1),
  "s":  new Vector( 0,  1),
  "sw": new Vector(-1,  1),
  "w":  new Vector(-1,  0),
  "nw": new Vector(-1, -1)
};

var directionNames = "n ne nw e w s se sw".split(" ");

function randomElement(array){
	//["n", "ne", "nw", "e", "w", "s", "se", "sw"]
	return array[Math.floor(Math.random() * array.length)];//=> "ne"
}

//BouncingCritter
function BouncingCritter(){
	this.direction = randomElement(directionNames);
}
BouncingCritter.prototype.act =...