-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.js
163 lines (146 loc) · 5.07 KB
/
world.js
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
/*
Object svet
Vo svete su ulozene policka na mriezke, policok moze byt na jednom mieste na sebe viacero
*/
function World() {
//rozmery sveta
this.x = 0;
this.y = 0;
this.map = [];
}
/* vytvori svet
x - dlzka sveta
y - sirka sveta
Na zaciatku je vsade len jedno policko, podlaha
*/
World.prototype.build = function(x,y) {
this.x = x;
this.y = y;
this.map = [];
//vytvori 3 rozmerne pole, indexujeme y-sirka, x-dlzka, h-vyska
for (var row=0; row<y; row++) {
var line = []
for (var column=0; column<x; column++) {
line.push([{'type':'ground'}]);
}
this.map.push(line);
}
this.syncScene();
}
/* Otazky na svet */
World.prototype.isHole = function(x,y) {
if (x<0 || x>=this.x || y<0 || y>=this.y) return true;
return this.map[y][x].length==0;
}
World.prototype.height = function(x,y) {
if (x<0 || x>=this.x || y<0 || y>=this.y) return -1;
return this.map[y][x].length-1;
}
World.prototype.isBrick = function(x,y) {
if (x<0 || x>=this.x || y<0 || y>=this.y) return false;
var pos = this.map[y][x];
if (pos.length==0) return false;
return pos[pos.length-1].type=='brick';
}
World.prototype.isColor = function(x,y,color) {
if (x<0 || x>=this.x || y<0 || y>=this.y) return false;
var pos = this.map[y][x];
if (pos.length==0 || !pos[pos.length-1].hasOwnProperty('color')) return false;
return pos[pos.length-1].color==color;
}
/* Uprava sveta */
World.prototype.makeHoleAt = function(x,y) {
if (this.scene != undefined) {
for (var height=0; height<this.map[y][x].length; height++) {
this.scene.scene.remove(this.map[y][x][height].mesh);
}
}
this.map[y][x]=[];
}
World.prototype.clearAt = function(x,y) {
if (this.scene != undefined) {
for (var height=0; height<this.map[y][x].length; height++) {
this.scene.scene.remove(this.map[y][x][height].mesh);
}
}
this.map[y][x]=[{'type':'ground'}];
this.syncAdd(y,x,this.map[y][x].length-1);
}
World.prototype.putAt = function(x,y,item) {
this.map[y][x].push(jQuery.extend({}, item)); //prida kopiu objektu na spravne miesto
this.syncAdd(y,x,this.map[y][x].length-1);
}
World.prototype.takeFrom = function(x,y) {
var pos = this.map[y][x];
if (pos.length>1) {
var obj = pos.pop();
if (this.scene != undefined) {
this.scene.scene.remove(obj.mesh);
}
}
}
World.prototype.getFrom = function(x,y) {
var pos = this.map[y][x];
if (pos.length>1) {
var obj = pos[pos.length-1];
return obj;
}
return {'type':'hole'};
}
World.prototype.colorAt = function(x,y) {
var pos = this.map[y][x];
if (pos.length==0 || !pos[pos.length-1].hasOwnProperty('color')) return 'nocolor';
return pos[pos.length-1].color;
}
/* Nastavi Three.js scenu */
World.prototype.setScene = function(scene) {
this.clearScene();
this.scene = scene;
//materialy pre tehlicky
this.materials = {
red: new THREE.MeshLambertMaterial( { color: 0xff0000}),
green: new THREE.MeshLambertMaterial( { color: 0x00ff00}),
blue: new THREE.MeshLambertMaterial( { color: 0x0000ff}),
nocolor: new THREE.MeshLambertMaterial( { color: 0x000000})
}
//ak nie je vytvorene mesh-e pre tehlicky, vytvorime ich
this.syncScene();
}
/* Synchronizuje Three.js scenu, odporuca sa nepouzivat pokial fakt neviete co robite */
World.prototype.syncScene = function() {
if (this.scene == undefined) return;
for (var row=0; row<this.y; row++) {
for (var column=0; column<this.x; column++) {
for (var height=0; height<this.map[row][column].length; height++) {
this.syncAdd(row, column, height);
}
}
}
}
/* Vymaze vsetko z sceny, odporuca sa nepouzivat pokial fakt neviete co robite */
World.prototype.clearScene = function() {
if (this.scene == undefined) return;
for (var row=0; row<this.y; row++) {
for (var column=0; column<this.x; column++) {
for (var height=0; height<this.map[row][column].length; height++) {
this.scene.scene.remove(this.map[row][column][height].mesh);
}
}
}
}
/* Vytvori mesh pre jeden objekt a prida ho do sceny na suradniciach, odporuca sa nepouzivat pokial fakt neviete co robite */
World.prototype.syncAdd = function(row,column,height) {
if (this.scene != undefined) {
switch (this.map[row][column][height].type) {
case 'ground' :
this.map[row][column][height].mesh = new THREE.Mesh (this.scene.ground.geometry, this.scene.ground.materials);
break;
case 'brick' :
this.map[row][column][height].mesh = new THREE.Mesh (this.scene.brick.geometry, this.materials[this.map[row][column][height].color]);
break;
}
this.map[row][column][height].mesh.position.set(column,0.1*height,-row);
this.map[row][column][height].mesh.receiveShadow = true;
this.scene.scene.add(this.map[row][column][height].mesh);
}
}