Project1

标题: 物品制作脚本 [打印本页]

作者: h2195458940    时间: 2017-11-6 14:09
标题: 物品制作脚本
我这里有个物品制作脚本
但是只能在菜单里才能制作物品   我想让它必须在地图内的一个事件才能制作物品
求大神帮忙


#encoding:utf-8
#==============================================================================
# 本腳本來自www.66RPG.com,使用和轉載請保留此信息
#==============================================================================
#==============================================================================
#
#每添加一个制造的类型,最上面的类别菜单就会多一项。
#
#添加类型的方法是:
#$game_party.add_cook_type(类别名称,类别介绍, 类别状态-默认false)
#
#举例:
#$game_party.add_cook_type("制药","调配药品", true)
#
#添加配方的方法:
#$game_party.add_cookbook(配方名称, 配方介绍, 配方类别名称,材料的二维数组,产出物的二维数组,配方状态)
#
#举例:
#$game_party.add_cookbook("初级补血药水", "制作初级补血药水", "制药",[[18,2]],[[1,1]],true)
#
#调用窗口的方法:SceneManager.call(Scene_Cook)
#
#
#==============================================================================

#==============================================================================
# ■ Cookbook_Type
#------------------------------------------------------------------------------
#  食谱类型类。
#==============================================================================

class Cookbook_Type
  attr_reader :index
  attr_reader :name
  attr_reader :description
  attr_reader :enabled
  #--------------------------------------------------------------------------
  # ● 初始化对象
  #--------------------------------------------------------------------------
  def initialize(index, name, description, enabled = false)
    @index = index
    @name = name
    @description = description
    @enabled = enabled
  end

  def enable(en)
    @enabled = en
  end
end

#==============================================================================
# ■ Cookbook
#------------------------------------------------------------------------------
#  食谱类。本类在 Game_Task 类的内部使用。
#   食谱属性:食谱序号,食谱名称,食谱类型,食谱介绍,原料,成品
#==============================================================================

class Cookbook
  attr_reader :index
  attr_reader :name
  attr_reader :description
  attr_reader :type
  attr_reader :input
  attr_reader :output
  attr_reader :enabled
  #--------------------------------------------------------------------------
  # ● 初始化对象
  #--------------------------------------------------------------------------
  def initialize(index, name, description, type, input, output, enabled = true)
    @index = index
    @name = name
    @description = description
    @type = type
    @input = input
    @output = output
    @enabled = enabled
  end

  def enable(en)
    @enabled = en
  end

  #--------------------------------------------------------------------------
  # ● 查询列表中的物品
  #--------------------------------------------------------------------------
  def in_list?(item, list)
    list.each do |arr|
      return true if arr[0] == item.id
    end
    return false
  end
  #--------------------------------------------------------------------------
  # ● 查询是否是材料列表中的物品
  #--------------------------------------------------------------------------
  def resource?(item)
    in_list?(item, @input)
  end
  #--------------------------------------------------------------------------
  # ● 查询是否是产出列表中的物品
  #--------------------------------------------------------------------------
  def output?(item)
    in_list?(item, @output)
  end
  #--------------------------------------------------------------------------
  # ● 查询材料需求量
  #--------------------------------------------------------------------------
  def amount(item, i)
    if i == 0
      @input.each do |arr|
        return arr[1] if arr[0] == item.id
      end
    else
      @output.each do |arr|
        return arr[1] if arr[0] == item.id
      end
    end
    return 0
  end
  #--------------------------------------------------------------------------
  # ● 查询材料是否足够
  #--------------------------------------------------------------------------
  def enough?
    input.each do |arr|
      return false if $data_items[arr[0]] && arr[1] > $game_party.item_number($data_items[arr[0]])
    end
    return true
  end
  #--------------------------------------------------------------------------
  # ● 查询某件材料是否足够
  #--------------------------------------------------------------------------
  def item_enough?(item)
    input.each do |arr|
      return false if arr[0] == item.id && arr[1] > $game_party.item_number(item)
    end
    return true
  end
