content/hifi-content/thoys/dev/2017/whackAMole/coinBox/carnivalCoinEntity.js
2022-02-14 02:04:11 +01:00

60 lines
2.4 KiB
JavaScript

//
// carnivalCoinEntity.js
//
// Created by Thijs Wenker on 5/19/17.
// Copyright 2017 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 MILLISECONDS_PER_SECOND = 1000;
var _this;
var CoinEntity = function() {
_this = this;
};
CoinEntity.prototype = {
entityID: null,
preload: function(entityID) {
_this.entityID = entityID;
},
collisionWithEntity: function(thisEntity, otherEntity, collision) {
if (collision.type === 0) {
var properties = Entities.getEntityProperties(otherEntity, ['name']);
var coinSwallowTime = 2; // seconds
if (properties.name === 'coin_collider') {
Entities.editEntity(thisEntity, {
collisionless: true,
gravity: {x: 0, y: 0, z: 0},
lifetime: Entities.getEntityProperties(thisEntity, 'age').age + coinSwallowTime,
userData: JSON.stringify({
grabbableKey: {
grabbable: true
}
})
});
var otherProperties = Entities.getEntityProperties(otherEntity, ['position', 'rotation', 'parentID']);
var actionIDs = Entities.getActionIDs(otherEntity);
actionIDs.forEach(function(actionID) {
Entities.deleteAction(otherEntity, actionID);
});
Entities.addAction('spring', thisEntity, {
targetPosition: otherProperties.position,
linearTimeScale: coinSwallowTime,
targetRotation: Quat.multiply(otherProperties.rotation, Quat.fromPitchYawRollDegrees(90, 0, 90)),
angularTimeScale: 0.1,
tag: "swallow the coin"
});
Script.setTimeout(function() {
Entities.callEntityMethod(otherProperties.parentID, 'emitTriggerEvent');
}, coinSwallowTime * MILLISECONDS_PER_SECOND);
}
}
}
};
return new CoinEntity();
});