SHARE
TWEET

Untitled

a guest Nov 9th, 2018 124 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ===================================================================
  2. #
  3. #   Script: Main
  4. #
  5. # ===================================================================
  6. #
  7. #   Entry point of your game.
  8. #
  9. # ===================================================================
  10. class Main
  11.     ###*
  12.     * Controls the boot-process of the game.
  13.     *
  14.     * @module gs
  15.     * @class Main
  16.     * @memberof gs
  17.     * @constructor
  18.     ###
  19.     constructor: ->
  20.         window.$ = jQuery.noConflict()
  21.  
  22.         @languagesLoaded = no
  23.         @frameCallback = null
  24.  
  25.     ###*
  26.     * Updates the current frame.
  27.     *
  28.     * @method updateFrame
  29.     ###
  30.     updateFrame: ->
  31.         if $PARAMS.showDebugInfo
  32.             window.startTime = if window.performance? then window.performance.now() else Date.now()
  33.  
  34.         SceneManager.update()
  35.         Graphics.frameCount++
  36.  
  37.         if $PARAMS.showDebugInfo
  38.             if not @debugSprite? then @debugSprite = new Sprite_Debug()
  39.  
  40.             window.endTime = if window.performance? then window.performance.now() else Date.now()
  41.             if Graphics.frameCount % 30 == 0
  42.                 @debugSprite.frameTime = (endTime - startTime)
  43.                 @debugSprite.redraw()
  44.  
  45.     ###*
  46.     * Loads game data.
  47.     *
  48.     * @method loadData
  49.     ###
  50.     loadData: ->
  51.         RecordManager.load()
  52.         DataManager.getDocumentsByType("global_variables")
  53.         DataManager.getDocumentsByType("language_profile")
  54.         DataManager.getDocumentsByType("vn.chapter")
  55.  
  56.     ###*
  57.     * Loads system data.
  58.     *
  59.     * @method loadSystemData
  60.     ###
  61.     loadSystemData: ->
  62.         DataManager.getDocument("RESOURCES")
  63.         DataManager.getDocument("SUMMARIES")
  64.  
  65.     ###*
  66.     * Loads system resources such as graphics, sounds, fonts, etc.
  67.     *
  68.     * @method loadSystemResources
  69.     ###
  70.     loadSystemResources: ->
  71.         ResourceManager.loadFonts()
  72.         ResourceLoader.loadSystemSounds(RecordManager.system)
  73.         ResourceLoader.loadSystemGraphics(RecordManager.system)
  74.  
  75.         for language in LanguageManager.languages
  76.             if language.icon?.name?.length > 0
  77.                 ResourceManager.getBitmap("Graphics/Icons/#{language.icon.name}")
  78.  
  79.         gs.Fonts.initialize()
  80.  
  81.     ###*
  82.     * Gets game settings.
  83.     *
  84.     * @method getSettings
  85.     ###
  86.     getSettings: ->
  87.         settings = GameStorage.getObject("settings")
  88.  
  89.         if not settings? or settings.version != 342
  90.             GameManager.resetSettings()
  91.             settings = GameManager.settings
  92.  
  93.         return settings
  94.  
  95.     ###*
  96.     * Sets up the game's global data. If it is outdated, this method will
  97.     * reset the global game data.
  98.     *
  99.     * @method setupGlobalData
  100.     ###
  101.     setupGlobalData: ->
  102.         globalData = GameStorage.getObject("globalData")
  103.         GameManager.globalData = globalData
  104.  
  105.         if !globalData || globalData.version != 342
  106.             GameManager.resetGlobalData()
  107.  
  108.     ###*
  109.     * Sets up game settings.
  110.     *
  111.     * @method setupGameSettings
  112.     * @param {Object} settings - Current game settings.
  113.     ###
  114.     setupGameSettings: (settings) ->
  115.         GameManager.settings = settings
  116.         GameManager.settings.fullScreen = Graphics.isFullscreen()
  117.  
  118.         for character, i in RecordManager.charactersArray
  119.             if character and !GameManager.settings.voicesByCharacter[character.index]
  120.                 GameManager.settings.voicesByCharacter[character.index] = 100
  121.         for cg, i in RecordManager.cgGalleryArray
  122.             if cg? and !GameManager.globalData.cgGallery[cg.index]
  123.                 GameManager.globalData.cgGallery[cg.index] = { unlocked: no }
  124.  
  125.     ###*
  126.     * Sets up audio settings.
  127.     *
  128.     * @method setupAudioSettings
  129.     * @param {Object} settings - Current game settings.
  130.     ###
  131.     setupAudioSettings: (settings) ->
  132.         AudioManager.generalSoundVolume = settings.seVolume
  133.         AudioManager.generalMusicVolume = settings.bgmVolume
  134.         AudioManager.generalVoiceVolume = settings.voiceVolume
  135.  
  136.     ###*
  137.     * Sets up video settings.
  138.     *
  139.     * @method setupVideoSettings
  140.     * @param {Object} settings - Current game settings.
  141.     ###
  142.     setupVideoSettings: (settings) ->
  143.         settings.renderer = 1
  144.         Graphics.keepRatio = !settings.adjustAspectRatio
  145.         Graphics.onResize()
  146.  
  147.     ###*
  148.     * Sets up settings.
  149.     *
  150.     * @method setupSettings
  151.     ###
  152.     setupSettings: ->
  153.         settings = @getSettings()
  154.  
  155.         @setupGlobalData()
  156.         @setupGameSettings(settings)
  157.         @setupAudioSettings(settings)
  158.         @setupVideoSettings(settings)
  159.  
  160.  
  161.         GameStorage.setObject("settings", settings)
  162.  
  163.     ###*
  164.     * Loads all system resources needed to start the actual game.
  165.     *
  166.     * @method load
  167.     * @param {Function} callback - Called when all system resources are loaded.
  168.     ###
  169.     load: (callback) ->
  170.         @loadSystemData()
  171.  
  172.         DataManager.events.on "loaded", =>
  173.             GameManager.tempFields = new gs.GameTemp()
  174.             window.$tempFields = GameManager.tempFields
  175.  
  176.             if @languagesLoaded
  177.                 RecordManager.initialize()
  178.                 LanguageManager.initialize()
  179.                 SceneManager.initialize()
  180.                 @setupSettings()
  181.             else
  182.                 @loadData()
  183.  
  184.             if @languagesLoaded
  185.                 @loadSystemResources()
  186.                 DataManager.events.off "loaded"
  187.                 ResourceManager.events.on "loaded", =>
  188.                     GameManager.setupCursor()
  189.                     ResourceManager.events.off "loaded"
  190.                     ui.UIManager.setup()
  191.                     callback()
  192.  
  193.             @languagesLoaded = yes
  194.  
  195.  
  196.     ###*
  197.     * Sets up the application.
  198.     *
  199.     * @method setupApplication
  200.     ###
  201.     setupApplication: ->
  202.         $PARAMS.showDebugInfo = no
  203.         window.ResourceManager = new window.ResourceManager()
  204.         window.DataManager = new window.DataManager()
  205.  
  206.         # Force OpenGL renderer
  207.         window.Graphics = new Graphics_OpenGL()
  208.         window.gs.Graphics = window.Graphics
  209.         window.Renderer = window.Renderer_OpenGL
  210.  
  211.         # Force linear filtering
  212.         Texture2D.filter = 1
  213.  
  214.     ###*
  215.     * Initializes the input system to enable support for keyboard, mouse, touch, etc.
  216.     *
  217.     * @method setupInput
  218.     ###
  219.     setupInput: ->
  220.         Input.initialize()
  221.         Input.Mouse.initialize()
  222.  
  223.     ###*
  224.     * Initializes the video system with the game's resolution. It is necessary to
  225.     * call this method before using graphic object such as bitmaps, sprites, etc.
  226.     *
  227.     * @method setupVideo
  228.     ###
  229.     setupVideo: ->
  230.         @frameCallback = @createFrameCallback()
  231.  
  232.         Graphics.initialize($PARAMS.resolution.width, $PARAMS.resolution.height)
  233.         #Graphics.onFocusReceive = => GameManager.tempSettings.skip = no
  234.         Graphics.onDispose = => ResourceManager.dispose()
  235.         Graphics.formats = [320, 384, 427]
  236.         Graphics.scale = 0.5 / 240 * Graphics.height
  237.         Font.defaultSize = Math.round(9 / 240 * Graphics.height)
  238.  
  239.         Graphics.onEachFrame(@frameCallback)
  240.  
  241.     ###*
  242.     * Registers shader-based effects. It is important to register all effects
  243.     * before the graphics system is initialized.
  244.     *
  245.     * @method setupEffects
  246.     ###
  247.     setupEffects: ->
  248.         # Register built-in LOD/Box Blur effect
  249.         gs.Effect.registerEffect(gs.Effect.fragmentShaderInfos.lod_blur)
  250.         # Register built-in pixelate effect
  251.         gs.Effect.registerEffect(gs.Effect.fragmentShaderInfos.pixelate)
  252.  
  253.         # This is an example of how to register your own shader-effect.
  254.         # See Effects > CircularDistortionEffect script for more info.
  255.         # gs.CircularDistortionEffect.register()
  256.     ###*
  257.     * Initializes the Live2D. If Live2D is not available, it does nothing. Needs to be
  258.     * called before using Live2D.
  259.     *
  260.     * @method setupLive2D
  261.     ###
  262.     setupLive2D: ->
  263.         Live2D.init()
  264.         Live2D.setGL($gl)
  265.         Live2DFramework.setPlatformManager(new L2DPlatformManager())
  266.  
  267.     ###*
  268.     * Creates the frame-callback function called once per frame to update and render
  269.     * the game.
  270.     *
  271.     * @method setupLive2D
  272.     * @return {Function} The frame-callback function.
  273.     ###
  274.     createFrameCallback: ->
  275.         callback = null
  276.  
  277.         if $PARAMS.preview? or ($PARAMS.testOffline && window.parent != window)
  278.             callback = (time) =>
  279.                 try
  280.                     if $PARAMS.preview && !$PARAMS.preview.error
  281.                         @updateFrame()
  282.                 catch ex
  283.                     if $PARAMS.preview or GameManager.inLivePreview
  284.                         $PARAMS.preview = error: ex
  285.                     console.log(ex)
  286.         else
  287.             callback = (time) => @updateFrame()
  288.  
  289.         return callback
  290.  
  291.     ###*
  292.     * Creates the start scene object. If an intro-scene is set, this method returns the
  293.     * intro-scene. If the game runs in Live-Preview, this method returns the selected
  294.     * scene in editor.
  295.     *
  296.     * @method createStartScene
  297.     * @return {gs.Object_Base} The start-scene.
  298.     ###
  299.     createStartScene: ->
  300.         scene = null
  301.         introScene = null
  302.  
  303.         if RecordManager.system.useIntroScene
  304.             introScene = DataManager.getDocumentSummary(RecordManager.system.introInfo?.scene?.uid)
  305.  
  306.         if $PARAMS.preview or introScene
  307.             scene = new vn.Object_Scene()
  308.             scene.sceneData.uid = $PARAMS.preview?.scene.uid || RecordManager.system.introInfo?.scene?.uid
  309.             scene.events.on "dispose", (e) -> GameManager.sceneData.uid = null
  310.         else if LanguageManager.languages.length > 1
  311.             scene = new gs.Object_Layout("languageMenuLayout")
  312.         else
  313.             scene = new gs.Object_Layout("titleLayout")
  314.  
  315.         return scene
  316.  
  317.     ###*
  318.     * Boots the game by setting up the application window as well as the video, audio and input system.
  319.     *
  320.     * @method start
  321.     ###
  322.     start: ->
  323.         @setupApplication()
  324.         @setupEffects()
  325.         @setupVideo()
  326.         @setupLive2D()
  327.         @setupInput()
  328.  
  329.         @load => SceneManager.switchTo(@createStartScene())
  330.  
  331.  
  332. # The entry point of the game.
  333. gs.Main = new Main()
  334. gs.Application.initialize()
  335. gs.Application.onReady = ->
  336.     # Add meta data to all class objects necessary for object serialization.
  337.     Object.keys(gs).forEach (k) -> gs[k].$namespace = "gs"; gs[k].$name = k
  338.     Object.keys(vn).forEach (k) -> vn[k].$namespace = "vn"; vn[k].$name = k
  339.     Object.keys(ui).forEach (k) -> ui[k].$namespace = "ui"; ui[k].$name = k
  340.  
  341.     gs.Main.start()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top