aboutsummaryrefslogtreecommitdiff
path: root/assets/js/global.js
blob: dd31ca880236898e39e90f7579f2822f87d5704b (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
window.onload = function () {

	window.debug = true;	// set to true iff you want console log output
	
	var canvas = document.getElementById("main-canvas");
	window.ctx = canvas.getContext("2d");
	// canvas.style.display = "none";

	window.CANVAS_X_OFFSET = 200;
	window.CANVAS_Y_OFFSET = 200;

	draw();

	//handleKeys();
}


function draw() {
	var coords = generateCubeConfig();

	consOut(coords);

	placeDice(coords[0] , coords[1]);

	// draw the cube
	drawDice();
		// generate the cube config
			// get the initial position
			// get the initial face
			// get the final position
			// get the final face

	//draw the board  3 x 3
	drawBoard();
}


function generateCubeConfig() {
	// generate a random x between 1-3 incl.
	var x = Math.floor((Math.random()) * 3 + 1);
	// generate a random y between 1-3 incl.
	var y = Math.floor((Math.random()) * 3 + 1);

	consOut(x);

	return [x,y];
}

function drawBoard() {

	for (var i = 0; i < 3; i++) {
		for (var j = 0; j < 3; j++) {
			window.ctx.moveTo(0, 200*j);
			window.ctx.lineTo(600, 200*j);
			window.ctx.stroke();

			window.ctx.moveTo(200*i, 0);
			window.ctx.lineTo(200*i, 600);
			window.ctx.stroke();

			var left = 0;
			for (var a = 0; a < 3; a++) {
				for (var b = 0; b < 3; b += 2) {
					startX = b * 200;
					if (a % 2 == 0) {
						startX = (b+1) * 200;
						window.ctx.fillStyle = "#FF0000";		//red 
					}
					window.ctx.fillRect(startX + left, (a * 200) , 200, 200);
				}
			}
		}
	}

	if (window.debug) {
		console.log("finished drawing game board");
	}
}

function drawDice() {
	//renderer
	var canvas = document.getElementById("cube-canvas");
	var renderer = new THREE.WebGLRenderer({ canvas: canvas, alpha: true, antialias: true });
	renderer.setSize(200, 200);
	document.body.appendChild(renderer.domElement);

	var scene = new THREE.Scene();		//scene

	// var geometry = new THREE.BoxGeometry(100, 100, 100);
	// var material = new THREE.MeshBasicMaterial({color: 0xfffff, wireframe: true});

	var materials = [];
	for (var i = 1; i <= 6; i++) {
		var img = new Image();
		
		switch (i) {
			case 1:
				img.src = 'assets/images/faces/3.png';
				break;
			case 2:
				img.src = 'assets/images/faces/4.png';
				break;
			case 3:
				img.src = 'assets/images/faces/5.png';
				break;
			case 4:
				img.src = 'assets/images/faces/2.png';
				break;
			case 5:
				img.src = 'assets/images/faces/1.png';
				break;
			case 6:
				img.src = 'assets/images/faces/6.png';
				break;
		}

		var tex = new THREE.Texture(img);
		img.tex = tex;
		img.onload = function() {
			this.tex.needsUpdate = true;
		};
		var mat = new THREE.MeshBasicMaterial({color: 0xffffff, map: tex});
		materials.push(mat);
	}

	var cubeGeo = new THREE.BoxGeometry(100,100,100,1,1,1);
	var cube = new THREE.Mesh(cubeGeo, new THREE.MeshFaceMaterial( materials ));

	// var cube = new THREE.Mesh(geometry, material);
	cube.rotation.y = Math.PI * 45 / 180;
	scene.add(cube);

	//camera
	var camera = new THREE.PerspectiveCamera(45, 1, 0.1, 10000);
	camera.position.y = 160;
	camera.position.z = 200;
	camera.lookAt(cube.position);

	scene.add(camera);

	var clock = new THREE.Clock();

	var render = function() {
		requestAnimationFrame(render);
		cube.rotation.y -= clock.getDelta();
		renderer.render(scene, camera);
	};

	render();
}

function placeDice(x, y) {

	var x_inc = (x-1) * CANVAS_X_OFFSET;
	var y_inc = (y-1) * CANVAS_Y_OFFSET;

	// get the current position
	var position = $('#cube-canvas').position();
	// adjust dice position
	$('#cube-canvas').css({ 
		left: position.left + x_inc, 
		top: position.top + y_inc
	});
	
	consOut('x: ' + position.left + ' | y: ' + position.top); //initial position
	consOut("dice placed!");
}

function handleKeys() {
	$(document).keydown(function(e) {
		switch (e.which) {
			case 37:
				placeDice(3,3);
			case 38:
				break;
			case 39:
				break;
			case 40:
				break;
			default: return;
		}
		e.preventDefault();
	});
}

// print mssg to console out only if debuging is set to true
function consOut(mssg) {
	if (window.debug) {
		console.log(mssg);
	}
}

function swap(arr, a, b) {
	var temp = arr[b];
	arr[b] = arr[a];
	arr[a] = temp;
	return arr;
}