| 赞 | 46 |
| VIP | |
| 好人卡 | |
| 积分 | 329 |
| 经验 | |
| 最后登录 | 2026-2-3 |
| 在线时间 | 248 小时 |
- 梦石
- 24
- 星屑
- 8942
- 在线时间
- 248 小时
- 注册时间
- 2020-12-4
- 回帖
- 259
  
|
楼主 |
发表于 2021-6-19 03:14:58
|
显示全部楼层
本帖最后由 纯属小虫 于 2021-6-19 14:41 编辑
感谢版主这么晚了还回复得这么快,能得到作者的亲自回复真是受宠若惊~~
合成说明已经成功加入了,虽然方法比较笨,但实现了编辑起来比较便捷的分行说明:(图片在附件)
以下是修正后的脚本
增添了说明的合成脚本
- #=============================================================================
- # 物品合成简易优化版 Ver 1.1
- #-----------------------------------------------------------------------------
- # By : RyanBern
- #-----------------------------------------------------------------------------
- # 功能介绍:
- # 这是一个简单的物品合成系统,和论坛上原有的有所不同。在这个系统里面你可以
- # 通过学习配方来获取某种道具的合成方法,也可以直接合成。有关数据存储我已经
- # 改成了外部文件的形式,下面就说明一下如何使用。
- #-----------------------------------------------------------------------------
- # 使用方法:
- # 1.有关数据编辑,在游戏根目录里面建立一个名为 Recipes.txt 的记事本,然后
- # 再里面写下所有配方的内容,下面是一个例子表明如何书写一组完整的数据。
- # <recipe=1>
- # <product>kind=0,id=3</product>
- # <ingredients>
- # kind=0,id=1,number=2
- # kind=0,id=2,number=2
- # </ingredients>
- # </recipe>
- # 其中,第一行recipe=1表示这是ID为1的配方的数据,这里ID是从1开始的。
- # 第二行是表示最终合成的物品,kind表示分类,道具为0,武器为1,防具为2;id
- # 就是对应的ID,最终合成物品在<product></product>之间
- # 第三行是原料的设定,所有的材料都必须在<ingredients></ingredients>之间
- # 每一行代表一种原料的分类,id,和数量,注意不要写重复了。分类(kind)为0,1,2
- # 的含义和产品相同,如果指定消耗某变量,则分类(kind)要写为3,id就是对应
- # 变量的ID。
- # 如果想再增加一条配方,则令起一行开一个<recipe>结点即可。
- # 2.有关学习配方:
- # $game_party.recipe_learn?(配方ID) 用于判断是否习得某一配方
- # $game_party.learn_recipe(配方ID) 学习该ID的配方
- # $game_party.forget_recipe(配方ID) 失去该ID的配方
- # 3.有关场景调用:
- # $scene = Scene_Craft.new 直接打开合成场景,配方为主角已有的配方
- # $scene = Scene_Craft.new(0/1/2) 打开合成场景,配方为产品分类为i的配方(无论主角是否习得)
- # $scene = Scene_Craft.new(3) 打开合成场景,配方为所有的配方
- #-----------------------------------------------------------------------------
- # 注意事项:
- # 1.Recipes.txt里面的缩进不是必须的。
- # 2.游戏发布时,可以删去Recipes.txt,不过建议在你的制作工程中保留一个副本
- # 以便日后做出更改。
- #-----------------------------------------------------------------------------
- # 更新记录:
- # Date | Version | Details
- # 2014.12.26 | 1.0 | 初版
- # 2016.04.30 | 1.1 | 范例优化,增加变量消耗判定
- # 2021.06.19 | ??? | 将最大合成数量改为了合成说明!感谢版主教导!
- #=============================================================================
- module RB
- Regex_Recipe = /<recipe\s*=\s*(\d+)>(.*?)<\/recipe>/m #整的合成内容
- Regex_Product = /<product>\s*kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*<\/product>/m #目标物
- #添加
- Regex_Txt1 = /<txt1>(.*?)<\/txt1>/m
- Regex_Txt2 = /<txt2>(.*?)<\/txt2>/m
- Regex_Txt3 = /<txt3>(.*?)<\/txt3>/m
- Regex_Txt4 = /<txt4>(.*?)<\/txt4>/m
- #——————————
- Regex_Ingredient = /<ingredients>(.*?)<\/ingredients>/m #需要素材
- Regex_Ingredient_Info = /kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*,\s*number\s*=\s*(\d+)/
- end
- module RPG
- class Recipe# 配方 大类
- attr_accessor :id
- attr_accessor :product
- attr_accessor :txt1
- attr_accessor :txt2
- attr_accessor :txt3
- attr_accessor :txt4
- attr_accessor :ingredients
- def initialize
- @id = 0
- @product = Ingredient.new
- @txt1 = ""
- @txt2 = ""
- @txt3 = ""
- @txt4 = ""
- @ingredients = []
- end
- # 素材类
- class Ingredient
- attr_accessor :kind
- attr_accessor :id
- attr_accessor :number
- def initialize(kind = 0, id = 0, number = 0)
- @kind = kind
- @id = id
- @number = number
- end
- end
- end
- # 变量大类
- class Variable_Wrapper
- attr_accessor :id
- attr_accessor :name
- attr_accessor :icon_name
- def initialize(id)
- @id = id
- @name = $data_system.variables[id]
- @icon_name = ""
- end
- end
- # 读取文本————并给文本分类型●●●●●●●
- def self.extract_recipe
- filename = "Recipes.txt"#读取文本的名字
- outfile_name = "Data/Recipes.rxdata"#输出转换文本的路径和名字
- data = []
- begin#每次测试开始都会进行这一读取程序
- file = File.open(filename, "r")#以读取模式打开文件"Recipes.txt"
- str = file.read#将字符串导入str
- file.close #关闭文件
- while str.slice!(RB::Regex_Recipe) #slice!的意思是:从字符串中删除指定的范围,然后返回被删除的子字符串
- id = $1.to_i #返回某符号所对应的整数,就是ID号,$1指的是正则表达式里第一个括号内容
- contents = $2 #$2指的是正则表达式里第二个括号里的内容,contents是合成物品总目录
- recipe = Recipe.new
- recipe.id = id
- if RB::Regex_Product =~ contents #导入制作目标的内容
- recipe.product.kind = $1.to_i #导入物品种类号(.to_i是整数)
- recipe.product.id = $2.to_i #导入物品id号
- end
- if RB::Regex_Txt1 =~ contents #●●●●添加txt字段●●●●
- recipe.txt1 = $1
- end
- if RB::Regex_Txt2 =~ contents #●●●●添加txt字段●●●●
- recipe.txt2 = $1
- end
- if RB::Regex_Txt3 =~ contents #●●●●添加txt字段●●●●
- recipe.txt3 = $1
- end
- if RB::Regex_Txt4 =~ contents #●●●●添加txt字段●●●●
- recipe.txt4 = $1
- end
- if RB::Regex_Ingredient =~ contents #导入制作材料
- ingredient_contents = $1 #这里导入的是ingredients总成,未分类
- ingredient_contents.scan(RB::Regex_Ingredient_Info) do |a|#这里是将ingredients里的数据分类
- recipe.ingredients << Recipe::Ingredient.new(a[0].to_i, a[1].to_i, a[2].to_i)
- #a[2]是合成素材需要数
- end
- end
- data[id] = recipe #data的排列id就是合成物的id
- end
- outfile = File.open(outfile_name, "wb") #输出文件
- Marshal.dump(data, outfile) #把 obj 递归地写入文件。
- outfile.close #关闭文件
- rescue #发生异常时就会执行这一个内容
- print "Recipes.txt中的数据可能含有无效格式"
- exit
- end
- end
- end
- class Game_Party #在Game_Party类里的再定义
- alias rb_initialize_20141226 initialize #为initialize 作别名
- def initialize #重新定义initialize(或者说,是在定义中添加新的东西!)
- rb_initialize_20141226 #别名,把原有的弄进来
- @recipes = {} #加入新的实例变量recipes,哈希值,也就是组好的合成表
- end
- def recipe_learn?(recipe_id) #判断配方是否学习的 def
- @recipes ||= {}
- return @recipes[recipe_id]
- end
- def learn_recipe(recipe_id) #学习配方的 def
- @recipes ||= {}
- @recipes[recipe_id] = true
- end
- def forget_recipe(recipe_id) #忘记配方的 def
- @recipes ||= {}
- @recipes[recipe_id] = nil
- end
- def product_max_num(recipe_id) #计算当前可以的最大合成数
- max = -1
- recipe = $data_recipes[recipe_id]
- return 0 if recipe.nil? #如果没有合成表,返回0为最大合成数
- recipe.ingredients.each do |ingredient| #循环,代入需要素材以及数量的数据
- num_need = ingredient.number #需求量等于素材需求量●●这里是不是可以做手脚)
- case ingredient.kind #判断物品种类
- when 0
- num_current = item_number(ingredient.id) #现有的物品数量
- when 1
- num_current = weapon_number(ingredient.id) #现有的武器数量
- when 2
- num_current = armor_number(ingredient.id) #现有的防具数量
- when 3
- num_current = $game_variables[ingredient.id] #现有的变量数量
- else
- num_current = 0
- end
- #计算最大可合成的数量的公式
- max = num_current / num_need if max == -1 || num_current / num_need < max
- end
- return max == -1 ? 0 : max #如果max为-1 啧最大合成数为0 ;如果不是则为max本身
- end
- end
- #————合成窗口左栏————
- class Window_Product < Window_Selectable
- def initialize(mode = -1) #初始化,mode默认值为-1
- super(0, 64, 240, 480 - 64) #super将调用被当前方法覆盖的父类中的同名方法,所以这里是创造窗口!
- self.index = 0 #默认的光标选择位置为0
- @mode = mode
- refresh
- end
- def recipe
- return @data[self.index] #输出自己序号的 配方
- end
- def item #获取描绘左侧窗口的项目
- recipe = self.recipe #recipe值为自己 的配方
- return nil if recipe.nil? #如果配方空则为空
- product = recipe.product
- case product.kind
- when 0
- item = $data_items[product.id]
- when 1
- item = $data_weapons[product.id]
- when 2
- item = $data_armors[product.id]
- else
- item = nil
- end
- return item
- end
- def refresh #闪烁?刷新?
- if self.contents != nil
- self.contents.dispose
- self.contents = nil
- end
- @data = []
- (1...$data_recipes.size).each do |i|
- if @mode == -1 && $game_party.recipe_learn?(i) || $data_recipes[i].product.kind == @mode || @mode == 3
- @data << $data_recipes[i] if $data_recipes[i] != nil
- end
- end
- @item_max = @data.size
- if @item_max > 0
- self.contents = Bitmap.new(width - 32, @item_max * 32)
- @data.each_index{|i| draw_item(i)}
- else
- self.contents = Bitmap.new(width - 32, 32)
- self.contents.draw_text(4, 0, 192, 32, "没有可用配方")
- end
- end
-
- def draw_item(index) #描绘项目
- x = 4
- y = 32 * index
- product = @data[index].product
- case product.kind
- when 0
- item = $data_items[product.id]
- when 1
- item = $data_weapons[product.id]
- when 2
- item = $data_armors[product.id]
- else
- item = nil
- end
- if item == nil
- return
- end
- disabled = $game_party.product_max_num(@data[index].id) == 0
- bitmap = RPG::Cache.icon(item.icon_name)
- opacity = disabled ? 128 : 255
- self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
- self.contents.font.color = disabled ? disabled_color : normal_color
- self.contents.draw_text(x + 28, y, 176, 32, item.name)
- end
- def update_help
- @help_window.set_text(self.item == nil ? "" : self.item.description)
- end
- end
- #————合成窗口右下——————————————————————————
- class Window_Ingredients < Window_Base
- def initialize
- super(240, 288, 400, 192)
- @waiting = 0
- @dy = 1
- @recipe = nil
- refresh
- end
- def recipe=(recipe)
- if @recipe != recipe
- @recipe = recipe
- @waiting = 40
- @dy = 1
- self.oy = 0
- refresh
- end
- end
- def refresh
- if self.contents != nil
- self.contents.dispose
- self.contents = nil
- end
- if @recipe == nil
- return
- end
- h = 32 * @recipe.ingredients.size
- if h > 0
- self.contents = Bitmap.new(width - 32, h)
- @recipe.ingredients.each_with_index do |ingredient, index|
- draw_item(ingredient, index)
- end
- end
- end
- def draw_item(ingredient, index)
- x = 4
- y = index * 32
- case ingredient.kind #带入需要的素材量,●●在这里可以改成随合成数量变化!
- when 0
- item = $data_items[ingredient.id]
- num = $game_party.item_number(ingredient.id) #*
- when 1
- item = $data_weapons[ingredient.id]
- num = $game_party.weapon_number(ingredient.id)
- when 2
- item = $data_armors[ingredient.id]
- num = $game_party.armor_number(ingredient.id)
- when 3
- item = RPG::Variable_Wrapper.new(ingredient.id)
- num = $game_variables[ingredient.id]
- else
- item = nil
- end
- if item == nil
- return
- end
- bitmap = RPG::Cache.icon(item.icon_name)
- disabled = num < ingredient.number
- opacity = disabled ? 128 : 255 #通过判定是否满足条件来展示不同透明度
- self.contents.blt(x, y+4, bitmap, Rect.new(0, 0, 24, 24), opacity)
- self.contents.font.color = disabled ? disabled_color : normal_color
- self.contents.draw_text(x + 28, y, 192, 32, item.name)
- self.contents.draw_text(x + 220, y, 64, 32, num.to_s, 2)
- self.contents.font.color = normal_color#这里可以描绘颜色!
- #下面这句是所需要素材量的描述,如果没法改就去前面改!
- self.contents.draw_text(x + 264, y, 64, 32, ingredient.number.to_s, 2)
- end
- def update
- super
- if self.contents != nil && self.contents.height > self.height - 32
- if @waiting > 0
- @waiting -= 1
- return
- end
- self.oy += @dy
- if self.oy + self.height - 32 >= self.contents.height
- @dy = -1
- @waiting = 60
- end
- if self.oy <= 0
- @dy = 1
- @waiting = 60
- end
- end
- end
- end
- #————合成窗口右上————●●●●●●●●●●●●
- class Window_Craft_Number < Window_Base
- def initialize #初始化
- super(240, 64, 400, 160)
- self.contents = Bitmap.new(width - 32, height - 32)
- @recipe = nil
- @max = 1
- @number = 1
- @number_current = 0
- end
- def recipe=(recipe) #带入一个实例变量(整个recipe的值,里头包含全部了)
- if @recipe != recipe
- @recipe = recipe
- reset
- refresh
- end
- end
- def reset #重置数据(刷新)
- if @recipe == nil
- @number = 0
- @number_current = 0
- return
- end
- product = @recipe.product
- case product.kind
- when 0
- @number_current = $game_party.item_number(product.id)
- when 1
- @number_current = $game_party.weapon_number(product.id)
- when 2
- @number_current = $game_party.armor_number(product.id)
- else
- @number_current = 0
- end
- @max = $game_party.product_max_num(@recipe.id)
- @max = [@max, 99 - @number_current].min
- @number = @max == 0 ? 0 : 1
- end
- def number
- return @number
- end
- #--------------------------------------------------------------------------
- # ● 刷新
- #--------------------------------------------------------------------------
- def refresh #描绘右上角的各个项目的【方法】
- self.contents.clear
- self.contents.font.color = system_color
- self.contents.draw_text(4, 0, 364, 32, "所持数")
-
- a=17
- b=18
- self.contents.font.size =8
- self.contents.draw_text(6, 32-9+b*0, 364, 32, "■") #文字前缀
- self.contents.draw_text(6, 32-9+b*1, 364, 32, "■") #文字前缀
- self.contents.draw_text(6, 32-9+b*2, 364, 32, "■") #文字前缀
- self.contents.draw_text(6, 32-9+b*3, 364, 32, "■") #文字前缀
- self.contents.font.size =20
-
- self.contents.draw_text(4, 96+3, 364, 32, "当前合成数量")
- self.contents.font.color = normal_color
- self.contents.draw_text(4, 0, 364, 32, @number_current.to_s, 2)
- #说明文字描绘●●●●●●●●
- self.contents.font.size =16
- self.contents.draw_text(a, 23+b*0, 364, 32, @recipe.txt1.to_s)
- self.contents.draw_text(a, 23+b*1, 364, 32, @recipe.txt2.to_s)
- self.contents.draw_text(a, 23+b*2, 364, 32, @recipe.txt3.to_s)
- self.contents.draw_text(a, 23+b*3, 364, 32, @recipe.txt4.to_s)
- self.contents.font.size =20
-
- self.contents.draw_text(252, 96+3, 32, 32, "×")
- self.contents.draw_text(308, 96+3, 24, 32, @number.to_s, 2)
- end
- def update
- super
- if self.active
- # 光标右 (+1)
- if Input.repeat?(Input::RIGHT) and @number < @max
- $game_system.se_play($data_system.cursor_se)
- @number += 1
- refresh
- end
- # 光标左 (-1)
- if Input.repeat?(Input::LEFT) and @number > 1
- $game_system.se_play($data_system.cursor_se)
- @number -= 1
- refresh
- end
- # 光标上 (+10)
- if Input.repeat?(Input::UP) and @number < @max
- $game_system.se_play($data_system.cursor_se)
- @number = [@number + 10, @max].min
- refresh
- end
- # 光标下 (-10)
- if Input.repeat?(Input::DOWN) and @number > 1
- $game_system.se_play($data_system.cursor_se)
- @number = [@number - 10, 1].max
- refresh
- end
- end
- end
- end
- #————————————描绘中间框——————————————
- class Window_Craft_Label < Window_Base
- def initialize
- super(240, 224, 400, 64)
- self.contents = Bitmap.new(width - 32, height - 32)
- refresh
- end
- def refresh
- self.contents.clear
- self.contents.font.color = system_color
- self.contents.draw_text(0, 0, width - 32, 32, "材料名称 现有 需要")
- end
- end
- class Scene_Title
- alias rb_main_20141226 main
- def main
- if $DEBUG
- RPG.extract_recipe
- end
- $data_recipes = load_data("Data/Recipes.rxdata")
- rb_main_20141226
- end
- end
- #——————————显示窗口 的最终显示处理————————————————
- class Scene_Craft
- def initialize(mode = -1)
- @mode = mode
- end
- def main
- @product_window = Window_Product.new(@mode)
- @help_window = Window_Help.new
- @product_window.help_window = @help_window
- @ingredient_window = Window_Ingredients.new
- @number_window = Window_Craft_Number.new #●●●右上角
- @label_window = Window_Craft_Label.new
- Graphics.transition
- loop do
- Graphics.update
- Input.update
- update
- if $scene != self
- break
- end
- end
- Graphics.freeze
- @product_window.dispose
- @help_window.dispose
- @ingredient_window.dispose
- @number_window.dispose
- @label_window.dispose
- end
- def update #显示各种数字等信息●●●
- @product_window.update
- @ingredient_window.update
- @ingredient_window.recipe = @product_window.recipe
- @number_window.recipe = @product_window.recipe #●●●右上角
- if @product_window.active
- update_product
- return
- end
- if @number_window.active
- update_number
- end
- end
- def update_product
- if Input.trigger?(Input::B)
- $game_system.se_play($data_system.cancel_se)
- $scene = Scene_Map.new
- return
- end
- if Input.trigger?(Input::C)
- recipe = @product_window.recipe
- if recipe == nil || $game_party.product_max_num(recipe.id) == 0
- $game_system.se_play($data_system.buzzer_se)
- return
- end
- $game_system.se_play($data_system.decision_se)
- @number_window.cursor_rect.set(304, 96, 32, 32)
- @product_window.active = false
- @number_window.active = true
- return
- end
- end
- def update_number
- @number_window.update
- if Input.trigger?(Input::B)
- $game_system.se_play($data_system.cancel_se)
- @number_window.active = false
- @product_window.active = true
- @number_window.cursor_rect.empty
- return
- end
- if Input.trigger?(Input::C)
- $game_system.se_play($data_system.shop_se)
- recipe = @product_window.recipe
- case recipe.product.kind
- when 0
- $game_party.gain_item(recipe.product.id, @number_window.number)
- when 1
- $game_party.gain_weapon(recipe.product.id, @number_window.number)
- when 2
- $game_party.gain_armor(recipe.product.id, @number_window.number)
- end
- recipe.ingredients.each do |ingredient|
- case ingredient.kind
- when 0
- $game_party.lose_item(ingredient.id, ingredient.number * @number_window.number)
- when 1
- $game_party.lose_weapon(ingredient.id, ingredient.number * @number_window.number)
- when 2
- $game_party.lose_armor(ingredient.id, ingredient.number * @number_window.number)
- when 3
- $game_variables[ingredient.id] -= ingredient.number * @number_window.number
- end
- end
- @product_window.refresh
- @ingredient_window.refresh
- @number_window.reset
- @number_window.refresh
- @product_window.active = true
- @number_window.active = false
- @number_window.cursor_rect.empty
- return
- end
- end
- end
复制代码
【素材需求量变动】还没有尝试,接下来打算拼入主工程中进行合成系统换血~~~
再次感谢版主大大~~~
==========================================================================================
这里尝试解决第二个问题:让需求量随着合成数量变化,但是失败了:
添加的脚本为 336行-341行 400行-405行
但是运行时会报错:
339行发生了 TypeError, nil can't be coerced into Fixnum
有点诡异,我明明判断了nil,可是为什么还会出错
还有 ingredient.number.to_i 这一句是不是有问题,ingredient的数据结构有点弄不清楚
附上脚本 20210619
- #=============================================================================
- # 物品合成简易优化版 Ver 1.1
- #-----------------------------------------------------------------------------
- # By : RyanBern
- #-----------------------------------------------------------------------------
- # 功能介绍:
- # 这是一个简单的物品合成系统,和论坛上原有的有所不同。在这个系统里面你可以
- # 通过学习配方来获取某种道具的合成方法,也可以直接合成。有关数据存储我已经
- # 改成了外部文件的形式,下面就说明一下如何使用。
- #-----------------------------------------------------------------------------
- # 使用方法:
- # 1.有关数据编辑,在游戏根目录里面建立一个名为 Recipes.txt 的记事本,然后
- # 再里面写下所有配方的内容,下面是一个例子表明如何书写一组完整的数据。
- # <recipe=1>
- # <product>kind=0,id=3</product>
- # <ingredients>
- # kind=0,id=1,number=2
- # kind=0,id=2,number=2
- # </ingredients>
- # </recipe>
- # 其中,第一行recipe=1表示这是ID为1的配方的数据,这里ID是从1开始的。
- # 第二行是表示最终合成的物品,kind表示分类,道具为0,武器为1,防具为2;id
- # 就是对应的ID,最终合成物品在<product></product>之间
- # 第三行是原料的设定,所有的材料都必须在<ingredients></ingredients>之间
- # 每一行代表一种原料的分类,id,和数量,注意不要写重复了。分类(kind)为0,1,2
- # 的含义和产品相同,如果指定消耗某变量,则分类(kind)要写为3,id就是对应
- # 变量的ID。
- # 如果想再增加一条配方,则令起一行开一个<recipe>结点即可。
- # 2.有关学习配方:
- # $game_party.recipe_learn?(配方ID) 用于判断是否习得某一配方
- # $game_party.learn_recipe(配方ID) 学习该ID的配方
- # $game_party.forget_recipe(配方ID) 失去该ID的配方
- # 3.有关场景调用:
- # $scene = Scene_Craft.new 直接打开合成场景,配方为主角已有的配方
- # $scene = Scene_Craft.new(0/1/2) 打开合成场景,配方为产品分类为i的配方(无论主角是否习得)
- # $scene = Scene_Craft.new(3) 打开合成场景,配方为所有的配方
- #-----------------------------------------------------------------------------
- # 注意事项:
- # 1.Recipes.txt里面的缩进不是必须的。
- # 2.游戏发布时,可以删去Recipes.txt,不过建议在你的制作工程中保留一个副本
- # 以便日后做出更改。
- #-----------------------------------------------------------------------------
- # 更新记录:
- # Date | Version | Details
- # 2014.12.26 | 1.0 | 初版
- # 2016.04.30 | 1.1 | 范例优化,增加变量消耗判定
- # 2021.06.19 | ??? | 将最大合成数量改为了合成说明!感谢版主教导!
- #=============================================================================
- module RB
- Regex_Recipe = /<recipe\s*=\s*(\d+)>(.*?)<\/recipe>/m #整的合成内容
- Regex_Product = /<product>\s*kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*<\/product>/m #目标物
- #添加
- Regex_Txt1 = /<txt1>(.*?)<\/txt1>/m
- Regex_Txt2 = /<txt2>(.*?)<\/txt2>/m
- Regex_Txt3 = /<txt3>(.*?)<\/txt3>/m
- Regex_Txt4 = /<txt4>(.*?)<\/txt4>/m
- #——————————
- Regex_Ingredient = /<ingredients>(.*?)<\/ingredients>/m #需要素材
- Regex_Ingredient_Info = /kind\s*=\s*(\d)\s*,\s*id\s*=\s*(\d+)\s*,\s*number\s*=\s*(\d+)/
- end
- module RPG
- class Recipe# 配方 大类
- attr_accessor :id
- attr_accessor :product
- attr_accessor :txt1
- attr_accessor :txt2
- attr_accessor :txt3
- attr_accessor :txt4
- attr_accessor :ingredients
- def initialize
- @id = 0
- @product = Ingredient.new
- @txt1 = ""
- @txt2 = ""
- @txt3 = ""
- @txt4 = ""
- @ingredients = []
- end
- # 素材类
- class Ingredient
- attr_accessor :kind
- attr_accessor :id
- attr_accessor :number
- def initialize(kind = 0, id = 0, number = 0)
- @kind = kind
- @id = id
- @number = number
- end
- end
- end
- # 变量大类
- class Variable_Wrapper
- attr_accessor :id
- attr_accessor :name
- attr_accessor :icon_name
- def initialize(id)
- @id = id
- @name = $data_system.variables[id]
- @icon_name = ""
- end
- end
- # 读取文本————并给文本分类型●●●●●●●
- def self.extract_recipe
- filename = "Recipes.txt"#读取文本的名字
- outfile_name = "Data/Recipes.rxdata"#输出转换文本的路径和名字
- data = []
- begin#每次测试开始都会进行这一读取程序
- file = File.open(filename, "r")#以读取模式打开文件"Recipes.txt"
- str = file.read#将字符串导入str
- file.close #关闭文件
- while str.slice!(RB::Regex_Recipe) #slice!的意思是:从字符串中删除指定的范围,然后返回被删除的子字符串
- id = $1.to_i #返回某符号所对应的整数,就是ID号,$1指的是正则表达式里第一个括号内容
- contents = $2 #$2指的是正则表达式里第二个括号里的内容,contents是合成物品总目录
- recipe = Recipe.new
- recipe.id = id
- if RB::Regex_Product =~ contents #导入制作目标的内容
- recipe.product.kind = $1.to_i #导入物品种类号(.to_i是整数)
- recipe.product.id = $2.to_i #导入物品id号
- end
- if RB::Regex_Txt1 =~ contents #●●●●添加txt字段●●●●
- recipe.txt1 = $1
- end
- if RB::Regex_Txt2 =~ contents #●●●●添加txt字段●●●●
- recipe.txt2 = $1
- end
- if RB::Regex_Txt3 =~ contents #●●●●添加txt字段●●●●
- recipe.txt3 = $1
- end
- if RB::Regex_Txt4 =~ contents #●●●●添加txt字段●●●●
- recipe.txt4 = $1
- end
- if RB::Regex_Ingredient =~ contents #导入制作材料
- ingredient_contents = $1 #这里导入的是ingredients总成,未分类
- ingredient_contents.scan(RB::Regex_Ingredient_Info) do |a|#这里是将ingredients里的数据分类
- recipe.ingredients << Recipe::Ingredient.new(a[0].to_i, a[1].to_i, a[2].to_i)
- #a[2]是合成素材需要数
- end
- end
- data[id] = recipe #data的排列id就是合成物的id
- end
- outfile = File.open(outfile_name, "wb") #输出文件
- Marshal.dump(data, outfile) #把 obj 递归地写入文件。
- outfile.close #关闭文件
- rescue #发生异常时就会执行这一个内容
- print "Recipes.txt中的数据可能含有无效格式"
- exit
- end
- end
- end
- class Game_Party #在Game_Party类里的再定义
- alias rb_initialize_20141226 initialize #为initialize 作别名
- def initialize #重新定义initialize(或者说,是在定义中添加新的东西!)
- rb_initialize_20141226 #别名,把原有的弄进来
- @recipes = {} #加入新的实例变量recipes,哈希值,也就是组好的合成表
- end
- def recipe_learn?(recipe_id) #判断配方是否学习的 def
- @recipes ||= {}
- return @recipes[recipe_id]
- end
- def learn_recipe(recipe_id) #学习配方的 def
- @recipes ||= {}
- @recipes[recipe_id] = true
- end
- def forget_recipe(recipe_id) #忘记配方的 def
- @recipes ||= {}
- @recipes[recipe_id] = nil
- end
- def product_max_num(recipe_id) #计算当前可以的最大合成数
- max = -1
- recipe = $data_recipes[recipe_id]
- return 0 if recipe.nil? #如果没有合成表,返回0为最大合成数
- recipe.ingredients.each do |ingredient| #循环,代入需要素材以及数量的数据
- num_need = ingredient.number #需求量等于素材需求量●●这里是不是可以做手脚)
- case ingredient.kind #判断物品种类
- when 0
- num_current = item_number(ingredient.id) #现有的物品数量
- when 1
- num_current = weapon_number(ingredient.id) #现有的武器数量
- when 2
- num_current = armor_number(ingredient.id) #现有的防具数量
- when 3
- num_current = $game_variables[ingredient.id] #现有的变量数量
- else
- num_current = 0
- end
- #计算最大可合成的数量的公式
- max = num_current / num_need if max == -1 || num_current / num_need < max
- end
- return max == -1 ? 0 : max #如果max为-1 啧最大合成数为0 ;如果不是则为max本身
- end
- end
- #————合成窗口左栏——————————————————————————
- class Window_Product < Window_Selectable
- def initialize(mode = -1) #初始化,mode默认值为-1
- super(0, 64, 240, 480 - 64) #super将调用被当前方法覆盖的父类中的同名方法,所以这里是创造窗口!
- self.index = 0 #默认的光标选择位置为0
- @mode = mode
- refresh
- end
- def recipe
- return @data[self.index] #输出自己序号的 配方
- end
- def item #获取描绘左侧窗口的项目
- recipe = self.recipe #recipe值为自己 的配方
- return nil if recipe.nil? #如果配方空则为空
- product = recipe.product
- case product.kind
- when 0
- item = $data_items[product.id]
- when 1
- item = $data_weapons[product.id]
- when 2
- item = $data_armors[product.id]
- else
- item = nil
- end
- return item
- end
- def refresh #闪烁?刷新?
- if self.contents != nil
- self.contents.dispose
- self.contents = nil
- end
- @data = []
- (1...$data_recipes.size).each do |i|
- if @mode == -1 && $game_party.recipe_learn?(i) || $data_recipes[i].product.kind == @mode || @mode == 3
- @data << $data_recipes[i] if $data_recipes[i] != nil
- end
- end
- @item_max = @data.size
- if @item_max > 0
- self.contents = Bitmap.new(width - 32, @item_max * 32)
- @data.each_index{|i| draw_item(i)}
- else
- self.contents = Bitmap.new(width - 32, 32)
- self.contents.draw_text(4, 0, 192, 32, "没有可用配方")
- end
- end
-
- def draw_item(index) #描绘项目
- x = 4
- y = 32 * index
- product = @data[index].product
- case product.kind
- when 0
- item = $data_items[product.id]
- when 1
- item = $data_weapons[product.id]
- when 2
- item = $data_armors[product.id]
- else
- item = nil
- end
- if item == nil
- return
- end
- disabled = $game_party.product_max_num(@data[index].id) == 0
- bitmap = RPG::Cache.icon(item.icon_name)
- opacity = disabled ? 128 : 255
- self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
- self.contents.font.color = disabled ? disabled_color : normal_color
- self.contents.draw_text(x + 28, y, 176, 32, item.name)
- end
- def update_help
- @help_window.set_text(self.item == nil ? "" : self.item.description)
- end
- end
- #————————————●●●●右下●●●●需求量 ————————————
- class Window_Ingredients < Window_Base
- def initialize
- super(240, 288, 400, 192)
- @waiting = 0
- @dy = 1
- @recipe = nil
- refresh
- end
- def recipe=(recipe)
- if @recipe != recipe
- @recipe = recipe
- @waiting = 40
- @dy = 1
- self.oy = 0
- refresh
- end
- end
- def refresh
- if self.contents != nil
- self.contents.dispose
- self.contents = nil
- end
- if @recipe == nil
- return
- end
- h = 32 * @recipe.ingredients.size
- if h > 0
- self.contents = Bitmap.new(width - 32, h)
- @recipe.ingredients.each_with_index do |ingredient, index|
- draw_item(ingredient, index)
- end
- end
- end
- def draw_item(ingredient, index)
- x = 4
- y = index * 32
- case ingredient.kind
- when 0
- item = $data_items[ingredient.id]
- num = $game_party.item_number(ingredient.id) # num是持有量
- when 1
- item = $data_weapons[ingredient.id]
- num = $game_party.weapon_number(ingredient.id)
- when 2
- item = $data_armors[ingredient.id]
- num = $game_party.armor_number(ingredient.id)
- when 3
- item = RPG::Variable_Wrapper.new(ingredient.id)
- num = $game_variables[ingredient.id]
- else
- item = nil
- end
- if item == nil
- return
- end
- bitmap = RPG::Cache.icon(item.icon_name)
- disabled = num < ingredient.number
- opacity = disabled ? 128 : 255 #通过判定是否满足条件来展示不同透明度
- self.contents.blt(x, y+4, bitmap, Rect.new(0, 0, 24, 24), opacity)
- self.contents.font.color = disabled ? disabled_color : normal_color
- self.contents.draw_text(x + 28, y, 192, 32, item.name)
- self.contents.draw_text(x + 220, y, 64, 32, num.to_s, 2)
- self.contents.font.color = normal_color#这里可以描绘颜色!
- #合成素材的需求量!
- @需求量 = ingredient.number.to_i
- if @需求量 != nil
- @需求量 *= $number_合成_translate
- end
- self.contents.draw_text(x + 264, y, 64, 32,@需求量, 2)
- #self.contents.draw_text(x + 264, y, 64, 32,ingredient.number.to_s, 2)
- end
- def update
- super
- if self.contents != nil && self.contents.height > self.height - 32
- if @waiting > 0
- @waiting -= 1
- return
- end
- self.oy += @dy
- if self.oy + self.height - 32 >= self.contents.height
- @dy = -1
- @waiting = 60
- end
- if self.oy <= 0
- @dy = 1
- @waiting = 60
- end
- end
- end
- end
- #——————————■■■■合成窗口右上■■■■————
- class Window_Craft_Number < Window_Base
- def initialize #初始化
- super(240, 64, 400, 160)
- self.contents = Bitmap.new(width - 32, height - 32)
- @recipe = nil
- @max = 1
- @number = 1
- @number_current = 0
- end
- def recipe=(recipe) #带入一个实例变量(整个recipe的值,里头包含全部了)
- if @recipe != recipe
- @recipe = recipe
- reset
- refresh
- end
- end
- def reset #重置数据(刷新)
- if @recipe == nil
- @number = 0
- @number_current = 0
- return
- end
- product = @recipe.product
- case product.kind
- when 0
- @number_current = $game_party.item_number(product.id)
- when 1
- @number_current = $game_party.weapon_number(product.id)
- when 2
- @number_current = $game_party.armor_number(product.id)
- else
- @number_current = 0
- end
- @max = $game_party.product_max_num(@recipe.id)
- @max = [@max, 99 - @number_current].min
- @number = @max == 0 ? 0 : 1
- #中转数
- if @number < 1
- $number_合成_translate = 1
- else
- $number_合成_translate = @number
- end
- end
- def number
- return @number
- end
- #--------------------------------------------------------------------------
- # ● 刷新
- #--------------------------------------------------------------------------
- def refresh #描绘右上角的各个项目的【方法】
- self.contents.clear
- self.contents.font.color = system_color
- self.contents.draw_text(4, 0, 364, 32, "所持数")
- self.contents.font.color = normal_color
- self.contents.draw_text(4+100, 0, 364, 32, @number_current.to_s)
- #●说明文字描绘
- a=17 #说明文字:左右偏移
- b=18 #说明文字:行间距
- self.contents.font.size =8
- self.contents.draw_text(6, 32-9+b*0, 364, 32, "·") #说明文字前缀1-4
- self.contents.draw_text(6, 32-9+b*1, 364, 32, "·")
- self.contents.draw_text(6, 32-9+b*2, 364, 32, "·")
- self.contents.draw_text(6, 32-9+b*3, 364, 32, "·")
- self.contents.font.size =16
- self.contents.font.color = system_color
- self.contents.draw_text(a, 23+b*0, 364, 32, @recipe.txt1.to_s)
- self.contents.draw_text(a, 23+b*1, 364, 32, @recipe.txt2.to_s)
- self.contents.draw_text(a, 23+b*2, 364, 32, @recipe.txt3.to_s)
- self.contents.draw_text(a, 23+b*3, 364, 32, @recipe.txt4.to_s)
- self.contents.font.size =20
- self.contents.font.color = normal_color
-
- self.contents.font.color = system_color
- self.contents.draw_text(4+150, 96+3, 364, 32, "当前合成数量")
- self.contents.draw_text(252+30, 96+3, 32, 32, "×")
- self.contents.font.color = normal_color
- self.contents.draw_text(308, 96+3, 24, 32, @number.to_s, 2)#合成数量
- end
- def update
- super
- if self.active
- # 光标右 (+1)
- if Input.repeat?(Input::RIGHT) and @number < @max
- $game_system.se_play($data_system.cursor_se)
- @number += 1
- refresh
- end
- # 光标左 (-1)
- if Input.repeat?(Input::LEFT) and @number > 1
- $game_system.se_play($data_system.cursor_se)
- @number -= 1
- refresh
- end
- # 光标上 (+10)
- if Input.repeat?(Input::UP) and @number < @max
- $game_system.se_play($data_system.cursor_se)
- @number = [@number + 10, @max].min
- refresh
- end
- # 光标下 (-10)
- if Input.repeat?(Input::DOWN) and @number > 1
- $game_system.se_play($data_system.cursor_se)
- @number = [@number - 10, 1].max
- refresh
- end
- end
- end
- end
- #————————————描绘中间框——————————————
- class Window_Craft_Label < Window_Base
- def initialize
- super(240, 224, 400, 64)
- self.contents = Bitmap.new(width - 32, height - 32)
- refresh
- end
- def refresh
- self.contents.clear
- self.contents.font.color = system_color
- self.contents.draw_text(0, 0, width - 32, 32, "材料名称 现有 需要")
- end
- end
- class Scene_Title
- alias rb_main_20141226 main
- def main
- if $DEBUG
- RPG.extract_recipe
- end
- $data_recipes = load_data("Data/Recipes.rxdata")
- rb_main_20141226
- end
- end
- #——————————显示窗口 的最终显示处理————————————————
- class Scene_Craft
- def initialize(mode = -1)
- @mode = mode
- end
- def main
- @product_window = Window_Product.new(@mode)
- @help_window = Window_Help.new
- @product_window.help_window = @help_window
- @ingredient_window = Window_Ingredients.new
- @number_window = Window_Craft_Number.new #●●●右上角
- @label_window = Window_Craft_Label.new
- Graphics.transition
- loop do
- Graphics.update
- Input.update
- update
- if $scene != self
- break
- end
- end
- Graphics.freeze
- @product_window.dispose
- @help_window.dispose
- @ingredient_window.dispose
- @number_window.dispose
- @label_window.dispose
- end
- def update #显示各种数字等信息●●●
- @product_window.update
- @ingredient_window.update
- @ingredient_window.recipe = @product_window.recipe
- @number_window.recipe = @product_window.recipe #●●●右上角
- if @product_window.active
- update_product
- return
- end
- if @number_window.active
- update_number
- end
- end
- def update_product
- if Input.trigger?(Input::B)
- $game_system.se_play($data_system.cancel_se)
- $scene = Scene_Map.new
- return
- end
- if Input.trigger?(Input::C)
- recipe = @product_window.recipe
- if recipe == nil || $game_party.product_max_num(recipe.id) == 0
- $game_system.se_play($data_system.buzzer_se)
- return
- end
- $game_system.se_play($data_system.decision_se)
- @number_window.cursor_rect.set(304, 96, 32, 32)
- @product_window.active = false
- @number_window.active = true
- return
- end
- end
- def update_number
- @number_window.update
- if Input.trigger?(Input::B)
- $game_system.se_play($data_system.cancel_se)
- @number_window.active = false
- @product_window.active = true
- @number_window.cursor_rect.empty
- return
- end
- if Input.trigger?(Input::C)
- $game_system.se_play($data_system.shop_se)
- recipe = @product_window.recipe
- case recipe.product.kind
- when 0
- $game_party.gain_item(recipe.product.id, @number_window.number)
- when 1
- $game_party.gain_weapon(recipe.product.id, @number_window.number)
- when 2
- $game_party.gain_armor(recipe.product.id, @number_window.number)
- end
- recipe.ingredients.each do |ingredient|
- case ingredient.kind
- when 0
- $game_party.lose_item(ingredient.id, ingredient.number * @number_window.number)
- when 1
- $game_party.lose_weapon(ingredient.id, ingredient.number * @number_window.number)
- when 2
- $game_party.lose_armor(ingredient.id, ingredient.number * @number_window.number)
- when 3
- $game_variables[ingredient.id] -= ingredient.number * @number_window.number
- end
- end
- @product_window.refresh
- @ingredient_window.refresh
- @number_window.reset
- @number_window.refresh
- @product_window.active = true
- @number_window.active = false
- @number_window.cursor_rect.empty
- return
- end
- end
- end
复制代码
|
-
|