269 lines
11 KiB
Ruby
269 lines
11 KiB
Ruby
# -*- coding: utf-8 -*-
|
|
#==============================================================================
|
|
# ** DataManager
|
|
#------------------------------------------------------------------------------
|
|
# This module manages the database and game objects. Almost all of the
|
|
# global variables used by the game are initialized by this module.
|
|
#==============================================================================
|
|
|
|
module DataManager
|
|
#--------------------------------------------------------------------------
|
|
# * Module Instance Variables
|
|
#--------------------------------------------------------------------------
|
|
@last_savefile_index = 0 # most recently accessed file
|
|
#--------------------------------------------------------------------------
|
|
# * Initialize Module
|
|
#--------------------------------------------------------------------------
|
|
def self.init
|
|
@last_savefile_index = 0
|
|
load_database
|
|
create_game_objects
|
|
setup_battle_test if $BTEST
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Load Database
|
|
#--------------------------------------------------------------------------
|
|
def self.load_database
|
|
if $BTEST
|
|
load_battle_test_database
|
|
else
|
|
load_normal_database
|
|
check_player_location
|
|
end
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Load Normal Database
|
|
#--------------------------------------------------------------------------
|
|
def self.load_normal_database
|
|
$data_actors = load_data("Data/Actors.rvdata2")
|
|
$data_classes = load_data("Data/Classes.rvdata2")
|
|
$data_skills = load_data("Data/Skills.rvdata2")
|
|
$data_items = load_data("Data/Items.rvdata2")
|
|
$data_weapons = load_data("Data/Weapons.rvdata2")
|
|
$data_armors = load_data("Data/Armors.rvdata2")
|
|
$data_enemies = load_data("Data/Enemies.rvdata2")
|
|
$data_troops = load_data("Data/Troops.rvdata2")
|
|
$data_states = load_data("Data/States.rvdata2")
|
|
$data_animations = load_data("Data/Animations.rvdata2")
|
|
$data_tilesets = load_data("Data/Tilesets.rvdata2")
|
|
$data_common_events = load_data("Data/CommonEvents.rvdata2")
|
|
$data_system = load_data("Data/System.rvdata2")
|
|
$data_mapinfos = load_data("Data/MapInfos.rvdata2")
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Load Battle Test Database
|
|
#--------------------------------------------------------------------------
|
|
def self.load_battle_test_database
|
|
$data_actors = load_data("Data/BT_Actors.rvdata2")
|
|
$data_classes = load_data("Data/BT_Classes.rvdata2")
|
|
$data_skills = load_data("Data/BT_Skills.rvdata2")
|
|
$data_items = load_data("Data/BT_Items.rvdata2")
|
|
$data_weapons = load_data("Data/BT_Weapons.rvdata2")
|
|
$data_armors = load_data("Data/BT_Armors.rvdata2")
|
|
$data_enemies = load_data("Data/BT_Enemies.rvdata2")
|
|
$data_troops = load_data("Data/BT_Troops.rvdata2")
|
|
$data_states = load_data("Data/BT_States.rvdata2")
|
|
$data_animations = load_data("Data/BT_Animations.rvdata2")
|
|
$data_tilesets = load_data("Data/BT_Tilesets.rvdata2")
|
|
$data_common_events = load_data("Data/BT_CommonEvents.rvdata2")
|
|
$data_system = load_data("Data/BT_System.rvdata2")
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Check Player Start Location Existence
|
|
#--------------------------------------------------------------------------
|
|
def self.check_player_location
|
|
if $data_system.start_map_id == 0
|
|
msgbox(Vocab::PlayerPosError)
|
|
exit
|
|
end
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Create Game Objects
|
|
#--------------------------------------------------------------------------
|
|
def self.create_game_objects
|
|
$game_temp = Game_Temp.new
|
|
$game_system = Game_System.new
|
|
$game_timer = Game_Timer.new
|
|
$game_message = Game_Message.new
|
|
$game_switches = Game_Switches.new
|
|
$game_variables = Game_Variables.new
|
|
$game_self_switches = Game_SelfSwitches.new
|
|
$game_actors = Game_Actors.new
|
|
$game_party = Game_Party.new
|
|
$game_troop = Game_Troop.new
|
|
$game_map = Game_Map.new
|
|
$game_player = Game_Player.new
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Set Up New Game
|
|
#--------------------------------------------------------------------------
|
|
def self.setup_new_game
|
|
create_game_objects
|
|
$game_party.setup_starting_members
|
|
$game_map.setup($data_system.start_map_id)
|
|
$game_player.moveto($data_system.start_x, $data_system.start_y)
|
|
$game_player.refresh
|
|
Graphics.frame_count = 0
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Set Up Battle Test
|
|
#--------------------------------------------------------------------------
|
|
def self.setup_battle_test
|
|
$game_party.setup_battle_test
|
|
BattleManager.setup($data_system.test_troop_id)
|
|
BattleManager.play_battle_bgm
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Determine Existence of Save File
|
|
#--------------------------------------------------------------------------
|
|
def self.save_file_exists?
|
|
!Dir.glob('Save*.rvdata2').empty?
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Maximum Number of Save Files
|
|
#--------------------------------------------------------------------------
|
|
def self.savefile_max
|
|
return 16
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Create Filename
|
|
# index : File Index
|
|
#--------------------------------------------------------------------------
|
|
def self.make_filename(index)
|
|
sprintf("Save%02d.rvdata2", index + 1)
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Execute Save
|
|
#--------------------------------------------------------------------------
|
|
def self.save_game(index)
|
|
begin
|
|
save_game_without_rescue(index)
|
|
rescue
|
|
delete_save_file(index)
|
|
false
|
|
end
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Execute Load
|
|
#--------------------------------------------------------------------------
|
|
def self.load_game(index)
|
|
load_game_without_rescue(index) rescue false
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Load Save Header
|
|
#--------------------------------------------------------------------------
|
|
def self.load_header(index)
|
|
load_header_without_rescue(index) rescue nil
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Execute Save (No Exception Processing)
|
|
#--------------------------------------------------------------------------
|
|
def self.save_game_without_rescue(index)
|
|
File.open(make_filename(index), "wb") do |file|
|
|
$game_system.on_before_save
|
|
Marshal.dump(make_save_header, file)
|
|
Marshal.dump(make_save_contents, file)
|
|
@last_savefile_index = index
|
|
end
|
|
return true
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Execute Load (No Exception Processing)
|
|
#--------------------------------------------------------------------------
|
|
def self.load_game_without_rescue(index)
|
|
File.open(make_filename(index), "rb") do |file|
|
|
Marshal.load(file)
|
|
extract_save_contents(Marshal.load(file))
|
|
reload_map_if_updated
|
|
@last_savefile_index = index
|
|
end
|
|
return true
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Load Save Header (No Exception Processing)
|
|
#--------------------------------------------------------------------------
|
|
def self.load_header_without_rescue(index)
|
|
File.open(make_filename(index), "rb") do |file|
|
|
return Marshal.load(file)
|
|
end
|
|
return nil
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Delete Save File
|
|
#--------------------------------------------------------------------------
|
|
def self.delete_save_file(index)
|
|
File.delete(make_filename(index)) rescue nil
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Create Save Header
|
|
#--------------------------------------------------------------------------
|
|
def self.make_save_header
|
|
header = {}
|
|
header[:characters] = $game_party.characters_for_savefile
|
|
header[:playtime_s] = $game_system.playtime_s
|
|
header
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Create Save Contents
|
|
#--------------------------------------------------------------------------
|
|
def self.make_save_contents
|
|
contents = {}
|
|
contents[:system] = $game_system
|
|
contents[:timer] = $game_timer
|
|
contents[:message] = $game_message
|
|
contents[:switches] = $game_switches
|
|
contents[:variables] = $game_variables
|
|
contents[:self_switches] = $game_self_switches
|
|
contents[:actors] = $game_actors
|
|
contents[:party] = $game_party
|
|
contents[:troop] = $game_troop
|
|
contents[:map] = $game_map
|
|
contents[:player] = $game_player
|
|
contents
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Extract Save Contents
|
|
#--------------------------------------------------------------------------
|
|
def self.extract_save_contents(contents)
|
|
$game_system = contents[:system]
|
|
$game_timer = contents[:timer]
|
|
$game_message = contents[:message]
|
|
$game_switches = contents[:switches]
|
|
$game_variables = contents[:variables]
|
|
$game_self_switches = contents[:self_switches]
|
|
$game_actors = contents[:actors]
|
|
$game_party = contents[:party]
|
|
$game_troop = contents[:troop]
|
|
$game_map = contents[:map]
|
|
$game_player = contents[:player]
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Reload Map if Data Is Updated
|
|
#--------------------------------------------------------------------------
|
|
def self.reload_map_if_updated
|
|
if $game_system.version_id != $data_system.version_id
|
|
$game_map.setup($game_map.map_id)
|
|
$game_player.center($game_player.x, $game_player.y)
|
|
$game_player.make_encounter_count
|
|
end
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Get Update Date of Save File
|
|
#--------------------------------------------------------------------------
|
|
def self.savefile_time_stamp(index)
|
|
File.mtime(make_filename(index)) rescue Time.at(0)
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Get File Index with Latest Update Date
|
|
#--------------------------------------------------------------------------
|
|
def self.latest_savefile_index
|
|
savefile_max.times.max_by {|i| savefile_time_stamp(i) }
|
|
end
|
|
#--------------------------------------------------------------------------
|
|
# * Get Index of File Most Recently Accessed
|
|
#--------------------------------------------------------------------------
|
|
def self.last_savefile_index
|
|
@last_savefile_index
|
|
end
|
|
end
|