-
Notifications
You must be signed in to change notification settings - Fork 827
Expand file tree
/
Copy pathlevel.rb
More file actions
126 lines (104 loc) · 2.99 KB
/
level.rb
File metadata and controls
126 lines (104 loc) · 2.99 KB
1
2
3
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
module RubyWarrior
class Level
attr_reader :profile, :number
attr_accessor :description, :tip, :clue, :warrior, :floor, :time_bonus, :ace_score
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
def initialize(profile, number)
@profile = profile
@number = number
@time_bonus = 0
end
def player_path
@profile.player_path
end
def load_path
@profile.tower_path + "/level_" + @number.to_s.rjust(3, '0') + ".rb"
end
def load_level
LevelLoader.new(self).instance_eval(File.read(load_path))
end
def load_player
$: << player_path
load 'player.rb'
end
def generate_player_files
load_level
PlayerGenerator.new(self).generate
end
def play(turns = 1000)
load_level
turns.times do |n|
if passed?
return true
elsif failed?
return false
end
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
return false
end
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
def grade_for(score)
if ace_score
self.class.grade_letter(score / ace_score.to_f)
end
end
def clear_bonus
((warrior.score + time_bonus)*0.2).round
end
def score_calculation(current_score, addition)
if current_score.zero?
addition.to_s
else
"#{current_score} + #{addition} = #{current_score + addition}"
end
end
def passed?
@floor.stairs_space.warrior?
end
def failed?
!@floor.units.include?(warrior)
end
def exists?
File.exist? load_path
end
def setup_warrior(warrior)
@warrior = warrior
@warrior.add_abilities(*profile.abilities)
@warrior.name = profile.warrior_name
end
end
end