end

#==============================================================================
# ■ Game_Party
#==============================================================================

class Game_Party < Game_Unit
  #--------------------------------------------------------------------------
  # ● 初始化对象
  #--------------------------------------------------------------------------
  alias old_init initialize
  def initialize
    old_init
    @cook_types = []
    @cookbooks = []
  end
  #--------------------------------------------------------------------------
  # ● 添加新的制造类型
  #--------------------------------------------------------------------------
  def add_cook_type(n, d, en)
    @cook_types.push(Cookbook_Type.new(@cook_types.size, n, d, en)) if !have_cook_type?(n)
  end
  #--------------------------------------------------------------------------
  # ● 添加新的食谱
  #--------------------------------------------------------------------------
  def add_cookbook(n, d, type, input, output, en)
    if !have_cookbook?(n) && have_cook_type?(type)
      @cookbooks.push(Cookbook.new(@cookbooks.size, n, d, type, input, output, en))
    end
  end
  #--------------------------------------------------------------------------
  # ● 判断名称为n的制造类型是否存在
  #--------------------------------------------------------------------------
  def have_cook_type?(n)
    @cook_types.each do |x|
      return true if x.name == n
    end
    return false
  end
  #--------------------------------------------------------------------------
  # ● 判断名称为n的食谱是否存在
  #--------------------------------------------------------------------------
  def have_cookbook?(n)
    @cookbooks.each do |x|
      return true if x.name == n
    end
    return false
  end
  #--------------------------------------------------------------------------
  # ● 返回制造类型列表
  #--------------------------------------------------------------------------
  def cook_types
    return @cook_types
  end
  #--------------------------------------------------------------------------
  # ● 返回食谱列表
  #--------------------------------------------------------------------------
  def cookbooks(type = nil)
    return @cookbooks if type == nil
    arr = []
    @cookbooks.each do |x|
      arr.push(x) if x.type == type
    end
    return arr
  end
  #--------------------------------------------------------------------------
  # ● 更改序号为i的制造类型的使用状态
  #--------------------------------------------------------------------------
  def set_cook_type(i, en)
    @cook_types.enable(en)
  end
  #--------------------------------------------------------------------------
  # ● 更改序号为i的食谱的使用状态
  #--------------------------------------------------------------------------
  def set_cookbook(i, en)
    @cookbooks.enable(en)
  end
  #--------------------------------------------------------------------------
  # ● 返回制造类型数目
  #--------------------------------------------------------------------------
  def cook_types_size
    return @cook_types.size
  end
  #--------------------------------------------------------------------------
  # ● 查询名称为n的制造类型的描述
  #--------------------------------------------------------------------------
  def cook_type_description(n)
    @cook_types.each do |x|
      return x.description if x.name == n
    end
    return ""
  end
end

#==============================================================================
# ■ Scene_Cook
#------------------------------------------------------------------------------
#  制造画面
#==============================================================================

