Skip to content

Instantly share code, notes, and snippets.

@Cazra
Last active August 29, 2015 14:05
Show Gist options
  • Save Cazra/b9451719989526f5f6df to your computer and use it in GitHub Desktop.
Save Cazra/b9451719989526f5f6df to your computer and use it in GitHub Desktop.
roll 20 Carry script v2
/**
* A set of chat commands used to set tokens to carry other tokens.
* When a token moves, the any tokens it is carrying move to its new location.
*
* The following commands are available:
* !carry When this command is entered, all selected tokens become
* carried by the bottommost selected token.
* !drop [name of carried token]
* When this command is entered, the selected token drops the
* token with the specified name.
* !dropAll
* When this command is entered, the selected token drops all
* tokens it is carrying.
*/
var _carryCmd = function(carrier, token) {
if(!carrier.carryList) {
carrier.carryList = [];
}
carrier.carryList.push(token);
}
var _dropCmd = function(carrier, token) {
if(!carrier.carryList) {
return;
}
var index = carrier.carryList.indexOf(token);
if(index !== -1) {
carrier.carryList.splice(index, 1);
}
}
/** Interpret the chat commands. */
on("chat:message", function(msg) {
var cmdName;
var msgTxt;
var curPageID = Campaign().get("playerpageid");
var tokens = [];
if(msg.selected) {
for(var i=0; i<msg.selected.length; i++) {
tokens.push(findObjs({
_id: msg.selected[i]._id,
_pageid: curPageID
})[0]
);
}
}
else {
return;
}
cmdName = "!carry";
msgTxt = msg.content;
if(msg.type == "api" && msgTxt.indexOf(cmdName) !== -1) {
tokens.sort(function(a,b) {
var aY = parseFloat(a.get("top"));
var bY = parseFloat(b.get("top"));
return bY - aY;
});
var carrier = tokens[0];
var size = tokens.length;
for(var i=0; i<size; i++) {
var token = tokens[i];
var tokenName = token.get("name");
if(token !== carrier) {
sendChat("", "" + carrier.get("name") + " picked up " + tokenName + ".");
_carryCmd(carrier, token);
toFront(carrier);
}
}
}
cmdName = "!drop ";
msgTxt = msg.content;
if(msg.type == "api" && msgTxt.indexOf(cmdName) !== -1) {
var carrier = tokens[0];
var tokenName = msgTxt.substring(cmdName.length);
var token = findObjs({
_pageid: curPageID,
name: tokenName
})[0];
if(token === undefined) {
var playerName = msg.who;
if(playerName.indexOf(" ") !== -1) {
playerName = playerName.substring(0, playerName.indexOf(" "));
}
sendChat("A mysterious voice", "/w " + playerName + " " +
carrier.get("name") + " is not carrying " + tokenName);
}
else {
sendChat("", carrier.get("name") + " dropped " + token.get("name"));
_dropCmd(carrier, token);
toBack(carrier);
}
}
cmdName = "!dropAll";
msgTxt = msg.content;
if(msg.type == "api" && msgTxt.indexOf(cmdName) !== -1) {
var carrier = tokens[0];
var listCopy = carrier.carryList.slice(0);
for(var i=0; i< listCopy.length; i++) {
var token = listCopy[i];
_dropCmd(carrier, token);
sendChat(msg.who, "/em : " + carrier.get("name") + " dropped " + token.get("name"));
}
}
});
/** Do the carrying logic when the carrier tokens move. */
on("change:graphic", function(obj, prev) {
if(obj.carryList) {
for(var i=0; i<obj.carryList.length; i++) {
var carried = obj.carryList[i];
carried.set("left", obj.get("left"));
carried.set("top", obj.get("top"));
carried.set("rotation", obj.get("rotation"));
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment