forked from therauli/WebNaali
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentities.js
267 lines (225 loc) · 7.47 KB
/
entities.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* Entity stuff is here
TODO:
* Get camera to better position
*/
entities = {};
function Entity(id) {
this.id = id;
this.components = new Array();
this.addComponent = function(component) {
this.components.push(component);
}
}
(function (Components, $, undefined) {
Components.EC_Placeable = function(params) {
this.componentName = 'EC_Placeable';
this.parent = params['id'];
//console.log(params)
this.transform = params['Transform']
}
Components.EC_Mesh = function(params) {
this.componentName = 'EC_Mesh';
this.parent = params['id'];
this.url = params['url']
this.url = "http://localhost:8000/man_mesh+armature_2.5.dae"; //XXX test override
/*if (this.url) {
this.mesh = new GLGE.Collada();
this.mesh.setId(this.parent);
//FIXME -- now just copy-pasted EC_Avatar from below
this.mesh.setDocument(this.url);
this.mesh.setScale(0.1);
this.mesh.setRotY(3 * Math.PI / 2);
scene.addCollada(this.mesh);
}*/
}
Components.EC_Avatar = function(params) {
this.componentName = 'EC_Avatar';
var id = params['id'];
this.parent = id;
this.url = "http://localhost:8000/man_mesh+armature_2.5.dae";
//this.url = "http://www.realxtend.org/webnaali/avatar/man_mesh%2Barmature_2.5.dae";
if (this.url) {
this.mesh = new GLGE.Collada();
this.mesh.setId(this.parent);
this.mesh.setDocument(this.url);
//this.mesh.docURL = "avatar/";
this.mesh.setScale(0.1);
//this.mesh.setRotY(3 * Math.PI / 2);
scene.addCollada(this.mesh);
// For some reason setting the animation frames is not
//executed immidiately so we delay execution a bit
window.setTimeout(function() {
var components = entities[id].components;
for (i = 0; i < components.length; i++) {
if (components[i].componentName == "EC_Avatar") {
components[i]['mesh'].setFrames(1)
break;
}
}
}, 10000);
}
}
Components.EC_DynamicComponent = function(params) {
this.parent = params['id'];
this.componentName = 'EC_DynamicComponent';
this.code = params['code'];
}
}(window.Components = window.Components || {}, jQuery));
function addEntity(params) {
var id = params['id'];
if (!entities[id])
entities[id] = new Entity(id);
}
function removeEntity(params) {
var id = params['id'];
for (c = 0; c < scene.children.length; c++) {
if (scene.children[c].getId() == id) {
scene.children.splice(c, 1);
break;
}
}
delete entities[id];
}
function addComponent(params) {
id = params['id']
var newComponent = params['component'];
var component;
// WS does not have any fancy sync state stuff so if we get an
// addcomponent message for an entity that hasn't been created yet
// we'll just add a new entity. This is not the way to go.
if (!entities[id])
addEntity({id: id})
if (Components[newComponent]) {
//FIXME check that entity does not already have a mesh. Should
//be done smarter
for (i = 0; i < entities[id].components.length; i++) {
if (entities[id].components[i].componentName == newComponent) {
return;
}
}
//console.log(id + ' making new ' + newComponent + ' ' + params)
component = new Components[newComponent](params);
//console.log(id + ' adding ' + newComponent + ' ' + params)
entities[id].addComponent(component);
//console.log(id + ' setting attrs ' + newComponent + ' ' + params)
}
}
function setAttr(params) {
var id = params['id'];
var component = params['component'];
//console.log(id + ' SETTING ' + component + ' ' + JSON.stringify(params))
var comp;
for (comp in entities[id].components) {
if (entities[id].components[comp].componentName == component) {
//console.log('FOUND corresponding component')
//Joins componenst. A bit silly... (this comment came
//second in the World's most useless comments in the world
//competition of 2011
jQuery.extend(entities[id].components[comp], params);
if (component == 'EC_Placeable') {
//console.log('IS PLACEABLE')
for (child in scene.children) {
var collada = scene.children[child];
if (collada.getId() == id) {
var transform = params['Transform'];
var x = transform[0];
var y = transform[1];
var z = transform[2];
var rotx = transform[3] * Math.PI / 180;
var roty = transform[4] * Math.PI / 180;
var rotz = transform[5] * Math.PI / 180;
collada.setLocX(x);
collada.setLocY(y);
collada.setLocZ(z);
collada.setRotX(rotx);
collada.setRotY(roty);
collada.setRotZ(rotz);
if (id == myid) {
// sync Camera
camera.setLoc(x, y + 1.5, z);
camera.setRot(rotx, roty, rotz);
//try to move it backwards for 3rd person view. parenting to av would be better, and probably easier too
//var ox = camera.getLocX();
//var oy = camera.getLocY();
//var oz = camera.getLocZ();
//var camrot = camera.getRotation();
//camera.setLoc(ox + camrot.x, oy - camrot.y, oz - camrot.z);
//that didn't work - resort to just hiding own av as a desperate fix
collada.setScale(0.01);
}
}
else {
//console.log('PLACEABLE for unknown entity: ' + id)
}
}
}
}
}
}
function getAttr(params) {
var id = params["id"];
var component = params["component"];
var keys = params["keys"];
var values = []
for (comp in entities[id].components) {
if (entities[id].components[comp].componentName == component) {
for (key in keys) {
values.push(entities[id].components[comp][keys[key]]);
}
}
}
return values
}
function loadScene(params) {
var xmlstring = params['xml'];
var scenexml = (new DOMParser()).parseFromString(xmlstring, "text/xml");
var data = {};
var loadentities = scenexml.getElementsByTagName("entity")
for (e = 0; e < loadentities.length; e++) {
var entity = loadentities[e];
var id = entity.getAttribute("id");
data[id] = {};
components = entity.getElementsByTagName("component");
for (c = 0; c < components.length; c++) {
var component = components[c].getAttribute("type")
data[id][component] = {};
var attributes = components[c].getElementsByTagName("attribute");
for (a = 0; a < attributes.length; a++) {
var attribute = attributes[a];
var name = attribute.getAttribute("name");
var value = attribute.getAttribute("value");
data[id][component][name] = value
}
}
}
//Add/update components
// Just use EC_Placeable and EC_MESH for time being
for (id in data) {
//add entity (if not in scene)
if (!entities[id]) {
addEntity({id: id});
} else {
console.log('ERROR: ' + id + ' already in scene!');
}
for (component in data[id]) {
//console.log(component)
if (component == 'EC_Placeable') {
//console.log('PLACE');
addComponent({id: id,
component: component,
transform: data[id][component]['Transform'].split(',')});
} else if (component == 'EC_Mesh') {
var meshref = data[id][component]['Mesh ref'];
console.log('MESH ' + meshref);
addComponent({id: id,
component: component,
url: meshref
});
} else if (component == 'EC_Avatar') {
//console.log('AVATAR');
addComponent({id: id,
component: component});
}
}
}
}