class Scene_Cook < Scene_ItemBase
  #--------------------------------------------------------------------------
  # ● 开始处理
  #--------------------------------------------------------------------------
  def start
    super
    create_category_window
    create_cookbook_window
    create_description_window
    create_needs_window
  end
  #--------------------------------------------------------------------------
  # ● 生成分类窗口
  #--------------------------------------------------------------------------
  def create_category_window
    @category_window = Window_CookCategory.new
    @category_window.viewport = @viewport
    @category_window.y = 0
    @category_window.set_handler(:ok,     method(:on_category_ok))
    @category_window.set_handler(:cancel, method(:return_scene))
  end
  #--------------------------------------------------------------------------
  # ● 生成食谱窗口
  #--------------------------------------------------------------------------
  def create_cookbook_window
    wy = @category_window.height
    wh = Graphics.height - wy
    @item_window = Window_CookList.new(0, wy, Graphics.width*0.5 , wh)
    @item_window.viewport = @viewport
    @item_window.set_handler(:ok,     method(:on_item_ok))
    @item_window.set_handler(:cancel, method(:on_item_cancel))
    @category_window.item_window = @item_window
  end
  #--------------------------------------------------------------------------
  # ● 生成描述窗口
  #--------------------------------------------------------------------------
  def create_description_window
    wy = @category_window.height
    @help_window = Window_Description.new(Graphics.width*0.5, wy)
    @help_window.viewport = @viewport
    @item_window.help_window = @help_window
    @category_window.help_window = @help_window
  end
    #--------------------------------------------------------------------------
  # ● 生成材料窗口
  #--------------------------------------------------------------------------
  def create_needs_window
    wy = @category_window.height + @help_window.height
    wh = Graphics.height - wy
    @needs_window = Window_NeedsList.new(Graphics.width*0.5, wy, Graphics.width*0.5 , wh)
    #@item_window.viewport = @viewport
    @item_window.needs_window = @needs_window

  end
  #--------------------------------------------------------------------------
  # ● 分类“确定”
  #--------------------------------------------------------------------------
  def on_category_ok
    @item_window.activate
    @item_window.select_last
  end
  #--------------------------------------------------------------------------
  # ● 食谱“确定”
  #--------------------------------------------------------------------------
  def on_item_ok
    #$game_party.last_item.object = item
    cook
  end
  #--------------------------------------------------------------------------
  # ● 食谱“取消”
  #--------------------------------------------------------------------------
  def on_item_cancel
    @item_window.unselect
    @category_window.activate
  end
  #--------------------------------------------------------------------------
  # ● 制造
  #--------------------------------------------------------------------------
  def cook
    if item.enough?
      play_se_for_item
      use_item
      @needs_window.refresh
      @item_window.refresh
      activate_item_window
    end
  end
  #--------------------------------------------------------------------------
  # ● 播放制作声效
  #--------------------------------------------------------------------------
  def play_se_for_item
    Sound.play_recovery
  end
  #--------------------------------------------------------------------------
  # ● 使用食谱
  #--------------------------------------------------------------------------
  def use_item
    #super
    #@item_window.redraw_current_item
    $data_items.each do |it|
      if it && !it.name.empty?
        $game_party.gain_item(it,-item.amount(it,0)) if item.resource?(it)
        $game_party.gain_item(it,item.amount(it,1)) if item.output?(it)
      end
    end
  end
end


#==============================================================================
# ■ Window_CookCategory
#------------------------------------------------------------------------------
#  制造画面中,显示制造类型的窗口。
#==============================================================================

class Window_CookCategory < Window_HorzCommand
  #--------------------------------------------------------------------------
  # ● 定义实例变量
  #--------------------------------------------------------------------------
  attr_reader   :item_window
  #--------------------------------------------------------------------------
  # ● 初始化对象
  #--------------------------------------------------------------------------
  def initialize
    super(0, 0)
  end
  #--------------------------------------------------------------------------
  # ● 获取窗口的宽度
  #--------------------------------------------------------------------------
  def window_width
    Graphics.width
  end
  #--------------------------------------------------------------------------
  # ● 获取列数
  #--------------------------------------------------------------------------
  def col_max
    return $game_party.cook_types_size
  end
  #--------------------------------------------------------------------------
  # ● 更新画面
  #--------------------------------------------------------------------------
  def update
    super
    #@item_window.category = current_symbol if @item_window
    @item_window.category = current_data[:name] if @item_window
  end
  #--------------------------------------------------------------------------
  # ● 生成制造列表
  #--------------------------------------------------------------------------
  def make_command_list
    $game_party.cook_types.each do |t|
      add_command(t.name, t.index, t.enabled)
    end
  end
  #--------------------------------------------------------------------------
  # ● 设置制造列表窗口
  #--------------------------------------------------------------------------
  def item_window=(item_window)
    @item_window = item_window
    update
  end
  #--------------------------------------------------------------------------
  # ● 更新帮助内容
  #--------------------------------------------------------------------------
  def update_help
    @help_window.set_text($game_party.cook_type_description(current_data[:name]))
  end
end

#==============================================================================
# ■ Window_CookList
#------------------------------------------------------------------------------
#  任务画面中,显示已获得任务的窗口。
#==============================================================================

class Window_CookList < Window_Selectable
  #--------------------------------------------------------------------------
  # ● 定义实例变量
  #--------------------------------------------------------------------------
  attr_reader   :needs_window
  #--------------------------------------------------------------------------
  # ● 初始化对象
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    super
    @data = []
    @category = $game_party.cook_types_size > 0 ? $game_party.cook_types[0].name : nil
    refresh
  end
  #--------------------------------------------------------------------------
  # ● 设置分类
  #--------------------------------------------------------------------------
  def category=(category)
    return if @category == category
    @category = category
    refresh
    self.oy = 0
  end
  #--------------------------------------------------------------------------
  # ● 设置材料窗口
  #--------------------------------------------------------------------------
  def needs_window=(needs_window)
    @needs_window = needs_window
    update
  end
  #--------------------------------------------------------------------------
  # ● 获取列数
  #--------------------------------------------------------------------------
  def col_max
    return 1
  end
  #--------------------------------------------------------------------------
  # ● 获取项目数
  #--------------------------------------------------------------------------
  def item_max
    @data ? @data.size : 1
  end
  #--------------------------------------------------------------------------
  # ● 获取食谱
  #--------------------------------------------------------------------------
  def item
    @data && index >= 0 ? @data[index] : nil
  end
  #--------------------------------------------------------------------------
  # ● 获取选择食谱的有效状态
  #--------------------------------------------------------------------------
  def current_item_enabled?
    enable?(@data[index])
  end
  #--------------------------------------------------------------------------
  # ● 查询此食谱是否可用
  #--------------------------------------------------------------------------
  def enable?(item)
    item.enabled && item.enough?
  end
  #--------------------------------------------------------------------------
  # ● 生成食谱列表
  #--------------------------------------------------------------------------
  def make_item_list
    @data = $game_party.cookbooks(@category)
  end
  #--------------------------------------------------------------------------
  # ● 返回上一个选择的位置
  #--------------------------------------------------------------------------
  def select_last
    select(0)
  end
  #--------------------------------------------------------------------------
  # ● 绘制项目
  #--------------------------------------------------------------------------
  def draw_item(index)
    item = @data[index]
    if item
      rect = item_rect(index)
      rect.width -= 4
      draw_item_name(item, rect.x, rect.y, enable?(item))
    end
  end
  #--------------------------------------------------------------------------
  # ● 绘制名称
  #     enabled : 有效的标志。false 的时候使用半透明效果绘制
  #--------------------------------------------------------------------------
  def draw_item_name(item, x, y, enabled = true, width = 300)
    return unless item
    text = item.name
    text += "[材料不足]" if !item.enough?
    change_color(normal_color, enabled)
    draw_text(x, y, width, line_height, text)
  end
  #--------------------------------------------------------------------------
  # ● 更新帮助内容
  #--------------------------------------------------------------------------
  def update_help
    @help_window.set_item(item)
  end
   #--------------------------------------------------------------------------
  # ● 更新食谱清单
  #--------------------------------------------------------------------------
  def update_needslist
    @needs_window.set_item(item)
  end
  #--------------------------------------------------------------------------
  # ● 调用帮助窗口的更新方法
  #--------------------------------------------------------------------------
  def call_update_help
    update_help if @help_window
    update_needslist if @needs_window
  end
  #--------------------------------------------------------------------------
  # ● 刷新
  #--------------------------------------------------------------------------
  def refresh
    make_item_list
    create_contents
    draw_all_items
  end
end
#==============================================================================
# ■ Window_Description
#------------------------------------------------------------------------------
#  显示食谱的说明的窗口
#==============================================================================

