Module: Adjective::Imbibable

Defined in:
lib/adjective/concerns/imbibable.rb

Instance Method Summary collapse

Instance Method Details

#can_level_up?Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/adjective/concerns/imbibable.rb', line 40

def can_level_up?
  return false if max_level?
  @experience >= @active_exp_set[@level+1]
end

#experience_to_next_levelObject



61
62
63
64
# File 'lib/adjective/concerns/imbibable.rb', line 61

def experience_to_next_level
  return nil if max_level?
  return @active_exp_set[@level+1] - @experience
end

#grant_experience(exp, opts = {}) ⇒ Object



45
46
47
48
49
# File 'lib/adjective/concerns/imbibable.rb', line 45

def grant_experience(exp, opts = {})
  return false if max_level?
  @experience += exp
  level_up if !opts[:suppress_level_up]      
end

#grant_levels(num, opts = {}) ⇒ Object



56
57
58
59
# File 'lib/adjective/concerns/imbibable.rb', line 56

def grant_levels(num, opts = {})
  @level += num
  @experience = @active_exp_set[@level] if !opts[:constrain_exp] 
end

#initialize_experience(opts) ⇒ Object

Able to be enhanced by experience



6
7
8
9
10
11
12
13
14
# File 'lib/adjective/concerns/imbibable.rb', line 6

def initialize_experience(opts)
  @level = opts[:level] ||= 1
  @experience = opts[:initial_exp] ||= 0
  # This may throw an error if the user decides to instantiate a class without a table present.
  @active_exp_set = opts[:exp_table]
  [:active_exp_set, :level].each {|attribute| self.class.send(:attr_reader, attribute)}
  self.class.send(:attr_accessor, :experience)
  set_experience_to_level_minimum
end

#level_upObject



20
21
22
23
24
# File 'lib/adjective/concerns/imbibable.rb', line 20

def level_up
  until !can_level_up?
    @level += 1
  end
end

#max_levelObject



32
33
34
# File 'lib/adjective/concerns/imbibable.rb', line 32

def max_level
  @active_exp_set.length - 1
end

#max_level?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/adjective/concerns/imbibable.rb', line 36

def max_level?
  @active_exp_set.length - 1 <= @level
end

#normalize_experienceObject

Level down functionality should work the same way as simply setting a level.



28
29
30
# File 'lib/adjective/concerns/imbibable.rb', line 28

def normalize_experience
  @experience = 0 if @experience < 0
end

#set_experience_to_level_minimumObject



16
17
18
# File 'lib/adjective/concerns/imbibable.rb', line 16

def set_experience_to_level_minimum
  @experience = @active_exp_set[@level]
end

#set_level(num, opts = {}) ⇒ Object



51
52
53
54
# File 'lib/adjective/concerns/imbibable.rb', line 51

def set_level(num, opts = {})
  @level = num
  @experience = @active_exp_set[num] if !opts[:constrain_exp]
end