Class: RubyWarrior::Level

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_warrior/level.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile, number) ⇒ Level

Returns a new instance of Level.



16
17
18
19
20
# File 'lib/ruby_warrior/level.rb', line 16

def initialize(profile, number)
  @profile = profile
  @number = number
  @time_bonus = 0
end

Instance Attribute Details

#ace_scoreObject

Returns the value of attribute ace_score.



4
5
6
# File 'lib/ruby_warrior/level.rb', line 4

def ace_score
  @ace_score
end

#clueObject

Returns the value of attribute clue.



4
5
6
# File 'lib/ruby_warrior/level.rb', line 4

def clue
  @clue
end

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/ruby_warrior/level.rb', line 4

def description
  @description
end

#floorObject

Returns the value of attribute floor.



4
5
6
# File 'lib/ruby_warrior/level.rb', line 4

def floor
  @floor
end

#numberObject (readonly)

Returns the value of attribute number.



3
4
5
# File 'lib/ruby_warrior/level.rb', line 3

def number
  @number
end

#profileObject (readonly)

Returns the value of attribute profile.



3
4
5
# File 'lib/ruby_warrior/level.rb', line 3

def profile
  @profile
end

#time_bonusObject

Returns the value of attribute time_bonus.



4
5
6
# File 'lib/ruby_warrior/level.rb', line 4

def time_bonus
  @time_bonus
end

#tipObject

Returns the value of attribute tip.



4
5
6
# File 'lib/ruby_warrior/level.rb', line 4

def tip
  @tip
end

#warriorObject

Returns the value of attribute warrior.



4
5
6
# File 'lib/ruby_warrior/level.rb', line 4

def warrior
  @warrior
end

Class Method Details

.grade_letter(percent) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/ruby_warrior/level.rb', line 6

def self.grade_letter(percent)
  if    percent >= 1.0 then "S"
  elsif percent >= 0.9 then "A"
  elsif percent >= 0.8 then "B"
  elsif percent >= 0.7 then "C"
  elsif percent >= 0.6 then "D"
  else                      "F"
  end
end

Instance Method Details

#clear_bonusObject



93
94
95
# File 'lib/ruby_warrior/level.rb', line 93

def clear_bonus
  ((warrior.score + time_bonus)*0.2).round
end

#exists?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/ruby_warrior/level.rb', line 113

def exists?
  File.exist? load_path
end

#failed?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/ruby_warrior/level.rb', line 109

def failed?
  !@floor.units.include?(warrior)
end

#generate_player_filesObject



43
44
45
46
# File 'lib/ruby_warrior/level.rb', line 43

def generate_player_files
  load_level
  PlayerGenerator.new(self).generate
end

#grade_for(score) ⇒ Object



87
88
89
90
91
# File 'lib/ruby_warrior/level.rb', line 87

def grade_for(score)
  if ace_score
    self.class.grade_letter(score / ace_score.to_f)
  end
end

#load_levelObject



34
35
36
# File 'lib/ruby_warrior/level.rb', line 34

def load_level
  LevelLoader.new(self).instance_eval(File.read(load_path))
end

#load_pathObject



26
27
28
29
30
31
32
# File 'lib/ruby_warrior/level.rb', line 26

def load_path
  File.join(
    File.expand_path('../../../towers/', __FILE__),
    File.basename(@profile.tower_path) + "/level_" +
      @number.to_s.rjust(3, '0') + ".rb"
  )
end

#load_playerObject



38
39
40
41
# File 'lib/ruby_warrior/level.rb', line 38

def load_player
  $: << player_path
  load 'player.rb'
end

#passed?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/ruby_warrior/level.rb', line 105

def passed?
  @floor.stairs_space.warrior?
end

#play(turns = 1000) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby_warrior/level.rb', line 48

def play(turns = 1000)
  load_level
  turns.times do |n|
    return if passed? || failed?
    UI.puts "- turn #{n+1} -"
    UI.print @floor.character
    @floor.units.each { |unit| unit.prepare_turn }
    @floor.units.each { |unit| unit.perform_turn }
    yield if block_given?
    @time_bonus -= 1 if @time_bonus > 0
  end
end

#player_pathObject



22
23
24
# File 'lib/ruby_warrior/level.rb', line 22

def player_path
  @profile.player_path
end

#score_calculation(current_score, addition) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/ruby_warrior/level.rb', line 97

def score_calculation(current_score, addition)
  if current_score.zero?
    addition.to_s
  else
    "#{current_score} + #{addition} = #{current_score + addition}"
  end
end

#setup_warrior(warrior) ⇒ Object



117
118
119
120
121
# File 'lib/ruby_warrior/level.rb', line 117

def setup_warrior(warrior)
  @warrior = warrior
  @warrior.add_abilities(*profile.abilities)
  @warrior.name = profile.warrior_name
end

#tally_pointsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ruby_warrior/level.rb', line 61

def tally_points
  score = 0
  
  UI.puts "Level Score: #{warrior.score}"
  score += warrior.score
  
  UI.puts "Time Bonus: #{time_bonus}"
  score += @time_bonus
  
  if floor.other_units.empty?
    UI.puts "Clear Bonus: #{clear_bonus}"
    score += clear_bonus
  end
  
  if @profile.epic?
    UI.puts "Level Grade: #{grade_for(score)}" if grade_for(score)
    UI.puts "Total Score: " + score_calculation(@profile.current_epic_score, score)
    @profile.current_epic_grades[@number] = (score / ace_score.to_f) if ace_score
    @profile.current_epic_score += score
  else
    UI.puts "Total Score: " + score_calculation(@profile.score, score)
    @profile.score += score
    @profile.abilities = warrior.abilities.keys
  end
end