Advertisement
TheoAllen

Theo - Basic Modules v1.5

May 11th, 2014
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 77.08 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Basic Modules
  3. # Version : 1.5
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # By : TheoAllen (Original Scripter)
  6. # =============================================================================
  7. $imported = {} if $imported.nil?
  8. # =============================================================================
  9. # This basic modules means to be my personal module to develop script or system.
  10. # It's to facilitate me on everything I do. Whether is just a little experiment
  11. # or make a big system.
  12. #
  13. # However, not all of my scripts needs this basic modules. Mostly not, but some
  14. # other requires.
  15. # -----------------------------------------------------------------------------
  16. # This module contains :
  17. # =============================================================================
  18. $imported[:Theo_BasicFuntions] = true  # Basic Funtions
  19. $imported[:Theo_BitmapAddons]  = true  # Bitmap Extra Addons
  20. $imported[:Theo_CoreDamage]    = true  # Core Damage Processing
  21. $imported[:Theo_CoreResult]    = true  # Core Damage Result
  22. $imported[:Theo_Movement]      = true  # Object Core Movement
  23. $imported[:Theo_CoreFade]      = true  # Object Core Fade
  24. $imported[:Theo_Circular]      = true  # Object Circular Movement
  25. $imported[:Theo_CloneImage]    = true  # Clone Image. For Afterimage Base
  26. $imported[:Theo_RotateImage]   = true  # To rotate sprite
  27. $imported[:Theo_SmoothMove]    = true  # Object Smooth Movement
  28. # =============================================================================
  29. # Note to users:
  30. # -----------------------------------------------------------------------------
  31. # Categorize this script as core script. It means that you have to put this
  32. # script above all custom scripts.
  33. #
  34. # I know sometimes basic modules provides incompatibility among others. In fact,
  35. # not all of these functions are being used in my scripts. I always mentioned
  36. # in any script that requires this basic modules which methods I use. So, to
  37. # provide more compatibility, you may disable unused functions by set it to
  38. # false.
  39. #
  40. # =============================================================================
  41. # Note to scripters :
  42. # -----------------------------------------------------------------------------
  43. # The documentation of each division is written below.
  44. #
  45. # You may edit this basic modules if you think you could make it better. And
  46. # please tell me what have you edited. I glad if there is any scripter who
  47. # willing to contribute to this basic modules to make it better.
  48. #
  49. # You may put your name to credit list if you have contributed to this basic
  50. # module. But please keep in mind. This basic module shouldn't affect default
  51. # script so much. It's only provides basic functions which will be used in
  52. # other script.
  53. # =============================================================================
  54. # Known incompatibility :
  55. # >> Modern Algebra - Bitmap Addons (Disable the Bitmap Addon)
  56. # >> Tsukihime - Core Damage (Disable the Core Damage processing)
  57. # >> YEA - Lunatic Damage (Disable the Core Damage processing)
  58. # >> Enu - Tankentai (I just heard. Haven't tried it yet)
  59. # =============================================================================
  60. # ChangeLogs :
  61. # =============================================================================
  62. # Version 1.0   : - Initial Release
  63. # Version 1.2   : - Add Bitmap Entire Fill
  64. #                 - Add Bitmap Border Fill
  65. #                 - Add To Center window position
  66. #                 - Add more description
  67. # Version 1.3   : - Added Clone Sprite function and afterimagebase
  68. # Version 1.3b  : - Added TSBS basic function
  69. #                 - Greatly reduced lag for afterimage effect
  70. # Version 1.4   : - Added rotate image basic module
  71. #                 - Fixed bug at Object Core Fade
  72. # Version 1.5   : - Added Smooth movement
  73. #                 - Fixed bug on circle movement
  74. #                 - Fixed bug on movement module where float may causes endless
  75. #                   moving
  76. #                 - Fixed wrong parameter in drawing eclipse / circle
  77. #                 - Fixed color parameter on drawing arrow
  78. #                 - Added Plane_Mask on basic functions
  79. #                 - Added step parameter on draw circle
  80. #                 - Fixed wrong target opacity in object core fade for Window
  81. #                 - Dispose afterimages from Sprite_Base
  82. # =============================================================================
  83. # RGSS3 ~ Bug Fixes (Taken from RMWeb.com)
  84. # forums.rpgmakerweb.com/index.php?/topic/1131-rgss3-unofficial-bugfix-snippets
  85. # =============================================================================
  86. # Screen shake bugfix
  87. # -----------------------------------------------------------------------------
  88. class Game_Interpreter
  89.  
  90.   def command_225
  91.     screen.start_shake(@params[0], @params[1], @params[2])
  92.     wait(@params[2]) if @params[3]
  93.   end
  94.  
  95. end
  96. # -----------------------------------------------------------------------------
  97. # Enemy targeting bugfix
  98. # -----------------------------------------------------------------------------
  99. class Game_Action
  100.   def targets_for_friends
  101.     if item.for_user?
  102.       [subject]
  103.     elsif item.for_dead_friend?
  104.       if item.for_one?
  105.         [friends_unit.smooth_dead_target(@target_index)]
  106.       else
  107.         friends_unit.dead_members
  108.       end
  109.     elsif item.for_friend?
  110.       if item.for_one?
  111.         if @target_index < 0
  112.           [friends_unit.random_target]
  113.         else
  114.           [friends_unit.smooth_target(@target_index)]
  115.         end
  116.       else
  117.         friends_unit.alive_members
  118.       end
  119.     end
  120.   end
  121. end
  122. # -----------------------------------------------------------------------------
  123. # process normal char bugfix
  124. # -----------------------------------------------------------------------------
  125. class Window_Base < Window
  126.   alias :process_normal_character_vxa :process_normal_character
  127.   def process_normal_character(c, pos)
  128.     return unless c >= ' '
  129.     process_normal_character_vxa(c, pos)
  130.   end
  131. end
  132. # -----------------------------------------------------------------------------
  133. # Disable Japanese input name
  134. # -----------------------------------------------------------------------------
  135. class Game_System
  136.   def japanese?
  137.     false
  138.   end
  139. end
  140. #==============================================================================
  141. # ** Basic Functions ~
  142. #------------------------------------------------------------------------------
  143. #  These are just basic functions. It will not do anything in script mechanics.
  144. # I only provide these functions to be used later in upcoming script or just
  145. # simply for experimental.
  146. #------------------------------------------------------------------------------
  147. if $imported[:Theo_BasicFuntions] # Activation flag
  148. #==============================================================================
  149. module Math
  150.  
  151.   # Convert degree to radian
  152.   def self.radian(degree)
  153.     return (degree.to_f/180) * Math::PI
  154.   end
  155.  
  156.   # Convert radian to degree
  157.   def self.degree(radian)
  158.     return (radian.to_f/Math::PI) * 180
  159.   end
  160.  
  161. end
  162.  
  163. class Object
  164.  
  165.   # Generate number with range from minimum to maximum
  166.   unless method_defined?(:rand_range)
  167.     def rand_range(min,max,float = false)
  168.       range = max - min
  169.       return float ? (min + (rand*range)) : min + rand(range)
  170.     end
  171.   end
  172.  
  173.   # ---------------------------------------------------------------------------
  174.   # Iterate instance variables one by one.
  175.   #
  176.   # Example usage :
  177.   # each_var do |ivar|
  178.   #   ivar.update if ivar.is_a?(Sprite)
  179.   # end
  180.   # ---------------------------------------------------------------------------
  181.   def each_var
  182.     instance_variables.each do |varsymb|
  183.       yield instance_variable_get(varsymb)
  184.     end
  185.   end
  186.  
  187. end
  188. #==============================================================================
  189. # ** Sprite_Screen
  190. #------------------------------------------------------------------------------
  191. #  Sprite that same size as the screen. It can be used to draw anything on
  192. # screen. I believe sometime you need it
  193. #
  194. # Example usage :
  195. # - http://goo.gl/E88ufV  ( Event Pointer )
  196. #==============================================================================
  197.  
  198. class Sprite_Screen < Sprite
  199.  
  200.   def initialize(viewport = nil)
  201.     super(viewport)
  202.     self.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  203.   end
  204.  
  205.   def dispose
  206.     self.bitmap.dispose
  207.     super
  208.   end
  209.  
  210. end
  211.  
  212. #==============================================================================
  213. # ** Plane_Mask
  214. #------------------------------------------------------------------------------
  215. #  Sprite that same size as the map size. It's also scrolled alongside the map
  216. # if it's updated. It can be used to draw anything on map. Can be used as base
  217. # class of parallax lock actually
  218. #==============================================================================
  219.  
  220. class Plane_Mask < Plane
  221.  
  222.   def initialize(vport)
  223.     super(vport)
  224.     @width = 1
  225.     @height = 1
  226.   end
  227.  
  228.   def update
  229.     if $game_map
  230.       if @width != $game_map.width || @height != $game_map.height
  231.         @width = $game_map.width
  232.         @height = $game_map.height
  233.         update_bitmap
  234.       end
  235.       self.ox = $game_map.display_x * 32
  236.       self.oy = $game_map.display_y * 32
  237.     end
  238.   end
  239.  
  240.   def update_bitmap
  241.     bmp = Bitmap.new(@width * 32, @height * 32)
  242.     self.bitmap = bmp
  243.   end
  244.  
  245. end
  246.  
  247. #==============================================================================
  248. # ** Window_Confirmation
  249. #------------------------------------------------------------------------------
  250. #  Window command class that holds yes and no command. Used to make
  251. # confirmation function. If you want some alteration of this class, just make
  252. # inheritance.
  253. #
  254. #  This is for my personal development. I mean, if there is a simple script
  255. # that display window yes/no confirmation, it will not require this basic
  256. # module. I prefer to duplicate this class to my script instead. You know,
  257. # most people hates Core Script / Basic Modules
  258. #==============================================================================
  259. class Window_Confirmation < Window_Command
  260.   def window_width
  261.     return 100
  262.   end
  263.  
  264.   def make_command_list
  265.     add_command(ok_vocab, :ok, ok_condition)
  266.     add_command(cancel_vocab, :cancel, cancel_condition)
  267.   end
  268.  
  269.   # Vocab yes
  270.   def ok_vocab
  271.     return "Yes"
  272.   end
  273.  
  274.   # Vocab cancel
  275.   def cancel_vocab
  276.     return "No"
  277.   end
  278.  
  279.   # Overwrite this method in child class
  280.   def ok_condition
  281.     return true
  282.   end
  283.  
  284.   # Overwrite this method in child class
  285.   def cancel_condition
  286.     return true
  287.   end
  288.  
  289.   def alignment
  290.     return 1
  291.   end
  292.  
  293. end
  294. #==============================================================================
  295. # ** Coordinate
  296. #------------------------------------------------------------------------------
  297. #  Coordinate class that holds x dan y point. Can be used to calculate point
  298. # operation such as vector
  299. #
  300. # Example usage :
  301. # - http://goo.gl/E88ufV  ( Event Pointer )
  302. #==============================================================================
  303. class Coordinate
  304.   attr_accessor :x,:y
  305.  
  306.   def initialize(x,y)
  307.     @x = x
  308.     @y = y
  309.   end
  310.  
  311.   def + (other)
  312.     other = other.to_c unless other.is_a?(Coordinate)
  313.     Coordinate.new(self.x + other.x, self.y + other.y)
  314.   end
  315.  
  316.   def - (other)
  317.     other = other.to_c unless other.is_a?(Coordinate)
  318.     Coordinate.new(self.x - other.x, self.y - other.y)
  319.   end
  320.  
  321.   def == (other)
  322.     other = other.to_c unless other.is_a?(Coordinate)
  323.     return self.x == other.x && self.y == other.y
  324.   end
  325.  
  326.   # To String function
  327.   def to_s
  328.     return "(#{self.x},#{self.y})"
  329.   end
  330.  
  331. end
  332. #==============================================================================
  333. # ** Vector
  334. #------------------------------------------------------------------------------
  335. #  Vector class that handles all vector functions. Note that some of these
  336. # methods and variable terms are named in Indonesian terms. Because I don't
  337. # really know what is in English terms
  338. #
  339. # Example usage :
  340. # - http://goo.gl/E88ufV  ( Event Pointer )
  341. #==============================================================================
  342. class Vector
  343.   attr_accessor :pangkal  # Starting point
  344.   attr_accessor :ujung    # End point
  345.  
  346.   def initialize(pangkal,ujung,radius = 0)
  347.     @pangkal = pangkal.is_a?(Coordinate) ? pangkal : pangkal.to_c
  348.     if ujung.is_a?(Numeric)
  349.       x_pos = @pangkal.x + (Math.cos(Math.radian(ujung)) * radius)
  350.       y_pos = @pangkal.y + (Math.sin(Math.radian(ujung)) * radius)
  351.       @ujung = Coordinate.new(x_pos,y_pos)
  352.       return
  353.     end
  354.     @ujung = ujung.is_a?(Coordinate) ? ujung : ujung.to_c
  355.   end
  356.  
  357.   # Get scalar value
  358.   def skalar
  359.     Math.sqrt(jarak_x**2 + jarak_y**2)
  360.   end
  361.  
  362.   def + (other)
  363.     if other.is_a?(Coordinate)
  364.       return Vector.new(pangkal + other, ujung + other)
  365.     end
  366.     Vector.new(pangkal, ujung + other.satuan)
  367.   end
  368.  
  369.   def - (other)
  370.     if other.is_a?(Coordinate)
  371.       return Vector.new(pangkal - other, ujung - other)
  372.     end
  373.     Vector.new(pangkal, ujung - other.satuan)
  374.   end
  375.  
  376.   # Get degree upon two different point
  377.   def degree
  378.     Math.degree(Math.atan2(jarak_y,jarak_x)) rescue 0
  379.   end
  380.  
  381.   # Get distance X
  382.   def jarak_y
  383.     ujung.y - pangkal.y
  384.   end
  385.  
  386.   # Get distance Y
  387.   def jarak_x
  388.     ujung.x - pangkal.x
  389.   end
  390.  
  391.   # Convert vector to coordinate
  392.   def satuan
  393.     Coordinate.new(jarak_x, jarak_y)
  394.   end
  395.  
  396.   # To string format
  397.   def to_s
  398.     return @pangkal.to_s + " ---> " + @ujung.to_s
  399.   end
  400.  
  401. end
  402. #==============================================================================
  403. # ** VectorObject
  404. #------------------------------------------------------------------------------
  405. # This class handles two objects that their vector is later will be used.
  406. #
  407. # Example usage :
  408. # - http://goo.gl/E88ufV  ( Event Pointer )
  409. #==============================================================================
  410. class VectorObject
  411.   attr_accessor :pangkal  # Starting object
  412.   attr_accessor :ujung    # End object
  413.   attr_accessor :color    # Color value (to draw lines)
  414.   attr_accessor :offset_x # Offset value X
  415.   attr_accessor :offset_y # Offset value Y
  416.  
  417.   # Basically, offset value is a increment value upon a new vector that created
  418.   # using to_v vector. I don't really know if 'offset' is the right word. I'm
  419.   # suck at english afterall
  420.  
  421.   def initialize(pangkal,ujung,color = Color.new(255,255,255))
  422.     @pangkal = pangkal
  423.     @ujung = ujung
  424.     @color = color
  425.     @offset_x = 0
  426.     @offset_y = 0
  427.   end
  428.  
  429.   # Two object converted into vector
  430.   def to_v
  431.     a = @pangkal.to_c
  432.     b = @ujung.to_c
  433.     a.x += @offset_x
  434.     b.x += @offset_x
  435.     a.y += @offset_y
  436.     b.y += @offset_y
  437.     Vector.new(a,b)
  438.   end
  439.  
  440. end
  441. #==============================================================================
  442. # ** Bitmap
  443. #------------------------------------------------------------------------------
  444. # Built in class that handles bitmap
  445. #==============================================================================
  446. class Bitmap
  447.  
  448.   # Fill entire bitmap with color
  449.   def entire_fill(color = Color.new(0,0,0,150))
  450.     fill_rect(self.rect,color)
  451.   end
  452.  
  453.   # Fill bitmap edge only
  454.   def border_fill(color = Color.new(255,255,255))
  455.     fill_rect(0,0,width,1,color)
  456.     fill_rect(0,0,1,height,color)
  457.     fill_rect(width-1,0,1,height,color)
  458.     fill_rect(0,height-1,width,1,color)
  459.   end
  460.  
  461. end
  462. #==============================================================================
  463. # ** Window_Base
  464. #------------------------------------------------------------------------------
  465. # This is a super class of all windows within the game.
  466. #==============================================================================
  467. class Window_Base < Window
  468.  
  469.   # Same as bitmap entire fill
  470.   def entire_fill(color = Color.new(0,0,0,150))
  471.     contents.entire_fill(color)
  472.   end
  473.  
  474.   # Set window to center position of the screen
  475.   def to_center
  476.     self.x = Graphics.width/2 - self.width/2
  477.     self.y = Graphics.height/2 - self.height/2
  478.   end
  479.  
  480. end
  481. #==============================================================================
  482. # ** Color
  483. #------------------------------------------------------------------------------
  484. # Built in class that handles color
  485. #==============================================================================
  486. class Color
  487.  
  488.   # Is color same as other?
  489.   def same?(*args)
  490.     args = args.select {|color| color.is_a?(Color)}
  491.     return false if args.empty?
  492.     return args.any?{|color| self.red == color.red &&
  493.       self.green == color.green && self.blue == color.blue &&
  494.       self.alpha == color.alpha}
  495.   end
  496.  
  497.   # Is color empty?
  498.   def empty?
  499.     return self.alpha <= 0
  500.   end
  501.  
  502. end
  503. # -----------------------------------------------------------------------------
  504. # New Method : To Coordinate Conversion
  505. # -----------------------------------------------------------------------------
  506. class Object
  507.  
  508.   def to_c
  509.     return Coordinate.new(0,0)
  510.   end
  511.  
  512. end
  513.  
  514. class Numeric
  515.  
  516.   def to_c
  517.     return Coordinate.new(self,self)
  518.   end
  519.  
  520. end
  521.  
  522. class Sprite
  523.  
  524.   def to_c
  525.     return Coordinate.new(self.x,self,y)
  526.   end
  527.  
  528. end
  529.  
  530. class Window
  531.  
  532.   def to_c
  533.     return Coordinate.new(self.x,self,y)
  534.   end
  535.  
  536. end
  537.  
  538. class Game_Enemy < Game_Battler
  539.  
  540.   def to_c
  541.     return Coordinate.new(self.screen_x,self,screen_y)
  542.   end
  543.  
  544. end
  545.  
  546. class Game_CharacterBase
  547.  
  548.   def to_c(map = false)
  549.     if map
  550.       return Coordinate.new(self.real_x,self.real_y)
  551.     end
  552.     return Coordinate.new(self.screen_x,self.screen_y)
  553.   end
  554.  
  555. end
  556. # -----------------------------------------------------------------------------
  557. # Added TSBS basic function. To calculate sprite anim cell
  558. # -----------------------------------------------------------------------------
  559. module TSBS
  560.   def self.cell(row, col)
  561.     result = (MaxCol * (row - 1)) + (col - 1)
  562.     return result
  563.   end
  564. end
  565. end
  566. #==============================================================================
  567. # ** Bitmap Extra addons v2.1
  568. #------------------------------------------------------------------------------
  569. #  This bitmap extra addons provide basic function of drawing shapes. It could
  570. # be drawing polygon, line, arrow, elipse or such.
  571. #
  572. # The code may look messy. Even myself forget how it works. To be note that
  573. # even this current version is 2.1, the draw filled polygon is imperfect. Do
  574. # not use at this moment.
  575. #
  576. # Some functions may need my Basic Functions to be activated. So I suggest you
  577. # to also activate it
  578. #
  579. # Example usage :
  580. # - http://goo.gl/E88ufV ( Event Pointer )
  581. # - http://goo.gl/DlrPuR ( Simple Polygon Status )
  582. #------------------------------------------------------------------------------
  583. if $imported[:Theo_BitmapAddons]  # Activation flag
  584. # =============================================================================
  585. class Bitmap
  586.   attr_accessor :start_degree
  587.  
  588.   # ------------------------------------------------------------------------
  589.   # Static Members
  590.   # ------------------------------------------------------------------------
  591.   @@default_color = Color.new(255,255,255)
  592.   @@arrow_degree = 30
  593.  
  594.   def self.default_color=(color)
  595.     @@default_color = color
  596.   end
  597.  
  598.   def self.arrow_degree=(degree)
  599.     @@arrow_degree = degree
  600.   end
  601.  
  602.   def self.default_color
  603.     @@default_color
  604.   end
  605.  
  606.   def self.arrow_degree
  607.     @@arrow_degree
  608.   end
  609.  
  610.   alias theo_bitmap_init initialize
  611.   def initialize(*args)
  612.     theo_bitmap_init(*args)
  613.     @start_degree = 270
  614.   end
  615.   # ------------------------------------------------------------------------
  616.   # Bitmap draw line. The avalaible arguments to put are
  617.   # - draw_line(x1,x2,y1,y2,[color])
  618.   # - draw_line(coordinate1, coordinate2, [color])
  619.   # - draw_line(vector, [color])
  620.   #
  621.   # coordinate must be Coordinate object from this basic module.
  622.   # So do the vector
  623.   # ------------------------------------------------------------------------
  624.   def draw_line(x1, y1=0, x2=0, y2=0, color=@@default_color.dup,
  625.       color_set_skip = false)
  626.     # ----------------------------------------------------------------------
  627.     # If the argument is a vector
  628.     # ----------------------------------------------------------------------
  629.     if x1.is_a?(Vector)
  630.       new_color = (y1.is_a?(Color) ? y1 : color)
  631.       draw_line(x1.pangkal,x1.ujung,new_color) # Recursive
  632.       return # Exit
  633.     # ----------------------------------------------------------------------
  634.     # If two arguments are coordinates
  635.     # ----------------------------------------------------------------------
  636.     elsif x1.is_a?(Coordinate) && y1.is_a?(Coordinate)
  637.       pangkal = x1
  638.       ujung = y1
  639.       new_color = (x2.is_a?(Color) ? x2 : color)
  640.       draw_line(pangkal.x,pangkal.y,ujung.x,ujung.y,new_color) # Recursive
  641.       return # Exit
  642.     end
  643.     # ----------------------------------------------------------------------
  644.     # If two coordinate is same
  645.     # ----------------------------------------------------------------------
  646.     if x1 == x2 && y1 == y2
  647.       set_pixel(x1,y1,color)
  648.       yield [x1,x2] if block_given?
  649.       return # Exit
  650.     end
  651.     # ----------------------------------------------------------------------
  652.     # Calculate distance X dan Y
  653.     # ----------------------------------------------------------------------
  654.     jarak_x = (x2-x1)
  655.     jarak_y = (y2-y1)
  656.     # ----------------------------------------------------------------------
  657.     # If line is horz line or vert line
  658.     # ----------------------------------------------------------------------
  659.     if jarak_y == 0 || jarak_x == 0
  660.      
  661.       # Horizontal
  662.       if jarak_y == 0
  663.         draw_horz(x1,y1,jarak_x,color)
  664.         for j in 0..jarak_x
  665.           yield [x1,y1]
  666.           x1 += 1
  667.         end if block_given?
  668.        
  669.       # Vertikal
  670.       elsif jarak_x == 0
  671.         draw_vert(x1,y1,jarak_y,color)
  672.         for k in 0..jarak_y
  673.           yield [x1,y1]
  674.           y1 += 1
  675.         end if block_given?
  676.       end
  677.       return # Exit
  678.      
  679.     end
  680.     # ----------------------------------------------------------------------
  681.     # If line is diagonal
  682.     # ----------------------------------------------------------------------
  683.     maximum = [jarak_x.abs,jarak_y.abs].max
  684.     rasio_x = jarak_x / maximum.to_f
  685.     rasio_y = jarak_y / maximum.to_f
  686.     real_x = x1.to_f
  687.     real_y = y1.to_f
  688.     for i in 0..maximum
  689.       set_pixel(x1,y1,color) unless get_pixel(x1,y1).same?(color) ||
  690.         color_set_skip ? !get_pixel(x1,y1).empty? : false
  691.       real_x += rasio_x
  692.       real_y += rasio_y
  693.       yield [x1,y1] if block_given?
  694.       x1 = real_x.round
  695.       y1 = real_y.round
  696.     end
  697.   end
  698.   # ------------------------------------------------------------------------
  699.   # Gradient line
  700.   # ------------------------------------------------------------------------
  701.   def gradient_line(x1,y1=0,x2=0,y2=0,color1=@@default_color.dup,
  702.       color2 = color1)
  703.     if x1.is_a?(Vector)
  704.       warna_pangkal = (y1.is_a?(Color) ? y1 : color1)
  705.       warna_ujung = (x2.is_a?(Color) ? x2 : color2)
  706.       gradient_line(x1.pangkal, x1.ujung, warna_pangkal, warna_ujung)
  707.       return
  708.     elsif x1.is_a?(Coordinate) && y1.is_a?(Coordinate)
  709.       pangkal = x1
  710.       ujung = y1
  711.       warna_pangkal = (x2.is_a?(Color) ? x2 : color1)
  712.       warna_ujung = (y2.is_a?(Color) ? y2 : color2)
  713.       gradient_line(pangkal.x,pangkal.y,ujung.x,ujung.y,warna_pangkal,
  714.         warna_ujung)
  715.       return
  716.     end
  717.     jarak_x = (x2-x1)
  718.     jarak_y = (y2-y1)
  719.     radius = Math.sqrt((jarak_x**2) + (jarak_y**2))
  720.     red_diff = (color2.red - color1.red) / radius
  721.     green_diff = (color2.green - color1.green) / radius
  722.     blue_diff = (color2.blue - color1.blue) / radius
  723.     alpha_diff = (color2.alpha - color1.alpha) / radius
  724.     red = color1.red
  725.     green = color1.green
  726.     blue = color1.blue
  727.     alpha = color1.alpha
  728.     if jarak_y.abs == 0 || jarak_x.abs == 0
  729.       gradient_fill_rect(x1,y1,1,jarak_y,color1,color2,true) if jarak_y.abs == 0
  730.       gradient_fill_rect(x1,y1,jarak_x,1,color1,color2) if jarak_x.abs == 0
  731.       return
  732.     end
  733.     maximum = [jarak_x.abs,jarak_y.abs].max
  734.     rasio_x = jarak_x / maximum.to_f
  735.     rasio_y = jarak_y / maximum.to_f
  736.     real_x = x1.to_f
  737.     real_y = y1.to_f
  738.     for i in 0..maximum
  739.       new_color = Color.new(red,green,blue,alpha)
  740.       set_pixel(x1,y1,new_color) unless get_pixel(x1,y1).same?(new_color)
  741.       real_x += rasio_x
  742.       real_y += rasio_y
  743.       x1 = real_x.round
  744.       y1 = real_y.round
  745.       red += red_diff
  746.       blue += blue_diff
  747.       green += green_diff
  748.       alpha += alpha_diff
  749.     end
  750.   end
  751.  
  752.   # Draw horizontal line
  753.   def draw_horz(x,y,width,color = Color.new(255,255,255))
  754.     if width < 0
  755.       fill_rect(x+width+1,y,width.abs,1,color)
  756.       return
  757.     end
  758.     fill_rect(x,y,width,1,color)
  759.   end
  760.  
  761.   # Draw vertical line
  762.   def draw_vert(x,y,height,color = Color.new(255,255,255))
  763.     if height < 0
  764.       fill_rect(x,y+height+1,1,height.abs,color)
  765.       return
  766.     end
  767.     fill_rect(x,y,1,height,color)
  768.   end
  769.  
  770.   # --------------------------------------------------------------------------
  771.   # Drawing ellipse.
  772.   #
  773.   # The parameters are :
  774.   # - x     >> Center coordinate X
  775.   # - y     >> Center coordinate Y
  776.   # - horz  >> Horizontal Radius value
  777.   # - vert  >> Vertical Radius value
  778.   # - color >> Line color
  779.   # - thick >> Thickness of line
  780.   # - step  >> Dot step. Greater number may causes ellipse being drawn
  781.   #            imperfect. But may perform better
  782.   # --------------------------------------------------------------------------
  783.   def draw_ellipse(x,y=0,horz=1,vert=1,color=Color.new(255,255,255),thick=1,
  784.       step=0.1)
  785.     return if thick < 1
  786.     ori_x = x
  787.     ori_y = y
  788.     x += horz
  789.     degree = 0.0
  790.     while degree <= 360
  791.       yield [x,y] if block_given?
  792.       set_pixel(x,y,color) unless get_pixel(x,y).same?(color)
  793.       x = Math.cos(Math.radian(degree)) * horz + ori_x
  794.       y = Math.sin(Math.radian(degree)) * vert + ori_y
  795.       degree = [degree+step,361].min
  796.     end
  797.     if thick > 1
  798.       draw_ellipse(ori_x+1,ori_y+1,horz,vert,color,thick-1,step)
  799.       draw_ellipse(ori_x-1,ori_y-1,horz,vert,color,thick-1,step)
  800.       draw_ellipse(ori_x+1,ori_y-1,horz,vert,color,thick-1,step)
  801.       draw_ellipse(ori_x-1,ori_y+1,horz,vert,color,thick-1,step)
  802.     end
  803.   end
  804.   # --------------------------------------------------------------------------
  805.   # Draw circle. Similar of drawing ellipse. But only have one radius parameter
  806.   # --------------------------------------------------------------------------
  807.   def draw_circle(x,y,radius,color=Color.new(255,255,255),step=0.1,thick=1)
  808.     draw_ellipse(x,y,radius,radius,color,thick,step) {|coordinate|
  809.       yield coordinate if block_given?
  810.     }
  811.   end
  812.  
  813.   # Do not use ~ !
  814.   def fill_circle(x,y,radius,color1,color2=color1,step=0.1)
  815.     fill_ellipse(x,y,radius,radius,color1,color2,step)
  816.   end
  817.  
  818.   # Do not use ~ !
  819.   def fill_ellipse(x,y,horz,vert,color1,color2=color1,step=0.1)
  820.     draw_ellipse(x,y,horz,vert,1,color1,0.5) {|cor|
  821.       draw_line(x,y,cor[0],cor[1],color2,true)
  822.     }
  823.     for i in 0..1
  824.       for baris in 0..horz*2
  825.         for kolom in 0..vert*2
  826.           pos_x = baris-horz+x
  827.           pos_y = kolom-vert+y
  828.           syarat = (get_pixel(pos_x+1,pos_y).same?(color1,color2) &&
  829.           get_pixel(pos_x-1,pos_y).same?(color1,color2)) ||
  830.           (get_pixel(pos_x,pos_y-1).same?(color1,color2) &&
  831.           get_pixel(pos_x,pos_y+1).same?(color1,color2)) ||
  832.           (get_pixel(pos_x+1,pos_y+1).same?(color1,color2) &&
  833.           get_pixel(pos_x-1,pos_y-1).same?(color1,color2)) ||
  834.           (get_pixel(pos_x-1,pos_y+1).same?(color1,color2) &&
  835.           get_pixel(pos_x+1,pos_y-1).same?(color1,color2))
  836.           if syarat && !get_pixel(pos_x,pos_y).same?(color1,color2)
  837.             set_pixel(pos_x,pos_y,color2)
  838.           end
  839.         end
  840.       end
  841.     end
  842.   end
  843.   # ---------------------------------------------------------------------------
  844.   # Drawing polygon
  845.   #
  846.   # The parameters are :
  847.   # - x       >> Center coordinate X
  848.   # - y       >> Center coordinate Y
  849.   # - corner  >> Number of corner. The mininmal value is 3 (Triangle)
  850.   # - length  >> Radius range from center
  851.   # - color1  >> Edge line color
  852.   # - bone    >> Drawing bone? (true/false)
  853.   # - color2  >> Bone Color
  854.   # ---------------------------------------------------------------------------
  855.   def draw_polygon(x,y,corner,length,color1,bone=true,color2=color1)
  856.     return unless corner.is_a?(Numeric)
  857.     draw_shape(x,y,Array.new(corner){1},length,color1,bone,false,color2)
  858.   end
  859.  
  860.   # Do not use ~ !
  861.   def fill_polygon(x,y,corner,length,color1,color2=color1)
  862.     return unless corner.is_a?(Numeric)
  863.     draw_shape(x,y,Array.new(corner),length,color1,false,true,color2)
  864.   end
  865.  
  866.   # Draw polygon parameter
  867.   def draw_shape_params(x,y,params,length,color1,bone=true,color2=color1)
  868.     draw_shape(x,y,params,length,color1,bone,false,color2)
  869.   end
  870.  
  871.   # Do not use ~ !
  872.   def fill_shape_params(x,y,params,length,color1,color2=color1)
  873.     draw_shape(x,y,params,length,color1,false,true,color2)
  874.   end
  875.  
  876.   # Core function of drawing shape
  877.   def draw_shape(x,y,params,length,color1 = Color.new(255,255,255),
  878.       include_bone = true ,fill=false,color2 = color1)
  879.     return unless params.is_a?(Array) # Corner lenght should be array
  880.     return unless params.size >= 3    # At the size should have min 3
  881.     degree_plus = 360 / params.size
  882.     degree = @start_degree
  883.     coordinate = []
  884.     edge = []
  885.     params.each do |i|
  886.       x_des = x + Math.cos(Math.radian(degree)) * (length*(i.to_f/params.max))
  887.       y_des = y + Math.sin(Math.radian(degree)) * (length*(i.to_f/params.max))
  888.       draw_line(x,y,x_des,y_des,color2) if include_bone
  889.       degree += degree_plus
  890.       coordinate.push(Coordinate.new(x_des,y_des))
  891.     end
  892.     for i in -1..coordinate.size-2
  893.       c = coordinate
  894.       draw_line(c[i].x,c[i].y,c[i+1].x,c[i+1].y,color1) {|cor| edge.push(cor)}
  895.     end
  896.     return unless fill
  897.     # -------------------------------------------------------------------------
  898.     # Yes, it should return. Because the code below this point is sooo much
  899.     # messy.
  900.     # -------------------------------------------------------------------------
  901.     edge.each do |line|
  902.       draw_line(x,y,line[0],line[1],color2,true)
  903.     end
  904.     for i in 0..1
  905.       for baris in 0..length*2
  906.         for kolom in 0..length*2
  907.           pos_x = baris-length+x
  908.           pos_y = kolom-length+y
  909.           syarat = (get_pixel(pos_x+1,pos_y).same?(color1,color2) &&
  910.           get_pixel(pos_x-1,pos_y).same?(color1,color2)) ||
  911.           (get_pixel(pos_x,pos_y-1).same?(color1,color2) &&
  912.           get_pixel(pos_x,pos_y+1).same?(color1,color2)) ||
  913.           (get_pixel(pos_x+1,pos_y+1).same?(color1,color2) &&
  914.           get_pixel(pos_x-1,pos_y-1).same?(color1,color2)) ||
  915.           (get_pixel(pos_x-1,pos_y+1).same?(color1,color2) &&
  916.           get_pixel(pos_x+1,pos_y-1).same?(color1,color2))
  917.           if syarat && !get_pixel(pos_x,pos_y).same?(color1,color2)
  918.             set_pixel(pos_x,pos_y,color2)
  919.           end
  920.         end
  921.       end
  922.     end
  923.   end
  924.  
  925.   # ---------------------------------------------------------------------------
  926.   # Bitmap drawing arrows. It's similar as draw line actually
  927.   # - draw_arrow(x1,x2,y1,y2,[color])
  928.   # - draw_arrow(coordinate1, coordinate2, [color])
  929.   # - draw_arrow(vector, [color])
  930.   #
  931.   # coordinate must be Coordinate object from this basic module.
  932.   # So do the vector
  933.   # ---------------------------------------------------------------------------
  934.   def draw_arrow(x1,y1=0,x2=0,y2=0,color=@@default_color.dup)
  935.     if x1.is_a?(Vector)
  936.       new_color = (y1.is_a?(Color) ? y1 : color)
  937.      
  938.       # Recursive call. Split a vector to two coordinate.
  939.       draw_arrow(x1.pangkal,x1.ujung,new_color)
  940.       # Return
  941.       return
  942.     elsif x1.is_a?(Coordinate) && y1.is_a?(Coordinate)
  943.       pangkal = x1
  944.       ujung = y1
  945.       new_color = (x2.is_a?(Color) ? x2 : color)
  946.      
  947.       # Recursive call. Split each coordinate into two primitive x,y value
  948.       draw_arrow(pangkal.x,pangkal.y,ujung.x,ujung.y,new_color)
  949.       # Return
  950.       return
  951.     end
  952.     # Draw basic line
  953.     draw_line(x1,y1,x2,y2,color)
  954.     # Get reversed degree
  955.     degree = Vector.new(Coordinate.new(x1,y1),Coordinate.new(x2,y2)).degree-180
  956.     # Draw arrow
  957.     draw_line(Vector.new(Coordinate.new(x2,y2),degree + @@arrow_degree,10),
  958.       color)
  959.     draw_line(Vector.new(Coordinate.new(x2,y2),degree - @@arrow_degree,10),
  960.       color)
  961.   end
  962.  
  963. end
  964. # -----------------------------------------------------------------------------
  965. # Draw Polygon status
  966. # -----------------------------------------------------------------------------
  967. class Game_Battler < Game_BattlerBase
  968.  
  969.   def params_array
  970.     return [self.atk,self.def,self.mat,self.mdf,self.agi,self.luk]
  971.   end
  972.  
  973. end
  974.  
  975. class Window_Base < Window
  976.  
  977.   def draw_polygon_params(x,y,actor,inner,outer,color1,color2)
  978.     contents.draw_shape_params(x,y,actor.params_array,inner,color1)
  979.     contents.draw_polygon(x,y,actor.params_array.size,outer,color2)
  980.   end
  981.  
  982. end
  983. end
  984. #==============================================================================
  985. # ** Core Damage Processing
  986. # -----------------------------------------------------------------------------
  987. # I altered the way how damage calculation to provide more flexibility. Any
  988. # scripts that also overwrite make_damage_value will highly incompatible.
  989. #
  990. # However, I don't have any script implements this module right now. So you
  991. # may disable it
  992. # -----------------------------------------------------------------------------
  993. if $imported[:Theo_CoreDamage]  # Activation flag
  994. #==============================================================================
  995. class Game_Battler < Game_BattlerBase
  996.   # ---------------------------------------------------------------------------
  997.   # *) Overwrite make damage value
  998.   # ---------------------------------------------------------------------------
  999.   def make_damage_value(user, item)
  1000.     value = base_damage(user, item)
  1001.     value = apply_element_rate(user, item, value)
  1002.     value = process_damage_rate(user, item, value)
  1003.     value = apply_damage_modifiers(user, item, value)
  1004.     @result.make_damage(value.to_i, item)
  1005.   end
  1006.   # ---------------------------------------------------------------------------
  1007.   # *) Make base damage. Evaling damage formula
  1008.   # ---------------------------------------------------------------------------
  1009.   def base_damage(user, item)
  1010.     value = item.damage.eval(user, self, $game_variables)
  1011.     value
  1012.   end
  1013.   # ---------------------------------------------------------------------------
  1014.   # *) Apply element rate
  1015.   # ---------------------------------------------------------------------------
  1016.   def apply_element_rate(user, item, value)
  1017.     value *= item_element_rate(user, item)
  1018.     value
  1019.   end
  1020.   # ---------------------------------------------------------------------------
  1021.   # *) Apply damage rate. Such as physical, magical, recovery
  1022.   # ---------------------------------------------------------------------------
  1023.   def process_damage_rate(user, item, value)
  1024.     value *= pdr if item.physical?
  1025.     value *= mdr if item.magical?
  1026.     value *= rec if item.damage.recover?
  1027.     value
  1028.   end
  1029.   # ---------------------------------------------------------------------------
  1030.   # *) Apply damage modifier. Such as guard, critical, variance
  1031.   # ---------------------------------------------------------------------------
  1032.   def apply_damage_modifiers(user, item, value)
  1033.     value = apply_critical(value, user) if @result.critical
  1034.     value = apply_variance(value, item.damage.variance)
  1035.     value = apply_guard(value)
  1036.     value
  1037.   end
  1038.   # ---------------------------------------------------------------------------
  1039.   # *) Applying critical
  1040.   # ---------------------------------------------------------------------------
  1041.   def apply_critical(damage, user)
  1042.     damage * 3
  1043.   end
  1044.  
  1045. end
  1046. end
  1047. #=============================================================================
  1048. # ** Core Damage Result ~
  1049. #-----------------------------------------------------------------------------
  1050. # I altered how action result is being handled. It's used within my sideview
  1051. # battle system.
  1052. #-----------------------------------------------------------------------------
  1053. if $imported[:Theo_CoreResult]  # Activation flag
  1054. #=============================================================================
  1055. class Game_Battler < Game_BattlerBase
  1056.  
  1057.   def item_apply(user, item)
  1058.     make_base_result(user, item)
  1059.     apply_hit(user, item) if @result.hit?
  1060.   end
  1061.  
  1062.   def make_base_result(user, item)
  1063.     @result.clear
  1064.     @result.used = item_test(user, item)
  1065.     @result.missed = (@result.used && rand >= item_hit(user, item))
  1066.     @result.evaded = (!@result.missed && rand < item_eva(user, item))
  1067.   end
  1068.  
  1069.   def apply_hit(user, item)  
  1070.     unless item.damage.none?
  1071.       determine_critical(user, item)
  1072.       make_damage_value(user, item)
  1073.       execute_damage(user)
  1074.     end
  1075.     apply_item_effects(user, item)
  1076.   end
  1077.  
  1078.   def determine_critical(user, item)
  1079.     @result.critical = (rand < item_cri(user, item))
  1080.   end
  1081.  
  1082.   def apply_item_effects(user, item)
  1083.     item.effects.each {|effect| item_effect_apply(user, item, effect) }
  1084.     item_user_effect(user, item)
  1085.   end
  1086.  
  1087. end
  1088. end
  1089. #==============================================================================
  1090. # ** Object Core Movement
  1091. #------------------------------------------------------------------------------
  1092. #  This basic module allow you to move objects like Window or Sprite to a
  1093. # certain position in a given time duration. The object must be updated in each
  1094. # frame. It could also applied to any classes that contains x,y and allow it to
  1095. # be modified
  1096. #
  1097. # Avalaible methods :
  1098. # - goto(x, y, duration, [jump])
  1099. #   Tells the object to move to specified x,y coordinate in a given time
  1100. #   duration in frame. By default, the jump value is zero.
  1101. #  
  1102. # - slide(x, y, duration, jump)
  1103. #   Tells the object to slide in x,y coordinate from original position in a
  1104. #   given time duration in frame. By default, the jump value is zero.
  1105. #
  1106. # I use this module within my sideview battle system movement sequences
  1107. #------------------------------------------------------------------------------
  1108. if $imported[:Theo_Movement]  # Activation flag
  1109. #==============================================================================
  1110. module THEO
  1111.   module Movement
  1112.     # =========================================================================
  1113.     # Exclusive class move object. To prevent adding to many instance variable
  1114.     # in object that implements this module
  1115.     # -------------------------------------------------------------------------
  1116.     class Move_Object
  1117.       attr_reader :obj # Reference object
  1118.       # -----------------------------------------------------------------------
  1119.       # *) Initialize
  1120.       # -----------------------------------------------------------------------
  1121.       def initialize(obj)
  1122.         @obj = obj
  1123.         clear_move_info
  1124.       end
  1125.       # -----------------------------------------------------------------------
  1126.       # *) Clear move info
  1127.       # -----------------------------------------------------------------------
  1128.       def clear_move_info
  1129.         @to_x = -1
  1130.         @to_y = -1
  1131.         @real_x = 0.0
  1132.         @real_y = 0.0
  1133.         @x_speed = 0.0
  1134.         @y_speed = 0.0
  1135.         @jump = 0.0
  1136.         @jump_interval = 0.0
  1137.         @offset = 0
  1138.       end
  1139.       # -----------------------------------------------------------------------
  1140.       # *) Tells the object to move
  1141.       # -----------------------------------------------------------------------
  1142.       def move_to(x,y=0,jump=0,duration=0)
  1143.         # You can also put the coordinate
  1144.         if x.is_a?(Coordinate)
  1145.           target = x
  1146.           move_to(target.x, target.y, y, jump)
  1147.           return
  1148.         end
  1149.         @to_x = x.to_i
  1150.         @to_y = y.to_i
  1151.         @real_x = @obj.x.to_f
  1152.         @real_y = @obj.y.to_f
  1153.         determine_speed(duration,jump)
  1154.       end
  1155.       # -----------------------------------------------------------------------
  1156.       # *) Determine traveling speed
  1157.       # -----------------------------------------------------------------------
  1158.       def determine_speed(duration,jump)
  1159.         @x_speed = (@to_x - @obj.x) / duration.to_f
  1160.         @y_speed = (@to_y - @obj.y) / duration.to_f
  1161.         @jump = jump.to_f
  1162.         @jump_interval = @jump/(duration/2.0)
  1163.       end
  1164.       # -----------------------------------------------------------------------
  1165.       # *) Is object currently moving?
  1166.       # -----------------------------------------------------------------------
  1167.       def moving?
  1168.         return false if @to_x == -1 && @to_y == -1
  1169.         result = @obj.x != @to_x || @obj.y != @to_y
  1170.         return result
  1171.       end
  1172.       # -----------------------------------------------------------------------
  1173.       # *) Update movement
  1174.       # -----------------------------------------------------------------------
  1175.       def update_move
  1176.         @real_x += @x_speed
  1177.         @real_y += @y_speed
  1178.         @obj.x = @real_x.round
  1179.         @obj.y = @real_y.round + @offset.round
  1180.         @jump -= @jump_interval
  1181.         @offset -= @jump
  1182.         clear_move_info unless moving?
  1183.       end
  1184.     end
  1185.     #==========================================================================
  1186.     # -------------------------------------------------------------------------
  1187.     # *) Set the movement object
  1188.     # -------------------------------------------------------------------------
  1189.     def set_obj(obj)
  1190.       # I just added only one instance variable
  1191.       @move_obj = Move_Object.new(obj)
  1192.     end
  1193.     # -------------------------------------------------------------------------
  1194.     # *) Tells the object to move
  1195.     # -------------------------------------------------------------------------
  1196.     def goto(x,y,duration = Graphics.frame_rate,jump = 0.0)
  1197.       @move_obj.move_to(x,y,jump,duration)
  1198.     end
  1199.     # -------------------------------------------------------------------------
  1200.     # *) Tells the object to slide
  1201.     # -------------------------------------------------------------------------
  1202.     def slide(x,y,duration = Graphics.frame_rate,jump = 0.0)
  1203.       slide_x = 0
  1204.       slide_y = 0
  1205.       if x.is_a?(Coordinate)
  1206.         target  = x
  1207.         slide_x = @move_obj.obj.x + target.x
  1208.         slide_y = @move_obj.obj.y + target.y
  1209.       else
  1210.         slide_x = @move_obj.obj.x + x
  1211.         slide_y = @move_obj.obj.y + y
  1212.       end
  1213.       goto(slide_x,slide_y,duration,jump) unless moving?
  1214.     end
  1215.     # -------------------------------------------------------------------------
  1216.     # *) Update movement
  1217.     # -------------------------------------------------------------------------
  1218.     def update_move
  1219.       @move_obj.update_move if moving?
  1220.     end
  1221.     # -------------------------------------------------------------------------
  1222.     # *) Is object moving?
  1223.     # -------------------------------------------------------------------------
  1224.     def moving?
  1225.       @move_obj.moving?
  1226.     end
  1227.     # -------------------------------------------------------------------------
  1228.     # *) Slide up
  1229.     # -------------------------------------------------------------------------
  1230.     def up(range,duration = Graphics.frame_rate,jump = 0.0)
  1231.       slide(0,-range,duration,jump)
  1232.     end
  1233.     # -------------------------------------------------------------------------
  1234.     # *) Slide down
  1235.     # -------------------------------------------------------------------------
  1236.     def down(range,duration = Graphics.frame_rate,jump = 0.0)
  1237.       slide(0,range,duration,jump)
  1238.     end
  1239.     # -------------------------------------------------------------------------
  1240.     # *) Slide to right
  1241.     # -------------------------------------------------------------------------
  1242.     def right(range,duration = Graphics.frame_rate,jump = 0.0)
  1243.       slide(range,0,duration,jump)
  1244.     end
  1245.     # -------------------------------------------------------------------------
  1246.     # *) Slide to left
  1247.     # -------------------------------------------------------------------------
  1248.     def left(range,duration = Graphics.frame_rate,jump = 0.0)
  1249.       slide(-range,0,duration,jump)
  1250.     end
  1251.     # -------------------------------------------------------------------------
  1252.     # *) Slide to upright
  1253.     # -------------------------------------------------------------------------
  1254.     def upright(range,duration = Graphics.frame_rate,jump = 0.0)
  1255.       slide(range,-range,duration,jump)
  1256.     end
  1257.     # -------------------------------------------------------------------------
  1258.     # *) Slide to upleft
  1259.     # -------------------------------------------------------------------------
  1260.     def upleft(range,duration = Graphics.frame_rate,jump = 0.0)
  1261.       slide(-range,-range,duration,jump)
  1262.     end
  1263.     # -------------------------------------------------------------------------
  1264.     # *) Slide to downright
  1265.     # -------------------------------------------------------------------------
  1266.     def downright(range,duration = Graphics.frame_rate,jump = 0.0)
  1267.       slide(range,-range,duration,jump)
  1268.     end
  1269.     # -------------------------------------------------------------------------
  1270.     # *) Slide to downleft
  1271.     # -------------------------------------------------------------------------
  1272.     def downleft(range,duration = Graphics.frame_rate,jump = 0.0)
  1273.       slide(-range,range,duration,jump)
  1274.     end    
  1275.   end
  1276. end
  1277. # -----------------------------------------------------------------------------
  1278. # Implemented to Sprite
  1279. # -----------------------------------------------------------------------------
  1280. class Sprite
  1281.   include THEO::Movement
  1282.  
  1283.   alias theolized_sprite_init initialize
  1284.   def initialize(*args)
  1285.     theolized_sprite_init(*args)
  1286.     set_obj(self)
  1287.   end
  1288.  
  1289.   alias theolized_move_update update
  1290.   def update
  1291.     theolized_move_update
  1292.     update_move
  1293.   end
  1294. end
  1295. # -----------------------------------------------------------------------------
  1296. # Implemented to Window
  1297. # -----------------------------------------------------------------------------
  1298. class Window
  1299.   include THEO::Movement
  1300.  
  1301.   alias theolized_move_window_init initialize
  1302.   def initialize(x, y, width, height)
  1303.     theolized_move_window_init(x, y, width, height)
  1304.     set_obj(self)
  1305.   end
  1306.  
  1307.   alias theolized_move_update update
  1308.   def update
  1309.     theolized_move_update
  1310.     update_move
  1311.   end
  1312.  
  1313. end
  1314. end
  1315. #==============================================================================
  1316. # ** Object Core Fade
  1317. #------------------------------------------------------------------------------
  1318. #  Same as core movement. But this one is dealing with opacity. It can be
  1319. # implemented to any object that has opacity value. Such as Window or Sprite
  1320. #
  1321. # Avalaible methods :
  1322. # - fade(target_opacity, duration)
  1323. # - fadeout(duration)
  1324. # - fadein(duration)
  1325. #
  1326. # I use this module within my sideview battle system
  1327. #------------------------------------------------------------------------------
  1328. if $imported[:Theo_CoreFade]  # Activation flag
  1329. #==============================================================================
  1330. module THEO
  1331.   module FADE
  1332.     # Default duration of fading
  1333.     DEFAULT_DURATION = 60
  1334.     # -------------------------------------------------------------------------
  1335.     # *) Init core fade instance variables
  1336.     # -------------------------------------------------------------------------
  1337.     def init_fade_members
  1338.       @obj = nil
  1339.       @target_opacity = -1
  1340.       @target_contents_opacity = -1
  1341.       @fade_speed = 0.0
  1342.       @fade_speed2 = 0.0
  1343.     end
  1344.     # -------------------------------------------------------------------------
  1345.     # *) Set object
  1346.     # -------------------------------------------------------------------------
  1347.     def setfade_obj(obj)
  1348.       @obj = obj
  1349.       @target_opacity = @obj.opacity
  1350.       @target_contents_opacity = @obj.contents_opacity if window?
  1351.     end
  1352.     # -------------------------------------------------------------------------
  1353.     # *) Fade function
  1354.     # -------------------------------------------------------------------------
  1355.     def fade(opacity, duration = DEFAULT_DURATION)
  1356.       @target_opacity = opacity
  1357.       @target_contents_opacity = opacity if @obj.is_a?(Window)
  1358.       make_fade_speed(duration)
  1359.     end
  1360.     # -------------------------------------------------------------------------
  1361.     # *) Only for window. contents fade
  1362.     # -------------------------------------------------------------------------
  1363.     def contents_fade(opacity, duration = DEFAULT_DURATION)
  1364.       return unless window?
  1365.       @target_contents_opacity = opacity
  1366.       make_fade_speed(duration)
  1367.     end
  1368.     # -------------------------------------------------------------------------
  1369.     # *) Only for window. back fade
  1370.     # -------------------------------------------------------------------------
  1371.     def back_fade(opacity, duration = DEFAULT_DURATION)
  1372.       return unless window?
  1373.       @target_opacity = opacity
  1374.       make_fade_speed(duration)
  1375.     end
  1376.     # -------------------------------------------------------------------------
  1377.     # *) Determine fade speed
  1378.     # -------------------------------------------------------------------------
  1379.     def make_fade_speed(duration)
  1380.       @fade_speed = (@target_opacity - @obj.opacity)/duration.to_f
  1381.       @fade_speed2 = (@target_contents_opacity - @obj.contents_opacity) /
  1382.       duration.to_f if window?
  1383.     end
  1384.     # -------------------------------------------------------------------------
  1385.     # *) Is object a window?
  1386.     # -------------------------------------------------------------------------
  1387.     def window?
  1388.       @obj.is_a?(Window)
  1389.     end
  1390.     # -------------------------------------------------------------------------
  1391.     # *) Fadeout function
  1392.     # -------------------------------------------------------------------------
  1393.     def fadeout(duration = DEFAULT_DURATION)
  1394.       fade(0, duration)
  1395.     end
  1396.     # -------------------------------------------------------------------------
  1397.     # *) Fadein function
  1398.     # -------------------------------------------------------------------------
  1399.     def fadein(duration = DEFAULT_DURATION)
  1400.       fade(255, duration)
  1401.     end
  1402.     # -------------------------------------------------------------------------
  1403.     # *) Update fade
  1404.     # -------------------------------------------------------------------------
  1405.     def update_fade
  1406.       return unless fade?
  1407.       @obj.opacity += @fade_speed
  1408.       @obj.contents_opacity += @fade_speed2 if window?
  1409.       @target_opacity = -1 unless fade?
  1410.     end
  1411.     # -------------------------------------------------------------------------
  1412.     # *) Is performing fade?
  1413.     # -------------------------------------------------------------------------
  1414.     def fade?
  1415.       return false if @target_opacity == -1
  1416.       if window?
  1417.         (@target_opacity != @obj.opacity) &&
  1418.         (@target_contents_opacity != @obj.contents_opacity)
  1419.       else
  1420.         @target_opacity != @obj.opacity
  1421.       end
  1422.     end
  1423.    
  1424.   end
  1425. end
  1426. # -----------------------------------------------------------------------------
  1427. # Implements to Window
  1428. # -----------------------------------------------------------------------------
  1429. class Window
  1430.  
  1431.   include THEO::FADE
  1432.  
  1433.   alias pre_fade_init initialize
  1434.   def initialize(*args)
  1435.     pre_fade_init(*args)
  1436.     init_fade_members
  1437.     setfade_obj(self)
  1438.   end
  1439.  
  1440.   alias pre_fade_update update
  1441.   def update
  1442.     pre_fade_update
  1443.     update_fade
  1444.   end
  1445.  
  1446. end
  1447. # -----------------------------------------------------------------------------
  1448. # Implements to Sprite
  1449. # -----------------------------------------------------------------------------
  1450. class Sprite
  1451.  
  1452.   include THEO::FADE
  1453.  
  1454.   alias pre_fade_init initialize
  1455.   def initialize(*args)
  1456.     pre_fade_init(*args)
  1457.     init_fade_members
  1458.     setfade_obj(self)
  1459.   end
  1460.  
  1461.   alias pre_fade_update update
  1462.   def update
  1463.     pre_fade_update
  1464.     update_fade
  1465.   end
  1466.  
  1467. end
  1468. end
  1469. #==============================================================================
  1470. # ** Object Circular Movement
  1471. #------------------------------------------------------------------------------
  1472. #   This is the most troublesome basic module I have ever made. I wrote this
  1473. # documentation after I made this modules months ago. So I little bit forgot
  1474. # how it works, why I did this, why I did that.
  1475. #
  1476. #   Well, this basic module is deal with circular movement of object. Like our
  1477. # Moon and Earth. You can set which coordinate/object that will be the center
  1478. # rotation. Then tells the object to surround. I'm aware this module is far
  1479. # from perfect. I'm open mind if anyone want to edit to make it better
  1480. #
  1481. # Avalaible Methods :
  1482. # - circle_move(degree, duration)
  1483. # - center_coordinate(x,y)
  1484. # - center_object(obj)
  1485. # - endless_circle(speed)
  1486. #------------------------------------------------------------------------------
  1487. if $imported[:Theo_Circular]  # Activation flag
  1488. #==============================================================================
  1489. module THEO
  1490.   module Circular
  1491.     class Circular_Obj
  1492.       attr_reader :ox           # Center coordinate X
  1493.       attr_reader :oy           # Center coordinate Y
  1494.       attr_reader :center_obj   # Center object
  1495.       attr_reader :obj          # Object
  1496.       # -----------------------------------------------------------------------
  1497.       # *) Initialize
  1498.       # -----------------------------------------------------------------------
  1499.       def initialize(obj,ox,oy,center_obj)
  1500.         @obj = obj
  1501.         @center_obj = center_obj if can_move?(center_obj)
  1502.         @ox = ox
  1503.         @oy = oy
  1504.         @speed = 0
  1505.         @endless = false
  1506.         @freeze = false
  1507.         refresh_info
  1508.       end
  1509.       # -----------------------------------------------------------------------
  1510.       # *) Refresh information
  1511.       # -----------------------------------------------------------------------
  1512.       def refresh_info
  1513.         @radius = radius
  1514.         @degree_dest = @current_degree = get_degree
  1515.       end
  1516.       # -----------------------------------------------------------------------
  1517.       # *) Set center object
  1518.       # -----------------------------------------------------------------------
  1519.       def center_obj=(center)
  1520.         return unless can_move?(center)
  1521.         @center_obj = center
  1522.         refresh_info
  1523.       end
  1524.       # -----------------------------------------------------------------------
  1525.       # *) Set center coordinate X
  1526.       # -----------------------------------------------------------------------
  1527.       def ox=(ox)
  1528.         @ox = ox
  1529.         refresh_info
  1530.       end
  1531.       # -----------------------------------------------------------------------
  1532.       # *) Set center coordinate Y
  1533.       # -----------------------------------------------------------------------
  1534.       def oy=(oy)
  1535.         @oy = oy
  1536.         refresh_info
  1537.       end
  1538.       # -----------------------------------------------------------------------
  1539.       # *) Is object can move?
  1540.       # -----------------------------------------------------------------------
  1541.       def can_move?(obj)
  1542.         obj.respond_to?("x") && obj.respond_to?("y")
  1543.       end
  1544.       # -----------------------------------------------------------------------
  1545.       # *) Circle move
  1546.       # -----------------------------------------------------------------------
  1547.       def circle_move(degree,duration)
  1548.         return if endless?
  1549.         @degree_dest = (@current_degree + degree)
  1550.         @speed = (@degree_dest - @current_degree) / duration.to_f
  1551.       end
  1552.       # -----------------------------------------------------------------------
  1553.       # *) Go to specified degree
  1554.       # -----------------------------------------------------------------------
  1555.       def circle_goto(degree,duration)
  1556.         return if endless?
  1557.         @degree_dest = degree
  1558.         @speed = (@degree_dest - @current_degree) / duration.to_f
  1559.       end
  1560.       # -----------------------------------------------------------------------
  1561.       # *) Endless circle movement
  1562.       # -----------------------------------------------------------------------
  1563.       def endless_circle(speed)
  1564.         @speed = speed
  1565.         @endless = true
  1566.       end
  1567.       # -----------------------------------------------------------------------
  1568.       # *) Get current degree from center point
  1569.       # -----------------------------------------------------------------------
  1570.       def get_degree
  1571.         Math.degree(Math.atan2(range_y,range_x)) rescue 0
  1572.       end
  1573.       # -----------------------------------------------------------------------
  1574.       # *) Get distance X from center point
  1575.       # -----------------------------------------------------------------------
  1576.       def range_x
  1577.         @obj.x - center_x
  1578.       end
  1579.       # -----------------------------------------------------------------------
  1580.       # *) Get distance Y from center point
  1581.       # -----------------------------------------------------------------------
  1582.       def range_y
  1583.         @obj.y - center_y
  1584.       end
  1585.       # -----------------------------------------------------------------------
  1586.       # *) Get coordinate X from center point
  1587.       # -----------------------------------------------------------------------
  1588.       def center_x
  1589.         @center_obj.nil? ? @ox : @center_obj.x
  1590.       end
  1591.       # -----------------------------------------------------------------------
  1592.       # *) Get coordinate Y from center point
  1593.       # -----------------------------------------------------------------------
  1594.       def center_y
  1595.         @center_obj.nil? ? @oy : @center_obj.y
  1596.       end
  1597.       # -----------------------------------------------------------------------
  1598.       # *) Get radius value from center point
  1599.       # -----------------------------------------------------------------------
  1600.       def radius
  1601.         Math.sqrt((range_x**2) + (range_y**2))
  1602.       end
  1603.       # -----------------------------------------------------------------------
  1604.       # *) Update circle movement
  1605.       # -----------------------------------------------------------------------
  1606.       def update_circle
  1607.         return if circle_frozen?
  1608.         return unless rotate? || @endless
  1609.         @current_degree += @speed
  1610.         update_x
  1611.         update_y
  1612.         @degree_dest = @current_degree if @endless
  1613.       end
  1614.       # -----------------------------------------------------------------------
  1615.       # *) Object is moving?
  1616.       # -----------------------------------------------------------------------
  1617.       def rotate?
  1618.         return @current_degree.round != @degree_dest.round
  1619.       end
  1620.       # -----------------------------------------------------------------------
  1621.       # *) Update X position
  1622.       # -----------------------------------------------------------------------
  1623.       def update_x
  1624.         @obj.x = center_x + (@radius *
  1625.           Math.cos(Math.radian(@current_degree))).round
  1626.       end
  1627.       # -----------------------------------------------------------------------
  1628.       # *) Update Y position
  1629.       # -----------------------------------------------------------------------
  1630.       def update_y
  1631.         @obj.y = center_y + (@radius *
  1632.           Math.sin(Math.radian(@current_degree))).round
  1633.       end
  1634.       # -----------------------------------------------------------------------
  1635.       # *) Freeze circle
  1636.       # -----------------------------------------------------------------------
  1637.       def circle_freeze
  1638.         @freeze = true
  1639.       end
  1640.       # -----------------------------------------------------------------------
  1641.       # *) Unfreeze circle
  1642.       # -----------------------------------------------------------------------
  1643.       def circle_unfreeze
  1644.         @freeze = false
  1645.       end
  1646.       # -----------------------------------------------------------------------
  1647.       # *) Is frozen?
  1648.       # -----------------------------------------------------------------------
  1649.       def circle_frozen?
  1650.         @freeze
  1651.       end
  1652.       # -----------------------------------------------------------------------
  1653.       # *) Stop circling
  1654.       # -----------------------------------------------------------------------
  1655.       def stop
  1656.         @endless = false
  1657.       end
  1658.       # -----------------------------------------------------------------------
  1659.       # *) Continue circling
  1660.       # -----------------------------------------------------------------------
  1661.       def continue
  1662.         @endless = true
  1663.       end
  1664.       # -----------------------------------------------------------------------
  1665.       # *) Endless circle?
  1666.       # -----------------------------------------------------------------------
  1667.       def endless?
  1668.         @endless
  1669.       end
  1670.     end
  1671.     # =========================================================================
  1672.     # These methods below aren't necessary to comment
  1673.     # -------------------------------------------------------------------------
  1674.     def set_circle(obj,ox = 0,oy = 0,center_obj = nil)
  1675.       @circle = Circular_Obj.new(obj,ox,oy,center_obj)
  1676.     end
  1677.    
  1678.     def stop
  1679.       @circle.stop
  1680.     end
  1681.    
  1682.     def continue
  1683.       @circle.continue
  1684.     end
  1685.    
  1686.     def endless?
  1687.       @circle.endless?
  1688.     end
  1689.    
  1690.     def circle_move(degree, dur = Graphics.frame_rate)
  1691.       @circle.circle_move(degree,dur)
  1692.     end
  1693.    
  1694.     def endless_circle(speed)
  1695.       @circle.endless_circle(speed)
  1696.     end
  1697.    
  1698.     def circle_freeze
  1699.       @circle.circle_freeze
  1700.     end
  1701.    
  1702.     def circle_unfreeze
  1703.       @circle.circle_unfreeze
  1704.     end
  1705.    
  1706.     def circle_frozed?
  1707.       @circle.circle_frozen?
  1708.     end
  1709.    
  1710.     def update_circle
  1711.       @circle.update_circle
  1712.     end
  1713.    
  1714.     def rotating?
  1715.       @circle.rotate?
  1716.     end
  1717.    
  1718.     def center_coordinate(ox,oy)
  1719.       @circle.ox = ox
  1720.       @circle.oy = oy
  1721.     end
  1722.    
  1723.     def center_distance(ox,oy)
  1724.       @circle.ox = @circle.obj.x + ox
  1725.       @circle.oy = @circle.obj.y + oy
  1726.     end
  1727.    
  1728.     def center_obj(obj)
  1729.       @circle.center_obj = obj
  1730.     end
  1731.    
  1732.     def refresh_info
  1733.       @circle.refresh_info
  1734.     end
  1735.    
  1736.   end
  1737. end
  1738. # -----------------------------------------------------------------------------
  1739. # Implements to window
  1740. # -----------------------------------------------------------------------------
  1741. class Window
  1742.  
  1743.   include THEO::Circular
  1744.  
  1745.   alias theolized_circle_window_init initialize
  1746.   def initialize(*args)
  1747.     theolized_circle_window_init(*args)
  1748.     set_circle(self,x,y)
  1749.   end
  1750.  
  1751.   alias window_circle_update update
  1752.   def update
  1753.     window_circle_update
  1754.     update_circle
  1755.   end
  1756.  
  1757. end
  1758. # -----------------------------------------------------------------------------
  1759. # Implements to sprite
  1760. # -----------------------------------------------------------------------------
  1761. class Sprite
  1762.  
  1763.   include THEO::Circular
  1764.  
  1765.   alias theolized_circle_init initialize
  1766.   def initialize(*args)
  1767.     theolized_circle_init(*args)
  1768.     set_circle(self,x,y)
  1769.   end
  1770.  
  1771.   alias sprite_circle_update update
  1772.   def update
  1773.     sprite_circle_update
  1774.     update_circle
  1775.   end
  1776.  
  1777. end
  1778. end
  1779. #==============================================================================
  1780. # ** Clone Image / Afterimage Base
  1781. #------------------------------------------------------------------------------
  1782. #  This basic modules is purposely to make sprite can be cloned / duplicated.
  1783. # In more complex concept, it could be used as base of afterimage. It's used
  1784. # within my sideview battle system
  1785. #------------------------------------------------------------------------------
  1786. if $imported[:Theo_CloneImage]  # Activation flag
  1787. #==============================================================================
  1788. class Sprite
  1789.   attr_reader :clone_bitmap
  1790.   # ---------------------------------------------------------------------------
  1791.   # *) Aliased Initialize
  1792.   # ---------------------------------------------------------------------------
  1793.   alias theo_clonesprites_init initialize
  1794.   def initialize(*args)
  1795.     @cloned_sprites = []
  1796.     theo_clonesprites_init(*args)
  1797.   end
  1798.   # ---------------------------------------------------------------------------
  1799.   # *) Base function to clone sprite
  1800.   # ---------------------------------------------------------------------------
  1801.   def clone(z_pos = 0, clone_bitmap = false)
  1802.     @cloned_sprites.delete_if {|spr| spr.disposed? }
  1803.     cloned = clone_class.new(viewport)
  1804.     cloned.x = x
  1805.     cloned.y = y
  1806.     cloned.bitmap = bitmap
  1807.     cloned.bitmap = bitmap.clone if clone_bitmap
  1808.     if z_pos != 0
  1809.       cloned.z = z + z_pos
  1810.     else
  1811.       @cloned_sprites.each do |spr|
  1812.         spr.z -= 1
  1813.       end
  1814.       cloned.z = z - 1
  1815.     end
  1816.     cloned.src_rect.set(src_rect)
  1817.     cloned.zoom_x = zoom_x
  1818.     cloned.zoom_y = zoom_y
  1819.     cloned.angle = angle
  1820.     cloned.mirror = mirror
  1821.     cloned.opacity = opacity
  1822.     cloned.blend_type = blend_type
  1823.     cloned.color.set(color)
  1824.     cloned.tone.set(tone)
  1825.     cloned.visible = visible
  1826.     cloned.bush_depth = bush_depth
  1827.     cloned.bush_opacity = bush_opacity
  1828.     cloned.ox = ox
  1829.     cloned.oy = oy
  1830.     on_after_cloning(cloned)
  1831.     @cloned_sprites.push(cloned)
  1832.     cloned
  1833.   end
  1834.    
  1835.   def on_after_cloning(cloned)
  1836.     # Abstract method. Overwrite it as you want
  1837.   end
  1838.  
  1839.   # Sprite class for cloned sprite
  1840.   def clone_class
  1841.     Sprite
  1842.   end
  1843.  
  1844. end
  1845.  
  1846. # =============================================================================
  1847. # Afterimages basic module for all sprite base instance object. Any classes
  1848. # that inherited from Sprite_Base will highly possible to make afterimage
  1849. # effect.
  1850. #
  1851. # How to setup :
  1852. # - Make sure "def afterimage" should return to true
  1853. # - Set rate and opacity easing
  1854. # - Higher rate means the higher delay between displayed afterimage
  1855. # - Higher opac means the faster afterimage will fadeout
  1856. # =============================================================================
  1857.  
  1858. class Sprite_Base < Sprite
  1859.   attr_accessor :afterimage       # Afterimage flag
  1860.   attr_accessor :afterimage_opac  # Afterimage opacity easing
  1861.   attr_accessor :afterimage_rate  # Afterimage thick rate
  1862.   # ---------------------------------------------------------------------------
  1863.   # *) Aliased initialize
  1864.   # ---------------------------------------------------------------------------
  1865.   alias theo_afterimagebase_init initialize
  1866.   def initialize(*args)
  1867.     init_afterimage_base
  1868.     theo_afterimagebase_init(*args)
  1869.   end
  1870.   # ---------------------------------------------------------------------------
  1871.   # *) Initialize afterimage variables
  1872.   # ---------------------------------------------------------------------------
  1873.   def init_afterimage_base
  1874.     @afterimage = false
  1875.     @afterimage_count = 0
  1876.     @afterimage_rate = 3
  1877.     @afterimage_opac = 5
  1878.     @afterimages = []
  1879.   end
  1880.   # ---------------------------------------------------------------------------
  1881.   # *) Aliased update method
  1882.   # ---------------------------------------------------------------------------
  1883.   alias theo_afterimagebase_update update
  1884.   def update
  1885.     theo_afterimagebase_update
  1886.     update_afterimage_effect
  1887.   end
  1888.   # ---------------------------------------------------------------------------
  1889.   # *) Update afterimage
  1890.   # ---------------------------------------------------------------------------
  1891.   def update_afterimage_effect
  1892.     # Update and delete afterimage once its opacity has reached zero
  1893.     @afterimages.delete_if do |image|
  1894.       image.opacity -= afterimage_opac
  1895.       image.update if updating_afterimages?
  1896.       if image.opacity == 0
  1897.         image.dispose
  1898.       end
  1899.       image.disposed?
  1900.     end
  1901.     return unless afterimage
  1902.     @afterimage_count += 1
  1903.     if @afterimage_count % afterimage_rate == 0
  1904.       @afterimages.push(clone)
  1905.     end
  1906.   end
  1907.   # ---------------------------------------------------------------------------
  1908.   # *) Is afterimages are need to be updated?
  1909.   # ---------------------------------------------------------------------------
  1910.   def updating_afterimages?
  1911.     return false
  1912.   end
  1913.   # ---------------------------------------------------------------------------
  1914.   # *) Disposes sprite alongside the afterimage to prevent RGSS3 crash
  1915.   # ---------------------------------------------------------------------------
  1916.   alias theo_afterimagebase_dispose dispose
  1917.   def dispose
  1918.     theo_afterimagebase_dispose
  1919.     @afterimages.each do |afimg|
  1920.       afimg.dispose
  1921.     end
  1922.   end
  1923.  
  1924. end
  1925. end
  1926. #==============================================================================
  1927. # ** Sprite Rotate Basic module
  1928. #------------------------------------------------------------------------------
  1929. #  This basic module only dealing with sprite angle. It's used within my
  1930. # sideview battle system to rotate weapon icon.
  1931. #------------------------------------------------------------------------------
  1932. if $imported[:Theo_RotateImage]
  1933. #==============================================================================
  1934. module Theo
  1935.   module Rotation
  1936.     # -------------------------------------------------------------------------
  1937.     # *) Init rotate function
  1938.     # -------------------------------------------------------------------------
  1939.     def init_rotate
  1940.       @degree = self.angle.to_f
  1941.       @target_degree = 0
  1942.       @rotating = false
  1943.       @rotate_speed = 0.0
  1944.     end
  1945.     # -------------------------------------------------------------------------
  1946.     # *) Change to specific angle
  1947.     # -------------------------------------------------------------------------
  1948.     def change_angle(target_degree, duration)
  1949.       @degree = self.angle.to_f
  1950.       @target_degree = target_degree.to_f
  1951.       if duration == 0
  1952.         @rotate_speed = target_degree
  1953.       else
  1954.         @rotate_speed = (target_degree - @degree) / duration
  1955.       end
  1956.       @rotating = true
  1957.     end
  1958.     # -------------------------------------------------------------------------
  1959.     # *) Update rotation
  1960.     # -------------------------------------------------------------------------
  1961.     def update_rotation
  1962.       return unless @rotating
  1963.       @degree += @rotate_speed
  1964.       new_angle = @degree.round
  1965.       self.angle = new_angle
  1966.       if new_angle == @target_degree
  1967.         init_rotate
  1968.       end
  1969.     end
  1970.     # -------------------------------------------------------------------------
  1971.     # *) Start rotate to additional degree
  1972.     # -------------------------------------------------------------------------
  1973.     def start_rotate(degree_plus, duration)
  1974.       change_angle(@degree + degree_plus, duration)
  1975.     end
  1976.    
  1977.   end
  1978. end
  1979. # -----------------------------------------------------------------------------
  1980. # Implements on sprite
  1981. # -----------------------------------------------------------------------------
  1982. class Sprite
  1983.   include Theo::Rotation
  1984.  
  1985.   alias theo_spr_rotate_init initialize
  1986.   def initialize(viewport = nil)
  1987.     theo_spr_rotate_init(viewport)
  1988.     init_rotate
  1989.   end
  1990.  
  1991.   alias theo_spr_rotate_update update
  1992.   def update
  1993.     theo_spr_rotate_update
  1994.     update_rotation
  1995.   end
  1996.  
  1997. end
  1998.  
  1999. end
  2000. #==============================================================================
  2001. # ** Smooth_Slide
  2002. #------------------------------------------------------------------------------
  2003. #  This module provides basic module for smooth sliding. It can be implemented
  2004. # to any classes as long as they have x and y.
  2005. #
  2006. # Avalaible methods :
  2007. # - smooth_move(x,y,dur,[reverse])
  2008. #   Tells the object to move to specific x,y coordinate in given time duration
  2009. #   in frame. If reverse set to true, object will start with maximum speed
  2010. #
  2011. # - smooth_slide(x,y,dur,[reverse])
  2012. #   Tells the object to slide to specific x,y coordinate from original position
  2013. #   in given time duration in frame. If reverse set to true, object will start
  2014. #   with maximum speed
  2015. #------------------------------------------------------------------------------
  2016. if $imported[:Theo_SmoothMove]  # Activation Flag
  2017. #==============================================================================
  2018. module Smooth_Slide
  2019.   # ---------------------------------------------------------------------------
  2020.   # Initialize smooth movement data
  2021.   # ---------------------------------------------------------------------------
  2022.   def init_smove
  2023.     @smooth_dur = 0     # Travel duration
  2024.     @accel_x = 0.0      # Acceleration X
  2025.     @accel_y = 0.0      # Acceleration Y
  2026.     @vel_x = 0.0        # Velocity X
  2027.     @vel_y = 0.0        # Velocity Y
  2028.     @sreal_x = self.x   # Real position X
  2029.     @sreal_y = self.y   # Real Position Y
  2030.     @starget_x = 0.0    # Target X
  2031.     @starget_y = 0.0    # Target Y
  2032.     @srev = false       # Reverse Flag
  2033.     @smove = false      # Moving Flag
  2034.    
  2035.     # The reason why I give 's' in front of most instance variables is for
  2036.     # uniqueness. Well, if there is any basic module / script out there that
  2037.     # also use same instance name, it may causes incompatibility
  2038.    
  2039.   end
  2040.   # ---------------------------------------------------------------------------
  2041.   # Do smooth movement
  2042.   # ---------------------------------------------------------------------------
  2043.   def smooth_move(x,y,dur,reverse = false)
  2044.     init_smove
  2045.     @srev = reverse
  2046.     @smove = true
  2047.     @smooth_dur = dur
  2048.     @sreal_x = self.x.to_f
  2049.     @sreal_y = self.y.to_f
  2050.     @starget_x = x.to_i
  2051.     @starget_y = y.to_i
  2052.     calc_accel
  2053.   end
  2054.   # ---------------------------------------------------------------------------
  2055.   # Do smooth slide
  2056.   # ---------------------------------------------------------------------------
  2057.   def smooth_slide(x,y,dur,reverse = false)
  2058.     tx = x + self.x
  2059.     ty = y + self.y
  2060.     smooth_move(tx,ty,dur,reverse)
  2061.   end
  2062.   # ---------------------------------------------------------------------------
  2063.   # Calculate acceleration
  2064.   # ---------------------------------------------------------------------------
  2065.   def calc_accel
  2066.     # Get travel distance
  2067.     dist_x = @starget_x - @sreal_x
  2068.     dist_y = @starget_y - @sreal_y
  2069.    
  2070.     # Calculate acceleration
  2071.     @accel_x = (dist_x/((@smooth_dur**2)/2.0))
  2072.     @accel_y = (dist_y/((@smooth_dur**2)/2.0))
  2073.    
  2074.     # If reverse, velocity started on max speed
  2075.     if @srev
  2076.       @vel_x = (@accel_x * (@smooth_dur.to_f - 0.5))
  2077.       @vel_y = (@accel_y * (@smooth_dur.to_f - 0.5))
  2078.     end
  2079.   end
  2080.   # ---------------------------------------------------------------------------
  2081.   # Execute end smooth
  2082.   # ---------------------------------------------------------------------------
  2083.   def end_smooth_slide
  2084.     init_smove
  2085.   end
  2086.   # ---------------------------------------------------------------------------
  2087.   # Update Smooth movement
  2088.   # ---------------------------------------------------------------------------
  2089.   def update_smove
  2090.     if @smove
  2091.       # If not reversed. Increase velocity on the first time executed
  2092.       if !@srev
  2093.         @vel_x += @accel_x
  2094.         @vel_y += @accel_y
  2095.       end
  2096.      
  2097.       # Give increment / decrement upon real position
  2098.       @sreal_x = (@starget_x > @sreal_x ?
  2099.         [@sreal_x + @vel_x, @starget_x].min :
  2100.         [@sreal_x + @vel_x, @starget_x].max)
  2101.       @sreal_y = (@starget_y > @sreal_y ?
  2102.         [@sreal_y + @vel_y, @starget_y].min :
  2103.         [@sreal_y + @vel_y, @starget_y].max)
  2104.      
  2105.       # Rounded real position
  2106.       self.x = @sreal_x.round
  2107.       self.y = @sreal_y.round
  2108.      
  2109.       # If reversed, decrease velocity after make a change to real position
  2110.       if @srev
  2111.         @vel_x -= @accel_x
  2112.         @vel_y -= @accel_y
  2113.       end
  2114.      
  2115.       # If target has reach it's destination, call end execution
  2116.       if (self.x == @starget_x && self.y == @starget_y)
  2117.         end_smooth_slide
  2118.       end
  2119.     end
  2120.   end
  2121.   # ---------------------------------------------------------------------------
  2122.   # Get smooth moving flag
  2123.   # ---------------------------------------------------------------------------
  2124.   def smoving?
  2125.     @smove
  2126.   end
  2127.  
  2128. end
  2129. # -----------------------------------------------------------------------------
  2130. # Implemented on sprite
  2131. # -----------------------------------------------------------------------------
  2132. class Sprite
  2133.   include Smooth_Slide
  2134.  
  2135.   alias smooth_init initialize
  2136.   def initialize(v=nil)
  2137.     smooth_init(v)
  2138.     init_smove
  2139.   end
  2140.  
  2141.   alias smooth_update update
  2142.   def update
  2143.     smooth_update
  2144.     update_smove
  2145.   end
  2146.  
  2147. end
  2148. # -----------------------------------------------------------------------------
  2149. # Implemented on window
  2150. # -----------------------------------------------------------------------------
  2151. class Window
  2152.   include Smooth_Slide
  2153.  
  2154.   alias smooth_init initialize
  2155.   def initialize(*args)
  2156.     smooth_init(*args)
  2157.     init_smove
  2158.   end
  2159.  
  2160.   alias smooth_update update
  2161.   def update
  2162.     smooth_update
  2163.     update_smove
  2164.   end
  2165.  
  2166. end
  2167.  
  2168. end
  2169. # =============================================================================
  2170. # Below this line are just miscellaneous functions
  2171. # -----------------------------------------------------------------------------
  2172. # Debug print line
  2173. # -----------------------------------------------------------------------------
  2174. def debug
  2175.   puts "This line is executed"
  2176. end
  2177. # -----------------------------------------------------------------------------
  2178. # Debug print frame
  2179. # -----------------------------------------------------------------------------
  2180. def debugc
  2181.   puts Graphics.frame_count
  2182. end
  2183. # -----------------------------------------------------------------------------
  2184. # Copy object. Because calling clone/dup sometimes is not enough
  2185. # -----------------------------------------------------------------------------
  2186. def copy(object)
  2187.   Marshal.load(Marshal.dump(object))
  2188. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement