Project1

标题: 没看懂只知道的向windows添加新方法!该怎么使用这个脚本? [打印本页]

作者: shengfeng    时间: 2021-4-3 16:11
标题: 没看懂只知道的向windows添加新方法!该怎么使用这个脚本?
  1. # Script:   Draw Animated Characters In Windows
  2. # Author:   Evgenij
  3. # Date:     07.01.2015
  4. # Version:  1.0a
  5. #
  6. # Description: With this script you can add a new method to windows
  7. #              which will draw animated characters
  8. #
  9. #              You need to include the EVG::AnimatedDrawCharacter Module
  10. #              into a Window class.
  11. #              Then you can use this methods:
  12. #                draw_animated_character(character_name, character_index, x, y, direction = 2)
  13. #                clear_animated_characters # clears all the characters from windows content
  14. #                      # and stops drawing them
  15. #              which will draw animated characters
  16. module EVG
  17.   # Adds a new method to a window when included:
  18.   #   draw_animated_character(character_name, character_index, x, y, direction = 2)
  19.   module AnimatedDrawCharacter
  20.     # wait between each character frame (in frames)
  21.     WAIT_BETWEEN_FRAMES = 10

  22.     def initialize(*args)
  23.       super(*args)
  24.       create_animation_state_machine
  25.     end

  26.     def create_animation_state_machine
  27.       @anim_state_machine = AnimationStateMachine.new
  28.       @animated_characters = []
  29.     end

  30.     def draw_animated_character(character_name, character_index, x, y, direction = 2)
  31.       return unless character_name
  32.       bitmap = Cache.character(character_name)
  33.       sign = character_name[/^[\!\$]./]
  34.       if sign && sign.include?(')
  35.         cw = bitmap.width / 3
  36.         ch = bitmap.height / 4
  37.       else
  38.         cw = bitmap.width / 12
  39.         ch = bitmap.height / 8
  40.       end
  41.       @animated_characters << [character_name, character_index, x, y, cw, ch, direction]
  42.       @animated_characters.uniq!
  43.       update_animated_characters
  44.     end

  45.     def clear_animated_characters
  46.       @animated_characters.each do |arr|
  47.         contents.clear_rect(arr[2] - arr[4] / 2, arr[3] - arr[5], arr[4], arr[5])
  48.       end
  49.       @animated_characters = []
  50.     end

  51.     def update
  52.       super
  53.       @anim_state_machine.update
  54.       update_animated_characters if @anim_state_machine.changed?
  55.     end

  56.     def update_animated_characters
  57.       @animated_characters.each{|arr| update_animated_character(arr)}
  58.     end

  59.     # Param looks like this: [character_name, character_index, x, y, ch, cw, direction]
  60.     def update_animated_character(arr)
  61.       contents.clear_rect(arr[2] - arr[4] / 2, arr[3] - arr[5], arr[4], arr[5])
  62.       bitmap = Cache.character(arr[0])
  63.       src_rect = @anim_state_machine.get_rect(arr[1], arr[4], arr[5], arr[6])
  64.       contents.blt(arr[2] - arr[4] / 2, arr[3] - arr[5], bitmap, src_rect)
  65.     end
  66.   end

  67.   class AnimationStateMachine
  68.     def initialize
  69.       super
  70.       @anime_count = 0
  71.       @pattern = 1
  72.       @frame_count = AnimatedDrawCharacter::WAIT_BETWEEN_FRAMES
  73.     end

  74.     def get_rect(char_index, cw, ch, d)
  75.       pattern = @pattern < 3 ? @pattern : 1
  76.       sx = (char_index % 4 * 3 + pattern) * cw
  77.       sy = (char_index / 4 * 4 + (d - 2) / 2) * ch
  78.       Rect.new(sx, sy, cw, ch)
  79.     end

  80.     def update
  81.       update_pattern
  82.     end

  83.     def update_pattern
  84.       @anime_count += 1
  85.       if @anime_count > @frame_count
  86.         @pattern = (@pattern + 1) % 4
  87.         @anime_count = 0
  88.       end
  89.     end

  90.     def changed?
  91.       @anime_count == 0
  92.     end
  93.   end
  94. end
复制代码

作者: alexncf125    时间: 2021-4-3 16:40
本帖最后由 alexncf125 于 2021-4-3 17:01 编辑
  1. class Window_Base < Window
  2.   include EVG::AnimatedDrawCharacter
  3.   alias window_base_initialize_evg_animated_draw_character initialize
  4.   def initialize(x, y, width, height)
  5.     window_base_initialize_evg_animated_draw_character(x, y, width, height)
  6.     4.times {|i| draw_animated_character("Actor#{i + 1}", i, 16 + 32 * i , 32 * (i + 1), [2, 4, 6, 8].sample)}
  7.   end
  8. end
复制代码

看懂没
作者: shengfeng    时间: 2021-4-3 18:48
能看懂!不过显示动态行走图应该在对应的窗口中使用?在wⅰndow_base中使用在任何窗囗中都会显示动态行走图!
作者: alexncf125    时间: 2021-4-3 19:58
我咋知你想在哪个窗口中使用...wⅰndow_base只是个示例好不
作者: shengfeng    时间: 2021-4-3 22:05
alexncf125 发表于 2021-4-3 19:58
我咋知你想在哪个窗口中使用...wⅰndow_base只是个示例好不

战斗中、菜单中和状态中




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1