aboutsummaryrefslogtreecommitdiff
path: root/js/Cell.js
blob: cb734dd1f22668d742c1029a4d562bb20b8327d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// ECMAScript 5 strict mode
/* jshint globalstrict: true*/
/* global THREE,BOARD_SIZE, COLS,ROWS*/
"use strict";
(function () {
	var a = "a".charCodeAt(0);
	function Cell() {
		this.position = null;
		this.index    = null;
		this.x = null;
		this.y = null;
		var coordinates = null;
		if( arguments.length === 1) {

			if (typeof(arguments[0]) === "string" && arguments[0].match(/[a-h][1-8]/) ) {
				// position like "a1", "b4", "e7"
				this.position = arguments[0];
				coordinates = getCoordinatesFromPosition(
					this.position
				);
				this.x = coordinates.x;
				this.y = coordinates.y;
				this.index = this.x + this.y * COLS;
			} else if (arguments[0] >= 0 && arguments[0] < ROWS*COLS) {
				// array index
				this.index = arguments[0];
				coordinates = getCoordinatesFromIndex(
					this.index
				);
				this.x = coordinates.x;
				this.y = coordinates.y;
				this.position = getPositionFromCoordinates(
					this.x,this.y
				);
			}
		} else if(  arguments.length === 2 &&
					isValid(arguments[0],arguments[1]) ) {
			// x and y position (0-based
			this.x = arguments[0];
			this.y = arguments[1];
			this.index = this.x + this.y * COLS;
			this.position = getPositionFromCoordinates(
				this.x,this.y
			);
		} else {
			throw arguments[0];
		}
	}

	Cell.prototype.toString = function() {
		return this.position;
	};

	Cell.prototype.equals = function () {
		if(arguments.length === 1) {
			var cell = arguments[0];
			if(cell instanceof Cell) {
				// it's a Cell object
				return cell.position === this.position;
			} else {
				// it's a string position
				return cell === this.position;
			}
		} else if (arguments.length === 2) {
			// it's x,y coordinates
			return  this.x === arguments[0] &&
					this.y === arguments[1];
		}

	};

	Cell.prototype.getWorldPosition = function() {
		var cs = BOARD_SIZE / ROWS;
		var middle = (BOARD_SIZE-cs)/2;


		return new THREE.Vector3(
			this.x * cs - middle,
			0,
			(this.y * cs - middle)
		);
	};

	// private
	function getPositionFromCoordinates(x,y) {
		return String.fromCharCode(x+a)+(7-y+1);
	}

	function getCoordinatesFromPosition(position) {
		return {
			x: position.charCodeAt(0) - a,
			y: 7-(parseInt(position.charAt(1),10)-1)
		};
	}

	function getCoordinatesFromIndex(index) {
		return {
			x: index%COLS,
			y: Math.floor(index/COLS)  // have to flip y since 3D starts from bottom left
		};
	}

	function isValid() {
		if( arguments.length == 2) {
			var x = arguments[0];
			var y = arguments[1];

			return  x >= 0 && x < COLS &&
					y >= 0 && y < ROWS;
		}
		return false;
	}

	window.Cell = Cell;

})();