// // whackAMole/whackAMoleConsoleEntity.js // // Created by Thijs Wenker on 8/29/16. // Copyright 2016 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'); var MALLET_NAME = 'Whack a mole - Mallet'; var MALLET_RANGE = 500; // meters - find mallets in this range var _this; var WhackAMoleEntity = function() { _this = this; }; WhackAMoleEntity.prototype = { consoleEntityID: null, serverChannel: null, preload: function(entityID) { _this.consoleEntityID = entityID; _this.serverChannel = constants.SERVER_CHANNEL_PREFIX + _this.consoleEntityID; if (constants.DEBUG && constants.DEBUG_CLICK) { Entities.clickReleaseOnEntity.connect(function (entityID, mouseEvent) { if (mouseEvent.isLeftButton && Entities.isChildOfParent(entityID, _this.consoleEntityID)) { Messages.sendMessage(_this.serverChannel, JSON.stringify({ action: 'debugFindMole', entityID: entityID })); } }); } }, unload: function() { }, clearMallets: function() { var entities = Entities.findEntities(Entities.getEntityProperties(_this.consoleEntityID, 'position').position, MALLET_RANGE); entities.forEach(function(entity) { var properties = Entities.getEntityProperties(entity, ['name', 'userData']); if (properties.name === MALLET_NAME) { try { var userData = JSON.parse(properties.userData); if (userData.whackAMole.consoleEntityID === _this.consoleEntityID) { Entities.deleteEntity(entity); } } catch (e) { // e } } }); }, createMallet: function(parent, left, consoleEntityID) { var position = { x: (left ? -1.0 : 1.0) * 0.5242646932601929, y: -0.07722461223602295, z: 0.36840522289276123 }; var rotation = { x: -0.8699671626091003, y: 0.09008268266916275, z: 0.1730472445487976, w: 0.4528762102127075 }; if (!left) { rotation.y = -rotation.y; rotation.z = -rotation.z; } var entityProperties = { collisionsWillMove: true, compoundShapeURL: helpers.getModelURL('mallet_phys.obj'), dimensions: { x: 0.24804636836051941, y: 0.53404051065444946, z: 0.24767011404037476 }, dynamic: true, gravity: { x: 0, y: -9.8, z: 0 }, modelURL: helpers.getModelURL(left ? 'wam_mallet_blue_emissive.fbx' : 'wam_mallet_red_emissive.fbx'), name: MALLET_NAME, position: position, rotation: rotation, shapeType: 'compound', type: 'Model', lifetime: constants.SESSION_TIME, collidesWith: "static,dynamic,kinematic,", collisionMask: 7, script: Script.resolvePath('whackAMoleMalletEntity.js') }; var mallet = helpers.createEntity(entityProperties, parent); Entities.editEntity(mallet.id, { userData: JSON.stringify({ grabbableKey: { grabbable: true }, whackAMole: { consoleEntityID: consoleEntityID, returnPosition: mallet.position, returnRotation: mallet.rotation, player: left ? constants.PLAYER_ONE : constants.PLAYER_TWO } }) }); helpers.createMalletTractor(mallet.id, mallet.position, mallet.rotation); return mallet; }, coinBoxTrigger: function(entity, args) { helpers.debugPrint('_this.serverChannel = ' + _this.serverChannel); Messages.sendMessage(_this.serverChannel, JSON.stringify({ action: 'start' })); var properties = Entities.getEntityProperties(entity, ['position', 'rotation']); var parentProperties = { position: properties.position, rotation: properties.rotation }; _this.clearMallets(); // left mallet _this.createMallet(parentProperties, true, _this.consoleEntityID); // right mallet _this.createMallet(parentProperties, false, _this.consoleEntityID); } }; // entity scripts always need to return a newly constructed object of our type return new WhackAMoleEntity(); });