Advertisement
LiTTleDRAgo

[RGSS] DRG - Pixel Movement

May 7th, 2013
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.18 KB | None | 0 0
  1. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  2. # DRG - Pixel Movement
  3. # Version: 1.12
  4. # Author : LiTTleDRAgo
  5. #:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:=:
  6. ($imported ||= {})[:drg_pixel_movement] = 1.12
  7. #==============================================================================
  8. #
  9. # Introduction :
  10. #
  11. #   This script makes player's movement became pixel movement.
  12. #   Event pixel movement turned off by default.
  13. #
  14. #   To activate / deactivate pixel movement
  15. #   - $game_player.pixel_movement     = true / false
  16. #   - $game_map.events[ID].pixel_movement = true / false
  17. #
  18. #==============================================================================
  19. #==============================================================================
  20. # ** Game_Character
  21. #------------------------------------------------------------------------------
  22. #  This class deals with characters. It's used as a superclass for the
  23. #  Game_Player and Game_Event classes.
  24. #==============================================================================
  25.  
  26. class Game_Character
  27.   #--------------------------------------------------------------------------
  28.   # * Public Instance Variables
  29.   #--------------------------------------------------------------------------
  30.   attr_accessor :pixel_movement, :direction_fix
  31.   #--------------------------------------------------------------------------
  32.   # * Others
  33.   #--------------------------------------------------------------------------
  34.   define_method(:x_with_direction) {|x, d| x + (d==6 ? 1 : d==4 ? -1 : 0)}
  35.   define_method(:y_with_direction) {|y, d| y + (d==2 ? 1 : d==8 ? -1 : 0)}
  36.   define_method(:x_pixel_direction) {|x, d| x + (d==6 ? 0.5 : d==4 ? -0.5 : 0)}
  37.   define_method(:y_pixel_direction) {|y, d| y + (d==2 ? 0.5 : d==8 ? -0.5 : 0)}
  38.   define_method(:revise_x) { pixel_disable? ? 0 : @revise_x ||= 0 }
  39.   define_method(:revise_y) { pixel_disable? ? 0 : @revise_y ||= 0 }
  40.   #--------------------------------------------------------------------------
  41.   # * Constant
  42.   #--------------------------------------------------------------------------
  43.   ALIASING_PIXEL = lambda do |x|
  44.     $@ || alias_method(:"update_move_unpixel_#{x}", :update_move)
  45.     $@ || alias_method(:"moving_unpixel_#{x}", :moving?)
  46.     [:move_down,:move_left,:move_right,:move_up].each do |meth|
  47.       $@ || alias_method(:"#{meth}_unpixel_#{x}", :"#{meth}")
  48.       define_method(:"#{meth}") do |*a|
  49.         pixel_disable? ?  send(:"#{meth}_unpixel_#{x}",*a) :
  50.           send(:"#{meth}_pixel",*a)
  51.       end
  52.     end
  53.     [:move_lower_left,:move_lower_right,:move_upper_left,
  54.      :move_upper_right].each do |meth|
  55.       $@ || alias_method(:"#{meth}_unpixel_#{x}", :"#{meth}")
  56.       define_method(:"#{meth}") do |*a|
  57.         reset_pixel && send(:"#{meth}_unpixel_#{x}",*a)
  58.       end
  59.     end
  60.    
  61.     define_method(:update_move) do |*a|
  62.       if pixel_disable?
  63.         reset_pixel && send(:"update_move_unpixel_#{x}",*a)
  64.       else
  65.         if (@x + revise_x) * 128 != @real_x && (@y + revise_y) * 128 != @real_y
  66.           distance = 2 ** @move_speed / Math.sqrt(2)
  67.         else
  68.           distance = 2 ** @move_speed
  69.         end
  70.         if (@y + revise_y) * 128 > @real_y
  71.           @real_y = [@real_y + distance, (@y + revise_y) * 128].min
  72.         end
  73.         if (@x + revise_x) * 128 < @real_x
  74.           @real_x = [@real_x - distance, (@x + revise_x) * 128].max
  75.         end
  76.         if (@x + revise_x) * 128 > @real_x
  77.           @real_x = [@real_x + distance, (@x + revise_x) * 128].min
  78.         end
  79.         if (@y + revise_y) * 128 < @real_y
  80.           @real_y = [@real_y - distance, (@y + revise_y) * 128].max
  81.         end
  82.         if @walk_anime
  83.           @anime_count += 1.5
  84.         elsif @step_anime
  85.           @anime_count += 1
  86.         end
  87.       end
  88.     end
  89.    
  90.     define_method(:moving?) do |*a|
  91.       if pixel_disable?
  92.         send(:"moving_unpixel_#{x}",*a)
  93.       else
  94.         (@real_x != (@x + revise_x) * 128 || @real_y != (@y + revise_y) * 128)
  95.       end
  96.     end
  97.   end
  98.   #--------------------------------------------------------------------------
  99.   # * xy_pixel_correction
  100.   #--------------------------------------------------------------------------
  101.   def xy_pixel_correction(direction)
  102.     case direction
  103.     when 2
  104.       unless passable?(@x + revise_x.ceil, @y + revise_y.ceil, 2)
  105.         if passable?(@x + @revise_x.floor, @y + @revise_y.ceil, 2)
  106.           (@x += @revise_x.floor) && @revise_x = 0
  107.         end
  108.       end
  109.       unless passable?(@x + revise_x.floor, @y + revise_y.ceil, 2)
  110.         if passable?(@x + @revise_x.ceil, @y + @revise_y.ceil, 2)
  111.           (@x += @revise_x.ceil) && @revise_x = 0  
  112.         end
  113.       end
  114.     when 4
  115.       unless passable?(@x + revise_x.floor, @y + revise_y.ceil, 4)
  116.         if passable?(@x + @revise_x.ceil, @y + @revise_y.ceil, 4)
  117.           (@x += @revise_x.floor) && @revise_x = 0
  118.         end
  119.       end
  120.       unless passable?(@x + revise_x.floor, @y + revise_y.ceil, 4)
  121.         if passable?(@x + @revise_x.floor, @y + @revise_y.floor, 4)
  122.           (@y += @revise_y.floor) && @revise_y = 0
  123.         end
  124.       end
  125.       unless passable?(@x + revise_x.floor, @y + revise_y.floor, 4)
  126.         if passable?(@x + @revise_x.floor, @y + @revise_y.ceil, 4)
  127.           (@y += @revise_y.ceil)  && @revise_y = 0
  128.         end
  129.       end
  130.     when 6
  131.       unless passable?(@x + revise_x.floor, @y + revise_y.ceil, 6)
  132.         if passable?(@x + @revise_x.floor, @y + @revise_y.floor, 6)
  133.           (@y += @revise_y.floor) && @revise_y = 0
  134.         end
  135.       end
  136.       unless passable?(@x + revise_x.ceil, @y + revise_y.floor, 6)
  137.         if passable?(@x + @revise_x.ceil, @y + @revise_y.ceil, 6)
  138.           (@y += @revise_y.ceil) && @revise_y = 0
  139.         end
  140.       end
  141.     when 8
  142.       unless passable?(@x + revise_x.floor, @y + revise_y.floor, 8)
  143.         if passable?(@x + @revise_x.floor, @y + @revise_y.ceil, 8)
  144.           (@y += @revise_y.floor) && @revise_y = 0
  145.         end
  146.       end
  147.       unless passable?(@x + revise_x.ceil, @y + revise_y.floor, 8)
  148.         if passable?(@x + @revise_x.floor, @y + @revise_y.floor, 8)
  149.           (@x += @revise_x.floor) && @revise_x = 0
  150.         end
  151.       end
  152.       unless passable?(@x + revise_x.floor, @y + revise_y.floor, 8)
  153.         if passable?(@x + @revise_x.ceil, @y + @revise_y.floor, 8)
  154.           (@x += @revise_x.ceil) && @revise_x = 0
  155.         end
  156.       end
  157.     end  
  158.     direction
  159.   end
  160.   #--------------------------------------------------------------------------
  161.   # * add_revise_coord
  162.   #--------------------------------------------------------------------------
  163.   def add_revise_coord
  164.     @revise_x % 1 == 0 && (@x += @revise_x.round) && (@revise_x = 0)
  165.     @revise_y % 1 == 0 && (@y += @revise_y.round) && (@revise_y = 0)
  166.   end
  167.   #--------------------------------------------------------------------------
  168.   # * Pixel Disable
  169.   #--------------------------------------------------------------------------
  170.   def pixel_disable?
  171.     return true unless self.pixel_movement  
  172.     return true if @move_route_forcing && @move_route.list.any? do |s|
  173.       (1..14).include?(s.code)
  174.     end
  175.     return false
  176.   end
  177.   #--------------------------------------------------------------------------
  178.   # * reset_pixel
  179.   #--------------------------------------------------------------------------
  180.   def reset_pixel
  181.     return 0 if (@revise_x ||= 0) == 0 && (@revise_y ||= 0) == 0
  182.     xy_pixel_correction(@direction)
  183.     @x, @y = (@x + revise_x).floor, (@y + revise_y).floor
  184.     @revise_x = @revise_y = 0
  185.   end
  186.   #--------------------------------------------------------------------------
  187.   # * Move Down
  188.   #     turn_enabled : a flag permits direction change on that spot
  189.   #--------------------------------------------------------------------------
  190.   def move_down_pixel(turn_enabled = true)
  191.     @quarter = false
  192.     turn_enabled && turn_down
  193.     if passable?(@x, @y, xy_pixel_correction(2))
  194.       turn_down
  195.       @revise_y = y_pixel_direction(@revise_y,2)
  196.       add_revise_coord
  197.       increase_steps
  198.     else
  199.       check_event_trigger_touch(@x, y_with_direction(@y,2))
  200.     end
  201.   end
  202.   #--------------------------------------------------------------------------
  203.   # * Move Left
  204.   #     turn_enabled : a flag permits direction change on that spot
  205.   #--------------------------------------------------------------------------
  206.   def move_left_pixel(turn_enabled = true)
  207.     @quarter = false
  208.     turn_enabled && turn_left
  209.     if passable?(@x, @y, xy_pixel_correction(4))
  210.       turn_left
  211.       @revise_x = x_pixel_direction(@revise_x,4)
  212.       add_revise_coord
  213.       increase_steps
  214.     else
  215.       check_event_trigger_touch(x_with_direction(@x,4), @y)
  216.     end
  217.   end
  218.   #--------------------------------------------------------------------------
  219.   # * Move Right
  220.   #     turn_enabled : a flag permits direction change on that spot
  221.   #--------------------------------------------------------------------------
  222.   def move_right_pixel(turn_enabled = true)
  223.     @quarter = false
  224.     turn_enabled && turn_right
  225.     if passable?(@x, @y, xy_pixel_correction(6))
  226.       turn_right
  227.       @revise_x = x_pixel_direction(@revise_x,6)
  228.       add_revise_coord
  229.       increase_steps
  230.     else
  231.       check_event_trigger_touch(x_with_direction(@x,6), @y)
  232.     end
  233.   end
  234.   #--------------------------------------------------------------------------
  235.   # * Move up
  236.   #     turn_enabled : a flag permits direction change on that spot
  237.   #--------------------------------------------------------------------------
  238.   def move_up_pixel(turn_enabled = true)
  239.     @quarter = false
  240.     turn_enabled && turn_up
  241.     if passable?(@x, @y, xy_pixel_correction(8))
  242.       turn_up
  243.       @revise_y = y_pixel_direction(@revise_y, 8)
  244.       add_revise_coord
  245.       increase_steps
  246.     else
  247.       check_event_trigger_touch(@x, y_with_direction(@y,8))
  248.     end
  249.   end    
  250. end
  251.  
  252. #==============================================================================
  253. # ** Game_Player
  254. #------------------------------------------------------------------------------
  255. #  This class handles the player. Its functions include event starting
  256. #  determinants and map scrolling. Refer to "$game_player" for the one
  257. #  instance of this class.
  258. #==============================================================================
  259. class Game_Player
  260.   #--------------------------------------------------------------------------
  261.   # * Constant
  262.   #--------------------------------------------------------------------------
  263.   ALIASING_PIXEL.call(0)
  264.   #--------------------------------------------------------------------------
  265.   # * Alias Method
  266.   #--------------------------------------------------------------------------
  267.   $@ || alias_method(:pixel_initialize,:initialize)
  268.   #--------------------------------------------------------------------------
  269.   # * Pixel Movement
  270.   #--------------------------------------------------------------------------
  271.   def initialize(*args)
  272.     pixel_initialize(*args)
  273.     @pixel_movement = true
  274.   end
  275. end
  276.  
  277. #==============================================================================
  278. # ** Game_Event
  279. #------------------------------------------------------------------------------
  280. #  This class deals with events. It handles functions including event page
  281. #  switching via condition determinants, and running parallel process events.
  282. #  It's used within the Game_Map class.
  283. #==============================================================================
  284. class Game_Event
  285.   #--------------------------------------------------------------------------
  286.   # * Constant
  287.   #--------------------------------------------------------------------------
  288.   ALIASING_PIXEL.call(1)
  289.   #--------------------------------------------------------------------------
  290.   # * Alias Method
  291.   #--------------------------------------------------------------------------
  292.   unless method_defined?(:pixel_check_touch)
  293.     alias_method :pixel_initialize,   :initialize
  294.   end
  295.   #--------------------------------------------------------------------------
  296.   # * Pixel Movement
  297.   #--------------------------------------------------------------------------
  298.   def initialize(*args)
  299.     pixel_initialize(*args)
  300.     @pixel_movement = false
  301.   end
  302. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement