Character Vault
Any Concept / Any System
Compendium
Your System Come To Life
Roll20 for Android
Streamlined for your Tablet
Roll20 for iPad
Streamlined for your Tablet

Personal tools

Difference between revisions of "Script:Dynamic Lighting Animation"

From Roll20 Wiki

Jump to: navigation, search
Line 1: Line 1:
 +
{{script overview
 +
|name=Dynamic Lighting Animation
 +
|author={{user profile|235259|Brian}}
 +
|version=2.0
 +
|lastmodified=2015-01-08}}
 +
 
The following script creates the API commands <code>!snapshot</code>, <code>!reset</code>, <code>!run</code>, and<code>!stop</code>.
 
The following script creates the API commands <code>!snapshot</code>, <code>!reset</code>, <code>!run</code>, and<code>!stop</code>.
  
==== Syntax ====
+
=== Syntax ===
 +
{{syntaxbox top|formal=true}}
 +
{{API command|snapshot}} {{API parameter|name=frames}}<br>
 +
{{API command|reset}}<br>
 +
{{API command|run}}<br>
 +
{{API command|stop}}
 +
{{Formal API command|
 +
{{token|S}} {{rarr}} {{API command|snapshot}} {{token|frames|-}}
 +
{{token|S}} {{rarr}} {{API command|reset}}<br>
 +
{{token|S}} {{rarr}} {{API command|run}}<br>
 +
{{token|S}} {{rarr}} {{API command|stop}}<br>
 +
{{token|frames}} {{rarr}} {{integer}}
 +
}}
 +
{{syntaxbox end}}
  
<blockquote style="border:1px #0088ee solid;background:#eee;padding:0.5em">!snapshot ''frames''<br>
+
{{param description top}}
!reset<br>
+
{{param description|name=frames|value=The number of frames to hold the snapshot when running the animation. One frame is equal to 1/20th of a second, so a value of 20 will hold the snapshot for 1 second.}}
!run<br>
+
{{param description bottom}}
!stop</blockquote>
+
  
{| class="wikitable"
+
=== Using the commands ===
|-
+
! Parameter
+
! Values
+
|-
+
| frames
+
| The number of frames to display the snapshot. The animation runs at 20fps, so using a value of 20 equates to a 1s snapshot.
+
|}
+
 
+
==== Installation ====
+
 
+
You need to enter the user ID of each GM in the game into the <code>gmIDs</code> variable (line 5). You can find someone's user ID from their wiki user page (there is a link to your personal user page in the sidebar of this wiki) or the URL of their profile page on the rest of the site.
+
 
+
If you do not accurately enter the GM's user IDs, the script will ignore your commands.
+
 
+
==== Using the commands ====
+
  
 
Only the GMs can use these API commands.
 
Only the GMs can use these API commands.
Line 34: Line 37:
  
 
You cannot record additional snapshots while the animation is running. If you need to change the animation or you're ready to record a different one, you need to use <code>!reset</code> to clear the old animation.
 
You cannot record additional snapshots while the animation is running. If you need to change the animation or you're ready to record a different one, you need to use <code>!reset</code> to clear the old animation.
 
==== Code ====
 
 
<pre data-language="javascript">
 
var animation = animation || {};
 
 
// Set to a list of the user IDs for each GM -- only the GM should be able to stop/start the animation!
 
// You can find your user ID by checking your userpage on the wiki, or the URL of your profile.
 
animation.gmIDs = [235259];
 
 
animation.running = false;
 
 
on('chat:message', function(msg) {
 
    if(msg.type != 'api') return;
 
   
 
    // Only use commands coming from a GM
 
    var isGM = false;
 
    var player = getObj('player', msg.playerid);
 
    _.each(animation.gmIDs, function(n) {
 
        if(player.get('d20userid') == n)
 
            isGM = true;
 
    });
 
    if(!isGM) return;
 
   
 
    var parts = msg.content.split(' ');
 
    var command = parts.shift().substring(1);
 
   
 
    switch(command)
 
    {
 
        case 'snapshot':
 
            if(animation.running)
 
            {
 
                sendChat("","/w gm You cannot add a frame while the animation is running.");
 
                break;
 
            }
 
           
 
            var pageid = Campaign().get('playerpageid');
 
            var dlPaths = findObjs({_type: 'path', _pageid: pageid, layer: 'walls'});
 
            var frames = parseInt(parts[0], 10);
 
            var pathdata = [];
 
           
 
            _.each(dlPaths, function(path) {
 
                var obj = {
 
                    id: path.id,
 
                    top: path.get('top'),
 
                    left: path.get('left'),
 
                    rotation: path.get('rotation'),
 
                    width: path.get('width'),
 
                    height: path.get('height'),
 
                    scaleX: path.get('scaleX'),
 
                    scaleY: path.get('scaleY')
 
                };
 
                pathdata.push(obj);
 
            });
 
            state.animation.frames.push({ data: pathdata, frames: frames });
 
            break;
 
        case 'reset':
 
            state.animation.curr_frame = 0;
 
            state.animation.frames = [];
 
            break;
 
        case 'run':
 
            animation.running = true;
 
            break;
 
        case 'stop':
 
            animation.running = false;
 
            break;
 
        default:
 
            break;
 
    }
 
});
 
 
on('ready', function() {
 
    if(!state.animation) state.animation = {};
 
    if(!state.animation.frames) state.animation.frames = [];
 
    if(!state.animation.curr_frame) state.animation.curr_frame = 0;
 
   
 
    var frame = state.animation.curr_frame;
 
    var frameCount = 0;
 
    setInterval(function() {
 
        if(!animation.running) return;
 
        if(!state.animation.frames[frame]) return;
 
       
 
        frameCount++;
 
        if(state.animation.frames[frame].frames <= frameCount)
 
        {
 
            animation.setupFrame(state.animation.frames[frame].data);
 
            frameCount -= state.animation.frames[frame].frames;
 
            frame++;
 
            if(frame == state.animation.frames.length) frame = 0;
 
            state.animation.curr_frame = frame;
 
        }
 
    }, 50);
 
});
 
 
/**
 
* Set the paths on the DL layer to the settings for the current animation frame.
 
*/
 
animation.setupFrame = function(pathdata) {
 
    _.each(pathdata, function(obj) {
 
        var path = getObj('path', obj.id);log(obj);
 
        path.set({
 
            top: obj.top,
 
            left: obj.left,
 
            rotation: obj.rotation,
 
            width: obj.width,
 
            height: obj.height,
 
            scaleX: obj.scaleX,
 
            scaleY: obj.scaleY
 
        });
 
    });
 
};
 
</pre>
 
  
 
==== Notes ====
 
==== Notes ====
  
 
This script does '''not''' create or delete paths. If you want a frame with one or more paths missing, you can simply move them off the map.
 
This script does '''not''' create or delete paths. If you want a frame with one or more paths missing, you can simply move them off the map.
 
[[Category:User API Scripts|Dynamic Lighting Animation]]
 
[[Category:API Commands|Dynamic Lighting Animation]]
 

Revision as of 22:20, 8 January 2015

API ScriptAuthor: Brian
Version: 2.0
Last Modified: 2015-01-08
Code: Dynamic Lighting Animation
Dependencies: None
Conflicts: None

The following script creates the API commands !snapshot, !reset, !run, and!stop.

Syntax

!snapshot <frames>
!reset
!run
!stop
Formally:

S

→ !snapshot frames


S

→ !reset

S

→ !run

S

→ !stop

frames

integer
Parameter Values
frames The number of frames to hold the snapshot when running the animation. One frame is equal to 1/20th of a second, so a value of 20 will hold the snapshot for 1 second.

Using the commands

Only the GMs can use these API commands.

In order to "record" the animation, move the player bookmark to the page with the DL layer you want to animate. Set up the paths on the DL layer as you want, and then use the !snapshot command.

The animation record will persist between game sessions, although the script is not set up to record different animations for different pages.

Once you're ready to show off your fantastical magical labyrinth to your players, use the !run command. Stop the animation with !stop.

You cannot record additional snapshots while the animation is running. If you need to change the animation or you're ready to record a different one, you need to use !reset to clear the old animation.

Notes

This script does not create or delete paths. If you want a frame with one or more paths missing, you can simply move them off the map.