赞 | 159 |
VIP | 0 |
好人卡 | 0 |
积分 | 263 |
经验 | 0 |
最后登录 | 2024-11-16 |
在线时间 | 5355 小时 |
Lv5.捕梦者
- 梦石
- 0
- 星屑
- 26264
- 在线时间
- 5355 小时
- 注册时间
- 2016-3-8
- 帖子
- 1655
|
本帖最后由 alexncf125 于 2020-10-5 09:59 编辑
设有一名敌人甲和两名敌人乙
1.@names_count在class Game_Troop 第49行賦值了为{}, 所以p @names_count得{}
2.@names_count为{}, 不存在enemy.original_name, 所以p @names_count[enemy.original_name]得nil
3.n = @names_count[enemy.original_name] || 0, 即n = nil || 0
nil || 0取0, 所以p n得0
4.p n % LETTER_TABLE.size, 即 0 % 26, 得0
enemy.letter = LETTER_TABLE[n % LETTER_TABLE.size], 即enemy.letter = LETTER_TABLE[0], 所以p enemy.letter得" A"
5.@names_count[enemy.original_name] = n + 1, 即@names_count[enemy.original_name] = 1, 所以@names_count由{}成了{"甲"=>1}
6.@names_count的值最终会是{"甲"=>1, "乙"=>2}, 所以p @names_count[enemy.original_name] 输出1 2 2
class Game_Troop < Game_Unit #-------------------------------------------------------------------------- # ● 同名的敌人附加字母后缀 #-------------------------------------------------------------------------- def make_unique_names members.each do |enemy| next unless enemy.alive? next unless enemy.letter.empty? p @names_count #=>{} {"甲"=>1} {"甲"=>1, "乙"=>1} #1. p @names_count[enemy.original_name] #=>nil nil 1 #2. n = @names_count[enemy.original_name] || 0 #3. p n #=>0 0 1 #3. p LETTER_TABLE #=>[" A", " B", " C", " D", " E", " F", " G", " H", " I", " J", " K", " L", " M", " N", " O", " P", " Q", " R", " S", " T", " U", " V", " W", " X", " Y", " Z"] p LETTER_TABLE.size #=>26 p (n % LETTER_TABLE.size) #=>0 0 1 #4. enemy.letter = LETTER_TABLE[n % LETTER_TABLE.size] #4. p enemy.letter #=>" A" " A" " B" #4. @names_count[enemy.original_name] = n + 1 #5. p @names_count #=>{"甲"=>1} {"甲"=>1, "乙"=>1} {"甲"=>1, "乙"=>2} #5. end members.each do |enemy| p @names_count[enemy.original_name] #=>1 2 2 #6. n = @names_count[enemy.original_name] || 0 enemy.plural = true if n >= 2 #当敌人"X"有两名或以上(例如{" 甲"=>1, " 乙"=>2}中, " 乙"的值>=2)时, 敌人"X"的名字附加后缀(enemy.letter) end end
class Game_Troop < Game_Unit
#--------------------------------------------------------------------------
# ● 同名的敌人附加字母后缀
#--------------------------------------------------------------------------
def make_unique_names
members.each do |enemy|
next unless enemy.alive?
next unless enemy.letter.empty?
p @names_count #=>{} {"甲"=>1} {"甲"=>1, "乙"=>1} #1.
p @names_count[enemy.original_name] #=>nil nil 1 #2.
n = @names_count[enemy.original_name] || 0 #3.
p n #=>0 0 1 #3.
p LETTER_TABLE #=>[" A", " B", " C", " D", " E", " F", " G", " H", " I", " J", " K", " L", " M", " N", " O", " P", " Q", " R", " S", " T", " U", " V", " W", " X", " Y", " Z"]
p LETTER_TABLE.size #=>26
p (n % LETTER_TABLE.size) #=>0 0 1 #4.
enemy.letter = LETTER_TABLE[n % LETTER_TABLE.size] #4.
p enemy.letter #=>" A" " A" " B" #4.
@names_count[enemy.original_name] = n + 1 #5.
p @names_count #=>{"甲"=>1} {"甲"=>1, "乙"=>1} {"甲"=>1, "乙"=>2} #5.
end
members.each do |enemy|
p @names_count[enemy.original_name] #=>1 2 2 #6.
n = @names_count[enemy.original_name] || 0
enemy.plural = true if n >= 2 #当敌人"X"有两名或以上(例如{" 甲"=>1, " 乙"=>2}中, " 乙"的值>=2)时, 敌人"X"的名字附加后缀(enemy.letter)
end
end
输出的结果由上至下,左至右理解, 即:
#=>a e i
#=>b f j
#=>c g k
#=>d h l
第26行#=>x y z
由a看到d,由e看到h,由 i 看到 l, 最后才算xyz |
|