Class: Adventure::World

Inherits:
Object
  • Object
show all
Defined in:
lib/adventure/world.rb

Overview

Represents the world where the game takes place.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ World

Create a new game instance - a new World.

Parameters:

  • opts (Hash)

    A list of named parameters.

Options Hash (**opts):

  • title (String)

    The adventure's title / name.

  • subtitle (String, nil)

    The adventure's subtitle / flavor text.

  • authors (Array<String>)

    An Array of authors, in the format Name <email> (url).

  • license (String)

    The full name of the license the adventure is released under.

  • year (String)

    The year (or years) of development of the adventure.

  • description (String)

    A description of the adventure.

  • player (Player)

    The adventure's player object.

  • atlas (Hash)

    An Array of all the rooms for the adventure.

  • current_room (Integer, Symbol)

    The index of the game's current Room, initially the starting Room.

  • global_vars (Hash)

    A Hash of variables to be used by the adventure --- currently unused, kept as placeholder.

Raises:

  • (StandardError)

    Throws an error if the given player is not a valid Player object.



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

#authorsObject (readonly)

Returns the value of attribute authors.



26
27
28
# File 'lib/adventure/world.rb', line 26

def authors
  @authors
end

#current_roomObject (readonly)

Returns the value of attribute current_room.



41
42
43
# File 'lib/adventure/world.rb', line 41

def current_room
  @current_room
end

#descriptionObject (readonly)

Returns the value of attribute description.



35
36
37
# File 'lib/adventure/world.rb', line 35

def description
  @description
end

#global_varsObject (readonly)

Returns the value of attribute global_vars.



44
45
46
# File 'lib/adventure/world.rb', line 44

def global_vars
  @global_vars
end

#licenseObject (readonly)

Returns the value of attribute license.



29
30
31
# File 'lib/adventure/world.rb', line 29

def license
  @license
end

#playerObject (readonly)

Returns the value of attribute player.



38
39
40
# File 'lib/adventure/world.rb', line 38

def player
  @player
end

#subtitleObject (readonly)

Returns the value of attribute subtitle.



23
24
25
# File 'lib/adventure/world.rb', line 23

def subtitle
  @subtitle
end

#titleObject (readonly)

Returns the value of attribute title.



20
21
22
# File 'lib/adventure/world.rb', line 20

def title
  @title
end

#yearObject (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

Raises:

  • (StandardError)


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