68 lines
No EOL
2.4 KiB
JavaScript
68 lines
No EOL
2.4 KiB
JavaScript
//
|
|
// avatarCounter.js
|
|
//
|
|
// Created by Rebecca Stankus on 07/09/2018.
|
|
// Copyright 2018 High Fidelity, Inc.
|
|
//
|
|
// Distributed under the Apache License, Version 2.0.
|
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
|
//
|
|
|
|
(function() {
|
|
var UPDATE_INTERVAL = 1000;
|
|
var HALF_MULTIPLIER = 0.5;
|
|
var _this;
|
|
|
|
var counterText;
|
|
|
|
var AvatarCounter = function() {
|
|
_this = this;
|
|
};
|
|
|
|
AvatarCounter.prototype = {
|
|
interval: null,
|
|
|
|
preload: function(entityID) {
|
|
_this.entityID = entityID;
|
|
Entities.getChildrenIDs(_this.entityID).forEach(function(childID) {
|
|
var name = Entities.getEntityProperties(childID, 'name').name;
|
|
if (name.indexOf("Counter") !== -1) {
|
|
counterText = childID;
|
|
}
|
|
});
|
|
_this.interval = Script.setInterval(function() {
|
|
var count;
|
|
var zoneProperties = Entities.getEntityProperties(_this.entityID,
|
|
["position", "dimensions","rotation", "userData"]);
|
|
AvatarList.getAvatarIdentifiers().forEach(function(avatarID) {
|
|
var avatar = AvatarList.getAvatar(avatarID);
|
|
if (_this.isPositionInsideBox(avatar.position, zoneProperties)) {
|
|
count++;
|
|
}
|
|
});
|
|
Entities.editEntity(counterText, { text: JSON.stringify(count) });
|
|
}, UPDATE_INTERVAL);
|
|
},
|
|
|
|
isPositionInsideBox: function(position, boxProperties) {
|
|
var localPosition = Vec3.multiplyQbyV(Quat.inverse(boxProperties.rotation),
|
|
Vec3.subtract(position, boxProperties.position));
|
|
var halfDimensions = Vec3.multiply(boxProperties.dimensions, HALF_MULTIPLIER);
|
|
return -halfDimensions.x <= localPosition.x &&
|
|
halfDimensions.x >= localPosition.x &&
|
|
-halfDimensions.y <= localPosition.y &&
|
|
halfDimensions.y >= localPosition.y &&
|
|
-halfDimensions.z <= localPosition.z &&
|
|
halfDimensions.z >= localPosition.z;
|
|
},
|
|
|
|
unload: function() {
|
|
if (_this.interval) {
|
|
Script.clearInterval(_this.interval);
|
|
}
|
|
}
|
|
};
|
|
|
|
return new AvatarCounter;
|
|
});
|
|
|