查看: 3551|回复: 2

[已经解决] 一个状态窗口中有一个人物行走踏步动态图脚本如何实现。

[复制链接]
梦石
0
星屑
208
在线时间
420 小时
注册时间
2020-1-11
回帖
175
发表于 2021-3-30 18:05:50 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
人物行走怎么描绘我能实现,但是要使之走动起来就茫然了。求指教。
梦石
0
星屑
29901
在线时间
5734 小时
注册时间
2016-3-8
回帖
1653
发表于 2021-3-30 19:04:10 | 显示全部楼层
[pre lang="ruby"]# Script:   Draw Animated Characters In Windows
# Author:   Evgenij
# Date:     07.01.2015
# Version:  1.0a
#
# Description: With this script you can add a new method to windows
#              which will draw animated characters
#
#              You need to include the EVG::AnimatedDrawCharacter Module
#              into a Window class.
#              Then you can use this methods:
#                draw_animated_character(character_name, character_index, x, y, direction = 2)
#                clear_animated_characters # clears all the characters from windows content
#                      # and stops drawing them
module EVG
  # Adds a new method to a window when included:
  #   draw_animated_character(character_name, character_index, x, y, direction = 2)
  module AnimatedDrawCharacter
    # wait between each character frame (in frames)
    WAIT_BETWEEN_FRAMES = 10

    def initialize(*args)
      super(*args)
      create_animation_state_machine
    end

    def create_animation_state_machine
      @anim_state_machine = AnimationStateMachine.new
      @animated_characters = []
    end

    def draw_animated_character(character_name, character_index, x, y, direction = 2)
      return unless character_name
      bitmap = Cache.character(character_name)
      sign = character_name[/^[\!\$]./]
      if sign && sign.include?('$')
        cw = bitmap.width / 3
        ch = bitmap.height / 4
      else
        cw = bitmap.width / 12
        ch = bitmap.height / 8
      end
      @animated_characters << [character_name, character_index, x, y, cw, ch, direction]
      @animated_characters.uniq!
      update_animated_characters
    end

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

    def update
      super
      @anim_state_machine.update
      update_animated_characters if @anim_state_machine.changed?
    end

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

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

  class AnimationStateMachine
    def initialize
      super
      @anime_count = 0
      @pattern = 1
      @frame_count = AnimatedDrawCharacter::WAIT_BETWEEN_FRAMES
    end

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

    def update
      update_pattern
    end

    def update_pattern
      @anime_count += 1
      if @anime_count > @frame_count
        @pattern = (@pattern + 1) % 4
        @anime_count = 0
      end
    end

    def changed?
      @anime_count == 0
    end
  end
end[/pre]
回复 1 0

使用道具 举报

梦石
0
星屑
208
在线时间
420 小时
注册时间
2020-1-11
回帖
175
 楼主| 发表于 2021-3-30 21:44:27 | 显示全部楼层
alexncf125 发表于 2021-3-30 19:04
# Script:   Draw Animated Characters In Windows
# Author:   Evgenij
# Date:     07.01.2015

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

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部