设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 2270|回复: 1
打印 上一主题 下一主题

[VIPArcher] 【八方向移动】

[复制链接]

无限の剣制

梦石
0
星屑
9956
在线时间
5019 小时
注册时间
2013-2-28
帖子
5030

开拓者贵宾

跳转到指定楼层
1
发表于 2014-11-2 00:19:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

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

x
本帖最后由 VIPArcher 于 2018-12-12 05:27 编辑

RUBY 代码复制
  1. #===============================================================================
  2. # ■ 八向移动待机行走图动画扩展
  3. # by :VIPArcher [email: [email protected]]
  4. #  -- https://viparcher.github.io/Hexo/2018/12/12/dir8_anime/
  5. #  -- Project1 论坛:https://rpg.blue/ 使用或转载请保留以上信息。
  6. #==============================================================================
  7. # ■ 使用说明:
  8. #   行走图素材文件名最前面添加 '@' 符号视为该角色/事件使用八方向行走图
  9. #   需为其配置对应的其他形态的行走图文件,文件名后缀规则请看设定部分
  10. #     例如  [email protected] / @Actor1.png
  11. #   文件名中带上 [f帧数#默认帧] 来控制行走图动画的帧数和停下时使用的帧
  12. #     例如  $@Actor[f8#4].png / @Actor1[f8#4].png
  13. #   事件也要使用待机动画需要在事件页(当前页)中备注上 <idle_anime>
  14. #   事件的移动速度超过 5 则使用奔跑行走图
  15. #==============================================================================
  16. $VIPArcherScript ||= {};$VIPArcherScript[:dir8_anime] = 20181212
  17. #==============================================================================
  18. # ★ 设定部分 ★
  19. #==============================================================================
  20. module VIPArcher end
  21. module VIPArcher::DIR8_ANIME
  22.   # 配置开始
  23.   DIR8_OFF_SW   = 0  # 控制关闭八方向行走的开关
  24.   IDLE_ANIME_SW = 0  # 控制关闭待机动画的开关
  25.   DASH_ANIME_SW = 0  # 控制关闭奔跑动画的开关
  26.   IDLE_TIME   = 120  # 静止后进入待机动画的时间(帧)
  27.   RIDLE_TIME  = 120  # 待机等待时间浮动值(上下浮动该值一半) 默认即 120 - 360 帧
  28.   ANIME_TIME  = 240  # 待机动画播放的时长(帧)
  29.   GAP_TIME    = 120  # 两次待机动画之间的时间间隔
  30.   IDLE_NOTE   = '<idle_anime>' # 事件使用待机动画的备注
  31.   DASH4_AFFIX = '_DASH'    # 奔跑普通行走图后缀
  32.   IDLE4_AFFIX = '_IDLE'    # 待机普通行走图后缀
  33.   DIRE8_AFFIX = '_8D'      # 普通斜向行走图后缀
  34.   DASH8_AFFIX = '_DASH_8D' # 奔跑斜向行走图后缀(该条仅参考命名,没有实际作用)
  35.   IDLE8_AFFIX = '_IDLE_8D' # 待机斜向行走图后缀(该条仅参考命名,没有实际作用)
  36.   # 配置结束
  37.   #--------------------------------------------------------------------------
  38.   # ● 判断是否多帧
  39.   #--------------------------------------------------------------------------
  40.   def is_multi_frames?
  41.     character_name =~ /\[f\d+#?\d*\]/i
  42.   end
  43.   #--------------------------------------------------------------------------
  44.   # ● 获取帧数
  45.   #--------------------------------------------------------------------------
  46.   def get_frame(character_name)
  47.     character_name =~ /\[f(\d+)#?\d*\]/i ? $1.to_i : 4
  48.   end
  49.   #--------------------------------------------------------------------------
  50.   # ● 获取原图案(静止时矫正帧)
  51.   #--------------------------------------------------------------------------
  52.   def get_halt_name(character_name)
  53.     character_name =~ /\[f\d+#(\d+)\]/i ? $1.to_i : 1
  54.   end
  55. end
  56. #==============================================================================
  57. # ■ Game_Player
  58. #==============================================================================
  59. class Game_Player
  60.   include VIPArcher::DIR8_ANIME
  61.   #--------------------------------------------------------------------------
  62.   # ● 由方向键移动
  63.   #--------------------------------------------------------------------------
  64.   def move_by_input
  65.     return if !movable? || $game_map.interpreter.running?
  66.     if [1, 3, 7, 9].include?(Input.dir8)
  67.       case Input.dir8
  68.       when 1 then move_diagonal(4, 2)
  69.       when 3 then move_diagonal(6, 2)
  70.       when 7 then move_diagonal(4, 8)
  71.       when 9 then move_diagonal(6, 8)
  72.       end; return if @move_succeed
  73.     end unless $game_switches[DIR8_OFF_SW]
  74.     move_straight(Input.dir4) if Input.dir4 > 0
  75.   end
  76.   #--------------------------------------------------------------------------
  77.   # ● 是否奔跑中
  78.   #--------------------------------------------------------------------------
  79.   def is_dash?
  80.     return dash? && @stop_count.zero?
  81.   end
  82.   #--------------------------------------------------------------------------
  83.   # ● 刷新
  84.   #--------------------------------------------------------------------------
  85.   alias set_frame_refresh refresh
  86.   def refresh
  87.     set_frame_refresh
  88.     @frame = get_frame(@character_name)
  89.     @original_pattern = get_halt_name(@character_name)
  90.   end
  91. end
  92. class Game_Follower
  93.   #--------------------------------------------------------------------------
  94.   # ● 是否奔跑中
  95.   #--------------------------------------------------------------------------
  96.   def is_dash?
  97.     $game_player.is_dash?
  98.   end
  99.   #--------------------------------------------------------------------------
  100.   # ● 刷新
  101.   #--------------------------------------------------------------------------
  102.   alias set_frame_refresh refresh
  103.   def refresh
  104.     set_frame_refresh
  105.     @frame = get_frame(@character_name)
  106.     @original_pattern = get_halt_name(@character_name)
  107.   end
  108.   #--------------------------------------------------------------------------
  109.   # ● 更新画面
  110.   # * 使队员待机动画不同步,没用alias,如有冲突,请把该脚本放置被冲突脚本上面
  111.   #--------------------------------------------------------------------------
  112.   def update
  113.     @move_speed     = $game_player.real_move_speed
  114.     @transparent    = $game_player.transparent
  115.     @walk_anime     = $game_player.walk_anime
  116.     @step_anime     = @step_anime  #踏步属性不继承队伍
  117.     @direction_fix  = $game_player.direction_fix
  118.     @opacity        = $game_player.opacity
  119.     @blend_type     = $game_player.blend_type
  120.     super
  121.   end
  122. end
  123. class Game_Event
  124.   #--------------------------------------------------------------------------
  125.   # ● 是否奔跑中
  126.   #--------------------------------------------------------------------------
  127.   def is_dash?
  128.     return !@locked && @move_speed >= 5 && @stop_count.zero?
  129.   end
  130.   #--------------------------------------------------------------------------
  131.   # ● 是否待机动画
  132.   #--------------------------------------------------------------------------
  133.   def idle_anime?
  134.     @list.each do |command|
  135.       if [108, 408].include?(command.code)
  136.         return @idle_anime if command.parameters.any? do |line|
  137.           line =~ /#{IDLE_NOTE}/i
  138.         end
  139.       end
  140.     end if @list
  141.     return false
  142.   end
  143.   #--------------------------------------------------------------------------
  144.   # ● 刷新
  145.   #--------------------------------------------------------------------------
  146.   alias set_frame_setup_page_settings setup_page_settings
  147.   def setup_page_settings
  148.     set_frame_setup_page_settings
  149.     @frame = get_frame(@character_name)
  150.     @original_pattern = get_halt_name(@character_name)
  151.   end
  152. end
  153. #==============================================================================
  154. # ■ Game_CharacterBase
  155. #==============================================================================
  156. class Game_CharacterBase
  157.   include VIPArcher::DIR8_ANIME
  158.   attr_reader :frame
  159.   attr_reader :static_anime
  160.   attr_accessor :idle_anime
  161.   #--------------------------------------------------------------------------
  162.   # ● 更新画面
  163.   #--------------------------------------------------------------------------
  164.   alias dir8_update update
  165.   def update
  166.     dir8_update
  167.     return if $game_switches[IDLE_ANIME_SW]
  168.     return unless idle_anime?
  169.     @step_anime = update_static_anime
  170.   end
  171.   #--------------------------------------------------------------------------
  172.   # ● 更新待机动画
  173.   #--------------------------------------------------------------------------
  174.   def update_static_anime
  175.     case @stop_count
  176.     when static_anime_idle_time...static_anime_idle_time + ANIME_TIME
  177.       @static_anime = true
  178.     when static_anime_idle_time + ANIME_TIME
  179.       @stop_count = [static_anime_idle_time - GAP_TIME,0].max
  180.       @pattern = @original_pattern
  181.       @static_anime = false
  182.     else @static_anime = false end
  183.   end
  184.   #--------------------------------------------------------------------------
  185.   # ● 进入待机前等待时间获取
  186.   #--------------------------------------------------------------------------
  187.   def static_anime_idle_time
  188.     return @anime_idle_time if @anime_idle_time
  189.     @anime_idle_time = (IDLE_TIME - RIDLE_TIME / 2 + rand(RIDLE_TIME)).to_i
  190.   end
  191.   #--------------------------------------------------------------------------
  192.   # ● 是否待机动画
  193.   #--------------------------------------------------------------------------
  194.   def idle_anime?; @idle_anime end
  195.   #--------------------------------------------------------------------------
  196.   # ● 是否奔跑
  197.   #--------------------------------------------------------------------------
  198.   def is_dash?; false end
  199.   #--------------------------------------------------------------------------
  200.   # ● 初始化私有成员变量
  201.   #--------------------------------------------------------------------------
  202.   alias set_frame_init_private_members init_private_members
  203.   def init_private_members
  204.     set_frame_init_private_members
  205.     @frame = 4
  206.   end
  207.   #--------------------------------------------------------------------------
  208.   # ● 更新步行/踏步动画
  209.   #--------------------------------------------------------------------------
  210.   def update_animation
  211.     update_anime_count
  212.     if @anime_count > animation_wait
  213.       update_anime_pattern
  214.       @anime_count = 0
  215.     end
  216.   end
  217.   #--------------------------------------------------------------------------
  218.   # ● 移动时两帧之间等待时间
  219.   #--------------------------------------------------------------------------
  220.   def animation_wait
  221.     [(9 - real_move_speed) * 3, 2].max
  222.   end
  223.   #--------------------------------------------------------------------------
  224.   # ● 矫正姿势
  225.   #--------------------------------------------------------------------------
  226.   def straighten
  227.     @pattern = @original_pattern if @walk_anime || @step_anime
  228.     @anime_count = 0
  229.   end
  230.   #--------------------------------------------------------------------------
  231.   # ● 更新动画图案
  232.   #--------------------------------------------------------------------------
  233.   def update_anime_pattern
  234.     if !@step_anime && @stop_count > 0
  235.       @pattern = @original_pattern
  236.     else
  237.       @pattern = (@pattern + 1) % @frame
  238.     end
  239.   end
  240.   #--------------------------------------------------------------------------
  241.   # ● 更改图像
  242.   #--------------------------------------------------------------------------
  243.   alias set_frame_set_graphic set_graphic
  244.   def set_graphic(character_name, character_index)
  245.     set_frame_set_graphic(character_name, character_index)
  246.     @frame = get_frame(character_name)
  247.     @original_pattern = get_halt_name(character_name)
  248.   end
  249.   #--------------------------------------------------------------------------
  250.   # ● 斜向移动
  251.   #--------------------------------------------------------------------------
  252.   alias dir8_move_diagonal move_diagonal
  253.   def move_diagonal(horz, vert)
  254.     return @move_succeed = false if !passable?(@x, @y, horz) && !passable?(@x, @y, vert)
  255.     return move_straight(horz) if passable?(@x, @y, horz) && !passable?(@x, @y, vert)
  256.     return move_straight(vert) if passable?(@x, @y, vert) && !passable?(@x, @y, horz)
  257.     dir8_move_diagonal(horz, vert)
  258.     case [horz,vert]
  259.     when [4,2] then set_direction(1)
  260.     when [4,8] then set_direction(7)
  261.     when [6,2] then set_direction(3)
  262.     when [6,8] then set_direction(9)
  263.     end
  264.   end
  265. end
  266. #==============================================================================
  267. # ■ Sprite_Character
  268. #==============================================================================
  269. class Sprite_Character < Sprite_Base
  270.   include VIPArcher::DIR8_ANIME
  271.   #--------------------------------------------------------------------------
  272.   # ● 是否是八方向素材
  273.   #--------------------------------------------------------------------------
  274.   def is_dir8?
  275.     return false if @character_name =~ /#{DIRE8_AFFIX}/
  276.     return @character_name =~ /^(\@|\!\@|\$@|\!\$\@).+/
  277.   end
  278.   #--------------------------------------------------------------------------
  279.   # ● 设置角色的位图
  280.   #--------------------------------------------------------------------------
  281.   alias dir8_move_set_character_bitmap set_character_bitmap
  282.   def set_character_bitmap
  283.     dir8_move_set_character_bitmap
  284.     return unless is_dir8?
  285.     @character_dir8 = {}
  286.     set_character_dir8("", "")
  287.     set_character_dir8(DASH4_AFFIX, DASH4_AFFIX)
  288.     set_character_dir8(IDLE4_AFFIX, IDLE4_AFFIX)
  289.     set_character_dir8(DIRE8_AFFIX, DIRE8_AFFIX)
  290.     set_character_dir8(DASH8_AFFIX, DASH8_AFFIX, DIRE8_AFFIX)
  291.     set_character_dir8(IDLE8_AFFIX, IDLE8_AFFIX, DIRE8_AFFIX)
  292.     frame = @character.is_multi_frames? ? @character.frame : 3
  293.     sign = @character_name[/^[\@\!\$]../]
  294.     @cw = bitmap.width / frame
  295.     @cw /= 4 unless sign && sign.include?('$')
  296.     self.ox = @cw / 2
  297.   end
  298.   #--------------------------------------------------------------------------
  299.   # ● 设置角色的各状态图
  300.   #--------------------------------------------------------------------------
  301.   def set_character_dir8(string, affix, default = "")
  302.     begin
  303.       @character_dir8[string.to_sym] = Cache.character(@character_name + affix)
  304.       @character.idle_anime = true if string.include?(IDLE4_AFFIX)
  305.     rescue
  306.       @character_dir8[string.to_sym] = @character_dir8[default.to_sym]
  307.     end
  308.   end
  309.   #--------------------------------------------------------------------------
  310.   # ● 更新源矩形
  311.   #--------------------------------------------------------------------------
  312.   def update_src_rect
  313.     return if @tile_id != 0
  314.     index = @character.character_index
  315.     frame = @character.is_multi_frames? ? @character.frame : 3
  316.     first = @character.is_multi_frames? ? 0 : 1
  317.     pattern = @character.pattern < frame ? @character.pattern : first
  318.     if (anime_symbol = "#{
  319.       @character.is_dash? && !$game_switches[DASH_ANIME_SW] ?
  320.       DASH4_AFFIX : @character.static_anime ? IDLE4_AFFIX : ''
  321.     }#{@character.direction.odd? ? DIRE8_AFFIX : ''}".to_sym) != @anime_symbol
  322.       @anime_symbol = anime_symbol
  323.       self.bitmap = @character_dir8[anime_symbol]
  324.     end if is_dir8?
  325.     sx = (index % 4 * frame + pattern) * @cw
  326.     if (dir = @character.direction).odd?
  327.       dir = [3, 7].include?(dir) && !is_dir8? ? 10 - dir : dir
  328.       sy = (index / 4 * 4 + (dir + 1) / 3) * @ch
  329.     else
  330.       sy = (index / 4 * 4 + (dir - 2) / 2) * @ch
  331.     end
  332.     self.src_rect.set(sx, sy, @cw, @ch)
  333.   end
  334.   #--------------------------------------------------------------------------
  335.   # ● 释放
  336.   #--------------------------------------------------------------------------
  337.   alias dir8_dispose dispose
  338.   def dispose
  339.     dir8_dispose
  340.     @character_dir8.each_value do |v|
  341.       v.dispose unless v.disposed?
  342.     end if @character_dir8
  343.   end
  344. end

无限の剣制

梦石
0
星屑
9956
在线时间
5019 小时
注册时间
2013-2-28
帖子
5030

开拓者贵宾

2
 楼主| 发表于 2018-11-8 17:26:42 | 只看该作者
本帖最后由 VIPArcher 于 2018-11-8 23:49 编辑

旧版丢失了Orz
回复 支持 反对

使用道具 举报

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-4-25 19:06

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表