Iavra

Iavra Splash Video

Dec 18th, 2015 (edited)
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*:
  2.  * @plugindesc Plays one or more videos prior to the title screen. They can be skipped by pressing a button.
  3.  * <Iavra Splash Video>
  4.  * @author Iavra
  5.  *
  6.  * @param Videos
  7.  * @desc Comma-separated list of videos to be played. Should exist as both .webm or .mp4 versions.
  8.  * @default
  9.  *
  10.  * @param Skip On Ok
  11.  * @desc If set to true, videos can be skipped by an "ok" event (Enter, Spacebar, Z, Left Mouse Click, Touch).
  12.  * @default true
  13.  *
  14.  * @param Skip On Cancel
  15.  * @desc If set to true, videos can be skipped by a "cancel" event (Esc, X, Right Mouse Click, Double Touch).
  16.  * @default true
  17.  *
  18.  * @param Skip All
  19.  * @desc If set to true, skipping one video will automatically skip all videos.
  20.  * @default false
  21.  *
  22.  * @help
  23.  * To play one or more videos prior to your title screen, put their .webm and .mp4 version in the "movies" folder of
  24.  * your game and add their names to the "Videos" plugin parameter.
  25.  *
  26.  * While a video is playing, it can be canceled by pressing either an "ok" button (if the parameter "Skip On Ok" is
  27.  * set to true) or a "cancel" button (if the parameter "Skip On Cancel" is set to true. If the parameter "Skip All"
  28.  * is set to true, this will skip all videos. Otherwise, the next one will start playing.
  29.  *
  30.  * The videos are playing, while game resources are being loaded, so this can also be used to cover up the rather
  31.  * boring part of your game.
  32.  */
  33. var IAVRA = IAVRA || {};
  34.  
  35. (function($) {
  36.     "use strict";
  37.  
  38.     /**
  39.      * Load plugin parameters independent from file name.
  40.      */
  41.     var _params = $plugins.filter(function(p) { return p.description.contains('<Iavra Splash Video>'); })[0].parameters;
  42.     var _param_videos = _params['Videos'].split(/\s*,\s*/).filter(function(v) { return !!v; });
  43.     var _param_skipOnOk = _params['Skip On Ok'].toLowerCase() === 'true';
  44.     var _param_skipOnCancel = _params['Skip On Cancel'].toLowerCase() === 'true';
  45.     var _param_skipAll = _params['Skip All'].toLowerCase() === 'true';
  46.  
  47.     /**
  48.      * If no videos are specified, we don't have any business here.
  49.      */
  50.     if(!_param_videos.length) { return; }
  51.  
  52.     /**
  53.      * Contains all video to be played. We can't initialize this directly, because the game not yet knows, what video
  54.      * file extension it should use.
  55.      */
  56.     var _videos = [];
  57.  
  58.     /**
  59.      * The currently playing video. If this is null, the next video in the list will be loaded and set to autoplay. The
  60.      * video will also be added to the DOM, so it's visible to the user.
  61.      */
  62.     var _video = null;
  63.  
  64.     /**
  65.      * Once no videos are left to play, this gets set to true and Scene_Boot.prototype.isReady finally returns true.
  66.      */
  67.     var _finished = false;
  68.  
  69.     /**
  70.      * Initializes all videos and preloads them. Once a video has finished playing, it will automatically remove itself
  71.      * from the document and set _video = null, which will cause the next video in the list to be played.
  72.      */
  73.     var _initialize = function() {
  74.         var videos = _param_videos, element, ext = Game_Interpreter.prototype.videoFileExt();
  75.         _videos.length = 0;
  76.         for(var i = 0, max = videos.length; i < max; ++i) {
  77.             var element = document.createElement('video');
  78.             element.id = 'splashVideo';
  79.             element.width = Graphics.width;
  80.             element.height = Graphics.height;
  81.             element.style.zIndex = 100;
  82.             Graphics._centerElement(element);
  83.             element.src = 'movies/' + videos[i] + ext;
  84.             element.onended = function() { _video = null; document.body.removeChild(this); };
  85.             element.onerror = function() { throw new Error('There was an error loading the splash video.'); };
  86.             element.load();
  87.             _videos.push(element);
  88.         }
  89.     };
  90.  
  91.     /**
  92.      * If there is currently a video playing, do nothing. Otherwise, load the next video, add it to the DOM and set it
  93.      * to autoplay. Once all videos have been played, set _finished to true, which will cause Scene_Boot to proceed.
  94.      */
  95.     var _playVideo = function() {
  96.         if(_finished || _video) { return; }
  97.         var video = _videos.shift();
  98.         if(video) {
  99.             _video = video;
  100.             document.body.appendChild(video);
  101.             video.currentTime = 0;
  102.             video.autoplay = true;
  103.         } else { _finished = true; }
  104.     };
  105.  
  106.     /**
  107.      * Pressing the ok button or doing a touch input (including mouse click) will skip the splash videos.
  108.      */
  109.     var _handleSkip = function() {
  110.         if(_param_skipOnCancel && (Input.isTriggered('cancel') || TouchInput.isCancelled())) { return _skip(); }
  111.         if(_param_skipOnOk && (Input.isTriggered('ok') || TouchInput.isTriggered())) { return _skip(); }
  112.     };
  113.  
  114.     /**
  115.      * If there is currently a video playing, pause it, remove it from the DOM and set _video to null. Also, empty the
  116.      * video list, which will cause _playVideo to set _finished to true.
  117.      */
  118.     var _skip = function() {
  119.         if(_video) { _video.pause(); document.body.removeChild(_video); _video = null; }
  120.         if(_param_skipAll) { _videos.length = 0; }
  121.     };
  122.  
  123.     //=============================================================================
  124.     // Scene_Boot
  125.     //=============================================================================
  126.  
  127.     (function($) {
  128.  
  129.         /**
  130.          * On scene create, initialize all videos, so they start preloading.
  131.          */
  132.         var alias_create = $.prototype.create;
  133.         $.prototype.create = function() {
  134.             alias_create.call(this);
  135.             _initialize();
  136.         };
  137.  
  138.         /**
  139.          * While Scene_Boot is loading everything else, the splash videos will play, so we also wait until _finished is
  140.          * set to true, before we continue to the next scene.
  141.          */
  142.         var alias_isReady = $.prototype.isReady;
  143.         $.prototype.isReady = function() {
  144.             _handleSkip();
  145.             _playVideo();
  146.             return _finished && alias_isReady.call(this);
  147.         };
  148.  
  149.     })(Scene_Boot);
  150.  
  151. })(IAVRA);
Add Comment
Please, Sign In to add comment