Class: Adventure::World
- Inherits:
-
Object
- Object
- Adventure::World
- Defined in:
- lib/adventure/world.rb
Overview
Represents the world where the game takes place.
Instance Attribute Summary collapse
-
#authors ⇒ Object
readonly
Returns the value of attribute authors.
-
#current_room ⇒ Object
readonly
Returns the value of attribute current_room.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#global_vars ⇒ Object
readonly
Returns the value of attribute global_vars.
-
#license ⇒ Object
readonly
Returns the value of attribute license.
-
#player ⇒ Object
readonly
Returns the value of attribute player.
-
#subtitle ⇒ Object
readonly
Returns the value of attribute subtitle.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
-
.load(file) ⇒ Object
Class method.
Instance Method Summary collapse
-
#initialize(**opts) ⇒ World
constructor
Create a new game instance - a new World.
-
#save(file) ⇒ Object
Instance methods.
Constructor Details
#initialize(**opts) ⇒ World
Create a new game instance - a new World.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/adventure/world.rb', line 61 def initialize(**opts) @title = opts.key?(:title) ? opts[:title].trim : '' @subtitle = opts.key?(:subtitle) ? opts[:subtitle].trim : nil @authors = opts.key?(:authors) ? opts[:authors].to_a : [] @license = opts.key?(:license) ? opts[:license].trim : 'Creative Commons 0 1.0 Universal' @year = opts.key?(:year) ? opts[:year].trim : Date.today.year.to_s @description = opts.key?(:description) ? opts[:description].trim : '' @player = opts.key?(:player) ? opts[:player].to_i : nil @atlas = opts.key?(:atlas) ? opts[:atlas].to_a : [] @current_room = opts.key?(:current_room) ? opts[:current_room].to_i : 0 @global_vars = opts.key?(:global_vars) ? opts[:global_vars].to_h : {} raise StandardError, 'Player is not a valid object.' unless @player.is_a? Player end |
Instance Attribute Details
#authors ⇒ Object (readonly)
Returns the value of attribute authors.
26 27 28 |
# File 'lib/adventure/world.rb', line 26 def @authors end |
#current_room ⇒ Object (readonly)
Returns the value of attribute current_room.
41 42 43 |
# File 'lib/adventure/world.rb', line 41 def current_room @current_room end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
35 36 37 |
# File 'lib/adventure/world.rb', line 35 def description @description end |
#global_vars ⇒ Object (readonly)
Returns the value of attribute global_vars.
44 45 46 |
# File 'lib/adventure/world.rb', line 44 def global_vars @global_vars end |
#license ⇒ Object (readonly)
Returns the value of attribute license.
29 30 31 |
# File 'lib/adventure/world.rb', line 29 def license @license end |
#player ⇒ Object (readonly)
Returns the value of attribute player.
38 39 40 |
# File 'lib/adventure/world.rb', line 38 def player @player end |
#subtitle ⇒ Object (readonly)
Returns the value of attribute subtitle.
23 24 25 |
# File 'lib/adventure/world.rb', line 23 def subtitle @subtitle end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
20 21 22 |
# File 'lib/adventure/world.rb', line 20 def title @title end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
32 33 34 |
# File 'lib/adventure/world.rb', line 32 def year @year end |
Class Method Details
.load(file) ⇒ Object
Class method
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/adventure/world.rb', line 78 def self.load(file) yaml = Psych.safe_load_file( file, permitted_classes: [ Adventure::Being, Adventure::Inventory, Adventure::Item, # Adventure::Player, Adventure::Purse, Adventure::Room, Adventure::World ], aliases: true ) raise StandardError, 'Invalid input.' if yaml.nil? yaml end |
Instance Method Details
#save(file) ⇒ Object
Instance methods
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/adventure/world.rb', line 99 def save(file) yaml = Psych.safe_dump( self, permitted_classes: [ Adventure::Being, Adventure::Inventory, Adventure::Item, # Adventure::Player, Adventure::Purse, Adventure::Room, Adventure::World ] ) File.write(file, yaml) end |