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.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/adventure/world.rb', line 47

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 : {}
end

Instance Attribute Details

#authorsObject (readonly)

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



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

def authors
  @authors
end

#current_roomObject (readonly)

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



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

def current_room
  @current_room
end

#descriptionObject (readonly)

A description of the adventure.



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

def description
  @description
end

#global_varsObject (readonly)

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



32
33
34
# File 'lib/adventure/world.rb', line 32

def global_vars
  @global_vars
end

#licenseObject (readonly)

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



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

def license
  @license
end

#playerObject (readonly)

The adventure's player object.



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

def player
  @player
end

#subtitleObject (readonly)

The adventure's subtitle / flavor text.



18
19
20
# File 'lib/adventure/world.rb', line 18

def subtitle
  @subtitle
end

#titleObject (readonly)

The adventure's title / name.



16
17
18
# File 'lib/adventure/world.rb', line 16

def title
  @title
end

#yearObject (readonly)

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



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

def year
  @year
end

Class Method Details

.load(file) ⇒ Object

Class method

Raises:

  • (StandardError)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/adventure/world.rb', line 62

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/adventure/world.rb', line 82

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