From f53595c24046af032ffe82546b282b775a94617a Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Fri, 2 Dec 2016 13:04:50 -0800 Subject: [PATCH] Add targetEntity.js --- .../tower-defense/targetEntity.js | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 unpublishedScripts/tower-defense/targetEntity.js diff --git a/unpublishedScripts/tower-defense/targetEntity.js b/unpublishedScripts/tower-defense/targetEntity.js new file mode 100644 index 0000000000..322d721cb2 --- /dev/null +++ b/unpublishedScripts/tower-defense/targetEntity.js @@ -0,0 +1,73 @@ +if (!Function.prototype.bind) { + Function.prototype.bind = function(oThis) { + if (typeof this !== 'function') { + // closest thing possible to the ECMAScript 5 + // internal IsCallable function + throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); + } + + var aArgs = Array.prototype.slice.call(arguments, 1), + fToBind = this, + fNOP = function() {}, + fBound = function() { + return fToBind.apply(this instanceof fNOP + ? this + : oThis, + aArgs.concat(Array.prototype.slice.call(arguments))); + }; + + if (this.prototype) { + // Function.prototype doesn't have a prototype property + fNOP.prototype = this.prototype; + } + fBound.prototype = new fNOP(); + + return fBound; + }; +} + +(function() { + function parseJSON(json) { + try { + return JSON.parse(json); + } catch(e) { + return undefined; + } + } + + Target = function() { + } + Target.prototype = { + preload: function(entityID) { + this.entityID = entityID; + Script.addEventHandler(entityID, "collisionWithEntity", this.onCollide.bind(this)); + + this.entityIDsThatHaveCollidedWithMe = []; + + var userData = Entities.getEntityProperties(this.entityID, 'userData').userData; + var data = parseJSON(userData); + if (data !== undefined && data.gameChannel) { + this.gameChannel = data.gameChannel + } else { + print("targetEntity.js | ERROR: userData does not contain a game channel"); + } + }, + onCollide: function(entityA, entityB, collision) { + print("Collided with: ", entityB); + var colliderName = Entities.getEntityProperties(entityB, 'name').name; + + // If the other entity's name includes 'projectile' and we haven't hit it before, + // continue on. + if (colliderName.indexOf("projectile") > -1 + && this.entityIDsThatHaveCollidedWithMe.indexOf(entityB) === -1) { + this.entityIDsThatHaveCollidedWithMe.push(entityB); + Messages.sendMessage(this.gameChannel, JSON.stringify({ + type: "target-hit", + entityID: this.entityID + })); + } + } + }; + + return new Target(); +});