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

79 lines
2.9 KiB
JavaScript

//
// whackAMole/whackAMoleMalletEntity.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 constants = Script.require('./constants.js');
var helpers = Script.require('./helpers.js');
// TODO: make this threshold depend on how the console/mole is rotated,
// so that it doesn't matter when you play the game upside-down
var DOWNWARD_VELOCITY_THRESHOLD = -0.1;
var _this;
var WhackAMoleMalletEntity = function () {
_this = this;
};
WhackAMoleMalletEntity.prototype = {
entityID: null,
grabbing: null,
// returnPosition: null,
// returnRotation: null,
player: null,
consoleEntityID: null,
hand: null,
preload: function(entityID) {
_this.grabbing = false;
_this.entityID = entityID;
try {
var userData = JSON.parse(Entities.getEntityProperties(entityID, ['userData']).userData);
_this.player = userData.whackAMole.player;
_this.consoleEntityID = userData.whackAMole.consoleEntityID;
} catch (e) {
// e
}
},
hit: function(consoleID, colliderID) {
Messages.sendMessage(constants.SERVER_CHANNEL_PREFIX + consoleID, JSON.stringify({
action: 'whackMole',
entityID: colliderID,
player: _this.player
}));
var hand = _this.hand === 'left' ? 0 : 1;
Controller.triggerShortHapticPulse(1, hand);
},
collisionWithEntity: function(thisEntity, otherEntity, collision) {
print('colliding ' + JSON.stringify({thisEntity:thisEntity,otherEntity:otherEntity,collision:collision}));
if (collision.type === 0) {
var properties = Entities.getEntityProperties(otherEntity, ['name', 'parentID']);
if (properties.name.indexOf(constants.MOLE_COLLIDER_PREFIX) === 0 &&
collision.velocityChange.y <= DOWNWARD_VELOCITY_THRESHOLD) {
_this.hit(properties.parentID, otherEntity);
}
}
},
startNearGrab: function (entityID, args) {
_this.hand = args[0];
// try starting if it didn't already start a game
Messages.sendMessage(constants.SERVER_CHANNEL_PREFIX + _this.consoleEntityID, JSON.stringify({action: 'start'}));
},
continueNearGrab: function (entityID, args) {
//Entities.getEntity
},
releaseGrab: function (entityID, args) {
// FIXME: delete ~ seconds after deletion
}
};
return new WhackAMoleMalletEntity();
});