This repository has been archived by the owner on Apr 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
123 lines (112 loc) · 3.61 KB
/
index.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
/* global AFRAME */
if (typeof AFRAME === 'undefined') {
throw new Error('Component attempted to register before AFRAME was available.');
}
/**
* A-Frame Network System for A-Frame.
*/
AFRAME.registerSystem('network', {
dependencies: ['position', 'rotation'],
schema: {
url: {
type: 'string',
default: null
},
port: {
type: 'number',
default: 4000
},
path: {
type: 'string',
default: '/chat'
}
},
onNetworkConnect: function () {
var self = this;
// unfortunately, our position and rotation attributes aren't set when we call this
self.socket.emit('spawn', {
position: {x: 0, y: 0, z: 0},
rotation: {x: 0, y: 0, z: 0}
});
self.socket.on('message', function (data) {
console.log(data);
}).on('spawn', function (data) {
var entityEl = document.createElement('a-box');
entityEl.setAttribute('network', {
local: false,
serverId: data.id
});
console.log("Spawning remote object: ", data.id);
entityEl.setAttribute('position', data.position);
entityEl.setAttribute('rotation', data.rotation);
if (entityEl.components.material !== undefined) {
entityEl.setAttribute('material', 'color', data.color);
}
var scene = document.querySelector('a-scene');
scene.appendChild(entityEl);
self.registerMe(entityEl);
}).on('position', function (data) {
var entityEl = self.entities[data.id];
entityEl.setAttribute('position', data.position);
}).on('rotation', function (data) {
var entityEl = self.entities[data.id];
entityEl.setAttribute('rotation', data.rotation);
}).on('despawn', function (data) {
console.log("Despawning remote object: ", data.id);
var entityEl = self.entities[data.id];
self.unregisterMe(entityEl);
entityEl.parentNode.removeChild(entityEl);
})
},
init: function () {
this.entities = {};
if (this.data.url == undefined || this.data.url == "") {
this.data.url = location.protocol + '//' + location.hostname + ':' + this.data.port + this.data.path;
}
this.socket = io.connect(this.data.url);
this.socket.on('connect', this.onNetworkConnect.bind(this));
},
registerMe: function (el) {
this.entities[el.components.network.attrValue.serverId] = el;
},
unregisterMe: function (el) {
delete this.entities[el.components.network.attrValue.serverId];
},
emit: function (message, data) {
this.socket.emit(message, data);
}
});
/**
* A-Frame Network Component for A-Frame.
*/
AFRAME.registerComponent('network', {
schema: {
local: { type: 'boolean' },
serverId: { type: 'string' }
},
/**
* Called once when component is attached. Generally for initial setup.
*/
init: function () {
if (this.data.local) {
this.el.addEventListener('componentchanged', this.onComponentChanged.bind(this));
}
},
onComponentChanged: function (evt) {
if (evt.detail.name === 'position') {
var oldData = this.lastPosition;
var newData = evt.detail.newData;
if (oldData == undefined || oldData.x !== newData.x || oldData.y !== newData.y || oldData.z !== newData.z) {
this.system.emit('position', evt.detail.newData);
this.lastPosition = newData;
}
} else if (evt.detail.name === 'rotation') {
var oldData = this.lastRotation;
var newData = evt.detail.newData;
if (oldData == undefined || oldData.x !== newData.x || oldData.y !== newData.y || oldData.z !== newData.z) {
this.system.emit('rotation', evt.detail.newData);
this.lastRotation = newData;
}
}
}
});