-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacter.rb
303 lines (249 loc) · 9.84 KB
/
character.rb
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# frozen_string_literal: true
require_relative 'attributes'
require_relative 'exceptions/character_not_in_party'
require_relative 'exceptions/crystal_already_bound'
require_relative 'exceptions/crystal_limit_reached'
require_relative 'exceptions/level_too_low_for_crystal_binding'
require_relative 'exceptions/same_element_crystal_already_bound'
require_relative 'crystal'
require_relative 'job'
require_relative 'race'
require_relative 'stats'
# rubocop:disable Metrics/ClassLength
# Represents a Character.
#
# @author Sergio Bobillier <[email protected]>
class Character
# Level cap
MAX_LEVEL = 50
# Experience needed to reach level 2. The experience needed for level up is
# calculated using this figure as a base.
BASE_EXP = 100
# The maximum number of crystals a character can have.
MAX_CRYSTALS = 3
# @return [Party] The party the character belongs to.
attr_accessor :party
# @return [Integer] The character's level
attr_reader :level
# @return [Integer] The experience needed to reach the next level
attr_reader :next_level
# @return [Integer] The experience gained in the current level
attr_reader :experience
# @return [Stats] The character's basic stats
attr_reader :stats
# @return [Attributes] The character's attributes
attr_reader :attributes
# @return [Race] The character's race.
attr_reader :race
# @return [Job] The character's current job.
attr_reader :job
# Creates a new character with the supplied race, level and job
#
# @param race [Race] The character's race.
# @param level [Integer] The character's level.
# @param job [Job] The character's job.
# @return [Character] The new Character instance.
# @raise [ArgumentError] If `race` is not an instance of `Race`, `level` is
# not an Integer or is outside the valid range or `job` is not an instance
# of `Job`
def initialize(race, level = 1, job = nil)
unless race.is_a?(Race)
raise ArgumentError, 'race should be an instance of `Race`'
end
@race = race
@base_stats = Stats.new
@base_stats << @race.stats
self.job = job
# Attributes needs to be recalculated when stats change.
@stats.change_listeners << lambda { |_stat, _current_value, _new_value|
recalculate_attributes
}
@attributes = Attributes.new
@crystals = []
# NOTE: Setting the level should be the last thing in the initializer
self.level = level
end
# Sets the character's level.
#
# @param level [Integer] The character's level.
# @raise [ArgumentError] If `level` is not an integer or ir is less than one
# or greater than MAX_LEVEL.
def level=(level)
unless level.is_a?(Integer)
raise ArgumentError, '`level` must be an Integer'
end
unless level >= 1 && level <= MAX_LEVEL
raise ArgumentError, "`level` must be between 1 and #{MAX_LEVEL}"
end
@level = level
@experience = 0
# Calculates the experience needed to reach the next level. Should be 1.5
# times the experience needed to reach the current level.
experience = BASE_EXP
(@level - 1).times { experience = (experience * 1.5).floor } if @level >= 2
@next_level = experience
recalculate_attributes(true) unless @dont_recalculate
end
# Sets the character's experience in the current level. If the experience set
# is greater than the experience needed to level up then the character will
# level up and the remaining experience will be awarded for the next level. If
# the experience is negative then the character will level down and the
# remaining experience will be substracted from the previous level.
#
# @param experience [Integer] The experience.
# @raise [ArgumentError] If the experience is not an integer.
def experience=(experience)
unless experience.is_a?(Integer)
raise ArgumentError, '`experience` should be an integer'
end
return if experience.zero?
if experience.positive?
add_experience experience
else
substract_experience experience
end
end
# Leaves the current party.
#
# @raise [CharacterNotInParty] If the character is not a party member.
# @raise [CharacterNotFound] If the character tries to leave a party from
# which it is not a member. (Should never be raised under normal
# circunstances).
def leave_party
raise CharacterNotInParty unless party
party.remove!(self)
end
# Sets the character's current job.
#
# @param job [Job] The character's current job.
# @raise [ArgumentError] If `job` is not an instance of `Job` or `nil`
def job=(job)
unless job.is_a?(Job) || job.nil?
raise ArgumentError, '`job` muest be an instance of Job or `nil`'
end
@job = job
@stats = (job ? @base_stats + job.stats : @base_stats.clone)
recalculate_attributes if @level
end
# Returns an array with the crystals that are currently bound to the
# character.
#
# @return [Array] The bound crystals array.
def crystals
@crystals.clone
end
# Binds the given crystal to the character.
#
# @param crystal [Crystal] The crystal to bind.
# @raise [ArgumentError] If the given value is not an instance of `Crystal`.
# @raise [CrystalLimitReached] If the crystal limit has been reached.
# @raise [LevelTooLowForCrystalBinding] If the character's level is not high
# enough to bind another crystal.
# @raise [CrystalAlreadyBound] If an attempt is made to bind the same crystal
# twice.
# @raise [CrystalAlreadyBound] If the crystal has already been bound to a
# character.
# @raise [SameElementCrystalAlreadyBound] If the character has a bound Crystal
# of the same element.
def bind_crystal(crystal)
validate_crystal_binding(crystal)
# The first crystal can be bound as soon as the character is created, that
# is, when character is at level 1. The second crystal can only be bound
# after the character has reached 1 * (MAX_LEVEL/MAX_CRYSTALS), normally
# that would be 1 * (50 / 3) = 16. The third crystal can only be bound
# after the character has reached 2 * (MAX_LEVEL/MAX_CRSTALS), i.e. 2 *
# (50 / 3) = 32 and so on. Note that, if MAX_LEVEL or MAX_CRYSTALS change
# these values will change too.
min_level = @crystals.length * (MAX_LEVEL / MAX_CRYSTALS)
raise LevelTooLowForCrystalBinding if level < min_level
validate_crystal_element(crystal)
crystal.bind_to(self)
@crystals << crystal
end
private
# Validates that the crystal binding that is about to be performed is valid.
#
# @raise [ArgumentError] If the given value is not an instance of `Crystal`.
# @raise [CrystalLimitReached] If the crystal limit has been reached.
# @raise [LevelTooLowForCrystalBinding] If the character's level is not high
# enough to bind another crystal.
# @raise [CrystalAlreadyBound] If an attempt is made to bind the same crystal
# twice.
# @raise [CrystalAlreadyBound] If the crystal has already been boundto a
# character.
def validate_crystal_binding(crystal)
raise ArgumentError unless crystal.is_a?(Crystal)
raise CrystalLimitReached if @crystals.length >= MAX_CRYSTALS
raise CrystalAlreadyBound if @crystals.include?(crystal) || crystal.bound_to
end
# Validates that the character doesn't have a Crystal with the same element
# already bound.
#
# @param crystal [Crystal] The crystal.
# @raise [SameElementCrystalAlreadyBound] If the character has a bound Crystal
# of the same element.
def validate_crystal_element(crystal)
@crystals.each do |bound_crystal|
if bound_crystal.element == crystal.element
raise SameElementCrystalAlreadyBound
end
end
end
# Recalculates the character attributes with the current level and ststs.
#
# @param reset_transient_attributes [Boolean] If true transient attributes
# like health and mana will be reset to their maximum.
def recalculate_attributes(reset_transient_attributes = false)
# Recalculate the character attributes for this level
@attributes.calculate_attributes(@stats, @level, reset_transient_attributes)
end
# Increases the character's experience by the given amount. If the experience
# is greater than the experience needed to reach the next level the method
# will increase the character's level apropriately and will continue to do so
# until the character has reached MAX_LEVEL or the amount of experience
# becomes less than the experience needed for next level.
#
# @param experience [Integer] The amount of experience to add.
def add_experience(experience)
if experience < @next_level
@experience = experience
else
@dont_recalculate = true
while experience >= @next_level && @level < MAX_LEVEL
experience -= @next_level
self.level += 1
end
recalculate_attributes(true)
@dont_recalculate = false
@experience = (experience > @next_level ? @next_level : experience)
end
end
# Reduces the character's experience by the given amount. If the character's
# experience goes below zero the character's level will go down. The method
# lowers the character's level until the character has reached level 1 or
# until it has substracted the specified amount of experience.
#
# @param experience [Integer] The amount of experience to substract.
def substract_experience(experience)
if @level == 1
@experience = 0 # Character cannot go below level 1
return # so bail out.
end
experience = @experience + experience.abs
@dont_recalculate = true
while experience >= @experience
experience -= @experience
self.level -= 1
@experience = @next_level
break if @level == 1 # Don't allow the charater to go below level 1
end
recalculate_attributes(true)
@dont_recalculate = false
if experience > @experience
@experience = 0
else
@experience -= experience
end
end
end
# rubocop:enable Metrics/ClassLength