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

Project1

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

按键事件

[复制链接]

…あたしは天使なんかじゃないわ

梦石
0
星屑
2207
在线时间
4033 小时
注册时间
2010-10-4
帖子
10779

开拓者贵宾

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

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

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

x
本帖最后由 taroxd 于 2014-11-28 18:18 编辑

RUBY 代码复制
  1. #--------------------------------------------------------------------------
  2. # ● require Taroxd基础设置
  3. #    按键事件
  4. #--------------------------------------------------------------------------
  5. module Taroxd end
  6.  
  7. class << Taroxd::InputEvent = Module.new
  8.   attr_accessor :event # 当前事件,作用类似于全局变量
  9. end
  10.  
  11. class Taroxd::InputEvent::Base
  12.   # 各属性与默认值列表
  13.   @options = {}
  14.   @option_blocks = {}
  15.  
  16.   # 事件是否成功
  17.   attr_accessor :result
  18.  
  19.   # 建立简单的事件,返回事件的结果
  20.   def self.main(*args)
  21.     Taroxd::InputEvent.event = event = new(*args)
  22.     event.main
  23.     Taroxd::InputEvent.event = nil
  24.     event.result
  25.   end
  26.  
  27.   # :call-seq:
  28.   # option opt1: default1, opt2: default2, ...
  29.   # option(:opt) { |option_name| value }
  30.   #
  31.   # 定义属性。
  32.   # 该属性会在实例生成时定义一个名称对应的实例变量,值为默认的值。
  33.   # 在调用可以通过 InputEvent.new(option: value) 的形式进行覆盖。
  34.   # 该方法包含了 attr_reader 的效果。
  35.   def self.option(name_pair, &block)
  36.     if block
  37.       attr_reader name_pair
  38.       option_blocks[name_pair] = block
  39.     else
  40.       attr_reader(*name_pair.keys)
  41.       options.merge!(name_pair)
  42.     end
  43.   end
  44.  
  45.   # 获取属性列表(默认值)
  46.   def self.options
  47.     @options ||= superclass.options.dup
  48.   end
  49.  
  50.   # 获取属性列表(default_proc)
  51.   def self.option_blocks
  52.     @option_blocks ||= superclass.option_blocks
  53.   end
  54.  
  55.   # 要按下的键
  56.   option key: :C
  57.  
  58.   # 值槽被填充的比例
  59.   option rate: 0
  60.  
  61.   # 是否显示
  62.   option visible: true
  63.  
  64.   # 显示位置(当然,你一定想要重定义)
  65.   option x: 0, y: 0, z: 0, width: Graphics.width, height: Graphics.height
  66.  
  67.   # 值槽方向
  68.   option vertical: false
  69.  
  70.   # 填充颜色
  71.   option color: Color.new(255, 255, 255)
  72.  
  73.   # :call-seq:
  74.   # InputEventClass.new([opt1: value1, opt2: value2, ... ]) -> event
  75.   #
  76.   # 生成实例。可选的 options 参数可以覆盖默认的设置。
  77.   def initialize(options = {})
  78.     self.class.options.each do |name, default|
  79.       instance_variable_set :"@#{name}", options.fetch(name, default)
  80.     end
  81.  
  82.     self.class.option_blocks.each do |name, default|
  83.       instance_variable_set :"@#{name}", options.fetch(name, &default)
  84.     end
  85.   end
  86.  
  87.   # 暂时以该事件的主逻辑代替场景
  88.   def main
  89.     update_scene while update
  90.   end
  91.  
  92.   # 更新事件。如果事件结束,返回 false,否则返回 true。
  93.   def update
  94.     hit? ? on_hit : on_not_hit
  95.     update_common
  96.     @result.nil?
  97.   end
  98.  
  99.   private
  100.  
  101.   # 事件调用 main 时,更新场景的方式。
  102.   def update_scene
  103.     SceneManager.scene.update_for_input_event
  104.   end
  105.  
  106.   # 是否按下按键。
  107.   def hit?
  108.     Input.trigger?(key)
  109.   end
  110.  
  111.   # 更新方式。在子类定义。
  112.   def on_hit; end
  113.   alias_method :on_not_hit, :on_hit
  114.   alias_method :update_common, :on_hit
  115.  
  116.   # 终止事件,并返回结果。
  117.   alias_method :terminate, :result=
  118. end
  119.  
  120. class Taroxd::InputEvent::Sprite < Sprite
  121.  
  122.   # 更新
  123.   def update
  124.     update_event_change
  125.     update_property if @event
  126.   end
  127.  
  128.   # 释放
  129.   def dispose
  130.     bitmap.dispose
  131.     super
  132.   end
  133.  
  134.   private
  135.  
  136.   # 获取当前事件
  137.   def event
  138.     Taroxd::InputEvent.event
  139.   end
  140.  
  141.   # 判断事件改变
  142.   def update_event_change
  143.     return if @event.equal?(event)
  144.     @event = event
  145.     update_bitmap
  146.   end
  147.  
  148.   # 事件改变时的刷新
  149.   def update_bitmap
  150.     bitmap.dispose if bitmap
  151.     return unless @event
  152.     self.bitmap = Bitmap.new(width, height)
  153.     bitmap.fill_rect(0, 0, width, height, @event.color)
  154.   end
  155.  
  156.   # 更新属性
  157.   def update_property
  158.     self.visible = @event.visible
  159.     update_position
  160.     update_src_rect
  161.   end
  162.  
  163.   # 更新位置
  164.   def update_position
  165.     self.x = @event.x
  166.     self.y = @event.y
  167.     self.z = @event.z
  168.   end
  169.  
  170.   # 更新值槽
  171.   def update_src_rect
  172.     if @event.vertical
  173.       src_rect.y = height * (1 - rate)
  174.       src_rect.height = height * rate
  175.     else
  176.       src_rect.width = width * rate
  177.     end
  178.   end
  179.  
  180.   # 总宽度。覆盖了父类的方法!
  181.   def width
  182.     @event.width
  183.   end
  184.  
  185.   # 总高度
  186.   def height
  187.     @event.height
  188.   end
  189.  
  190.   # 值槽填充程度
  191.   def rate
  192.     @event.rate
  193.   end
  194. end
  195.  
  196. class Scene_Base
  197.   # 对事件调用 main 时,场景的更新方式
  198.   def update_for_input_event
  199.     update_basic
  200.   end
  201. end
  202.  
  203. class Scene_Map < Scene_Base
  204.   # 对事件调用 main 时,场景的更新方式
  205.   def update_for_input_event
  206.     update_basic
  207.     @spriteset.update
  208.   end
  209. end
  210.  
  211. # 导入 Spriteset_Map
  212. Spriteset_Map.use_sprite(Taroxd::InputEvent::Sprite) { @viewport2 }
拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

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

GMT+8, 2024-5-1 16:04

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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