pigu_6

Jet| Animated battlers

Nov 24th, 2012
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.19 KB | None | 0 0
  1. #===============================================================================
  2. # Animated Battlers
  3. # By Jet10985 (Jet)
  4. #===============================================================================
  5. # This script will allow the use of spritesheets to represent enemies and actors
  6. # inside the battle scene.
  7. # This script has: 14 customization options.
  8. #===============================================================================
  9. # Overwritten Methods:
  10. # None
  11. #-------------------------------------------------------------------------------
  12. # Aliased methods:
  13. # Cache: load_bitmap
  14. # Game_Battler: initialize, on_damage
  15. # BattleManager: process_victory, process_defeat, battle_start
  16. # Sprite_Battler: initialize, update, update_origin, start_effect,
  17. # revert_to_normal
  18. # Sprite: update
  19. #===============================================================================
  20. =begin
  21. If you need to set up a custom animated sprite for a specific actor or enemy,
  22. you need to use these inside the character's notebox:
  23.  
  24. <anim battler: name> (This sets the name of the animated spritesheet to use)
  25.  
  26. The above is the only needed notetag to use a custom spritesheet, but if you
  27. need to go against default configuration below as well, you may use these:
  28.  
  29. <anim speed: speed> (This sets the speed at which the animation changes frames)
  30. <anim frames: frames> (This sets how many frames there are in a single row)
  31. <anim rows: rows> (This sets how many rows there are in the spritesheet)
  32. --------------------------------------------------------------------------------
  33. Byd efault, all states except being dead will set the row to be the one
  34. set for :state, but if you need a state which does not change the current row,
  35. use this in the state's notebox:
  36.  
  37. <anim no pose>
  38. --------------------------------------------------------------------------------
  39. To have an enemy follow the opposite of the below FLIP_ENEMIES config, use this
  40. notetag:
  41.  
  42. <anim flip>
  43.  
  44. Putting this in an actor's notebox will always cause the actor's sprite to be
  45. flipped.
  46. --------------------------------------------------------------------------------
  47. You can change an enemy's attack animation by using this notetag:
  48.  
  49. <atk anim: id>
  50. --------------------------------------------------------------------------------
  51. If you need to have a special sheet for when the character is a specific class,
  52. use this tag in the class notebox:
  53.  
  54. <anim suffix: suffix>
  55.  
  56. This will make the script look for whatever their default sheet would be,
  57. but with suffix at the end. So if the tag was <anim suffix: _paladin> and the
  58. base sheet was "Jimmy", the script would look for "Jimmy_paladin"
  59. --------------------------------------------------------------------------------
  60. If you need to have a specific row for when the character has a specific state,
  61. use this tag in the state notebox:
  62.  
  63. <anim row: row>
  64.  
  65. This will make it so if this state is inflicted, the character's row will be
  66. that of whatever the row is in the notetag.
  67. If multiple states have a notetag, and are inflicted, the state with the highest
  68. "Display Priority" will be used.
  69. --------------------------------------------------------------------------------
  70. By default, this is what the POSE_ROWS configuration correlates to in choosing
  71. which row of the spritesheet to show. This uses a format which Holder uses in
  72. their animated battlers by default, and this is taken directly from them:
  73.  
  74. 1. Idle - Waiting.
  75. 2. Defend - Guarding.
  76. 3. Woozy - Under status effect. (Also low hp value)
  77. 4. Struck - Being hit. (Taking damage)
  78. 5. Attack - Striking the target.
  79. 6. Item - Using an item.
  80. 7. Skill - Using a technique.
  81. 8. Magic - Casting a spell.
  82. 9. Advance - Moving forward.
  83. 10. Retreat - Moving away. (From target or fleeing from battle)
  84. 11. Victory - Battle end.
  85. 12. Enter - Battle start.
  86. 13. Dead - Corpse. (Dead-dood-ded)
  87. 14. Credits - Information about the graphic. (Not shown in game)
  88. =end
  89.  
  90. module Jet
  91. module AnimatedBattlers
  92.  
  93. # This is how many rows on poses there are in a default spritesheet.
  94. # The height of each row is determined by dividing the animation
  95. # bitmap height by this.
  96. ANIMATION_ROWS = 14
  97.  
  98. # This is how many frames are in a row/pose.
  99. # This width of each frame is determined by dividing the animation
  100. # bitmap width by this.
  101. ANIMATION_FRAMES = 4
  102.  
  103. # This is how long it takes in frames to change which frame is displayed
  104. # on the current pose.
  105. ANIMATION_SPEED = 10
  106.  
  107. # This is the suffix added to the character's name to try to find a default
  108. # animated spritesheet. If an enemy's name was "Slime", this script will
  109. # first scan the note for a specific spritesheet, and if it does not find
  110. # it, this script will then search for a file by the enemy's name combined
  111. # with this suffix. If the suffix was "_animated", it'd look for
  112. # "Sline_animated"
  113. ANIM_SUFFIX = "_animated"
  114.  
  115. # Do you want to flip enemy's spritesheets by default?
  116. FLIP_ENEMIES = true
  117.  
  118. # Don't touch.
  119. PLAYER_SPOTS = []
  120.  
  121. # This is where an actor's battler is displayed on the screen.
  122. # You can add one for each actor by following this format:
  123. # PLAYER_SPOTS[party_index] = [x, y]
  124. PLAYER_SPOTS[0] = [315, 50]
  125. PLAYER_SPOTS[1] = [335, 80]
  126. PLAYER_SPOTS[2] = [355, 110]
  127. PLAYER_SPOTS[3] = [375, 140]
  128.  
  129. # Do you want the battlers to move to the target when attacking?
  130. MOVE_TO_TARGET = true
  131.  
  132. # This is the amount of time it takes to move an animated battler to their
  133. # target during a physical attack. This is in seconds.
  134. TIME_TO_MOVE = 0.5
  135.  
  136. # This is a switch to turn animated battlers off and to revert to normal.
  137. TURN_OFF = 0
  138.  
  139. # This is the percent of hp a battler's current hp has to be below to be
  140. # considered "critical condition"
  141. CRIT_PERC = 25
  142.  
  143. # What is an enemy's default attack animation id?
  144. ENEMY_DEF_ANIM = 1
  145.  
  146. # This is the harder config, fair warning.
  147. # By default, these options work with Holder's animated battlers.
  148. # These are used to determine which condition goes to which row on
  149. # the sheet.
  150. # :symbol => row_id
  151. POSE_ROWS = {
  152.  
  153. :idle => 0,
  154. :guard => 1,
  155. :state => 2,
  156. :damage => 3,
  157. :attack => 4,
  158. :item => 5,
  159. :skill => 6,
  160. :magic => 7,
  161. :advance => 8,
  162. :retreat => 9,
  163. :victory => 10,
  164. :begin => 11,
  165. :dead => 12,
  166. :credits => 13
  167.  
  168. }
  169.  
  170. #---------------------------------------------------------------------------
  171. # This is the most difficult and optional config.
  172. # You may set specific amounts of frames, and speed for each row of
  173. # specific spritesheets.
  174. # "SheetName" => {row_index => [frames, speed], next_row => [frames, speed]}
  175. # row_index is which row you're configuring
  176. # frames is how many frames there are on this row
  177. # speed if how long it takes to make from one frame to the other.
  178. #---------------------------------------------------------------------------
  179. ROW_CONFIG = {
  180.  
  181. "ExampleSheet" => {
  182. 0 => [14, 10],
  183. 1 => [7, 10]
  184. },
  185. "ExampleSheet_paladin" => {
  186. 8 => [27, 20],
  187. 9 => [3, 5]
  188. }
  189. }
  190.  
  191. # This is the default for rows for every sheet if you need to change them.
  192. # It follows the format of the second hash in the above config.
  193. # row_index => [frames, speed]
  194. ROW_HASHES = {}
  195.  
  196. # Don't touch these.
  197. ROW_HASHES.default = [ANIMATION_FRAMES, ANIMATION_SPEED]
  198. ROW_CONFIG.default = ROW_HASHES
  199.  
  200. end
  201. end
  202.  
  203. #===============================================================================
  204. # DON'T EDIT FURTHER UNLESS YOU KNOW WHAT TO DO.
  205. #===============================================================================
  206.  
  207. ($imported ||= {})[:jet] ||= {}
  208. $imported[:jet][:AnimatedBattlers] = true
  209.  
  210. class Game_Actor
  211.  
  212. def screen_x
  213. Jet::AnimatedBattlers::PLAYER_SPOTS[self.index][0] rescue 0
  214. end
  215.  
  216. def screen_y
  217. Jet::AnimatedBattlers::PLAYER_SPOTS[self.index][1] rescue 0
  218. end
  219.  
  220. def screen_z
  221. 100
  222. end
  223.  
  224. def battler_name
  225. self.actor.name
  226. end
  227. end
  228.  
  229. class Game_Enemy
  230.  
  231. def atk_animation_id1
  232. begin
  233. self.enemy.note.match(/<atk[ ]*anim[ ]*(\:|=)[ ]*(\d+)/i)[2]
  234. rescue
  235. Jet::AnimatedBattlers::ENEMY_DEF_ANIM
  236. end
  237. end
  238.  
  239. def atk_animation_id2
  240. 0
  241. end
  242. end
  243.  
  244. class << Cache
  245.  
  246. alias jet3746_load_bitmap load_bitmap
  247. def load_bitmap(*args, &block)
  248. (jet3746_load_bitmap(*args, &block)).dup
  249. end
  250. end
  251.  
  252. class Game_Battler
  253.  
  254. attr_accessor :animation_row
  255. attr_accessor :animation_delay
  256.  
  257. alias jet1824_initialize initialize
  258. def initialize(*args, &block)
  259. @animation_row = Jet::AnimatedBattlers::POSE_ROWS[:idle]
  260. @animation_delay = 0
  261. jet1824_initialize(*args, &block)
  262. end
  263.  
  264. def animated?
  265. false
  266. end
  267.  
  268. def full_frame_time
  269. self.animation_frames * self.animation_speed
  270. end
  271.  
  272. alias jet3745_on_damage on_damage
  273. def on_damage(*args, &block)
  274. jet3745_on_damage(*args, &block)
  275. if !dead?
  276. @animation_row = guard? ? @animation_row : Jet::AnimatedBattlers::POSE_ROWS[:damage]
  277. @animation_delay = self.full_frame_time
  278. end
  279. end
  280.  
  281. def pose_state?
  282. states.each {|a|
  283. return true unless a.id == 1 || !(a.note =~ /<anim[ ]*no[ ]*pose>/i).nil?
  284. }
  285. return true if self.mhp / self.hp.to_f >= Jet::AnimatedBattlers::CRIT_PERC
  286. return false
  287. end
  288.  
  289. def state_row
  290. sort_states
  291. states.each {|a|
  292. if !(a.note =~ /<anim[ ]*row[ ]*(\:|=)[ ]*(\d+)>/i).nil?
  293. return $2.to_i
  294. end
  295. }
  296. return Jet::AnimatedBattlers::POSE_ROWS[:state]
  297. end
  298.  
  299. def battle_sprite
  300. return nil unless SceneManager.scene_is?(Scene_Battle)
  301. SceneManager.scene.spriteset.battler_sprites.each {|a|
  302. return a if a.battler == self
  303. }
  304. return nil
  305. end
  306.  
  307. def anim_reset
  308. @animation_row = Jet::AnimatedBattlers::POSE_ROWS[:idle]
  309. @animation_delay = 0
  310. @animated = nil
  311. @anim_rows = nil
  312. @anim_speed = nil
  313. @anim_frames = nil
  314. @anim_bitmap = nil
  315. end
  316. end
  317.  
  318. class << BattleManager
  319.  
  320. alias jet8335_battle_start battle_start
  321. def battle_start(*args, &block)
  322. ($game_party.alive_members + $game_troop.alive_members).each {|a|
  323. a.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:begin]
  324. a.animation_delay = a.full_frame_time
  325. }
  326. jet8335_battle_start(*args, &block)
  327. end
  328.  
  329. alias jet2834_process_victory process_victory
  330. def process_victory(*args, &block)
  331. $game_party.alive_members.each {|a|
  332. a.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:victory]
  333. a.animation_delay = -1
  334. }
  335. jet2834_process_victory(*args, &block)
  336. end
  337.  
  338. alias jet3745_process_defeat process_defeat
  339. def process_defeat(*args, &block)
  340. $game_troop.alive_members.each {|a|
  341. a.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:victory]
  342. a.animation_delay = -1
  343. }
  344. jet3745_process_defeat(*args, &block)
  345. end
  346. end
  347.  
  348. %w[enemy actor].each {|a|
  349. aStr = %Q{
  350.  
  351. class Game_#{a.capitalize}
  352.  
  353. def animated?
  354. @animated ||= (
  355. note = !(/<anim[ ]*battler[ ]*(\\:|=)[ ]*(.+)>/i =~ #{a}.note).nil?
  356. name = self.battler_name + Jet::AnimatedBattlers::ANIM_SUFFIX
  357. graph = (!Cache.battler(name, self.battler_hue).nil? rescue false)
  358. note || graph)
  359. end
  360.  
  361. def animation_rows
  362. @anim_rows ||= (
  363. if /<anim[ ]*rows[ ]*(\\:|=)[ ]*(\\d+)/i =~ #{a}.note
  364. $2.to_i
  365. else
  366. Jet::AnimatedBattlers::ANIMATION_ROWS
  367. end)
  368. end
  369.  
  370. def animation_speed
  371. @anim_speed ||= (
  372. if /<anim[ ]*speed[ ]*(\\:|=)[ ]*(\\d+)/i =~ #{a}.note
  373. $2.to_i
  374. else
  375. Jet::AnimatedBattlers::ROW_CONFIG[self.animation_bitmap][@animation_row][1]
  376. end)
  377. end
  378.  
  379. def animation_frames
  380. @anim_frames ||= (
  381. if /<anim[ ]*frames[ ]*(\\:|=)[ ]*(\\d+)/i =~ #{a}.note
  382. $2.to_i
  383. else
  384. Jet::AnimatedBattlers::ROW_CONFIG[self.animation_bitmap][@animation_row][0]
  385. end)
  386. end
  387.  
  388. def animation_bitmap_name
  389. @anim_bitmap ||= (
  390. begin
  391. self.#{a}.note.match(/<anim[ ]*battler[ ]*(\\:|=)[ ]*(.+)>/i)[2]
  392. rescue
  393. self.battler_name + Jet::AnimatedBattlers::ANIM_SUFFIX
  394. end)
  395. end
  396.  
  397. def animation_bitmap
  398. actor_bitmap = self.animation_bitmap_name
  399. #{
  400. if a == "actor"
  401. "class_suffix = (self.class.note.match(
  402. /<anim[ ]*suffix[ ]*(\:|=)[ ]*(.+)>/i)[2] rescue '')"
  403. else
  404. "class_suffix = ''"
  405. end
  406. }
  407. actor_bitmap + (class_suffix || "")
  408. end
  409. end
  410. }
  411. eval(aStr)
  412. }
  413.  
  414. class Sprite_Battler
  415.  
  416. attr_reader :animated
  417. attr_accessor :anim_pause
  418.  
  419. alias jet2734_initialize initialize
  420. def initialize(viewport, battler = nil)
  421. @animated = (!battler.nil? && battler.animated?) ? true : false
  422. jet2734_initialize(viewport, battler)
  423. end
  424.  
  425. def battler=(new_battler)
  426. @animated = (!new_battler.nil? && new_battler.animated?) ? true : false
  427. @battler = new_battler
  428. end
  429.  
  430. alias jet8234_update update
  431. def update(*args, &block)
  432. @animated = ([email protected]? && @battler.animated?) ? true : false
  433. @animated = false if $game_switches[Jet::AnimatedBattlers::TURN_OFF]
  434. @animated ? (super; animated_update) : jet8234_update(*args, &block)
  435. end
  436.  
  437. def animated_update
  438. if @old_battler != @battler
  439. self.bitmap = Cache.battler(@battler.animation_bitmap, @battler.battler_hue)
  440. @old_battler = @battler
  441. @use_sprite = true
  442. init_visibility
  443. if @battler.enemy?
  444. self.mirror = Jet::AnimatedBattlers::FLIP_ENEMIES
  445. if /<anim[ ]*flip>/i =~ @battler.enemy.note
  446. self.mirror = !self.mirror
  447. end
  448. elsif @battler.actor?
  449. self.mirror = !(/<anim[ ]*flip>/i =~ @battler.actor.note).nil?
  450. end
  451. end
  452. #------------------------
  453. update_origin
  454. update_position unless @in_anim_move
  455. setup_new_effect
  456. setup_new_animation
  457. update_effect
  458. update_battler_pose
  459. #------------------------
  460. width = self.bitmap.width
  461. height = self.bitmap.height
  462. if @anim_row != @battler.animation_row
  463. @anim_frame = 0
  464. @frame_count = 0
  465. end
  466. @anim_row = @battler.animation_row
  467. width = self.bitmap.width / @battler.animation_frames
  468. height = self.bitmap.height / @battler.animation_rows
  469. x = @anim_frame * width
  470. y = @battler.animation_row * height
  471. self.src_rect.set(x, y, width, height)
  472. @frame_count += 1
  473. @anim_pause ||= false
  474. if @frame_count >= @battler.animation_speed
  475. @anim_frame += 1
  476. @frame_count = 0
  477. if @anim_frame >= @battler.animation_frames
  478. @anim_frame = @anim_pause ? (@battler.animation_frames - 1) : 0
  479. end
  480. end
  481. end
  482.  
  483. def update_battler_pose
  484. if @battler.animation_delay == -1
  485. return
  486. end
  487. if @battler.animation_delay > 0
  488. @battler.animation_delay -= 1
  489. return
  490. end
  491. if @battler.dead?
  492. @battler.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:dead]
  493. elsif @battler.guard?
  494. @battler.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:guard]
  495. elsif @battler.pose_state?
  496. @battler.animation_row = @battler.state_row
  497. else
  498. @battler.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:idle]
  499. end
  500. end
  501.  
  502. alias jet7345_update_origin update_origin
  503. def update_origin(*args, &block)
  504. if @animated
  505. self.ox = @battler.enemy? ? self.src_rect.width / 2 : 0
  506. self.oy = @battler.enemy? ? self.src_rect.height / 2 : 0
  507. else
  508. jet7345_update_origin(*args, &block)
  509. end
  510. end
  511.  
  512. alias jet2734_revert_to_normal revert_to_normal
  513. def revert_to_normal(*args, &block)
  514. jet2734_revert_to_normal(*args, &block)
  515. if @animated
  516. self.ox = @battler.enemy? ? self.src_rect.width / 2 : 0
  517. self.oy = @battler.enemy? ? self.src_rect.height / 2 : 0
  518. end
  519. end
  520.  
  521. alias jet8344_start_effect start_effect
  522. def start_effect(effect)
  523. return if effect == :collapse && @animated
  524. jet8344_start_effect(effect)
  525. end
  526.  
  527. def anim_move_to(target)
  528. self.z = 101
  529. return unless Jet::AnimatedBattlers::MOVE_TO_TARGET
  530. min_x = target.x - target.src_rect.width / 2 + self.src_rect.width / 2
  531. min_x -= self.src_rect.width / 3
  532. max_x = target.x + target.src_rect.width / 2 - self.src_rect.width / 2
  533. max_x += self.src_rect.width / 3
  534. @orig_x ||= self.x
  535. @orig_y ||= self.y
  536. if self.x > min_x && @orig_x > min_x
  537. x = min_x
  538. elsif self.x < max_x && @orig_x < min_x
  539. x = max_x
  540. else
  541. x = self.x
  542. end
  543. if self.y > target.y + target.src_rect.height / 2
  544. y = target.y + target.src_rect.height / 2
  545. elsif self.y < target.y - target.src_rect.height / 2
  546. y = target.y - target.src_rect.height / 2
  547. else
  548. y = self.y
  549. end
  550. if x > self.x
  551. @battler.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:retreat]
  552. else
  553. @battler.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:advance]
  554. end
  555. @battler.animation_delay = -1
  556. @in_anim_move = true
  557. do_tween(x, y, Tween::Linear, Jet::AnimatedBattlers::TIME_TO_MOVE)
  558. until @tween.nil?
  559. [SceneManager.scene.spriteset, Graphics].each {|a| a.update }
  560. end
  561. end
  562.  
  563. def anim_return
  564. self.z = 100
  565. return unless Jet::AnimatedBattlers::MOVE_TO_TARGET
  566. @orig_x = nil
  567. @orig_y = nil
  568. x = @battler.screen_x
  569. y = @battler.screen_y
  570. if x >= self.x
  571. @battler.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:retreat]
  572. else
  573. @battler.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:advance]
  574. end
  575. @battler.animation_delay = -1
  576. do_tween(x, y, Tween::Linear, Jet::AnimatedBattlers::TIME_TO_MOVE)
  577. until @tween.nil?
  578. [SceneManager.scene.spriteset, Graphics].each {|a| a.update }
  579. end
  580. @in_anim_move = false
  581. @battler.animation_delay = 0
  582. end
  583. end
  584.  
  585. class Scene_Battle
  586.  
  587. attr_reader :spriteset
  588.  
  589. alias jet3845_show_normal_animation show_normal_animation
  590. def show_normal_animation(targets, animation_id, mirror = false)
  591. if !$game_switches[Jet::AnimatedBattlers::TURN_OFF]
  592. animation = $data_animations[animation_id]
  593. if animation
  594. did_to_screen = false
  595. targets.each do |target|
  596. next if did_to_screen
  597. @subject.animation_delay = 0
  598. @subject.battle_sprite.anim_pause = false
  599. obj = @subject.current_action.item
  600. if @subject.battle_sprite.animated
  601. if obj.physical?
  602. @subject.battle_sprite.anim_move_to(target.battle_sprite)
  603. end
  604. end
  605. target.animation_id = animation_id
  606. target.animation_mirror = mirror
  607. did_to_screen = animation.to_screen?
  608. case obj
  609. when RPG::Item
  610. @subject.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:item]
  611. when RPG::Skill
  612. if obj.id == @subject.attack_skill_id
  613. @subject.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:attack]
  614. elsif obj.magical?
  615. @subject.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:magic]
  616. elsif obj.id != @subject.guard_skill_id
  617. @subject.animation_row = Jet::AnimatedBattlers::POSE_ROWS[:skill]
  618. end
  619. end
  620. @subject.animation_delay = -1
  621. @subject.battle_sprite.anim_pause = true
  622. abs_wait_short unless animation.to_screen?
  623. end
  624. abs_wait_short if animation.to_screen?
  625. end
  626. else
  627. jet3845_show_normal_animation(*args, &block)
  628. end
  629. end
  630.  
  631. alias jet3745_terminate terminate
  632. def terminate(*args, &block)
  633. jet3745_terminate(*args, &block)
  634. $game_party.members.each {|a| a.anim_reset }
  635. end
  636.  
  637. alias jet3434_use_item use_item
  638. def use_item(*args, &block)
  639. jet3434_use_item(*args, &block)
  640. @subject.animation_delay = 0
  641. @subject.battle_sprite.anim_pause = false
  642. if @subject.battle_sprite.animated
  643. if @subject.current_action.item.physical?
  644. @subject.battle_sprite.anim_return
  645. end
  646. end
  647. end
  648.  
  649. alias jet3345_show_attack_animation show_attack_animation
  650. def show_attack_animation(targets)
  651. if !$game_switches[Jet::AnimatedBattlers::TURN_OFF]
  652. show_normal_animation(targets, @subject.atk_animation_id1, false)
  653. show_normal_animation(targets, @subject.atk_animation_id2, true)
  654. else
  655. jet3345_show_attack_animation(*args, &block)
  656. end
  657. end
  658. end
  659.  
  660. class Sprite
  661.  
  662. attr_reader :tween
  663.  
  664. def calculate_delta
  665. @this_frame = Time.now
  666. @delta = ((@this_frame - @last_frame) * 1000.0).to_i / 1000.0
  667. @last_frame = @this_frame
  668. end
  669.  
  670. def do_tween(new_x, new_y, style = Tween::Linear, time = 1)
  671. @last_frame = Time.now
  672. @tween = Tween.new([self.x.to_f, self.y.to_f], [new_x.to_f, new_y.to_f],
  673. style, time)
  674. end
  675.  
  676. alias jet2724_update update unless $@
  677. def update(*args, &block)
  678. jet2724_update(*args, &block)
  679. calculate_delta
  680. @tween.update(@delta)
  681. self.x, self.y = @tween.x, @tween.y
  682. @tween = nil if @tween.done
  683. end
  684. end
  685. end
  686.  
  687. class Tween
  688. attr_reader :done
  689. attr_reader :easer
  690.  
  691. def initialize(start, finish, easer, duration)
  692. @start, @finish = start, finish
  693. @easer, @duration = easer, duration
  694. unless @start.is_a? Enumerable
  695. @start = [@start]
  696. end
  697. unless @finish.is_a? Enumerable
  698. @finish = [@finish]
  699. end
  700. @time = 0
  701. @done = false
  702. end
  703.  
  704. def update(delta)
  705. @time += delta
  706. if @time > @duration
  707. @time = @duration
  708. @done = true
  709. end
  710. end
  711.  
  712. def [](idx)
  713. @easer.ease(
  714. @time,
  715. @start[idx],
  716. (@finish[idx] - @start[idx]),
  717. @duration
  718. )
  719. end
  720.  
  721. def value; self[0]; end
  722. def x; self[0]; end
  723. def y; self[1]; end
  724. def z; self[2]; end
  725.  
  726. module Linear
  727. def self.ease(t, st, ch, d)
  728. ch * t / d + st
  729. end
  730. end
  731. end
Advertisement
Add Comment
Please, Sign In to add comment