-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgrid.html
More file actions
246 lines (211 loc) · 6.42 KB
/
grid.html
File metadata and controls
246 lines (211 loc) · 6.42 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<html>
<head>
<title>Moving a box over a flat surface</title>
<style>
body {
margin: 0;
}
canvas {
width: 100%;
height: 100%
}
</style>
</head>
<body>
<script src="three.js"></script>
<script src="http://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="Grid.js"></script>
<script src="Furniture.js"></script>
<script>
//grid
var grid = new Grid(14,14,0,-5,0);
var debugGrid;
//variables
var heldItem;
var itemHeld = false;
var movingX = 0;
var movingZ = 0;
var thingY = 0;
var returnPositionX;
var returnPositionZ;
var furnitures = [];
//------------------------------------------------------------camera, scene, renderer
//renderer
var renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0x889988);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
//scene
var scene = new THREE.Scene();
//Camera
var camera = new THREE.PerspectiveCamera(40, window.innerWidth / innerHeight, 1, 1000);
camera.position.set(20, 20, 20);
scene.add(camera);
//scene.add(camera);
//raycast
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
//controls
controls = new THREE.OrbitControls(camera);
//
//------------------------------------------------------------------objects in scene
//building materials1
var buildingGeo1 = new THREE.BoxGeometry(14, 14, 0.25);
var buildingMat1 = new THREE.MeshPhongMaterial({
color: 0x808080
});
//floor
floor = new THREE.Mesh(buildingGeo1, buildingMat1);
floor.position.set(0, -5, 0);
floor.rotation.set(-Math.PI / 2, 0, 0);
scene.add(floor);
//shameful display below, i apologise. The three js documentation doesnt tell me anything
var cheatArray = [floor];
//thing 1
//geometry
var thingGeometry = new THREE.BoxBufferGeometry(2, 2, 2);
//material
var thingMaterial = new THREE.MeshPhongMaterial({
color: 'sandybrown'
});
//mesh1
var thing1 = new THREE.Mesh(thingGeometry, thingMaterial);
thingY = -5 + 2 / 2;
thing1.position.set(0, thingY, 0);
//scene.add(thing1);
//
//thing 2
//geometry
var thing2Geometry = new THREE.BoxBufferGeometry(2, 2, 2);
//material
var thing2Material = new THREE.MeshPhongMaterial({
color: 'sandybrown'
});
//thing2Material.name = 'Mat';
//mesh1
var thing2 = new THREE.Mesh(thing2Geometry, thing2Material);
//thing2.name = 'test';
thingY = -5 + 2 / 2;
thing2.position.set(3, thingY, 3);
//scene.add(thing2);
//
function FurnitureInit(furnitureAnchor,shape,gridRef,position){
var furniture = new THREE.Group();
var furnitureRef = new Furniture(furnitureAnchor,shape,gridRef,position);
furnitureAnchor.name = "Furniture";
furnitures.push(furniture);
furniture.add(furnitureAnchor);
scene.add(furniture);
furniture.RotateShape = function(){
furnitureRef.RotateShape();
}
furniture.CheckSittingOn = function(){
return furnitureRef.CheckSittingOn();
}
}
//FURNITURE TESTS
var box1Shape = [
[0,0,0,0],
[0,1,0,0],
[0,0,0,0],
[0,0,0,0],
];
FurnitureInit(thing1,box1Shape,grid,thing1.position);
FurnitureInit(thing2,box1Shape,grid,thing2.position);
//
//---------------------------------------------------------------------------------Lights
//Directional light
var light = new THREE.PointLight(0xffffff, 1, 100);
light.position.set(0, 10, 0);
scene.add(light);
// ----------------------------------------------------------------------------------functions
function onMouseMove(event) {
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
function whatsTheGridLookLike(){
if(debugGrid == null){
debugGrid = grid.giveMeSquares();
}
//geometry--- change x and z sizes to correctly view grid
var debugGeo = new THREE.BoxBufferGeometry(1, 5, 1);
//material
var debugMaterial = new THREE.MeshBasicMaterial({
color: 0x330000
});
var tempgrid = debugGrid;
for( i =0; i < tempgrid.length; i++){
var debugNode = new THREE.Mesh(debugGeo,debugMaterial);
if(tempgrid[i].isOccupied()){
debugNode.material.color.setHex( 0xF1CBFF );
}else{
debugNode.material.color.setHex( 0x330000 );
}
debugNode.position.set(tempgrid[i].xPos,-5,tempgrid[i].yPos);
scene.add(debugNode);
}
}
function onClick(event) {
//raycast
raycaster.setFromCamera(mouse, camera);
// calculate objects intersecting the picking ray
var intersects = raycaster.intersectObjects(furnitures,true);
//this checks if the array has hit thing1
if (intersects.length > 0) {
//console.log(intersects);
//console.log(intersects[0].object.name);
if (intersects[0].object.name == "Furniture") {
console.log("HIT");
//itemHeld means thing1 will travel with the mouse on the plane
if(itemHeld){
//dropping off
if(intersects[0].object.parent.CheckSittingOn()){
debugGrid = null;
itemHeld = false;
heldItem = null;
}else{
//console.log(returnPosition);
heldItem.position.set(returnPositionX,thingY,returnPositionZ);
itemHeld = false;
heldItem = null;
}
}else{
//picking up
returnPositionX = intersects[0].object.position.x;
returnPositionZ = intersects[0].object.position.z;
thingY = intersects[0].object.position.y;
console.log(thingY);
heldItem = intersects[0].object
//returnPosition = intersects[0].object.position;
itemHeld = true;
}
}
}
}
window.addEventListener('mousemove', onMouseMove, false);
window.addEventListener('mousedown', onClick, false); //need to change from mouse down to whatever just the leftclick would be
animate();
function animate() {
scene.get
if(itemHeld){
//raycast onto the floor
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(cheatArray);
if(intersects[0] != null){
var tempNode = grid.nodeFromWorldPoint(intersects[0].point);
movingX = tempNode.xPos;
movingZ = tempNode.yPos;
}
heldItem.position.set(movingX, thingY, movingZ);
}
//enable this to view the grid
//whatsTheGridLookLike();
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
</script>
</body>
</html>