赞 | 0 |
VIP | 135 |
好人卡 | 6 |
积分 | 3 |
经验 | 37799 |
最后登录 | 2020-4-30 |
在线时间 | 812 小时 |
Lv2.观梦者 旅之愚者
- 梦石
- 0
- 星屑
- 275
- 在线时间
- 812 小时
- 注册时间
- 2007-7-28
- 帖子
- 2148
|
2楼
楼主 |
发表于 2010-10-1 16:35:01
|
只看该作者
连一帖发布代码
窗体和场景的基本设置,用window的update_method属性实现场景的update多态,取消if xxx.active?;update_xxx;end部分。
以及将窗口动态打开的方法封装到Scene_Base内- class Window_Base
- attr_accessor :update_method
- end
- class Scene_Base
- attr_accessor :active_window
- def open_window_with_duration(opened_window,hash={})
- opened_window.openness = 0
- opened_window.x = hash[:x] || opened_window.x
- opened_window.y = hash[:y] || opened_window.y
- opened_window.open
- begin
- opened_window.update
- Graphics.update
- end until opened_window.openness == 255
- end
-
- def close_window_with_duration(closed_window)
- return if closed_window.nil?
- closed_window.close
- begin
- closed_window.update
- Graphics.update
- end until closed_window.openness == 0
- end
- end
复制代码 将读写存档封装到独立的模块内- module DQ
- def self.do_save(i = 1)
- file = File.open("Save#{i}.rvdata", "wb")
- write_save_data(file)
- file.close
- $scene = Scene_Map.new
- end
- #--------------------------------------------------------------------------
- # ● 执行读档
- #--------------------------------------------------------------------------
- def self.do_load(i = 1)
- file = File.open("Save#{i}.rvdata", "rb")
- read_save_data(file)
- file.close
- $scene = Scene_Map.new
- RPG::BGM.fade(1500)
- Graphics.fadeout(60)
- Graphics.wait(40)
- @last_bgm.play
- @last_bgs.play
- end
- #--------------------------------------------------------------------------
- # ● 写入存档数据
- # file : 写入存档对象(已开启)
- #--------------------------------------------------------------------------
- def self.write_save_data(file)
- characters = []
- for actor in $game_party.members
- characters.push([actor.character_name, actor.character_index])
- end
- $game_system.version_id = $data_system.version_id
- @last_bgm = RPG::BGM::last
- @last_bgs = RPG::BGS::last
- Marshal.dump(characters, file)
- Marshal.dump(Graphics.frame_count, file)
- Marshal.dump(@last_bgm, file)
- Marshal.dump(@last_bgs, file)
- Marshal.dump($game_system, file)
- Marshal.dump($game_message, file)
- Marshal.dump($game_switches, file)
- Marshal.dump($game_variables, file)
- Marshal.dump($game_self_switches, file)
- Marshal.dump($game_actors, file)
- Marshal.dump($game_party, file)
- Marshal.dump($game_troop, file)
- Marshal.dump($game_map, file)
- Marshal.dump($game_player, file)
- end
- #--------------------------------------------------------------------------
- # ● 读出存档数据
- # file : 读出存档对象(已开启)
- #--------------------------------------------------------------------------
- def self.read_save_data(file)
- characters = Marshal.load(file)
- Graphics.frame_count = Marshal.load(file)
- @last_bgm = Marshal.load(file)
- @last_bgs = Marshal.load(file)
- $game_system = Marshal.load(file)
- $game_message = Marshal.load(file)
- $game_switches = Marshal.load(file)
- $game_variables = Marshal.load(file)
- $game_self_switches = Marshal.load(file)
- $game_actors = Marshal.load(file)
- $game_party = Marshal.load(file)
- $game_troop = Marshal.load(file)
- $game_map = Marshal.load(file)
- $game_player = Marshal.load(file)
- 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)
- end
- end
-
- def self.copy_save_data(index_from,index_to)
- File.open("Save#{index_to}.rvdata","wb") do |writer|
- File.open("Save#{index_from}.rvdata","rb") do |reader|
- writer.write(reader.read)
- end
- end
- end
- def self.delete_save_data(index)
- if FileTest.exist?("Save#{index}.rvdata")
- File.delete("Save#{index}.rvdata")
- end
- end
-
- end
复制代码 DQ::File_Window的定义- module DQ
- class Window_File < ::Window_Command
- attr_accessor :action
- def command
- @commands[@index]
- end
-
- def initialize(action = :none)
- @action = action
-
- case action
- when :new
- list = new_saves
- when :continue
- list = exist_saves
- when :copy_from
- list = exist_saves
- when :copy_to
- list = new_saves
- when :delete
- list = exist_saves
- else
- list = []
- end
- super(196,list)
- end
-
- def new_saves
- list = []
- for i in 1..3
- unless FileTest.exist?("Save#{i}.rvdata")
- list.push("新的冒险书#{i}")
- end
- end
- return list
- end
-
- def exist_saves
- list = []
- for i in 1..3
- if FileTest.exist?("Save#{i}.rvdata")
- list.push("已有的冒险书#{i}")
- end
- end
- return list
- end
- end
- end
复制代码 最后是DQ::Scene_Title,从Scene_Title改的,自己写的代码大约200行- #==============================================================================
- # ■ Scene_Title
- #------------------------------------------------------------------------------
- # 处理标题画面的类。
- #==============================================================================
- module DQ
- class Scene_Title < Scene_Base
- attr_accessor :active_window
- #--------------------------------------------------------------------------
- # ● 开始处理
- #--------------------------------------------------------------------------
- def start
- super
- load_database # 载入数据库
- create_game_objects # 生成游戏对象
- check_continue # 判断继续是否有效
- check_new # 判断是否可以开始新游戏
- create_command_window # 生成指令窗口
- play_title_music # 播放标题画面音乐
- end
- def post_start
- set_active_window(@command_window)
- open_window_with_duration(@command_window,{:x => 24,:y => 24})
- end
-
- #--------------------------------------------------------------------------
- # ● 结束前处理
- #--------------------------------------------------------------------------
- def pre_terminate
- super
- close_window_with_duration(@copy_window)
- close_window_with_duration(@file_window)
- close_window_with_duration(@command_window)
- end
- #--------------------------------------------------------------------------
- # ● 结束处理
- #--------------------------------------------------------------------------
- def terminate
- super
- @copy_window.dispose unless @copy_window.nil?
- @file_window.dispose unless @file_window.nil?
- @command_window.dispose
- snapshot_for_background
- end
- #--------------------------------------------------------------------------
- # ● 更新画面
- #--------------------------------------------------------------------------
- def update
- super
- self.send active_window.update_method
- end
-
- def update_command
- @command_window.update
- if Input.trigger?(Input::C)
- Sound.play_decision
- self.send command
- end
- end
-
- def update_file
- @file_window.update
- if Input.trigger?(Input::C)
- Sound.play_decision
- if @file_window.action == :copy_from
- create_copy_window if @copy_window.nil?
- open_window_with_duration(@copy_window,{:x => 96,
- :y => 104 + 24 * @command_window.index + 24 * @file_window.index})
- @copy_window.z = @file_window.z + 1
- set_active_window(@copy_window)
- return
- end
- close_window_with_duration(@file_window)
- if @file_window.action == :continue
- if FileTest.size("Save#{@file_window.command[/\d/]}.rvdata") > 250
- DQ.do_load(@file_window.command[/\d/])
- else
- command_new_game
- DQ.do_save(@file_window.command[/\d/])
- end
- return
- end
- case @file_window.action
- when :new
- make_a_new_save(@file_window.command[/\d/])
- when :delete
- DQ.delete_save_data(@file_window.command[/\d/])
- end
- $scene = DQ::Scene_Title.new
- end
- if Input.trigger?(Input::B)
- Sound.play_cancel
- close_window_with_duration(@file_window)
- set_active_window(@command_window)
- end
- end
- def update_copy
- @copy_window.update
- if Input.trigger?(Input::C)
- Sound.play_decision
- close_window_with_duration(@copy_window)
- DQ.copy_save_data(@file_window.command[/\d/],
- @copy_window.command[/\d/])
- $scene = DQ::Scene_Title.new
- end
- if Input.trigger?(Input::B)
- Sound.play_cancel
- close_window_with_duration(@copy_window)
- set_active_window(@file_window)
- return
- end
- end
-
- #--------------------------------------------------------------------------
- # ● 判断继续的有效性
- #--------------------------------------------------------------------------
- def check_continue
- @continue_enabled = (Dir.glob('Save[1-3].rvdata').size > 0)
- end
- def check_new
- @new_enabled = (Dir.glob('Save[1-3].rvdata').size < 3)
- end
- #--------------------------------------------------------------------------
- # ● 生成命令窗口
- #--------------------------------------------------------------------------
- def create_command_window
- menu_list = remove_disabled_commands(make_menu_list)
- @command_window = ::Window_Command.new(172, menu_list)
- @command_window.update_method = :update_command
- @command_window.openness = 0
- end
- def make_menu_list
- menu_list = []
- menu_list.push(s1 = "继续冒险书")
- menu_list.push(s2 = "制作冒险书")
- menu_list.push(s3 = "复制冒险书")
- menu_list.push(s4 = "删除冒险书")
- menu_list.push(s5 = "离开游戏")
- def s1.sym
- :command_continue
- end
- def s2.sym
- :command_choose_book
- end
- def s3.sym
- :command_copy
- end
- def s4.sym
- :command_delete
- end
- def s5.sym
- :command_shutdown
- end
- return menu_list
- end
- def remove_disabled_commands(all_list)
- menu_list = all_list
- unless @continue_enabled
- menu_list.delete "继续冒险书"
- menu_list.delete "复制冒险书"
- menu_list.delete "删除冒险书"
- end
- unless @new_enabled
- menu_list.delete "制作冒险书"
- menu_list.delete "复制冒险书"
- end
- return menu_list
- end
-
- # 存档窗口2
- def create_copy_window
- @copy_window = DQ::Window_File.new(:copy_to)
- @copy_window.update_method = :update_copy
- end
- def command_choose_book
- @file_window = DQ::Window_File.new(:new)
- @file_window.update_method = :update_file
- open_window_with_duration(@file_window,{:x => 64,
- :y =>64 + 24 * @command_window.index})
- set_active_window(@file_window)
- end
-
- def command_continue
- open_file_window(:continue)
- end
-
- def command_copy
- open_file_window(:copy_from)
- end
-
- def command_delete
- open_file_window(:delete)
- end
-
- def open_file_window(file_command)
- @file_window = DQ::Window_File.new(file_command)
- @file_window.update_method = :update_file
- open_window_with_duration(@file_window,{:x => 64,
- :y =>64 + 24 * @command_window.index})
- set_active_window(@file_window)
- end
-
- def make_a_new_save(index)
- File.open("Save#{index}.rvdata","wb") do |file|
- end
- end
-
- def set_active_window(active_window)
- @active_window = active_window
- end
-
- def command
- @command_window.commands[@command_window.index].sym
- end
- #以下为默认脚本部分
-
- #--------------------------------------------------------------------------
- # ● 执行渐变
- #--------------------------------------------------------------------------
- def perform_transition
- Graphics.transition(20)
- end
-
- #--------------------------------------------------------------------------
- # ● 主处理
- #--------------------------------------------------------------------------
- def main
- if $BTEST # 战斗测试的情况下
- battle_test # 开始战斗测试处理
- else # 普通游戏的情况下
- super # 原来的主处理
- end
- end
-
- #--------------------------------------------------------------------------
- # ● 战斗测试
- #--------------------------------------------------------------------------
- def battle_test
- load_bt_database # 载入战斗测试数据库
- create_game_objects # 生成个各种游戏对象
- Graphics.frame_count = 0 # 初始化游戏时间
- $game_party.setup_battle_test_members
- $game_troop.setup($data_system.test_troop_id)
- $game_troop.can_escape = true
- $game_system.battle_bgm.play
- snapshot_for_background
- $scene = Scene_Battle.new
- end
-
- def command_new_game
- confirm_player_location
- $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
- $scene = Scene_Map.new
- RPG::BGM.fade(1500)
- close_window_with_duration(@command_window)
- Graphics.fadeout(30)
- Graphics.wait(20)
- Graphics.frame_count = 0
- RPG::BGM.stop
- $game_map.autoplay
- end
-
- #--------------------------------------------------------------------------
- # ● 命令:离开游戏
- #--------------------------------------------------------------------------
- def command_shutdown
- Sound.play_decision
- RPG::BGM.fade(800)
- RPG::BGS.fade(800)
- RPG::ME.fade(800)
- $scene = nil
- end
-
- #--------------------------------------------------------------------------
- # ● 播放标题音乐
- #--------------------------------------------------------------------------
- def play_title_music
- $data_system.title_bgm.play
- RPG::BGS.stop
- RPG::ME.stop
- end
- #--------------------------------------------------------------------------
- # ● 检查主角初期位置是否存在
- #--------------------------------------------------------------------------
- def confirm_player_location
- if $data_system.start_map_id == 0
- print "主角初始位置未设定。"
- exit
- end
- end
-
- #--------------------------------------------------------------------------
- # ● 载入数据库
- #--------------------------------------------------------------------------
- def load_database
- $data_actors = load_data("Data/Actors.rvdata")
- $data_classes = load_data("Data/Classes.rvdata")
- $data_skills = load_data("Data/Skills.rvdata")
- $data_items = load_data("Data/Items.rvdata")
- $data_weapons = load_data("Data/Weapons.rvdata")
- $data_armors = load_data("Data/Armors.rvdata")
- $data_enemies = load_data("Data/Enemies.rvdata")
- $data_troops = load_data("Data/Troops.rvdata")
- $data_states = load_data("Data/States.rvdata")
- $data_animations = load_data("Data/Animations.rvdata")
- $data_common_events = load_data("Data/CommonEvents.rvdata")
- $data_system = load_data("Data/System.rvdata")
- $data_areas = load_data("Data/Areas.rvdata")
- end
- #--------------------------------------------------------------------------
- # ● 载入战斗测试数据库
- #--------------------------------------------------------------------------
- def load_bt_database
- $data_actors = load_data("Data/BT_Actors.rvdata")
- $data_classes = load_data("Data/BT_Classes.rvdata")
- $data_skills = load_data("Data/BT_Skills.rvdata")
- $data_items = load_data("Data/BT_Items.rvdata")
- $data_weapons = load_data("Data/BT_Weapons.rvdata")
- $data_armors = load_data("Data/BT_Armors.rvdata")
- $data_enemies = load_data("Data/BT_Enemies.rvdata")
- $data_troops = load_data("Data/BT_Troops.rvdata")
- $data_states = load_data("Data/BT_States.rvdata")
- $data_animations = load_data("Data/BT_Animations.rvdata")
- $data_common_events = load_data("Data/BT_CommonEvents.rvdata")
- $data_system = load_data("Data/BT_System.rvdata")
- end
- #--------------------------------------------------------------------------
- # ● 生成各种游戏对象
- #--------------------------------------------------------------------------
- def create_game_objects
- $game_temp = Game_Temp.new
- $game_message = Game_Message.new
- $game_system = Game_System.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
- end
- end
复制代码 |
|