khanhdu

TheoAllen - Basic Modules

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