Class: Adventure::NPC

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

Overview

Represents an ingame Non-Playable Character (NPC).

Defined Under Namespace

Modules: FriendshipLevel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, species, gender, **opts) ⇒ NPC

Create a new instance of NPC.

Parameters:

  • name (String)

    The NPC's name.

  • species (String)

    The NPC's species.

  • gender (String)

    The NPC's gender.

  • opts (Hash)

    A list of optional parameters.

Options Hash (**opts):

  • :friendship (Integer)

    The NPC's attitude towards the player, from 1 (hate) to 5 (love). Use FriendshipLevel for readability.

  • :purse ({Purse})

    The NPC's Purse. Will be set to an empty Purse if unset.

  • :inventory ({Inventory})

    The NPC's Inventory. Will be set to an empty Inventory if unset.

  • :dialogue (Array<Hash>)

    The NPC's list of conversation options.



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/adventure/npc.rb', line 52

def initialize(name, species, gender, **opts)
  # Mandatory parameters.
  @name       = name.strip
  @species    = species.strip.capitalize
  @gender     = gender.strip.capitalize
  # Optional parameters.
  @friendship = opts.key?(:friendship) ? opts[:friendship].to_i : FriendshipLevel::NEUTRAL
  @purse      = opts.key?(:purse) ? opts[:purse] : nil
  @purse      = Purse.new unless @purse.is_a? Purse
  @inventory  = opts.key?(:inventory) ? opts[:inventory] : nil
  @inventory  = Inventory.new unless @inventory.is_a? Inventory
  @dialogue   = opts.key?(:dialogue) ? opts[:dialogue].to_a : []
end

Instance Attribute Details

#dialogueArray<Hash>

Returns The NPC's list of conversation options. Each Hash must contain exactly two entries: a question and a reply.

Returns:

  • (Array<Hash>)

    The NPC's list of conversation options. Each Hash must contain exactly two entries: a question and a reply.



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

def dialogue
  @dialogue
end

#friendshipObject (readonly)

Returns the value of attribute friendship.



17
18
19
# File 'lib/adventure/npc.rb', line 17

def friendship
  @friendship
end

#genderObject (readonly)

Returns the value of attribute gender.



14
15
16
# File 'lib/adventure/npc.rb', line 14

def gender
  @gender
end

#inventoryObject (readonly)

Returns the value of attribute inventory.



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

def inventory
  @inventory
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/adventure/npc.rb', line 8

def name
  @name
end

#purseObject (readonly)

Returns the value of attribute purse.



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

def purse
  @purse
end

#speciesObject (readonly)

Returns the value of attribute species.



11
12
13
# File 'lib/adventure/npc.rb', line 11

def species
  @species
end

Instance Method Details

#charmInteger

Bump the NPC's friendship level by 1, to a maximum of 5.

Returns:

  • (Integer)

    The NPC's new friendship level.



83
84
85
86
# File 'lib/adventure/npc.rb', line 83

def charm
  @friendship += 1 unless @friendship >= 5
  @friendship
end

#offendInteger

Nerf the NPC's friendship level by 1, to a minimum of 1.

Returns:

  • (Integer)

    The NPC's new friendship level.



91
92
93
94
# File 'lib/adventure/npc.rb', line 91

def offend
  @friendship -= 1 unless @friendship <= 1
  @friendship
end

#to_iInteger

Return an Integer as the NPC's friendship level towards the Player.

Returns:

  • (Integer)

    The NPC's friendship level.



76
77
78
# File 'lib/adventure/npc.rb', line 76

def to_i
  @friendship
end

#to_sString

Return a String with the NPC's name, gender, and species.

Returns:

  • (String)

    A String with the NPC's name, gender, and species.



69
70
71
# File 'lib/adventure/npc.rb', line 69

def to_s
  "#{@name}, #{@gender.downcase} #{@species.downcase}"
end