class Window_Description < Window_Base
  #--------------------------------------------------------------------------
  # ● 初始化对象
  #--------------------------------------------------------------------------
  def initialize(x, y)
    super(x, y, Graphics.width*0.5, fitting_height(4))
  end
  #--------------------------------------------------------------------------
  # ● 设置内容
  #--------------------------------------------------------------------------
  def set_text(text)
    if text != @text
      @text = text
      refresh
    end
  end
  #--------------------------------------------------------------------------
  # ● 清除
  #--------------------------------------------------------------------------
  def clear
    set_text("")
  end
  #--------------------------------------------------------------------------
  # ● 设置食谱的介绍
  #--------------------------------------------------------------------------
  def set_item(item)
    set_text(item ? item.description : "")
  end
  #--------------------------------------------------------------------------
  # ● 刷新
  #--------------------------------------------------------------------------
  def refresh
    contents.clear
    draw_text_ex(4, 0, @text)
  end
end

#==============================================================================
# ■ Window_NeedsList
#------------------------------------------------------------------------------
#  制造画面中,显示食谱所需材料的窗口。
#==============================================================================

class Window_NeedsList < Window_Selectable
  #--------------------------------------------------------------------------
  # ● 初始化对象
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    super
    @category = :item
    @cookbook = nil
    @data = []
    refresh
  end
  #--------------------------------------------------------------------------
  # ● 获取列数
  #--------------------------------------------------------------------------
  def col_max
    return 1
  end
  #--------------------------------------------------------------------------
  # ● 获取项目数
  #--------------------------------------------------------------------------
  def item_max
    @data ? @data.size : 1
  end
  #--------------------------------------------------------------------------
  # ● 获取物品
  #--------------------------------------------------------------------------
  def item
    @data && index >= 0 ? @data[index] : nil
  end
  #--------------------------------------------------------------------------
  # ● 查询列表中是否含有此物品
  #--------------------------------------------------------------------------
  def include?(item)
    item.is_a?(RPG::Item) && !item.key_item?
  end
  #--------------------------------------------------------------------------
  # ● 获取选择食谱的有效状态
  #--------------------------------------------------------------------------
  def current_item_enabled?
    enable?(@data[index])
  end
  #--------------------------------------------------------------------------
  # ● 查询此食谱是否可用
  #--------------------------------------------------------------------------
  def enable?(item)
    $game_party.usable?(item) && @cookbook.item_enough?(item)
  end
  #--------------------------------------------------------------------------
  # ● 生成物品列表
  #--------------------------------------------------------------------------
  def make_item_list
    @data = []
    return if @cookbook== nil
    tmp1 = []
    tmp2 = []
    $data_items.each do |item|
      tmp1.push([item, 0]) if item && !item.name.empty? && @cookbook.resource?(item)
      tmp2.push([item, 1]) if item && !item.name.empty? && @cookbook.output?(item)
    end
    if tmp1.size > 0 && tmp2.size > 0
      @data.push(["需要材料:",0])
      @data += tmp1
      @data.push(["可以获得:",1])
      @data += tmp2
    end
  end
  #--------------------------------------------------------------------------
  # ● 返回上一个选择的位置
  #--------------------------------------------------------------------------
  def select_last
    select(0)
  end
  #--------------------------------------------------------------------------
  # ● 绘制项目
  #--------------------------------------------------------------------------
  def draw_item(index)
    item = @data[index]
    if item
      rect = item_rect(index)
      rect.width -= 4
      if item[0].is_a?(RPG::Item)
        draw_item_name(item, rect.x, rect.y, enable?(item[0]))
        draw_item_number(rect, item[0])
      else
        draw_input_output(item, rect.x, rect.y)
      end
    end
  end
  #--------------------------------------------------------------------------
  # ● 绘制物品名称
  #     enabled : 有效的标志。false 的时候使用半透明效果绘制
  #--------------------------------------------------------------------------
  def draw_item_name(item, x, y, enabled = true, width = 172)
    return unless item
    text = item[0].name + "*" + String(@cookbook.amount(item[0], item[1]))
    draw_icon(item[0].icon_index, x, y, enabled)
    change_color(normal_color, enable?(item[0]))
    draw_text(x + 24, y, width, line_height, text)
  end
  #--------------------------------------------------------------------------
  # ● 绘制输入和产出
  #--------------------------------------------------------------------------
  def draw_input_output(item, x, y, enabled = false, width = 172)
    return unless item
    if item[1] == 0
      text = "[需要]"
    else
      text = "[可以获得]"
    end
    change_color(normal_color, @cookbook.enough?)
    draw_text(x , y, width, line_height, text)
  end
  #--------------------------------------------------------------------------
  # ● 绘制物品个数
  #--------------------------------------------------------------------------
  def draw_item_number(rect, item)
    draw_text(rect, sprintf("现有%2d", $game_party.item_number(item)), 2)
  end
  #--------------------------------------------------------------------------
  # ● 刷新
  #--------------------------------------------------------------------------
  def refresh
    make_item_list
    create_contents
    draw_all_items
  end
  #--------------------------------------------------------------------------
  # ● 设置食谱的材料
  #--------------------------------------------------------------------------
  def set_item(item)
    @cookbook = item
    refresh
  end
end

#==============================================================================
# 本腳本來自www.66RPG.com,使用和轉載請保留此信息
#==============================================================================


# add by Sion 2013.12.17
# 作者修改了原范例的菜单,但是在原系统脚本里改的。
# 整理出来写在这里, 不需要改菜单的同学把这些删掉就可以。
class Scene_Menu
  def command_cook
    SceneManager.call(Scene_Cook)
  end
  alias_method :s20131217_create_command_window, :create_command_window
  def create_command_window
    s20131217_create_command_window
    @command_window.set_handler(:cook, method(:command_cook))
  end
end
class Window_MenuCommand
  alias_method :s20131217_add_main_commands, :add_main_commands
  def add_main_commands
    s20131217_add_main_commands
    add_command("制造",   :cook,   main_commands_enabled)
  end
end[attach]336019[/attach][attach]336019[/attach][attach]336019[/attach]

1.png (81.91 KB, 下载次数: 31)

1.png

2.png (127.3 KB, 下载次数: 27)

2.png

3.png (80.67 KB, 下载次数: 24)

3.png

作者: 魔法☆梅莉    时间: 2017-11-6 15:19
新建一个事件,在事件里选择“脚本…”输入这句代码:
SceneManager.call(Scene_Cook)
保存即可。
不想在菜单里显示的话,把“class Scene_Menu”那一段去掉就可以了。
作者: WantMy蕙    时间: 2017-11-6 21:11
本帖最后由 WantMy蕙 于 2017-11-6 21:13 编辑

事件里面选择脚本然后添加这一句SceneManager.call(Scene_Cook),应该就OK了
至于删菜单就删这一段就好了
RUBY 代码复制
  1. #==============================================================================
  2. # 本腳本來自[url]www.66RPG.com[/url],使用和轉載請保留此信息
  3. #==============================================================================
  4.  
  5.  
  6. # add by Sion 2013.12.17
  7. # 作者修改了原范例的菜单,但是在原系统脚本里改的。
  8. # 整理出来写在这里, 不需要改菜单的同学把这些删掉就可以。
  9. class Scene_Menu
  10.   def command_cook
  11.     SceneManager.call(Scene_Cook)
  12.   end
  13.   alias_method :s20131217_create_command_window, :create_command_window
  14.   def create_command_window
  15.     s20131217_create_command_window
  16.     @command_window.set_handler(:cook, method(:command_cook))
  17.   end
  18. end
  19. class Window_MenuCommand
  20.   alias_method :s20131217_add_main_commands, :add_main_commands
  21.   def add_main_commands
  22.     s20131217_add_main_commands
  23.     add_command("制造",   :cook,   main_commands_enabled)
  24.   end



作者: h2195458940    时间: 2017-11-6 21:28
魔法☆梅莉 发表于 2017-11-6 15:19
新建一个事件,在事件里选择“脚本…”输入这句代码:
SceneManager.call(Scene_Cook)
保存即可。

谢谢   问题已解决




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1