// // Created by Ryan Huffman on 1/10/2017 // 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 // /* globals utils */ (function() { Script.include('utils.js'); function Enemy() { } Enemy.prototype = { preload: function(entityID) { this.entityID = entityID; // To avoid sending extraneous messages and checking entities that we've already // seen, we keep track of which entities we've collided with previously. this.entityIDsThatHaveCollidedWithMe = []; Script.addEventHandler(entityID, "collisionWithEntity", this.onCollide.bind(this)); var userData = Entities.getEntityProperties(this.entityID, 'userData').userData; var data = utils.parseJSON(userData); if (data !== undefined && data.gameChannel !== undefined) { this.gameChannel = data.gameChannel; } else { print("enemyEntity.js | ERROR: userData does not contain a game channel and/or team number"); } }, onCollide: function(entityA, entityB, collision) { if (this.entityIDsThatHaveCollidedWithMe.indexOf(entityB) > -1) { return; } this.entityIDsThatHaveCollidedWithMe.push(entityB); var colliderName = Entities.getEntityProperties(entityB, 'name').name; if (colliderName.indexOf("projectile") > -1) { Messages.sendMessage(this.gameChannel, JSON.stringify({ type: "enemy-killed", entityID: this.entityID, position: Entities.getEntityProperties(this.entityID, 'position').position })); Entities.deleteEntity(this.entityID); } else if (colliderName.indexOf("GateCollider") > -1) { Messages.sendMessage(this.gameChannel, JSON.stringify({ type: "enemy-escaped", entityID: this.entityID, position: Entities.getEntityProperties(this.entityID, 'position').position })); Entities.deleteEntity(this.entityID); } } }; return new Enemy(); });