Class: RubyWarrior::Game

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

Instance Method Summary collapse

Instance Method Details

#current_levelObject

levels



128
129
130
# File 'lib/ruby_warrior/game.rb', line 128

def current_level
  @current_level ||= profile.current_level
end

#make_game_directoryObject



33
34
35
36
37
38
39
40
# File 'lib/ruby_warrior/game.rb', line 33

def make_game_directory
  if UI.ask("No ruby-warrior directory found, would you like to create one?")
    Dir.mkdir(Config.path_prefix + '/ruby-warrior')
  else
    UI.puts "Unable to continue without directory."
    exit
  end
end

#new_profileObject



107
108
109
110
111
112
# File 'lib/ruby_warrior/game.rb', line 107

def new_profile
  profile = Profile.new
  profile.tower_path = UI.choose('tower', towers).path
  profile.warrior_name = UI.request('Enter a name for your warrior: ')
  profile
end

#next_levelObject



132
133
134
# File 'lib/ruby_warrior/game.rb', line 132

def next_level
  @next_level ||= profile.next_level
end

#play_current_levelObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby_warrior/game.rb', line 42

def play_current_level
  continue = true
  current_level.load_player
  UI.puts "Starting Level #{current_level.number}"
  current_level.play
  if current_level.passed?
    if next_level.exists?
      UI.puts "Success! You have found the stairs."
    else
      UI.puts "CONGRATULATIONS! You have climbed to the top of the tower and rescue the fair maiden Ruby."
      continue = false
    end
    current_level.tally_points
    request_next_level unless profile.epic?
  else
    continue = false
    UI.puts "Sorry, you failed the level. Change your script and try again."
    if current_level.clue && UI.ask("Would you like to read the additional clues for this level?")
      UI.puts current_level.clue
    end
  end
  continue
end

#prepare_epic_modeObject



86
87
88
89
90
# File 'lib/ruby_warrior/game.rb', line 86

def prepare_epic_mode
  profile.enable_epic_mode
  profile.level_number = 0
  profile.save # this saves score too
end

#prepare_next_levelObject



80
81
82
83
84
# File 'lib/ruby_warrior/game.rb', line 80

def prepare_next_level
  next_level.generate_player_files
  profile.level_number += 1
  profile.save # this saves score and new abilities too
end

#profileObject



103
104
105
# File 'lib/ruby_warrior/game.rb', line 103

def profile
  @profile ||= choose_profile
end

#profile_pathsObject



99
100
101
# File 'lib/ruby_warrior/game.rb', line 99

def profile_paths
  Dir[Config.path_prefix + '/ruby-warrior/**/.profile']
end

#profilesObject

profiles



95
96
97
# File 'lib/ruby_warrior/game.rb', line 95

def profiles
  profile_paths.map { |profile| Profile.load(profile) }
end

#request_next_levelObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_warrior/game.rb', line 66

def request_next_level
  if (next_level.exists? ? UI.ask("Would you like to continue on to the next level?") : UI.ask("Would you like to continue on to epic mode?"))
    if next_level.exists?
      prepare_next_level
      UI.puts "See the ruby-warrior directory for the next level README."
    else
      prepare_epic_mode
      UI.puts "Run rubywarrior again to play epic mode."
    end
  else
    UI.puts "Staying on current level. Try to earn more points next time."
  end
end

#startObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_warrior/game.rb', line 4

def start
  UI.puts "Welcome to Ruby Warrior"
  
  if File.exists?(Config.path_prefix + '/.profile')
    @profile = Profile.load(Config.path_prefix + '/.profile')
  else
    make_game_directory unless File.exists?(Config.path_prefix + '/ruby-warrior')
  end
  
  if profile.epic?
    UI.delay /= 2 if UI.delay # speed up UI since we're going to be doing a lot here
    profile.current_epic_score = 0
    playing = true
    while playing
      @current_level = @next_level = nil
      profile.level_number += 1
      playing = play_current_level
    end
    profile.save # saves the score for epic mode
  else
    if current_level.number.zero?
      prepare_next_level
      UI.puts "First level has been generated. See the ruby-warrior directory for instructions."
    else
      play_current_level
    end
  end
end

#tower_pathsObject



121
122
123
# File 'lib/ruby_warrior/game.rb', line 121

def tower_paths
  Dir[File.expand_path(File.dirname(__FILE__) + '/../../towers/*')]
end

#towersObject

towers



117
118
119
# File 'lib/ruby_warrior/game.rb', line 117

def towers
  tower_paths.map { |path| Tower.new(path) }
end