Last active
December 17, 2015 00:59
-
-
Save rutan/5525160 to your computer and use it in GitHub Desktop.
Tweenアニメーションさん for RGSS3
http://d.hatena.ne.jp/ru_shalm/20130506/rgss_tween
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
#=============================================================================== | |
# ■ Tweenアニメーションさん for RGSS3 | |
#------------------------------------------------------------------------------- | |
# 2013/05/11 Ru/むっくRu | |
#------------------------------------------------------------------------------- | |
# FlashなどではおなじみのTweenアニメーションを扱う機能をRGSS3に追加します。 | |
# 指定したプロパティを指定時間で変化させることが可能になります。 | |
# 詳細は解説記事をどうぞ。 | |
#------------------------------------------------------------------------------- | |
# 【注意】 | |
# このスクリプトはRGSSに機能を追加するものであり、 | |
# このスクリプトだけで何か効果があるわけではありません。 | |
#------------------------------------------------------------------------------- | |
# 【更新履歴】 | |
# 2013/05/11 メソッド名間違えていたのを修正 | |
# 2013/05/06 公開 | |
#------------------------------------------------------------------------------- | |
#=============================================================================== | |
# ↓ 以下、スクリプト部 ↓ | |
#=============================================================================== | |
module HZM_VXA | |
module Tween | |
#--------------------------------------------------------------------------- | |
# ■ 全Tweenアニメーションの管理 | |
#--------------------------------------------------------------------------- | |
module Manager | |
#------------------------------------------------------------------------- | |
# ● 初期化 | |
#------------------------------------------------------------------------- | |
def self.init | |
@list = [] | |
end | |
#------------------------------------------------------------------------- | |
# ● フレーム更新 | |
#------------------------------------------------------------------------- | |
def self.update | |
@list.delete_if do |animation| | |
animation.update_from_manager | |
!animation.play? | |
end | |
end | |
#------------------------------------------------------------------------- | |
# ● アニメーションの追加 | |
#------------------------------------------------------------------------- | |
def self.push(animation) | |
@list.push animation unless @list.include?(animation) | |
end | |
#------------------------------------------------------------------------- | |
# ● 再生中? | |
#------------------------------------------------------------------------- | |
def self.play? | |
@list.size > 0 | |
end | |
end | |
#--------------------------------------------------------------------------- | |
# ■ 各Tweenアニメーション用クラス | |
#--------------------------------------------------------------------------- | |
class Animation | |
#------------------------------------------------------------------------- | |
# ● 生成 | |
#------------------------------------------------------------------------- | |
def initialize(obj, *params) | |
@obj = obj | |
@animation_params = params | |
@playing = false | |
end | |
#------------------------------------------------------------------------- | |
# ● 再生開始 | |
#------------------------------------------------------------------------- | |
def play | |
@playing = true | |
@timer = 0 | |
@index = 0 | |
init_animation | |
Manager.push(self) | |
end | |
#------------------------------------------------------------------------- | |
# ● 再生停止 | |
#------------------------------------------------------------------------- | |
def stop | |
@playing = false | |
end | |
#------------------------------------------------------------------------- | |
# ● 再生中か | |
#------------------------------------------------------------------------- | |
def play? | |
@playing | |
end | |
#------------------------------------------------------------------------- | |
# ● 更新(Managerから呼び出される) | |
#------------------------------------------------------------------------- | |
def update_from_manager | |
# 停止中は処理しない | |
return unless @playing | |
# 再生時間の増加 | |
@timer += 1 | |
# 各要素を更新 | |
begin | |
@playing_params.each do |key, value| | |
n = @transition_method.call(@timer, value[1], value[2], @timer_max) | |
value[0].call(n.to_i) | |
end | |
rescue RGSSError | |
# 既に解放済みなので強制終了する | |
stop | |
return | |
end | |
# 再生が完了したら次のステップへ | |
unless @timer < @timer_max | |
@index += 1 | |
init_animation | |
end | |
end | |
private | |
#------------------------------------------------------------------------- | |
# ● アニメーション開始の準備 | |
#------------------------------------------------------------------------- | |
def init_animation | |
# 全アニメーションが終了していたら再生停止 | |
return stop unless @index < @animation_params.size | |
# 初期化 | |
@timer = 0.0 | |
# 再生時間の確認 | |
@timer_max = @animation_params[@index][:time].to_f | |
# トランジションメソッドの確認 | |
transition = @animation_params[@index][:transition] | |
transition = nil if transition == :default | |
if transition == nil | |
transition = Transition.default | |
elsif transition.class == Symbol | |
begin | |
transition = Transition.method_cache(transition) | |
rescue | |
transition = Transition.default | |
end | |
end | |
@transition_method = transition | |
# 各要素の初期値、ターゲット値を準備 | |
params = {} | |
@animation_params[@index].each do |key, value| | |
next if key == :time or key == :transition | |
method = @obj.method("#{key}=".to_sym) | |
start = @obj.method(key).call.to_i | |
change = value.to_i - start | |
params[key] = [method, start.to_f, change.to_f] | |
end | |
@playing_params = params | |
true | |
end | |
end | |
#--------------------------------------------------------------------------- | |
# ■ 標準トランジション用モジュール | |
# 新たに追加したくなった場合は、ここに追加する | |
#--------------------------------------------------------------------------- | |
# 参考元URL | |
# http://www.robertpenner.com/easing/ | |
# http://gizma.com/easing/ | |
#--------------------------------------------------------------------------- | |
module Transition | |
@@caches = {} | |
#------------------------------------------------------------------------- | |
# ● 標準トランジションの取得 | |
#------------------------------------------------------------------------- | |
def self.default | |
self.method_cache(:linear) | |
end | |
#------------------------------------------------------------------------- | |
# ● メソッドキャッシュの取得 | |
#------------------------------------------------------------------------- | |
def self.method_cache(name) | |
@@caches[name] ||= self.method(name) | |
end | |
#------------------------------------------------------------------------- | |
# ● Linear | |
#------------------------------------------------------------------------- | |
def self.linear(time, begin_value, change_value, duration) | |
begin_value + change_value * time / duration | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In Quad | |
#------------------------------------------------------------------------- | |
def self.ease_in_quad(time, begin_value, change_value, duration) | |
time /= duration | |
begin_value + change_value * time * time | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing Out Quad | |
#------------------------------------------------------------------------- | |
def self.ease_out_quad(time, begin_value, change_value, duration) | |
time /= duration | |
begin_value - change_value * time * (time - 2) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In/Out Quad | |
#------------------------------------------------------------------------- | |
def self.ease_in_out_quad(time, begin_value, change_value, duration) | |
time /= duration / 2 | |
change_value /= 2 | |
if time < 1 | |
begin_value + change_value * time * time | |
else | |
time -= 1 | |
begin_value - change_value * (time * (time - 2) - 1) | |
end | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In Cubic | |
#------------------------------------------------------------------------- | |
def self.ease_in_cubic(time, begin_value, change_value, duration) | |
time /= duration | |
begin_value + change_value * (time ** 3) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing Out Cubic | |
#------------------------------------------------------------------------- | |
def self.ease_out_cubic(time, begin_value, change_value, duration) | |
time /= duration | |
time -= 1 | |
begin_value + change_value * (time ** 3 + 1) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In/Out Cubic | |
#------------------------------------------------------------------------- | |
def self.ease_in_out_cubic(time, begin_value, change_value, duration) | |
time /= duration / 2 | |
change_value /= 2 | |
if time < 1 | |
begin_value + change_value * (time ** 3) | |
else | |
time -= 2 | |
begin_value + change_value * (time ** 3 + 2) | |
end | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In Quartic | |
#------------------------------------------------------------------------- | |
def self.ease_in_quart(time, begin_value, change_value, duration) | |
time /= duration | |
begin_value + change_value * (time ** 4) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing Out Quartic | |
#------------------------------------------------------------------------- | |
def self.ease_out_quart(time, begin_value, change_value, duration) | |
time /= duration | |
time -= 1 | |
begin_value - change_value * (time ** 4 - 1) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In/Out Quartic | |
#------------------------------------------------------------------------- | |
def self.ease_in_out_quart(time, begin_value, change_value, duration) | |
time /= duration / 2 | |
change_value /= 2 | |
if time < 1 | |
begin_value + change_value * (time ** 4) | |
else | |
time -= 2 | |
begin_value - change_value * (time ** 4 - 2) | |
end | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In Quintic | |
#------------------------------------------------------------------------- | |
def self.ease_in_quint(time, begin_value, change_value, duration) | |
time /= duration | |
begin_value + change_value * (time ** 5) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing Out Quintic | |
#------------------------------------------------------------------------- | |
def self.ease_out_quint(time, begin_value, change_value, duration) | |
time /= duration | |
time -= 1 | |
begin_value + change_value * (time ** 5 + 1) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In/Out Quintic | |
#------------------------------------------------------------------------- | |
def self.ease_in_out_quint(time, begin_value, change_value, duration) | |
time /= duration / 2 | |
change_value /= 2 | |
if time < 1 | |
begin_value + change_value * (time ** 5) | |
else | |
time -= 2 | |
begin_value + change_value * (time ** 5 + 2) | |
end | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In Sine | |
#------------------------------------------------------------------------- | |
def self.ease_in_sine(time, begin_value, change_value, duration) | |
time /= duration | |
begin_value + change_value * (1 - Math.cos(time * (Math::PI / 2))) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing Out Sine | |
#------------------------------------------------------------------------- | |
def self.ease_out_sine(time, begin_value, change_value, duration) | |
time /= duration | |
begin_value + change_value * Math.sin(time * (Math::PI / 2)) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In/Out Sine | |
#------------------------------------------------------------------------- | |
def self.ease_in_out_sine(time, begin_value, change_value, duration) | |
time /= duration | |
begin_value - change_value / 2 * (Math.cos(Math::PI * time) - 1) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In Exponential | |
#------------------------------------------------------------------------- | |
def self.ease_in_expo(time, begin_value, change_value, duration) | |
time /= duration | |
begin_value + change_value * (2 ** (10 * (time - 1))) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing Out Exponential | |
#------------------------------------------------------------------------- | |
def self.ease_out_expo(time, begin_value, change_value, duration) | |
time /= duration | |
begin_value + change_value * (1 - (2 ** (-10 * time))) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In/Out Exponential | |
#------------------------------------------------------------------------- | |
def self.ease_in_out_expo(time, begin_value, change_value, duration) | |
time /= duration / 2 | |
change_value /= 2 | |
if time < 1 | |
begin_value + change_value * (2 ** (10 * (time - 1))) | |
else | |
begin_value + change_value * (-2 ** (10 * (time - 1)) + 2) | |
end | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In Circular | |
#------------------------------------------------------------------------- | |
def self.ease_in_circ(time, begin_value, change_value, duration) | |
time /= duration | |
begin_value - change_value * (Math.sqrt(1 - time * time) - 1) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing Out Circular | |
#------------------------------------------------------------------------- | |
def self.ease_out_circ(time, begin_value, change_value, duration) | |
time /= duration | |
time -= 1 | |
begin_value + change_value * Math.sqrt(1 - time * time) | |
end | |
#------------------------------------------------------------------------- | |
# ● Easing In/Out Circular | |
#------------------------------------------------------------------------- | |
def self.ease_in_out_circ(time, begin_value, change_value, duration) | |
time /= duration / 2 | |
change_value /= 2 | |
if (time < 1) | |
begin_value - change_value * (Math.sqrt(1 - time * time) - 1) | |
else | |
begin_value + change_value * (Math.sqrt(1 - time * time) + 1) | |
end | |
end | |
end | |
end | |
end | |
#=============================================================================== | |
# ■ SceneManager | |
#=============================================================================== | |
class << SceneManager | |
#----------------------------------------------------------------------------- | |
# ● 実行(エイリアス) | |
#----------------------------------------------------------------------------- | |
alias hzm_vxa_tween_run run | |
def run | |
HZM_VXA::Tween::Manager.init | |
hzm_vxa_tween_run | |
end | |
end | |
#=============================================================================== | |
# ■ Scene_Base | |
#=============================================================================== | |
class Scene_Base | |
#----------------------------------------------------------------------------- | |
# ● フレーム更新(基本)(エイリアス) | |
#----------------------------------------------------------------------------- | |
alias hzm_vxa_tween_update_basic update_basic | |
def update_basic | |
HZM_VXA::Tween::Manager.update | |
hzm_vxa_tween_update_basic | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment