Advertisement
Trihan

HorrorVale - ScanSkill with word wrap

Jul 28th, 2023
1,372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.57 KB | Source Code | 0 0
  1. #======================================================================
  2. # Scan Skill v2 by mjshi (edits by Trihan)
  3. #----------------------------------------------------------------------
  4. # Place images into Graphics/System.
  5. #----------------------------------------------------------------------
  6. # Usage: Set a skill ID to be the scan skill, then, in that skill's
  7. #        effects, call a common event.
  8. #        The common event should look like this:
  9. #          Script command: SceneManager.scene.run_scan_skill
  10. #----------------------------------------------------------------------
  11.  
  12. module ScanSkill
  13.  
  14.   #--------------------------------------------------------------------
  15.   #  Config
  16.  
  17.  # ID of the scan skill
  18.   SkillID = [ 45, 163, 364, 384, 468, 553 ]
  19.  
  20.   # Order to draw elements in (by type id)
  21.   ElementOrder = [ 2, 3, 4, 5, 6, 7, 8 ]
  22.  
  23.   # Mini element images, grabbed off the icon sheet
  24.   ElementIcons = {
  25.     #---------------------------
  26.     # Format:
  27.     # ElementId => IconIndex
  28.     #
  29.     1 => 313,
  30.     2 => 306,
  31.     3 => 307,
  32.     4 => 308,
  33.     5 => 309,
  34.     6 => 310,
  35.     7 => 311,
  36.     8 => 312,
  37.     #---------------------------
  38.   }
  39.  
  40.   # Images associated with each id
  41.   ElementImages = {
  42.     #---------------------------
  43.     # Format:
  44.     # ElementId => "icon image",
  45.     #
  46.     2 => "Bone",
  47.     3 => "Flesh",
  48.     4 => "Blood",
  49.     5 => "Nature",
  50.     6 => "Lightning",
  51.     7 => "Ectoplasm",
  52.     8 => "Sugar",
  53.     #---------------------------
  54.   }
  55.  
  56.   WeakText = "Wk"
  57.   StrongText = "Res"
  58.   NormalText = "--"
  59.   NullText = "Null"
  60.  
  61.   # Text that precedes the state icons. Leave blank for none.
  62.   StatusText = {
  63.     # Set this to "" for no text
  64.     :text => "Status",
  65.     :x => 325,
  66.     :y => 0,
  67.   }
  68.  
  69.   # If damage taken is 150% normal or higher, consider weak
  70.   WeakPercent = 1.5
  71.   # If damage taken is 50% normal or lower, consider strong
  72.   StrongPercent = 0.5
  73.   # If damage taken is 0%, consider null
  74.   NullPercent = 0
  75.  
  76.   NameWindow = {
  77.     :x => 0,
  78.     :y => 22,
  79.     :width => 544,
  80.     :height => 48,
  81.     :name_x => 0,
  82.     :name_y => 0,
  83.     :icon_x => 5,
  84.     :icon_y => 0,
  85.     :state_x => 415,
  86.     :state_y => 0,
  87.   }
  88.  
  89.   ElementWindow = {
  90.     :x => 0,
  91.     :y => 70,
  92.     :width => 544,
  93.     :height => 120,
  94.    
  95.     :image_x => 4,
  96.     :image_y => 8,
  97.     :text_x => 0,
  98.     :text_y => 70,
  99.   }
  100.  
  101.   DescriptionWindow = {
  102.     :x => 0,
  103.     :y => 190,
  104.     :width => 544,
  105.     :height => 120
  106.   }
  107.  
  108.   DescriptionOn = /<description>/i
  109.  
  110.   DescriptionOff = /<\/description>/i
  111.  
  112.   #  End Config
  113.   #--------------------------------------------------------------------
  114. end
  115.  
  116. #----------------------------------------------------------------------
  117. # Scene_Battle
  118. #
  119. class Scene_Battle
  120.  
  121.   def wait_for_scan
  122.     @scan_status_window.update
  123.     update_for_wait while @scan_status_window.visible
  124.   end
  125.  
  126.     alias mjshi_scan_create_all_windows create_all_windows
  127.     def create_all_windows
  128.       mjshi_scan_create_all_windows
  129.       create_scan_window
  130.   end
  131.  
  132.   def create_scan_window
  133.     @scan_status_window = Window_ScanStatus.new
  134.     @scan_resist_window = Window_Resistances.new
  135.     @scan_description_window = Window_ScanDescription.new
  136.   end
  137.  
  138.   alias mjshi_scan_on_enemy_ok on_enemy_ok
  139.   def on_enemy_ok
  140.     if (@should_scan)
  141.       @should_scan = false
  142.       @enemy_to_scan = $game_troop.members[@enemy_window.enemy.index]
  143.     end
  144.     mjshi_scan_on_enemy_ok
  145.   end
  146.  
  147.   alias mjshi_scan_on_skill_ok on_skill_ok
  148.   def on_skill_ok
  149.     @should_scan = true if (ScanSkill::SkillID.include?(@skill_window.item.id))
  150.     mjshi_scan_on_skill_ok
  151.   end
  152.  
  153.   def run_scan_skill
  154.     return if ($game_troop.all_dead?)
  155.     if (!$game_troop.alive_members.include?(@enemy_to_scan))
  156.       @enemy_to_scan = $game_troop.alive_members[0]
  157.     end
  158.     RPG::SE.new("ScanWindow Open", 100, 100).play
  159.  
  160.     @log_window.hide
  161.     @scan_status_window.set_enemy(@enemy_to_scan)
  162.     @scan_resist_window.set_enemy(@enemy_to_scan)
  163.     @scan_description_window.set_enemy(@enemy_to_scan)
  164.    
  165.     @scan_status_window.show
  166.     @scan_resist_window.show
  167.     @scan_description_window.show
  168.    
  169.     wait_for_scan
  170.   end
  171.  
  172.   def stop_scan_skill
  173.     RPG::SE.new("ScanWindow Close", 100, 100).play
  174.     @scan_status_window.hide
  175.     @scan_resist_window.hide
  176.     @scan_description_window.hide
  177.     @log_window.show
  178.   end
  179. end
  180.  
  181. #----------------------------------------------------------------------
  182. # Window_Resistances
  183. #
  184. class Window_ScanStatus < Window_Selectable
  185.   # icons = (actor.state_icons + actor.buff_icons)[0, width / 24]
  186.   # icons.each_with_index {|n, i| draw_icon(n, x + 24 * i, y) }
  187.  
  188.   def initialize
  189.     super(ScanSkill::NameWindow[:x], ScanSkill::NameWindow[:y], window_width, window_height)
  190.     self.visible = false
  191.   end
  192.  
  193.   def visible_line_number
  194.     return 1
  195.   end
  196.  
  197.   def col_max
  198.     return 1
  199.   end
  200.  
  201.   def window_width
  202.     ScanSkill::NameWindow[:width]
  203.   end
  204.  
  205.   def window_height
  206.     ScanSkill::NameWindow[:height]
  207.   end
  208.   def set_enemy(enemy)
  209.     @enemy = enemy
  210.     refresh
  211.   end
  212.  
  213.   def draw_all_items
  214.     rect = item_rect_for_text(0)
  215.  
  216.     change_color(normal_color)
  217.     draw_text_ex(rect.x + ScanSkill::NameWindow[:name_x], rect.y + ScanSkill::NameWindow[:name_y], @enemy.original_name)
  218.     name_stripped = @enemy.original_name.sub(/^\\C\[\d+\]/i, '').sub(/\\C\[\d+\]$/i, '')
  219.     icon_x = text_size(name_stripped).width # cache end x
  220.    
  221.     draw_text(rect.x + ScanSkill::StatusText[:x], rect.y + ScanSkill::StatusText[:y], rect.width, rect.height, ScanSkill::StatusText[:text])
  222.  
  223.     # attack element
  224.     if !@enemy.atk_elements.empty? && ScanSkill::ElementIcons.include?(@enemy.atk_elements[0])
  225.       draw_icon(ScanSkill::ElementIcons[@enemy.atk_elements[0]], icon_x + ScanSkill::NameWindow[:icon_x], ScanSkill::NameWindow[:icon_y])
  226.     end
  227.    
  228.     # states
  229.     draw_actor_icons(@enemy, ScanSkill::NameWindow[:state_x], ScanSkill::NameWindow[:state_y])
  230.  
  231.     reset_font_settings
  232.   end
  233.  
  234.   def update
  235.     super
  236.     return if (!self.visible)
  237.     if Input.trigger?(:B) || Input.trigger?(:C)
  238.       SceneManager.scene.stop_scan_skill
  239.     end
  240.   end
  241.  
  242. end
  243.  
  244. #----------------------------------------------------------------------
  245. # Window_Resistances
  246. #
  247. class Window_Resistances < Window_Selectable
  248.  
  249.   def initialize
  250.     super(ScanSkill::ElementWindow[:x], ScanSkill::ElementWindow[:y], window_width, window_height)
  251.     self.visible = false
  252.   end
  253.  
  254.   def visible_line_number
  255.     return 1
  256.   end
  257.  
  258.   def col_max
  259.     return ScanSkill::ElementOrder.size
  260.   end
  261.  
  262.   def window_width
  263.     ScanSkill::ElementWindow[:width]
  264.   end
  265.  
  266.   def window_height
  267.     ScanSkill::ElementWindow[:height]
  268.   end
  269.  
  270.   def item_max
  271.     return col_max
  272.   end
  273.  
  274.   def set_enemy(enemy)
  275.     @enemy = enemy
  276.     refresh
  277.   end
  278.  
  279.   def draw_image(file, index, x, y, enabled = true)
  280.     bitmap = Cache.system(file)
  281.     rect = Rect.new(0, 0, bitmap.width, bitmap.height)
  282.     contents.blt(x, y, bitmap, rect, enabled ? 255 : translucent_alpha)
  283.   end
  284.  
  285.   def spacing
  286.     return 5
  287.   end
  288.  
  289.   def draw_item(index)
  290.     elementID = ScanSkill::ElementOrder[index]
  291.     rect = item_rect_for_text(index)
  292.     text = ScanSkill::NormalText
  293.     text = ScanSkill::WeakText if @enemy.element_rate(elementID) >= ScanSkill::WeakPercent
  294.     text = ScanSkill::StrongText if @enemy.element_rate(elementID) <= ScanSkill::StrongPercent
  295.     text = ScanSkill::NullText if @enemy.element_rate(elementID) == ScanSkill::NullPercent
  296.  
  297.     draw_image(ScanSkill::ElementImages[elementID], index, rect.x + ScanSkill::ElementWindow[:image_x], rect.y + ScanSkill::ElementWindow[:image_y])
  298.    
  299.     rect.x += ScanSkill::ElementWindow[:text_x]
  300.     rect.y += ScanSkill::ElementWindow[:text_y]
  301.    
  302.     change_color(normal_color)
  303.     draw_text(rect, text, 1)
  304.     reset_font_settings
  305.   end
  306. end
  307.  
  308. #----------------------------------------------------------------------
  309. # Window_ScanDescription
  310. #
  311. class Window_ScanDescription < Window_Base
  312.  
  313.   def initialize
  314.     super(ScanSkill::DescriptionWindow[:x], ScanSkill::DescriptionWindow[:y], window_width, window_height)
  315.     self.visible = false
  316.     @description_on = false
  317.     @description = ""
  318.   end
  319.  
  320.   def visible_line_number
  321.     return 3
  322.   end
  323.  
  324.   def window_width
  325.     ScanSkill::DescriptionWindow[:width]
  326.   end
  327.  
  328.   def window_height
  329.     ScanSkill::DescriptionWindow[:height]
  330.   end
  331.  
  332.   def set_enemy(enemy)
  333.     @enemy = enemy
  334.     refresh
  335.   end
  336.  
  337.   def refresh
  338.     if @enemy
  339.       @enemy.note.split(/[\r\n]+/).each { |line|
  340.         case line
  341.           when ScanSkill::DescriptionOn
  342.             @description_on = true
  343.           when ScanSkill::DescriptionOff
  344.             @description_on = false
  345.           else
  346.             @description += line.to_s + "\n" if @description_on
  347.           end
  348.       }
  349.       process_all_text
  350.     end
  351.   end
  352.  
  353.   def process_all_text
  354.     text = convert_escape_characters(@description)
  355.     col = contents.width / text_size("W").width
  356.     text.gsub!(/\e([ic])\[(\d+)\]/," \\1\\2 ")
  357.     text = wrap_text(text, col)
  358.     lines = text.split("\n")
  359.     output = []
  360.     lines.each_with_index { |line, index|
  361.       loop do
  362.         if lines[index+1]
  363.           next_line = lines[index+1]
  364.           if next_line == ""
  365.             lines.delete(next_line)
  366.             next
  367.           end
  368.           words = next_line.split(" ")
  369.           word = words[0]
  370.           break unless word
  371.           line_width = text_size(line).width
  372.           word_width = text_size(" " + word).width
  373.           if line_width + word_width < contents.width
  374.             line << (line == "" ? "" : " ") + word
  375.             next_line.slice!(0, (word + " ").length)
  376.           else
  377.             break
  378.           end
  379.         else
  380.           break
  381.         end
  382.       end
  383.       output << line
  384.     }
  385.     text = output.join("\n")
  386.     text.gsub!(/ ?([ic])(\d+) ?/,"\e\\1\[\\2\]")
  387.     pos = {}
  388.     new_page(text, pos)
  389.     process_character(text.slice!(0, 1), text, pos) until text.empty?
  390.   end
  391.  
  392.   def wrap_text(txt, col = 80)
  393.     txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}}(?: ?[ic]?\d+? )?)/,"\\1\\3\n")
  394.   end
  395.  
  396.   def new_page(text, pos)
  397.     contents.clear
  398.     reset_font_settings
  399.     pos[:x] = new_line_x
  400.     pos[:y] = 0
  401.     pos[:new_x] = new_line_x
  402.     pos[:height] = calc_line_height(text)
  403.   end
  404.  
  405.   def new_line_x
  406.     0
  407.   end
  408. end
  409.  
  410. class Window_BattleLog < Window_Selectable
  411.  
  412.   alias mjshi_scan_display_failure display_failure
  413.   def display_failure(target, item)
  414.     return if item.is_a?(RPG::Skill) && ScanSkill::SkillID.include?(item.id)
  415.     mjshi_scan_display_failure(target, item)
  416.   end
  417.  
  418. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement