Skip to content

Instantly share code, notes, and snippets.

@chrahunt
Created March 10, 2015 01:30
Show Gist options
  • Save chrahunt/b61ae05c3531c3d940f2 to your computer and use it in GitHub Desktop.
Save chrahunt/b61ae05c3531c3d940f2 to your computer and use it in GitHub Desktop.
Show time remaining before a player respawns.
// ==UserScript==
// @name Respawn Timer
// @description Show time remaining before a player respawns.
// @include http://tagpro-*.koalabeast.com:*
// @include http://maptest*.newcompte.fr:*
// @include http://tangent.jukejuice.com:*
// @downloadURL https://gist.github.com/chrahunt/b61ae05c3531c3d940f2/raw/spawn-timer.user.js
// @author snaps
// @license MIT
// @version 0.1.0
// ==/UserScript==
// Variables.
var TEXT_COLOR = "#FFFFFF";
// End of Variables.
var TILE_SIZE = 40;
// Wait until the tagpro object exists, and add the function to tagpro.ready
function addToTagproReady(fn) {
// Make sure the tagpro object exists.
if (typeof tagpro !== "undefined") {
tagpro.ready(fn);
} else {
// If not ready, try again after a short delay.
setTimeout(function() {
addToTagproReady(fn);
}, 0);
}
}
/**
* Get text for overlaying on center of tiles.
* @param {string} [color="#FFFFFF"] - The fill color to use for the text.
* @return {PIXI.Text} - The created text.
*/
function makeText(color) {
if (typeof color == 'undefined') color = "#FFFFFF";
var text = new PIXI.Text("", {
font: "bold 10pt Arial",
fill: color,
stroke: "#000000",
strokeThickness: 3,
align: "center"
});
text.anchor = new PIXI.Point(0.5, 0.5);
text.visible = false;
return text;
}
var Timer = function(position, timeout) {
this.pos = {
x: position.x + (TILE_SIZE / 2),
y: position.y + (TILE_SIZE / 2)
};
this.text = makeText(TEXT_COLOR);
this.addText(tagpro.renderer.layers.foreground);
this.respawn = timeout;
// Set position of text.
this.text.x = this.pos.x;
this.text.y = this.pos.y;
this.text.visible = true;
this.textUpdate = setInterval(function() {
// Set text to be last drawn.
this.text.parent.setChildIndex(this.text, this.text.parent.children.length - 1);
var now = Date.now();
var remaining = this.respawn - now;
var seconds = Math.floor(remaining / 1000);
var decimal = Math.floor((remaining % 1000) / 100);
if (remaining > 0) {
var text = seconds + "." + decimal;
this.text.setText(text);
} else {
this.reset();
}
}.bind(this), 90);
};
/**
* Allow tile to set text as child of container.
* @param {PIXI.DisplayObjectContainer} container - The container to set the timer text as
* a child of.
*/
Timer.prototype.addText = function(container) {
container.addChild(this.text);
};
Timer.prototype.reset = function() {
this.text.parent.removeChild(this.text);
clearInterval(this.textUpdate);
};
var main = function() {
tagpro.socket.on("spawn", function(update) {
var respawnTime = Date.now() + update.w;
var pos = {
x: update.x,
y: update.y
};
new Timer(pos, respawnTime);
});
};
addToTagproReady(function() {
main();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment