Class: PFM::Bag
- Defined in:
- scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb
Overview
InGame Bag management
The global Bag object is stored in $bag and PFM.game_state.bag
Constant Summary collapse
- SHORTCUT_AMOUNT =
Number of shortcut
4
Instance Attribute Summary collapse
-
#alpha_sorted ⇒ Boolean
Tell if the bag is alpha sorted.
-
#game_state ⇒ PFM::GameState
Get the game state responsive of the whole game state.
-
#last_battle_item_db_symbol ⇒ Symbol
Set the last battle item.
-
#last_index ⇒ Integer
Last index in the socket.
-
#last_socket ⇒ Integer
Last socket used in the bag.
-
#locked ⇒ Boolean
If the bag is locked (and react as being empty).
Instance Method Summary collapse
-
#add_item(db_symbol, nb = 1)
(also: #store_item)
Add items in the bag and trigger the right quest objective.
-
#contain_item?(db_symbol) ⇒ Boolean
(also: #has_item?)
If the bag contain a specific item.
-
#convert_to_dot26
Convert bag to .26 format.
-
#empty? ⇒ Boolean
Tell if the bag is empty.
-
#get_order(socket) ⇒ Array
Get the order of items in a socket.
-
#initialize(game_state = PFM.game_state) ⇒ Bag
constructor
Create a new Bag.
-
#item_quantity(db_symbol) ⇒ Integer
The quantity of an item in the bag.
-
#last_battle_item ⇒ Studio::Item
Get the last battle item.
-
#remove_item(db_symbol, nb = 999)
(also: #drop_item)
Remove items from the bag.
-
#reset_order(socket) ⇒ Array
(also: #sort_ids)
Reset the order of items in a socket.
-
#shortcuts ⇒ Array<Symbol>
(also: #get_shortcuts)
Get the shortcuts.
-
#sort_alpha(socket, reverse = false)
Sort the item of a socket by their names.
Constructor Details
#initialize(game_state = PFM.game_state) ⇒ Bag
Create a new Bag
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 30 def initialize(game_state = PFM.game_state) self.game_state = game_state # @type [Hash<Symbol => Integer>] @items = Hash.new(0) @orders = [[], [], [], [], [], [], []] @last_socket = 1 @last_index = 0 @shortcut = Array.new(SHORTCUT_AMOUNT, :__undef__) @locked = false @last_battle_item_db_symbol = :__undef__ @alpha_sorted = false end |
Instance Attribute Details
#alpha_sorted ⇒ Boolean
Tell if the bag is alpha sorted
21 22 23 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 21 def alpha_sorted @alpha_sorted end |
#game_state ⇒ PFM::GameState
Get the game state responsive of the whole game state
24 25 26 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 24 def game_state @game_state end |
#last_battle_item_db_symbol ⇒ Symbol
Set the last battle item
18 19 20 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 18 def last_battle_item_db_symbol @last_battle_item_db_symbol end |
#last_index ⇒ Integer
Last index in the socket
12 13 14 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 12 def last_index @last_index end |
#last_socket ⇒ Integer
Last socket used in the bag
9 10 11 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 9 def last_socket @last_socket end |
#locked ⇒ Boolean
If the bag is locked (and react as being empty)
15 16 17 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 15 def locked @locked end |
Instance Method Details
#add_item(db_symbol, nb = 1) Also known as: store_item
Add items in the bag and trigger the right quest objective
83 84 85 86 87 88 89 90 91 92 93 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 83 def add_item(db_symbol, nb = 1) return if @locked return remove_item(db_symbol, -nb) if nb < 0 db_symbol = data_item(db_symbol).db_symbol if db_symbol.is_a?(Integer) return if db_symbol == :__undef__ @items[db_symbol] += nb add_item_to_order(db_symbol) game_state.quests.add_item(data_item(db_symbol).id) unless game_state.bag != self end |
#contain_item?(db_symbol) ⇒ Boolean Also known as: has_item?
If the bag contain a specific item
59 60 61 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 59 def contain_item?(db_symbol) return item_quantity(db_symbol) > 0 end |
#convert_to_dot26
Convert bag to .26 format
44 45 46 47 48 49 50 51 52 53 54 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 44 def convert_to_dot26 return if @items.is_a?(Hash) items = Hash.new(0) items.merge!( @items.map.with_index { |quantity, id| [data_item(id).db_symbol, quantity] }.reject { |v| v.last == 0 }.to_h ) items.delete(:__undef__) @items = items @orders.map! { |order| order.map { |id| data_item(id).db_symbol }.reject { |db_symbol| db_symbol == :__undef__ } } end |
#empty? ⇒ Boolean
Tell if the bag is empty
66 67 68 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 66 def empty? return @items.empty? end |
#get_order(socket) ⇒ Array
Get the order of items in a socket
117 118 119 120 121 122 123 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 117 def get_order(socket) return [] if @locked return @shortcut if socket == :favorites return process_battle_order(socket) if socket.is_a?(Symbol) # TODO return (@orders[socket] ||= []) end |
#item_quantity(db_symbol) ⇒ Integer
The quantity of an item in the bag
73 74 75 76 77 78 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 73 def item_quantity(db_symbol) return 0 if @locked db_symbol = data_item(db_symbol).db_symbol if db_symbol.is_a?(Integer) return @items[db_symbol] end |
#last_battle_item ⇒ Studio::Item
Get the last battle item
162 163 164 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 162 def last_battle_item data_item(@last_battle_item_db_symbol) end |
#remove_item(db_symbol, nb = 999) Also known as: drop_item
Remove items from the bag
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 99 def remove_item(db_symbol, nb = 999) return if @locked return add_item(db_symbol, -nb) if nb < 0 db_symbol = data_item(db_symbol).db_symbol if db_symbol.is_a?(Integer) return if db_symbol == :__undef__ @items[db_symbol] -= nb if @items[db_symbol] <= 0 @items.delete(db_symbol) remove_item_from_order(db_symbol) end end |
#reset_order(socket) ⇒ Array Also known as: sort_ids
Reset the order of items in a socket
128 129 130 131 132 133 134 135 136 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 128 def reset_order(socket) arr = get_order(socket) arr.select! { |db_symbol| data_item(db_symbol).socket == socket && @items[db_symbol] > 0 } unless socket == :favorites unless each_data_item.select { |item| item.socket == socket }.all? { |item| item.position.zero? } arr.sort! { |a, b| data_item(a).position <=> data_item(b).position } end @alpha_sorted = false return arr end |
#shortcuts ⇒ Array<Symbol> Also known as: get_shortcuts
Get the shortcuts
154 155 156 157 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 154 def shortcuts @shortcut ||= Array.new(SHORTCUT_AMOUNT, :__undef__) return @shortcut end |
#sort_alpha(socket, reverse = false)
Sort the item of a socket by their names
142 143 144 145 146 147 148 149 150 |
# File 'scripts/01450 Systems/00103 Bag/00001 PFM/00300 Bag.rb', line 142 def sort_alpha(socket, reverse = false) if reverse reset_order(socket).sort! { |a, b| data_item(b).name <=> data_item(a).name } @alpha_sorted = false else reset_order(socket).sort! { |a, b| data_item(a).name <=> data_item(b).name } @alpha_sorted = true end end |