Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # =============================================================================
- # Autor: Redclaw
- # Thanks to: Playm (www.rpgmakerstudio.org)
- # Scriptname: Key System
- # Version : 0.8 Alpha
- # Contact : www.rpgmakerstudio.org jeremias.nigg@gmail.com
- # (English notes)
- # =============================================================================
- ($imported ||= {})[:Red_keysystem] = true
- # =============================================================================
- # Changelog:
- # -----------------------------------------------------------------------------
- # 2017.02.24 - Creation of the script
- # =============================================================================
- module SC #Module for shortcuts
- BRK = "\r\n" #Shortcut for newline
- end
- module REDCLAW
- module KEYSYS
- module CONFIG
- #=============================================================================
- # REDCLAW::KEYSYS::CONFIG
- #=============================================================================
- #Changeable settings for the script are located here
- MENU_BACKGROUND_IMAGE = "Graphics/Titles1/Book.png" #The path of the background image
- MENU_KEYINFO_PICTUREFOLDER = "Graphics/Pictures/Key/" #Folder which contains the pictures for the keys
- MENU_KEYINFO_PICTURE_HEIGHT = 128 #Defines the height for the pictures
- MENU_KEYINFO_PICTURE_OUTLINE_STRENGTH = 2
- MENU_KEYINFO_PICTURE_OUTLINE_COLOR = [0,0,0]
- end
- module VOCAB
- #=============================================================================
- # REDCLAW::KEYSYS::VOCAB
- #=============================================================================
- MENU_BUTTON_NAME = "Keys and Cards" #The name which will be used to display the button in the main menu.
- MENU_WINDOW_TITLE = MENU_BUTTON_NAME
- end #VOCAB
- #=============================================================================
- # REDCLAW::KEYSYS
- #=============================================================================
- TYPES = []
- TYPES[0] = {
- "name" => "Key",
- "id" => 1
- }
- TYPES[1] = {
- "name" => "Card",
- "id" => 2
- }
- KEYS = []
- KEYS[0] = {
- "name" => "Masterkey", #The name of the key which will be displayed in the menu
- "type" => 1, #The Id of TYPES
- "id" => 1, #Id for this keyitem
- "icon_index" => 243, #Icon to display (number of the icon, look it up in your database)
- "picture" => "", #filename of picture displayed in the menu - use "" to display the icon there instead
- "description" => "The best key at all, you are able to open any door with it.", #The description which will be used to be displayed in the menu
- "consumable" => false, #Defines if the player loses the keyitem on usage
- "amount" => 1 #Amount of keys the player will get in a new game. - Usually you should set this on 0 because the player shouldn't spawn with it
- }
- KEYS[1] = {
- "name" => "Entry ticket", #The name of the key which will be displayed in the menu
- "type" => 2, #The Id of TYPES
- "id" => 2, #Id for this keyitem
- "icon_index" => 239, #Icon to display (number of the icon, look it up in your database)
- "picture" => "", #filename of picture displayed in the menu - use "" to display the icon there instead
- "description" => "A ticket to get in the holy special entry.", #The description which will be used to be displayed in the menu
- "consumable" => true, #Defines if the player loses the keyitem on usage
- "amount" => 1 #Amount of keys the player will get in a new game. - Usually you should set this on 0 because the player shouldn't spawn with it
- }
- KEYS[2] = {
- "name" => "Housekey", #The name of the key which will be displayed in the menu
- "type" => 1, #The Id of TYPES
- "id" => 3, #Id for this keyitem
- "icon_index" => 241, #Icon to display (number of the icon, look it up in your database)
- "picture" => "", #filename of picture displayed in the menu - use "" to display the icon there instead
- "description" => "This is the key of my sweet home.", #The description which will be used to be displayed in the menu
- "consumable" => false, #Defines if the player loses the keyitem on usage
- "amount" => 1 #Amount of keys the player will get in a new game. - Usually you should set this on 0 because the player shouldn't spawn with it
- }
- end #KEYSYS
- end #REDCLAW
- class Keysystem
- attr_accessor :last_keys
- attr_accessor :active_type
- def initialize()
- @all_items = [] #Array including all items (from the keysystem - not regular items)
- @all_types = [] #Array including all types
- @active_type = :none
- @last_keys = []
- REDCLAW::KEYSYS::KEYS.each do |item_key| #initializing all the item objects in the array
- @all_items.push(Item.new(item_key["name"],item_key["type"],item_key["id"],item_key["icon_index"],item_key["picture"],item_key["description"],item_key["consumable"],item_key["amount"]))
- end
- REDCLAW::KEYSYS::TYPES.each do |item_type| #initializing all the type objects in the array
- @all_types.push(Type.new(item_type["name"],item_type["id"]))
- end
- #@all_types.each do |item_type|
- #@last_keys.push({"type_sym" => item_type.name.to_sym ,"item_id" => -1})
- #end
- end
- def get_items()
- return @all_items
- end
- def get_item(id)
- return @all_items[id-1]
- end
- def get_types
- return @all_types
- end
- def typename(id)
- return get_types[id-1].name
- end
- def typesym(id)
- typename(id).to_sym
- end
- def typesym_id(sym)
- sym = sym.to_s
- id = 0
- get_types.each do |type|
- if type.name == sym
- id = type.id
- end
- end
- return id
- end
- def possess?(id,takeitem = false) #use this method to check if the player actually possesses the item.
- if @all_items[id-1].amount > 0
- if @all_items[id-1].consumable && takeitem
- @all_items[id-1].amount -= 1
- end
- return true
- else
- return false
- end
- end
- def exhibit(id) #shortcut for check and remove an item (if the player posseses it)
- possess?(id,true)
- end
- def take(id,take_amount)
- @all_items[id-1].amount -= take_amount if(posess?(id,true) == true)
- end
- def give(id,give_amount)
- @all_items[id-1].amount += give_amount
- end
- def set(id,set_amount)
- @all_items[id-1].amount = set_amount
- end
- def report(id,showmsg = false) #Report method for testing purposes
- text = ""
- text << ("Name: " + get_item(id).name + SC::BRK)
- text << ("Type: " + typename(get_item(id).type) + SC::BRK)
- text << ("Id: " + get_item(id).id.to_s + SC::BRK)
- text << ("Iconnumber: " + get_item(id).icon_index.to_s + SC::BRK)
- text << ("Description: " + get_item(id).description + SC::BRK)
- text << ("Consumable: " + get_item(id).consumable.to_s + SC::BRK)
- text << ("Amount: " + get_item(id).amount.to_s)
- if !showmsg
- return text
- else
- msgbox(text)
- end
- end
- def report_all(showmsg = false) #Report_all method for testing purposes
- all_text = []
- get_items.each do |item_obj|
- all_text.push(report(item_obj.id))
- end
- if !showmsg
- return all_text
- else
- all_text.each do |txt|
- msgbox(txt)
- end
- end
- end
- end #Keysystem
- class Type < Keysystem
- attr_reader :name
- attr_reader :id
- @name = ""
- @id = 0
- def initialize(name,id)
- @name = name
- @id = id
- end
- end #Type
- class Item < Type
- attr_reader :name
- attr_reader :type
- attr_reader :id
- attr_reader :icon_index
- attr_reader :picture
- attr_reader :description
- attr_reader :consumable
- attr_accessor :amount
- @name = ""
- @type = 0
- @id = 0
- @icon_index = 0
- @picture = 0
- @description = ""
- @consumable = false
- @amount = 0;
- def initialize(name,type,id,icon_index,picture,description,consumable,amount)
- @name = name
- @type = type
- @id = id
- @icon_index = icon_index
- @picture = picture
- @description = description
- @consumable = consumable
- @amount = amount
- end
- end #Item
- class << DataManager
- alias keysys_create_game_objects create_game_objects
- def create_game_objects
- keysys_create_game_objects
- $game_keysystem = Keysystem.new
- end
- def 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[:keysystem] = $game_keysystem
- contents
- end
- def 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]
- $game_keysystem = contents[:keysystem]
- end
- end #DataManager
- class Window_KeyType < Window_HorzCommand
- #--------------------------------------------------------------------------
- # * Public Instance Variables
- #--------------------------------------------------------------------------
- attr_reader :item_window
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize
- super(0, 0)
- @type = :none
- @type = $game_keysystem.active_type
- end
- #--------------------------------------------------------------------------
- # * Get Window Width
- #--------------------------------------------------------------------------
- def window_width
- Graphics.width
- end
- #--------------------------------------------------------------------------
- # * Get Digit Count
- #--------------------------------------------------------------------------
- def col_max
- return $game_keysystem.get_types.size
- end
- #--------------------------------------------------------------------------
- # * Frame Update
- #--------------------------------------------------------------------------
- def update
- super
- $game_keysystem.active_type = current_symbol
- end
- #--------------------------------------------------------------------------
- # * Create Command List
- #--------------------------------------------------------------------------
- def make_command_list
- $game_keysystem.get_types.each do |type|
- add_command(type.name, type.name.to_sym)
- end
- end
- #--------------------------------------------------------------------------
- # * Set Item Window
- #--------------------------------------------------------------------------
- def item_window=(item_window)
- @item_window = item_window
- update
- end
- end
- class Window_MenuCommand < Window_Command
- alias add_original_commands_keysys add_original_commands
- def add_original_commands
- add_original_commands_keysys
- add_command(REDCLAW::KEYSYS::VOCAB::MENU_BUTTON_NAME, :keys, true)
- end
- end
- class Window_Keys < Window_Selectable
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize(x, y, width, height)
- super
- @type = $game_keysystem.active_type
- @category = @type
- @data = []
- end
- #--------------------------------------------------------------------------
- # * Set Category
- #--------------------------------------------------------------------------
- def category=(category)
- return if @category == category
- @category = category
- refresh
- self.oy = 0
- end
- #--------------------------------------------------------------------------
- # * Get Digit Count
- #--------------------------------------------------------------------------
- def col_max
- return 1
- end
- #--------------------------------------------------------------------------
- # * Get Number of Items
- #--------------------------------------------------------------------------
- def item_max
- @data ? @data.size : 1
- end
- #--------------------------------------------------------------------------
- # * Get Item
- #--------------------------------------------------------------------------
- def item
- @data && index >= 0 ? @data[index] : nil
- end
- #--------------------------------------------------------------------------
- # * Get Activation State of Selection Item
- #--------------------------------------------------------------------------
- def current_item_enabled?
- enable?(@data[index])
- end
- #--------------------------------------------------------------------------
- # * Include in Item List?
- #--------------------------------------------------------------------------
- def include?(item)
- type_id = item.type
- return $game_keysystem.typesym(type_id) == @type && $game_keysystem.possess?(item.id)
- end
- #--------------------------------------------------------------------------
- # * Display in Enabled State?
- #--------------------------------------------------------------------------
- def enable?(item)
- return true
- end
- #--------------------------------------------------------------------------
- # * Create Item List
- #--------------------------------------------------------------------------
- def make_key_list
- @data.clear
- @type = $game_keysystem.active_type
- $game_keysystem.get_items.each do |item|
- @data.push(item) if include?(item)
- end
- end
- #--------------------------------------------------------------------------
- # * Restore Previous Selection Position
- #--------------------------------------------------------------------------
- def select_last
- select(0)
- end
- #--------------------------------------------------------------------------
- # * Draw Item
- #--------------------------------------------------------------------------
- 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))
- draw_item_number(rect, item)
- end
- end
- #--------------------------------------------------------------------------
- # * Draw Number of Items
- #--------------------------------------------------------------------------
- def draw_item_number(rect, item)
- draw_text(rect, sprintf(":%2d", item.amount.to_s), 2)
- end
- #--------------------------------------------------------------------------
- # * Update Help Text
- #--------------------------------------------------------------------------
- def update_help
- @help_window.set_item(item)
- end
- #--------------------------------------------------------------------------
- # * Refresh
- #--------------------------------------------------------------------------
- def refresh
- make_key_list
- create_contents
- draw_all_items
- end
- def clear
- self.contents.clear
- end
- end #Window_Keys
- class Window_Keyinfo < Window_Selectable
- #--------------------------------------------------------------------------
- # * Object Initialization
- #--------------------------------------------------------------------------
- def initialize
- super(0, 0, Graphics.width, Graphics.height)
- refresh
- draw_picture(0,0)
- end
- def picture_path
- return REDCLAW::KEYSYS::CONFIG::MENU_KEYINFO_PICTUREFOLDER + "key2.png"
- #return REDCLAW::KEYSYS::CONFIG::MENU_BACKGROUND_IMAGE
- end
- #--------------------------------------------------------------------------
- # * Refresh
- #--------------------------------------------------------------------------
- def refresh
- contents.clear
- draw_picture(0,0)
- end
- #--------------------------------------------------------------------------
- # * Open Window
- #--------------------------------------------------------------------------
- def open
- refresh
- super
- end
- def too_big?(rect)
- rect.width > contents.width or rect.height > contents.height
- end
- def rectscale(rect,axe,value)
- scaled_x = 0.0
- scaled_y = 0.0
- ratio = 0.0
- if axe.upcase == "X"
- ratio = 1.0 / (rect.width / value)
- scaled_x = value
- scaled_y = rect.height * ratio
- end
- if axe.upcase == "Y"
- ratio = 1.0 / (rect.height / value)
- scaled_y = value
- scaled_x = rect.width * ratio
- end
- scaled_rect = Rect.new(0,0, scaled_x, scaled_y)
- return scaled_rect
- end
- def draw_picture(x, y, enabled = true)
- h = REDCLAW::KEYSYS::CONFIG::MENU_KEYINFO_PICTURE_HEIGHT
- os = REDCLAW::KEYSYS::CONFIG::MENU_KEYINFO_PICTURE_OUTLINE_STRENGTH
- oc = make_color(REDCLAW::KEYSYS::CONFIG::MENU_KEYINFO_PICTURE_OUTLINE_COLOR)
- image = Bitmap.new(picture_path)
- n_rect = rectscale(image.rect,"y", h)
- image.stretch_blt(n_rect,image,image.rect)
- s_rect = Rect.new(0,0,n_rect.width+os*2,n_rect.height+os*2)
- bitmap = Bitmap.new(s_rect.width,s_rect.height)
- bitmap.fill_rect(s_rect,oc)
- s_rect.width -= os*2
- s_rect.height -= os*2
- bitmap.blt(os, os, image, s_rect)
- s_rect.width += os*2
- s_rect.height += os*2
- contents.blt((contents.width / 2) - s_rect.width, 0, bitmap, s_rect)
- unless $game_keysystem.last_keys[0] == nil
- text_rect = text_size($game_keysystem.last_keys[0].name)
- draw_text(text_rect, $game_keysystem.last_keys[0].name)
- end
- end
- def make_color(colors)
- return Color.new(colors[0],colors[1],colors[2])
- end
- end
- # =============================================================================
- # Scene classes goes here
- # =============================================================================
- # =============================================================================
- # ? Scene_KeysBase
- # =============================================================================
- class Scene_KeysBase < Scene_MenuBase
- def dispose_main_viewport
- @viewport.dispose if @viewport != nil
- end
- end
- class Scene_KeysBase < Scene_MenuBase
- #--------------------------------------------------------------------------
- # * Start Processing
- #--------------------------------------------------------------------------
- def start
- super
- end
- def draw_background(path)
- @background_sprite = Sprite.new
- @background_sprite = Bitmap.new(path)
- end
- def create_background
- draw_background(REDCLAW::KEYSYS::CONFIG::MENU_BACKGROUND_IMAGE)
- end
- #--------------------------------------------------------------------------
- # * Get Currently Selected Item
- #--------------------------------------------------------------------------
- def item
- @keyitems_window.item
- end
- #--------------------------------------------------------------------------
- # * Determine if Cursor Is in Left Column
- #--------------------------------------------------------------------------
- def cursor_left?
- @item_window.index % 2 == 0
- end
- #--------------------------------------------------------------------------
- # * Show Subwindow
- #--------------------------------------------------------------------------
- def show_sub_window(window)
- width_remain = Graphics.width - window.width
- window.x = cursor_left? ? width_remain : 0
- @viewport.rect.x = @viewport.ox = cursor_left? ? 0 : window.width
- @viewport.rect.width = width_remain
- window.show.activate
- end
- #--------------------------------------------------------------------------
- # * Hide Subwindow
- #--------------------------------------------------------------------------
- def hide_sub_window(window)
- @viewport.rect.x = @viewport.ox = 0
- @viewport.rect.width = Graphics.width
- window.hide.deactivate
- activate_item_window
- end
- #--------------------------------------------------------------------------
- # * Activate Item Window
- #--------------------------------------------------------------------------
- def activate_item_window
- @keyitems_window.refresh
- @keyitems_window.activate
- end
- #--------------------------------------------------------------------------
- # * Determine if Common Event Is Reserved
- # Transition to the map screen if the event call is reserved.
- #--------------------------------------------------------------------------
- def check_common_event
- SceneManager.goto(Scene_Map) if $game_temp.common_event_reserved?
- end
- end
- # =============================================================================
- # ? Scene_Keys
- # =============================================================================
- class Scene_Keys < Scene_KeysBase
- def start
- create_keytype_window
- create_keyitems_window
- create_keyinfo_window
- create_background
- end
- #--------------------------------------------------------------------------
- # * Create Category Window
- #--------------------------------------------------------------------------
- def on_keytype_ok
- @keyitems_window.activate
- @keyitems_window.refresh
- @keyitems_window.select(0)
- end
- def on_item_cancel
- @keyitems_window.unselect
- @keytype_window.activate
- end
- def create_keytype_window
- @keytype_window = Window_KeyType.new
- @keytype_window.viewport = @viewport
- @keytype_window.y = 0
- @keytype_window.item_window = @keyitems_window
- @keytype_window.set_handler(:ok, method(:on_keytype_ok))
- @keytype_window.set_handler(:cancel, method(:return_scene))
- end
- def create_keyitems_window
- @keyitems_window = Window_Keys.new(0,@keytype_window.height + 1, Graphics.width, Graphics.height - @keytype_window.height)
- @keyitems_window.set_handler(:ok, method(:on_confirm))
- @keyitems_window.set_handler(:cancel, method(:return_typeselect))
- end
- def create_keyinfo_window
- @keyinfo_window = Window_Keyinfo.new
- @keyinfo_window.set_handler(:cancel, method(:close_keyinfo))
- @keyinfo_window.hide
- end
- def show_keyinfo
- @keytype_window.hide
- @keyitems_window.deactivate
- @keyitems_window.hide
- @keyinfo_window.show
- @keyinfo_window.activate
- @keyinfo_window.refresh
- end
- def close_keyinfo
- @keyinfo_window.hide
- @keytype_window.show
- @keyitems_window.show
- @keyitems_window.activate
- end
- def return_typeselect
- @keyitems_window.unselect
- @keyitems_window.clear
- @keyitems_window.deactivate
- @keytype_window.activate
- end
- def create_category_window
- create_keytype_window
- end
- def create_item_window
- create_keyitems_window
- end
- def on_category_ok
- on_keytype_ok
- end
- def on_confirm
- unless $game_keysystem.last_keys.include?(@keyitems_window.item)
- $game_keysystem.last_keys[0] = @keyitems_window.item
- end
- show_keyinfo
- end
- def update_help
- @help_window.set_item(item)
- end
- def make_key_list
- keys = []
- $game_keysystem.get_items.each do |key|
- keys.push(key) unless key.amount <= 0
- end
- end
- end #Scene_Keys
- # =============================================================================
- # ? Scene_Menu
- # =============================================================================
- class Scene_Menu < Scene_MenuBase
- alias redclaw_keysys create_command_window
- def command_keys
- SceneManager.call(Scene_Keys)
- end
- def create_command_window
- redclaw_keysys
- @command_window.set_handler(:keys, method(:command_keys))
- end
- end #Scene_Menu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement