content/hifi-content/rebecca/Trivia/Four Square/gameZone.js
2022-02-14 02:04:11 +01:00

60 lines
No EOL
1.8 KiB
JavaScript

//
// gameZone.js
//
// Created by Rebecca Stankus on 08/19/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 HALF_MULTIPLIER = 0.5;
var _this;
var zoneProperties;
var GameZone = function() {
_this = this;
};
GameZone.prototype = {
preload: function(entityID) {
_this.entityID = entityID;
zoneProperties = Entities.getEntityProperties(_this.entityID,
['name', 'parentID', 'rotation', 'position', 'dimensions']);
},
isAvatarInsideZone: function() {
var localPosition = Vec3.multiplyQbyV(Quat.inverse(zoneProperties.rotation),
Vec3.subtract(MyAvatar.position, zoneProperties.position));
var halfDimensions = Vec3.multiply(zoneProperties.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;
},
enterEntity: function() {
print("subscribing to messages");
Messages.subscribe("TriviaChannel");
},
leaveEntity: function() {
print("unsubscribed from messages");
Messages.unsubscribe("TriviaChannel");
},
unload: function() {
if (_this.isAvatarInsideZone) {
_this.leaveEntity();
}
}
};
return new GameZone;
});