Class: RubyWarrior::Level

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile, number) ⇒ Level

Returns a new instance of Level.



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

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

Instance Attribute Details

#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

Instance Method Details

#clear_bonusObject



71
72
73
# File 'lib/ruby_warrior/level.rb', line 71

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

#exists?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/ruby_warrior/level.rb', line 83

def exists?
  File.exist? load_path
end

#failed?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/ruby_warrior/level.rb', line 79

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

#generate_player_filesObject



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

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

#load_levelObject



20
21
22
# File 'lib/ruby_warrior/level.rb', line 20

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

#load_pathObject



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

def load_path
  @profile.tower_path + "/level_" + @number.to_s.rjust(3, '0') + ".rb"
end

#load_playerObject



24
25
26
27
# File 'lib/ruby_warrior/level.rb', line 24

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

#passed?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/ruby_warrior/level.rb', line 75

def passed?
  @floor.stairs_space.warrior?
end

#play(turns = 1000) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_warrior/level.rb', line 34

def play(turns = 1000)
  load_level
  turns.times do |n|
    return if passed? || failed?
    UI.puts "- turn #{n+1} -"
    UI.print @floor.to_map
    @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



12
13
14
# File 'lib/ruby_warrior/level.rb', line 12

def player_path
  @profile.player_path
end

#setup_warrior(warrior) ⇒ Object



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

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

#tally_pointsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby_warrior/level.rb', line 47

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?
    @profile.current_epic_score += score
    UI.puts "Total Score: #{@profile.current_epic_score}"
  else
    @profile.score += score
    @profile.abilities = warrior.abilities.keys
    UI.puts "Total Score: #{@profile.score}"
  end
end