Skip to content

Instantly share code, notes, and snippets.

@BuncyTheFrog
Last active January 13, 2019 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BuncyTheFrog/76dad3d45050c90be11e189da8699729 to your computer and use it in GitHub Desktop.
Save BuncyTheFrog/76dad3d45050c90be11e189da8699729 to your computer and use it in GitHub Desktop.
//This is very optional - it's for movement with the arrow keys which looks worse than the below commands
on("change:graphic", function(obj, prev) {
if (obj.get("pageid") == findObjs({type: 'page', name: 'Roleplay'})[0].get("_id")) {
//Dragging the token from one part of the screen to another is not a reason to flip them around.
if (obj.get("top") != prev.top) {
return;
}
if (obj.get("left") < prev.left && obj.get("fliph") ) {
obj.set( {
fliph: false
});
}
else if (obj.get("left") > prev.left && ! obj.get("fliph") ) {
obj.set( {
fliph: true
});
}
}
});
//Commands:
//!reg: Lets a player use the macro buttons to control their character without having it selected
//The below commands affect the selected token, or if there aren't any, the registered token
//!left & !right: Moves the token one space in that direction, and makes sure they're facing the right way
//!turn: Flips the token horizontally
//!neutral & !happy & !sad & !angry: Changes the token's expression - will not stick if token is talking
//!talk: (Toggle) The token alternates between the "neutral" and "talk" states with a random period
//When setting up your token, this script expects the following rollable table: Neutral, Talk, Happy, Angry, Sad
//This script expects that:
//Your roleplaying is done on an 18x10 (1260x700 px) map named Roleplay
//Your talk tokens are 2x2 squares in size
//You have an "acting area" that is on row 5 and 6
on("chat:message", function(msg) {
if(msg.type == "api") {
if (!state.talkToggles) {
state.talkToggles = {};
}
if (!state.talkPrev) {
state.talkPrev = {};
}
if (!state.playerToks) {
state.playerToks = {};
}
var currentPageID = Campaign().get('playerpageid');
if (currentPageID != findObjs({type: 'page', name: 'Roleplay'})[0].get("_id") && !playerIsGM(msg.playerid)) {
return;
}
if (msg.content.startsWith("!reg")) {
if (typeof msg.selected == 'undefined') {
sendChat('System', '/w ' + msg.who + ' You need to select your token to do this');
return;
}
if (msg.selected.length > 1) {
sendChat('System', '/w ' + msg.who + ' select only one token to register');
return;
}
var pid = msg.playerid;
state.playerToks[pid] = msg.selected[0]._id;
tok = getObj('graphic',msg.selected[0]._id);
sendChat('System', '/w ' + msg.who + ' You can now control ' + tok.get('name') + ' without having them selected!');
return;
}
var tok;
if (typeof msg.selected != 'undefined') {
tok = getObj('graphic',msg.selected[0]._id);
}
else {
if (!state.playerToks.hasOwnProperty(msg.playerid)) {
sendChat('System', '/w ' + msg.who + ' You need to select your token to do this');
return;
}
tok = getObj('graphic',state.playerToks[msg.playerid]);
if (typeof tok == 'undefined') {
sendChat('System', '/w ' + msg.who + " Your registered token doesn't seem to exist. Please !reg your token in case it's been replaced.");
return;
}
}
if(typeof tok == 'undefined') {
sendChat('System', '/w ' + msg.who + ' You need to select or register your token to do this');
return;
}
if(msg.content.startsWith("!left")) {
var newPos;
var tokTop = 350;
if (tok.get('top') != 350) {
newPos = 1190;
}
else {
var oldPos = tok.get('left');
if (oldPos <= 70) {
newPos = 70;
tokTop = 140;
}
else {
newPos = oldPos - 140;
}
newPos = newPos - (newPos - 70) % 140;
}
tok.set({
fliph: false,
top: tokTop,
left: newPos
});
return;
}
if(msg.content.startsWith("!right")) {
var newPos;
var tokTop = 350;
if (tok.get('top') != 350) {
newPos = 70;
}
else {
var oldPos = tok.get('left');
if (oldPos >= 1190) {
newPos = 70;
tokTop = 140;
}
else {
newPos = oldPos + 140;
}
newPos = newPos + (newPos - 70) % 140;
}
tok.set({
fliph: true,
top: tokTop,
left: newPos
});
return;
}
if (tok.get('top') != 350 && !playerIsGM(msg.playerid)) {
sendChat('System', '/w ' + msg.who + ' Your token is not on stage.');
return;
}
if(msg.content.startsWith("!turn")) {
tok.set({
fliph: !tok.get('fliph')
});
return;
}
if(msg.content.startsWith("!talk")) {
var tid = tok.get('_id');
if (!state.talkToggles.hasOwnProperty(tid)) {
state.talkToggles[tid] = false;
}
startTalking = !state.talkToggles[tid];
state.talkToggles[tid] = startTalking;
if (startTalking) {
state.talkPrev[tid] = tok.get('currentSide');
var talk = setInterval(function() {
nextSide = tok.get('currentSide') + 1;
nextSide %= 2;
changeSide(tok, nextSide);
if (!state.talkToggles[tid]) {
clearInterval(talk);
changeSide(tok, state.talkPrev[tid]);
}
}, 100 + randomInteger(300));
}
return;
}
if(msg.content.startsWith("!neutral")) {
changeEmotion(tok, 0);
return;
}
if(msg.content.startsWith("!happy")) {
changeEmotion(tok, 2);
return;
}
if(msg.content.startsWith("!angry")) {
changeEmotion(tok, 3);
return;
}
if(msg.content.startsWith("!sad")) {
changeEmotion(tok, 4);
return;
}
function changeSide(tok, sideNum) {
sides = tok.get('sides').split('|');
fullurl = decodeURIComponent(sides[sideNum]);
fullurl = fullurl.replace('med','thumb');
tok.set({
currentSide: sideNum,
imgsrc: fullurl
});
}
function changeEmotion(tok, sideNum) {
tid = tok.get('_id');
if (state.talkToggles[tid]) {
state.talkPrev[tid] = sideNum;
state.talkToggles[tid] = false;
}
else {
changeSide(tok, sideNum);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment