[ACE] AEE - Music Player

Introduction

The first real AEE script!
This offers you a wide range of features, most of which are optional.

Features

  • Locking/Unlocking of tracks
  • Cover art
  • Locked cover art
  • Allow unlocking of music by hearing it in the game
  • Allow the player to hear the music on the map
  • Ability to add tracks on the fly
  • Ability to lock/unlock tracks on the fly

Requirements

Requires: AEE - Data Manager: http://www.planetdev.co.uk/viewtopic.php?f=15&p=371
Place this music player script below AEE - Data Manager

Screenshots

Image

Script

  1. #===============================================================================
  2. # * AEE - Music Player
  3. # * By Crazyninjaguy
  4. # * http://www.planetdev.co.uk
  5. # * Part of Cng's Ace Engine Evolution
  6. #  ---------------------------------------------------------------------------
  7. # * REQUIRES: AEE - Data Manager
  8. #   http://www.planetdev.co.uk/viewtopic.php?f=15&p=371
  9. #  ---------------------------------------------------------------------------
  10. # * INSTALLATION:
  11. #   Put this script below AEE - Data Manager.
  12. #  ---------------------------------------------------------------------------
  13. # * USAGE:
  14. #   Follow the instructions in script to set it up properly.
  15. #   To call this from an event command, use SceneManager.goto(Scene_Music)
  16. #   When AEE - Menu is finished, this script will have a menu option.
  17. #  ---------------------------------------------------------------------------
  18. # * SCRIPT COMMANDS:
  19. #   The following can be used as script commands:
  20. #
  21. #   $game_music.lock("Track Name")
  22. #   $game_music.unlock("Track Name")
  23. #   $game_music.add("Track Name", unlocked, "Cover Art", "Locked Cover Art")
  24. #   Set the unlocked value to true or false depending on whether you want them
  25. #   to be able to access it straight away.
  26. #
  27. #===============================================================================
  28.  
  29. $imported = {} if $imported == nil
  30. $imported["AEE-MusicPlayer"] = true
  31.  
  32. module AEE
  33.   module Music
  34.    
  35.     # Icon ID of the icon relating to Unlocked Items
  36.     UNLOCKED_ICON = 189
  37.     # Icon ID of the icon relating to locked Items
  38.     LOCKED_ICON = 187
  39.     # Show locked tracks in the track selector?
  40.     SHOW_LOCKED_ITEMS = true
  41.     # Unlock music just by hearing it in game?
  42.     UNLOCK_BY_HEARING = true
  43.     # Keep playing the music when the player exits this scene?
  44.     KEEP_PLAYING_ON_EXIT = true
  45.     # Default Cover art filename in Graphics/Pictures 150 pixels x 150 pixels.
  46.     # Used for tracks added by the system.
  47.     DEFAULT_COVER = "Cover"
  48.     # Default locked Cover art filename in Graphics/Pictures 150 pixels x 150 pixels.
  49.     # Used for tracks added by the system.
  50.     DEFAULT_LOCKED = "Cover_locked"
  51.    
  52.     # Leave the next line alone
  53.     FILES = []
  54.     # These are the tracks available to the player by default.
  55.     # FILES[Index] = ["Filename", unlocked, "Cover Image", "Locked Cover Image"]
  56.     # Set unlocked to true or false, depending on whether you want the player
  57.     # to be able to access it straight away.
  58.     FILES[0] = ["Battle1", true, "Cover", "Cover_locked"]
  59.     FILES[1] = ["Battle2", true, "", "Cover_locked"]
  60.     FILES[2] = ["Battle3", true, "", "Cover_locked"]
  61.     FILES[3] = ["Battle4", true, "", "Cover_locked"]
  62.     FILES[4] = ["Battle5", false, "", "Cover_locked"]
  63.     FILES[5] = ["Battle6", false, "", "Cover_locked"]
  64.     FILES[6] = ["Battle7", false, "", "Cover_locked"]
  65.     FILES[7] = ["Battle8", true, "", "Cover_locked"]
  66.     FILES[8] = ["Battle9", true, "", "Cover_locked"]
  67.   end
  68. end
  69.  
  70. class Game_Music
  71.   include AEE::Music
  72.   attr_accessor :files
  73.   attr_accessor :filenames
  74.   def initialize
  75.     @files = []
  76.     @filenames = []
  77.     for i in 0...FILES.size
  78.       @files.push(Game_MusicFile.new(FILES[i][0], FILES[i][1], FILES[i][2], FILES[i][3]))
  79.       @filenames.push(FILES[i][0])
  80.     end
  81.   end
  82.   def unlock(name)
  83.     for i in [email hidden]
  84.       if @filenames[i] == name
  85.         @files[i].unlocked = true
  86.       end
  87.     end
  88.   end
  89.   def lock(name)
  90.     for i in [email hidden]
  91.       if @filenames[i] == name
  92.         @files[i].unlocked = false
  93.       end
  94.     end
  95.   end
  96.   def add(name, unlocked, image, locked_image)
  97.     if [email protected]?(name)
  98.       @filenames.push(name)
  99.       @files.push(Game_MusicFile.new(name, unlocked, image, locked_image))
  100.     end
  101.   end
  102. end
  103.  
  104. class Game_MusicFile
  105.   attr_accessor :filename
  106.   attr_accessor :unlocked
  107.   attr_accessor :image
  108.   attr_accessor :unlocked_image
  109.   def initialize(filename, unlocked, image, unlocked_image)
  110.     @filename = filename
  111.     @unlocked = unlocked
  112.     @image = image
  113.     @unlocked_image = unlocked_image
  114.   end
  115. end
  116.  
  117. class Scene_Music < Scene_Base
  118.   include AEE::Music
  119.   def start
  120.     super
  121.     create_command_window
  122.     @index = 0
  123.   end
  124.   def terminate
  125.     super
  126.   end
  127.   def update
  128.     super
  129.     if @index != @command_window.index
  130.       if SHOW_LOCKED_ITEMS
  131.         @music_info.refresh($game_music.files[@command_window.index])
  132.         @index = @command_window.index
  133.       else
  134.         @music_info.refresh(@unlocked[@command_window.index])
  135.         @index = @command_window.index
  136.       end
  137.     end
  138.     if Input.trigger?(:B)
  139.       Sound.play_cancel
  140.       if !KEEP_PLAYING_ON_EXIT
  141.         Audio.bgm_stop
  142.       end
  143.       SceneManager.goto(Scene_Map)
  144.     end
  145.   end
  146.   def create_command_window
  147.     @command_window = Window_MusicCommand.new
  148.     @command_window.height = Graphics.height / 2
  149.     @command_window.y = Graphics.height / 2
  150.     @music_info = Window_MusicInfo.new($game_music.files[@command_window.index])
  151.     if !SHOW_LOCKED_ITEMS
  152.       @unlocked = []
  153.       for i in 0...$game_music.files.size
  154.         if $game_music.files[i].unlocked
  155.           @unlocked.push($game_music.files[i])
  156.         end
  157.       end
  158.     end
  159.   end
  160. end
  161.  
  162. class Window_MusicCommand < Window_Command
  163.   include AEE::Music
  164.   def initialize
  165.     super(0, 0)
  166.   end
  167.   def window_width
  168.     return Graphics.width
  169.   end
  170.   def visible_line_number
  171.     item_max
  172.   end
  173.   def make_command_list
  174.     add_main_commands
  175.   end
  176.   def col_max
  177.     return 2
  178.   end
  179.   def add_main_commands
  180.     for i in 0...$game_music.files.size
  181.       if SHOW_LOCKED_ITEMS
  182.         add_command($game_music.files[i].filename, $game_music.files[i].filename, $game_music.files[i].unlocked)
  183.       else
  184.         if $game_music.files[i].unlocked
  185.           add_command($game_music.files[i].filename, $game_music.files[i].filename, $game_music.files[i].unlocked)
  186.         end
  187.       end
  188.     end
  189.   end
  190.   def draw_item(index)
  191.     change_color(normal_color, command_enabled?(index))
  192.     temp = item_rect_for_text(index)
  193.     temp.x += 24
  194.     draw_text(temp, command_name(index), alignment)
  195.     if SHOW_LOCKED_ITEMS
  196.       if $game_music.files[index].unlocked
  197.         draw_icon(UNLOCKED_ICON, temp.x - 24, temp.y)
  198.       else
  199.         draw_icon(LOCKED_ICON, temp.x - 24, temp.y)
  200.       end
  201.     else
  202.       draw_icon(UNLOCKED_ICON, temp.x - 24, temp.y)
  203.     end
  204.   end
  205.   def call_ok_handler
  206.     if handle?(current_symbol)
  207.       call_handler(current_symbol)
  208.     elsif handle?(:ok)
  209.       super
  210.     else
  211.       activate
  212.       Audio.bgm_play("Audio/BGM/#{$game_music.files[index].filename}", 100, 100)
  213.     end
  214.   end
  215. end
  216.  
  217. class AEEDataManager
  218.   alias aee_datamanager_musicplayer_createobjects create_extra_objects
  219.   def create_extra_objects
  220.     aee_datamanager_musicplayer_createobjects
  221.     $game_music = Game_Music.new
  222.   end
  223.   alias aee_datamanager_musicplayer_getfilestosave get_files_to_save
  224.   def get_files_to_save(contents)
  225.     aee_datamanager_musicplayer_getfilestosave(contents)
  226.     contents[:music] = $game_music
  227.   end
  228.   alias aee_datamanager_musicplayer_extractfiles extract_save_contents
  229.   def extract_save_contents(contents)
  230.     aee_datamanager_musicplayer_extractfiles(contents)
  231.     $game_music = contents[:music]
  232.   end
  233. end
  234.  
  235. class Window_MusicInfo < Window_Base
  236.   def initialize(song)
  237.     super(0, 0, Graphics.width, Graphics.height / 2)
  238.     @cover = Sprite_Cover.new(song)
  239.     @cover.z = self.z + 1
  240.     refresh(song)
  241.   end
  242.   def refresh(song)
  243.     self.contents.clear
  244.     @cover.refresh(song)
  245.     if song.unlocked
  246.       self.contents.font.color = power_up_color
  247.     else
  248.       self.contents.font.color = power_down_color
  249.     end
  250.     self.contents.draw_text(0, 0, width - 32, 24, song.filename, 1)
  251.     self.contents.font.color = normal_color
  252.   end
  253.   alias aee_musicinfo_musicplayer_dispose dispose
  254.   def dispose
  255.     aee_musicinfo_musicplayer_dispose
  256.     @cover.dispose
  257.   end
  258. end
  259.  
  260. class Game_Map
  261.   include AEE::Music
  262.   alias aee_gamemap_musicplayer_autoplay autoplay
  263.   def autoplay
  264.     aee_gamemap_musicplayer_autoplay
  265.     if UNLOCK_BY_HEARING
  266.       if !$game_music.filenames.include?(@map.bgm.name)
  267.         $game_music.files.push(Game_MusicFile.new(@map.bgm.name, true, "", ""))
  268.         $game_music.filenames.push(@map.bgm.name)
  269.       end
  270.     end
  271.   end
  272. end
  273.  
  274. class Sprite_Cover < Sprite_Base
  275.   def initialize(file)
  276.     super(nil)
  277.     refresh(file)
  278.   end
  279.   def dispose
  280.     super
  281.     self.bitmap.dispose
  282.   end
  283.   def refresh(file)
  284.     if file.unlocked
  285.       self.bitmap = Cache.picture(file.image)
  286.     else
  287.       self.bitmap = Cache.picture(file.unlocked_image)
  288.     end
  289.     self.x = (Graphics.width - self.bitmap.width) / 2
  290.     self.y = (((Graphics.height / 2) - self.bitmap.width) / 2) + 10
  291.   end
  292. end
  293.  
  294. class Game_Interpreter
  295.   include AEE::Music
  296.   alias aee_interpreter_musicplayer_command241 command_241
  297.   def command_241
  298.     aee_interpreter_musicplayer_command241
  299.     if UNLOCK_BY_HEARING
  300.       if !$game_music.filenames.include?(@params[0].name)
  301.         $game_music.files.push(Game_MusicFile.new(@params[0].name, true, DEFAULT_COVER, DEFAULT_LOCKED))
  302.         $game_music.filenames.push(@params[0].name)
  303.       end
  304.     end
  305.   end
  306. end
  307.  
  308. class Scene_Map < Scene_Base
  309.   include AEE::Music
  310.   alias aee_scenemap_musicplayer_prebattlescene pre_battle_scene
  311.   def pre_battle_scene
  312.     aee_scenemap_musicplayer_prebattlescene
  313.     if UNLOCK_BY_HEARING
  314.       if !$game_music.filenames.include?($game_system.battle_bgm.name)
  315.         $game_music.files.push(Game_MusicFile.new($game_system.battle_bgm.name, true, DEFAULT_COVER, DEFAULT_LOCKED))
  316.         $game_music.filenames.push($game_system.battle_bgm.name)
  317.       end
  318.     end
  319.   end
  320. end
Expand to see the code.


Credits

Credit me if used please!
Not available for use in commercial projects unless i'm contacted about it. Thanks.
Dude this is brilliant, I love the fact you can unlock things as you play. For a game with a good and lengthy musical score this could be a great addition.
Thanks!
I made this with everything I thought anyone could possibly want, so glad to hear you like it!

Any suggestions for features?

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=76092