Project1

标题: 一个状态窗口中有一个人物行走踏步动态图脚本如何实现。 [打印本页]

作者: alianlord    时间: 2021-3-30 18:05
标题: 一个状态窗口中有一个人物行走踏步动态图脚本如何实现。
人物行走怎么描绘我能实现,但是要使之走动起来就茫然了。求指教。
作者: alexncf125    时间: 2021-3-30 19:04
RUBY 代码复制
  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. module EVG
  16.   # Adds a new method to a window when included:
  17.   #   draw_animated_character(character_name, character_index, x, y, direction = 2)
  18.   module AnimatedDrawCharacter
  19.     # wait between each character frame (in frames)
  20.     WAIT_BETWEEN_FRAMES = 10
  21.  
  22.     def initialize(*args)
  23.       super(*args)
  24.       create_animation_state_machine
  25.     end
  26.  
  27.     def create_animation_state_machine
  28.       @anim_state_machine = AnimationStateMachine.new
  29.       @animated_characters = []
  30.     end
  31.  
  32.     def draw_animated_character(character_name, character_index, x, y, direction = 2)
  33.       return unless character_name
  34.       bitmap = Cache.character(character_name)
  35.       sign = character_name[/^[\!\$]./]
  36.       if sign && sign.include?('$')
  37.         cw = bitmap.width / 3
  38.         ch = bitmap.height / 4
  39.       else
  40.         cw = bitmap.width / 12
  41.         ch = bitmap.height / 8
  42.       end
  43.       @animated_characters << [character_name, character_index, x, y, cw, ch, direction]
  44.       @animated_characters.uniq!
  45.       update_animated_characters
  46.     end
  47.  
  48.     def clear_animated_characters
  49.       @animated_characters.each do |arr|
  50.         contents.clear_rect(arr[2] - arr[4] / 2, arr[3] - arr[5], arr[4], arr[5])
  51.       end
  52.       @animated_characters = []
  53.     end
  54.  
  55.     def update
  56.       super
  57.       @anim_state_machine.update
  58.       update_animated_characters if @anim_state_machine.changed?
  59.     end
  60.  
  61.     def update_animated_characters
  62.       @animated_characters.each{|arr| update_animated_character(arr)}
  63.     end
  64.  
  65.     # Param looks like this: [character_name, character_index, x, y, ch, cw, direction]
  66.     def update_animated_character(arr)
  67.       contents.clear_rect(arr[2] - arr[4] / 2, arr[3] - arr[5], arr[4], arr[5])
  68.       bitmap = Cache.character(arr[0])
  69.       src_rect = @anim_state_machine.get_rect(arr[1], arr[4], arr[5], arr[6])
  70.       contents.blt(arr[2] - arr[4] / 2, arr[3] - arr[5], bitmap, src_rect)
  71.     end
  72.   end
  73.  
  74.   class AnimationStateMachine
  75.     def initialize
  76.       super
  77.       @anime_count = 0
  78.       @pattern = 1
  79.       @frame_count = AnimatedDrawCharacter::WAIT_BETWEEN_FRAMES
  80.     end
  81.  
  82.     def get_rect(char_index, cw, ch, d)
  83.       pattern = @pattern < 3 ? @pattern : 1
  84.       sx = (char_index % 4 * 3 + pattern) * cw
  85.       sy = (char_index / 4 * 4 + (d - 2) / 2) * ch
  86.       Rect.new(sx, sy, cw, ch)
  87.     end
  88.  
  89.     def update
  90.       update_pattern
  91.     end
  92.  
  93.     def update_pattern
  94.       @anime_count += 1
  95.       if @anime_count > @frame_count
  96.         @pattern = (@pattern + 1) % 4
  97.         @anime_count = 0
  98.       end
  99.     end
  100.  
  101.     def changed?
  102.       @anime_count == 0
  103.     end
  104.   end
  105. end

作者: alianlord    时间: 2021-3-30 21:44
alexncf125 发表于 2021-3-30 19:04
# Script:   Draw Animated Characters In Windows
# Author:   Evgenij
# Date:     07.01.2015

看起来有点复杂,让我花点时间研究一下。谢谢。




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