Class: Adventure::Item
- Inherits:
-
Object
- Object
- Adventure::Item
- Defined in:
- lib/adventure/item.rb
Overview
Represents an ingame item.
Defined Under Namespace
Modules: DamageType, Property, Rarity, Type
Instance Attribute Summary collapse
-
#attunement ⇒ Object
readonly
Whether the item requires attunement or not.
-
#dmg_type ⇒ Object
readonly
Item's type of damage.
-
#known ⇒ Object
readonly
Whether the item is identified (known) or not.
-
#rarity ⇒ Object
readonly
Item's rarity, if applicable.
-
#source ⇒ Object
The source of the item, a String, with source name and page when applicable, or
nilwhen unset. -
#type ⇒ Object
readonly
Item's type, either one of Type or a String.
-
#value ⇒ Object
The value of the item, a Float representing such value in gold pieces, or
nilwhen unset. -
#weight ⇒ Object
The weight of the item, a Float in kilograms, or
nilwhen unset.
Instance Method Summary collapse
-
#ac(dex_bonus = 0) ⇒ Integer?
The wearer's AC with this item, if it is an armor,
nilotherwise. -
#armor? ⇒ Boolean
Whether this item is an armor or not.
-
#damage(advantage: false) ⇒ Integer?
Return the damage dealt by the item this iteration.
-
#damage_notation ⇒ String?
Return the default RPG notation for damage calculation.
-
#description ⇒ String
Return a String with the item's description, or its placeholder if unknown.
-
#identify ⇒ Object
Set the Item as known.
-
#initialize(name, description, type, **options) ⇒ Item
constructor
Create a new instance of Item.
-
#magic? ⇒ Boolean
Whether this item is magical or not.
-
#name ⇒ String
Return a String with the item's name, or its placeholder if unknown.
-
#parse_damage_notation(notation) ⇒ Object
private
Parse damage notation to actual damage data.
-
#price ⇒ Float
Alias for value.
-
#to_f ⇒ Float
Return a Float with the item's weight.
-
#to_s ⇒ String
An alias for name.
-
#weapon? ⇒ Boolean
Whether this item is a weapon or not.
Constructor Details
#initialize(name, description, type, **options) ⇒ Item
Create a new instance of Item.
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/adventure/item.rb', line 207 def initialize(name, description, type, **) # Mandatory parameters @name = name.strip @description = description.strip @type = type # Optional parameters # > General properties. @rarity = .key?(:rarity) ? [:rarity] : nil @value = .key?(:value) ? [:value].to_f : 0.0 @weight = .key?(:weight) ? [:weight].to_f : 0.0 @source = .key?(:source) ? [:source] : nil # > Armor properties. @ac = .key?(:ac) ? [:ac] : nil @max_dex_bonus = .key?(:max_dex_bonus) ? [:max_dex_bonus].to_i : 1_000 # > Weapon properties. if .key?(:dmg_notation) && ![:dmg_notation].strip.empty? @dmg_die_count, @dmg_die_type, @dmg_mod = parse_damage_notation([:dmg_notation].strip) else @dmg_die_count = .key?(:dmg_die_count) ? [:dmg_die_count].to_i : 0 @dmg_die_type = .key?(:dmg_die_type) ? [:dmg_die_type].to_i : 0 @dmg_mod = .key?(:dmg_mod) ? [:dmg_mod].to_i : 0 end @dmg_type = .key?(:dmg_type) ? [:dmg_type] : nil # > Magic properties. @magic = .key?(:magic) || false @rarity = Rarity::UNKNOWN if @magic && @rarity.nil? if .key?(:attunement) @attunement = 'Requires attunement' @attunement += " #{[:attunement].strip}" if [:attunement].is_a?(String) else @attunement = '' end # > Unkown properties. @known = .key?(:known) ? ([:known] == true) : true @unknown_name = .key?(:unknown_name) ? [:unknown_name].strip : "Unknown #{@type}" @unknown_desc = .key?(:unknown_desc) ? [:unknown_desc].strip : "Unknown #{@type}." # > Misc properties. @properties = .key?(:properties) ? [:properties] : nil end |
Instance Attribute Details
#attunement ⇒ Object (readonly)
Whether the item requires attunement or not. If a string, it will be prepended by "Requires attunement ".
17 18 19 |
# File 'lib/adventure/item.rb', line 17 def attunement @attunement end |
#dmg_type ⇒ Object (readonly)
Item's type of damage.
10 11 12 |
# File 'lib/adventure/item.rb', line 10 def dmg_type @dmg_type end |
#known ⇒ Object (readonly)
Whether the item is identified (known) or not.
19 20 21 |
# File 'lib/adventure/item.rb', line 19 def known @known end |
#rarity ⇒ Object (readonly)
Item's rarity, if applicable. Either one of
Rarity, a String, or
nil when unset.
14 15 16 |
# File 'lib/adventure/item.rb', line 14 def rarity @rarity end |
#source ⇒ Object
The source of the item, a String, with source
name and page when applicable, or nil when
unset.
29 30 31 |
# File 'lib/adventure/item.rb', line 29 def source @source end |
#type ⇒ Object (readonly)
Item's type, either one of Type or a String.
8 9 10 |
# File 'lib/adventure/item.rb', line 8 def type @type end |
#value ⇒ Object
The value of the item, a Float representing
such value in gold pieces, or nil when unset.
22 23 24 |
# File 'lib/adventure/item.rb', line 22 def value @value end |
#weight ⇒ Object
The weight of the item, a Float in kilograms,
or nil when unset.
25 26 27 |
# File 'lib/adventure/item.rb', line 25 def weight @weight end |
Instance Method Details
#ac(dex_bonus = 0) ⇒ Integer?
The wearer's AC with this item, if it is an armor, nil otherwise.
343 344 345 346 347 348 349 |
# File 'lib/adventure/item.rb', line 343 def ac(dex_bonus = 0) return nil unless armor? ac = @ac ac += [dex_bonus, @max_dex_bonus].min ac end |
#armor? ⇒ Boolean
Whether this item is an armor or not.
335 336 337 |
# File 'lib/adventure/item.rb', line 335 def armor? !@ac.nil? end |
#damage(advantage: false) ⇒ Integer?
Return the damage dealt by the item this iteration.
Each run of this method will return a different number, since each call represents one roll for damage --- which varies, obviously.
301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/adventure/item.rb', line 301 def damage(advantage: false) if @dmg_die_count.zero? || @dmg_die_type.zero? nil else count = advantage ? @dmg_die_count * 2 : @dmg_die_count damage = @dmg_mod count.times do damage += rand(@dmg_die_type) + 1 end damage end end |
#damage_notation ⇒ String?
Return the default RPG notation for damage calculation.
This does not return actual damage!
280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/adventure/item.rb', line 280 def damage_notation if @dmg_die_count.nil? || @dmg_die_type.nil? nil else mod = '' if @dmg_mod.negative? mod = @dmg_mod.to_s elsif @dmg_mod.positive? mod = "+#{@dmg_mod}" end "#{@dmg_die_count}d#{@dmg_die_type}#{mod}" end end |
#description ⇒ String
Return a String with the item's description, or its placeholder if unknown.
271 272 273 |
# File 'lib/adventure/item.rb', line 271 def description @known ? @description : @unknown_desc end |
#identify ⇒ Object
Set the Adventure::Item as known.
359 360 361 |
# File 'lib/adventure/item.rb', line 359 def identify @known = true end |
#magic? ⇒ Boolean
Whether this item is magical or not.
328 329 330 |
# File 'lib/adventure/item.rb', line 328 def magic? @magic end |
#name ⇒ String
Return a String with the item's name, or its placeholder if unknown.
250 251 252 |
# File 'lib/adventure/item.rb', line 250 def name @known ? @name : @unknown_name end |
#parse_damage_notation(notation) ⇒ Object (private)
Parse damage notation to actual damage data.
368 369 370 371 372 373 374 375 376 |
# File 'lib/adventure/item.rb', line 368 def parse_damage_notation(notation) if /\d+d\d+([+-]\d+)?/.match?(notation) dmg = notation.scan(/(\d+)d(\d+)([+-]\d+)?/)[0] dmg.map!(&:to_i) dmg else [nil, nil, 0] end end |
#price ⇒ Float
Alias for value.
354 355 356 |
# File 'lib/adventure/item.rb', line 354 def price @value end |
#to_f ⇒ Float
Return a Float with the item's weight.
264 265 266 |
# File 'lib/adventure/item.rb', line 264 def to_f @weight end |
#to_s ⇒ String
An alias for name.
257 258 259 |
# File 'lib/adventure/item.rb', line 257 def to_s name end |
#weapon? ⇒ Boolean
Whether this item is a weapon or not.
317 318 319 320 321 322 323 |
# File 'lib/adventure/item.rb', line 317 def weapon? if @type.downcase.include?('weapon') && !damage_notation.nil? true else false end end |