TheoAllen

Theo - Core Movement module

Apr 26th, 2013
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.02 KB | None | 0 0
  1. # =============================================================================
  2. # THEOALLEN - WINDOW AND SPRITE CORE MOVEMENT
  3. # VERSION : 1.17
  4. # CONTACT : www.rpgmakerid.com
  5. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. $imported = {} if $imported.nil?
  8. $imported[:Theo_Movement] = true
  9. # =============================================================================
  10. # CHANGE LOGS:
  11. # -----------------------------------------------------------------------------
  12. # - 2013.04.29 - Nambahin preset movement
  13. # - 2013.04.28 - Fixed bug di update movement
  14. # - 2013.04.27 - Script selesai
  15. # =============================================================================
  16. =begin
  17.  
  18.   PENGENALAN :
  19.   Script ini ngebikin sebuah object dengan class Window atau Sprite bisa
  20.   digerakkan dengan memanggil method goto dan slide. Script ini digunain
  21.   buat devtoolnya para scripter. Jadi butuh implementasi. Bukan cuman sekedar
  22.   cucuk dan main (plug n play) :v
  23.  
  24.   CARA PENGGUNAAN:
  25.   Tulis dalam script seperti ini :
  26.   (note1: object harus sebuah sprite atau window)
  27.   (note2: duration dalam hitungan frame (60 = 1 detik))
  28.  
  29.   - object.goto(x,y) >> mindah object ke koordinat x,y dengan waktu satu detik
  30.   - object.goto(x,y,dur) >> mindah object dengan waktu yg ditentuin
  31.   - object.goto(x,y,dur,jump) >> mindah object cuman ditambahin loncat  
  32.    
  33.   - object.slide(x,y) >> geser object sebanyak x,y
  34.   - object.slide(x,y,dur) >> geser object dengan waktu yg dah ditentuin
  35.   - object.slide(x,y,dur,jump) >> geser object dengan loncat
  36.  
  37.   -object.moving? >> ngecek lagi bergerak apa kaga
  38.  
  39.   PRESET MOVEMENT:
  40.   - object.up(range[,dur,jump])         >> geser ke atas
  41.   - object.down(range[,dur,jump])       >> geser ke bawah
  42.   - object.right(range[,dur,jump])      >> geser ke kanan
  43.   - object.left(range[,dur,jump])       >> geser ke kiri
  44.   - object.upright(range[,dur,jump])    >> geser ke kanan atas
  45.   - object.upleft(range[,dur,jump])     >> geser ke kiri atas
  46.   - object.downright(range[,dur,jump])  >> geser ke kanan bawah
  47.   - object.downleft(range[,dur,jump])   >> geser ke kiri bawah
  48.  
  49.   TERMS OF USE :
  50.   Karena ini emang cuman buat tool para scripter, silahkan2 aja kalo mo dipake.
  51.   Mau komersil juga boleh. Btw, kalo ada ide trus mo ngembangil ni modul
  52.   monggo terserah. Gw mah dmen banget kalo ada yg mo ngembangin script gw yg
  53.   satu ini. Tapi tetep kredit ya? \=w=/
  54.  
  55. =end
  56. # =============================================================================
  57. # Module THEO::Movement (diimplement ke window dan sprite)
  58. # =============================================================================
  59. module THEO
  60.   module Movement
  61.     # =========================================================================
  62.     # Class Move_Object ~
  63.     # -------------------------------------------------------------------------
  64.     # Class untuk ngehandle perpindahan object
  65.     # Tiap satu object sprite ato window punya satu object dengan class ini
  66.     # =========================================================================
  67.     class Move_Object
  68.       attr_reader :obj # <-- Sprite atau Window
  69.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  70.       # inisialisasi object
  71.       # -----------------------------------------------------------------------
  72.       # parameter obj adalah pointer menuju object aslinya, yaitu :
  73.       # Sprite atau Window yang udah diinstant
  74.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  75.       def initialize(obj)
  76.         @obj = obj
  77.         clear_move_info
  78.       end
  79.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  80.       # Bersihin info pergerakan
  81.       # -----------------------------------------------------------------------
  82.       # @to_x =>> koordinat tujuan x
  83.       # @to_y =>> koordinat tujuan y
  84.       # @real_x =>> posisi (asli) x object dalam bentuk float number
  85.       # @real_y =>> posisi (asli) y object dalam bentuk float number
  86.       # @x_speed =>> perpindahan posisi x per-frame
  87.       # @y_speed =>> perpindahan posisi y per-frame
  88.       # @jump =>> kekuatan loncat keatas
  89.       # @jump_interval =>> nilai untuk pengurangan kecepatan @jump per-frame
  90.       # @offset =>> nilai pelencengan y object dari garis vektor
  91.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  92.       def clear_move_info
  93.         @to_x = -1
  94.         @to_y = -1
  95.         @real_x = 0.0
  96.         @real_y = 0.0
  97.         @x_speed = 0.0
  98.         @y_speed = 0.0
  99.         @jump = 0.0
  100.         @jump_interval = 0.0
  101.         @offset = 0
  102.       end
  103.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  104.       # Ngeset pergerakan menuju x, y, kekuatan loncat dan durasi (frame)
  105.       # -----------------------------------------------------------------------
  106.       # x         = koordinat tujuan x
  107.       # y         = koordinat tujuan x
  108.       # jump      = kekuatan loncat
  109.       # duration  = lama perjalanan (60 = satu detik)
  110.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  111.       def move_to(x,y,jump,duration)
  112.         @to_x = x
  113.         @to_y = y
  114.         @real_x = @obj.x.to_f
  115.         @real_y = @obj.y.to_f
  116.         determine_speed(duration,jump)
  117.       end
  118.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  119.       # Penentuan perubahan nilai per-frame / kecepatan gerak
  120.       # -----------------------------------------------------------------------
  121.       # speed = jarak / waktu (u dont say)
  122.       # jump_interval = kekuatan loncat / setengah dari perjalanan
  123.       # (asumsi setelah setengah perjalanan akan turun)
  124.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  125.       def determine_speed(duration,jump)
  126.         @x_speed = (@to_x - @obj.x) / duration.to_f
  127.         @y_speed = (@to_y - @obj.y) / duration.to_f
  128.         @jump = jump.to_f
  129.         @jump_interval = @jump/(duration/2.0)
  130.       end
  131.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  132.       # Cek lagi bergerak apa kaga?
  133.       # -----------------------------------------------------------------------
  134.       # false >> tujuan x ama y = -1 (anggap aja g ada tujuannya)
  135.       # true >> kalo tujuannya g sama ama posisi object
  136.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  137.       def moving?
  138.         return false if @to_x == -1 && @to_y == -1
  139.         result = @obj.x != @to_x || @obj.y != @to_y
  140.         return result
  141.       end
  142.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  143.       # Untuk ngeupdate perpindahan posisi. (dipanggil perframe)
  144.       # -----------------------------------------------------------------------
  145.       # @real_x += kecepatan x (floating number)
  146.       # @real_y += kecepatan y (floating number)
  147.       # posisi x object = hasil pembulatan dari real_x
  148.       # posisi y object = hasil pembulatan dari real_y + offset (kalo loncat)
  149.       # @jump -= intervalnya (setengah perjalanan, jump = 0)
  150.       # @offset += kekuatan loncat (stengah perjalanan - akhir, berkurang)
  151.       # -----------------------------------------------------------------------
  152.       # bersihin info pergerakan kalo dah nyampe tujuan
  153.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  154.       def update_move
  155.         @real_x += @x_speed
  156.         @real_y += @y_speed
  157.         @obj.x = @real_x.round
  158.         @obj.y = @real_y.round + @offset.round
  159.         @jump -= @jump_interval
  160.         @offset -= @jump
  161.         clear_move_info unless moving?
  162.       end
  163.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  164.       # Akhir dari Class Move_Object ~
  165.       # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  166.     end
  167.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  168.     # Buat pengecekan aja (error handling)
  169.     # -------------------------------------------------------------------------
  170.     # false >> kalo bukan window || sprite
  171.     # true >> kalo window || sprite
  172.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  173.     def can_move?(obj)
  174.       return obj.is_a?(Window) || obj.is_a?(Sprite)
  175.     end
  176.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  177.     # pengesetan object dengan class Move_Object
  178.     # kecuali objectnya ga bisa digerakin :v
  179.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  180.     def set_obj(obj)
  181.       @move_obj = Move_Object.new(obj)
  182.     end
  183.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  184.     # Perjalanan langsung ke koordinat (x,y)
  185.     # -------------------------------------------------------------------------
  186.     # jump      = kekuatan loncat (default : 0 / ga loncat)
  187.     # duration  = lama perjalanan (default : 60 frame)
  188.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  189.     def goto(x,y,duration = Graphics.frame_rate,jump = 0.0)
  190.       @move_obj.move_to(x,y,jump,duration)
  191.     end
  192.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  193.     # Geser posisi sebanyak (x,y)
  194.     # -------------------------------------------------------------------------
  195.     # jump      = kekuatan loncat (default : 0 / ga loncat)
  196.     # duration  = lama perjalanan (default : 60 frame)
  197.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  198.     def slide(x,y,duration = Graphics.frame_rate,jump = 0.0)
  199.       slide_x = @move_obj.obj.x + x
  200.       slide_y = @move_obj.obj.y + y
  201.       goto(slide_x,slide_y,duration,jump) unless moving?
  202.     end
  203.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  204.     # Ngeupdate pergerakan objectnya. Dipanggil sekali tiap frame
  205.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  206.     def update_move
  207.       @move_obj.update_move if moving?
  208.     end
  209.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  210.     # Ngecek, lagi gerak apa kaga
  211.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  212.     def moving?
  213.       @move_obj.moving?
  214.     end
  215.     # =========================================================================
  216.     # PRESET MOVEMENT :
  217.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  218.     # Geser atas
  219.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  220.     def up(range,duration = Graphics.frame_rate,jump = 0.0)
  221.       slide(0,-range,duration,jump)
  222.     end
  223.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  224.     # Geser bawah
  225.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  226.     def down(range,duration = Graphics.frame_rate,jump = 0.0)
  227.       slide(0,range,duration,jump)
  228.     end
  229.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  230.     # Geser kanan
  231.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  232.     def right(range,duration = Graphics.frame_rate,jump = 0.0)
  233.       slide(range,0,duration,jump)
  234.     end
  235.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  236.     # Geser kiri
  237.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  238.     def left(range,duration = Graphics.frame_rate,jump = 0.0)
  239.       slide(-range,0,duration,jump)
  240.     end
  241.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  242.     # Geser kanan atas
  243.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  244.     def upright(range,duration = Graphics.frame_rate,jump = 0.0)
  245.       slide(range,-range,duration,jump)
  246.     end
  247.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  248.     # Geser kiri atas
  249.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  250.     def upleft(range,duration = Graphics.frame_rate,jump = 0.0)
  251.       slide(-range,-range,duration,jump)
  252.     end
  253.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  254.     # Geser kanan bawah
  255.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  256.     def downright(range,duration = Graphics.frame_rate,jump = 0.0)
  257.       slide(range,-range,duration,jump)
  258.     end
  259.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  260.     # Geser kiri bawah
  261.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  262.     def downleft(range,duration = Graphics.frame_rate,jump = 0.0)
  263.       slide(-range,range,duration,jump)
  264.     end
  265.     # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  266.   end
  267. end
  268. # =============================================================================
  269. # Akhir dari Module THEO::Movement
  270. # -----------------------------------------------------------------------------
  271. # class Sprite (yg ngehandle gambar2 gitu)
  272. # =============================================================================
  273. class Sprite
  274.   # ---------------------------------------------------------------------------
  275.   include THEO::Movement # <-- implementasinya ke class Sprite
  276.   # ---------------------------------------------------------------------------
  277.   alias theolized_sprite_init initialize
  278.   def initialize(viewport = nil)
  279.     theolized_sprite_init(viewport)
  280.     set_obj(self) # <-- ngeset diri sendiri untuk digerakin
  281.   end
  282.   # ---------------------------------------------------------------------------
  283.   # Masukin update
  284.   # ---------------------------------------------------------------------------
  285.   alias theolized_move_update update
  286.   def update
  287.     theolized_move_update
  288.     update_move
  289.   end
  290.   # ---------------------------------------------------------------------------
  291. end
  292. # =============================================================================
  293. # Class Window (yg ngehandle window) <-- u dont say
  294. # =============================================================================
  295. class Window
  296.   # ---------------------------------------------------------------------------
  297.   include THEO::Movement # <-- implementasinya ke class Window
  298.   # ---------------------------------------------------------------------------
  299.   alias theolized_move_window_init initialize
  300.   def initialize(x, y, width, height)
  301.     theolized_move_window_init(x, y, width, height)
  302.     set_obj(self) # <-- ngeset diri sendiri untuk digerakin
  303.   end
  304.   # ---------------------------------------------------------------------------
  305.   # Masukin update
  306.   # ---------------------------------------------------------------------------
  307.   alias theolized_move_update update
  308.   def update
  309.     theolized_move_update
  310.     update_move
  311.   end
  312.   # ---------------------------------------------------------------------------
  313. end
  314. # =============================================================================
  315. #
  316. #                                END OF SCRIPT
  317. #
  318. # =============================================================================
Advertisement
Add Comment
Please, Sign In to add comment