molegato

diagonal movement fixes

Oct 15th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.40 KB | None | 0 0
  1. #==============================================================================
  2. # diagonal movement fixes
  3. # Author Molegato
  4. # Version 1.0
  5. #------------------------------------------------------------------------------
  6. # Fixes a number of issues with diagonal movement
  7. #------------------------------------------------------------------------------
  8. #
  9. # NOTE: this script doesn't add diagonal movement. Other scripts do, like
  10. # for example my 'SideTop Characters' script.
  11. #
  12. # What this script does:
  13. #   -Makes possible for events to keep their diagonal direction even
  14. #     when they are not moving. (By default they snap to a cardinal one)
  15. #   -Allows events to move diagonally when chasing the player of fleeing
  16. #     from it. (By default they'll retardedly zig-zag their way)
  17. #   -Makes the event condition 'event/player looking this direction'
  18. #     detect diagonal directions as both cardinal directions combined
  19. #     (by default, moving diagonally won't trigger any direction in the events)
  20.  
  21. #==============================================================================
  22.  
  23. $imported = {} if $imported.nil?
  24. $imported['Molegato-diagonal_fixes'] = true
  25. class Game_Character
  26.      
  27.   def move_toward_character(character)
  28.     sx = distance_x_from(character.x)
  29.     sy = distance_y_from(character.y)
  30.     if (sx==0 || sy==0)
  31.       #straight
  32.       if sx.abs > sy.abs
  33.         move_straight(sx > 0 ? 4 : 6)
  34.         move_straight(sy > 0 ? 8 : 2) if !@move_succeed && sy != 0
  35.       elsif sy != 0
  36.         move_straight(sy > 0 ? 8 : 2)
  37.         move_straight(sx > 0 ? 4 : 6) if !@move_succeed && sx != 0
  38.       end
  39.     else
  40.       #diagonal
  41.       move_diagonal(sx > 0 ? 4 : 6,sy > 0 ? 8 : 2)
  42.     end
  43.   end
  44.  
  45.   def move_away_from_character(character)
  46.     sx = distance_x_from(character.x)
  47.     sy = distance_y_from(character.y)
  48.     if (sx==0 || sy==0)
  49.       if sx.abs > sy.abs
  50.         move_straight(sx > 0 ? 6 : 4)
  51.         move_straight(sy > 0 ? 2 : 8) if !@move_succeed && sy != 0
  52.       elsif sy != 0
  53.         move_straight(sy > 0 ? 2 : 8)
  54.         move_straight(sx > 0 ? 6 : 4) if !@move_succeed && sx != 0
  55.       end
  56.     else
  57.       #diagonal
  58.       move_diagonal(sx > 0 ? 6 : 4,sy > 0 ? 2 : 8)
  59.     end
  60.   end
  61. end
  62.  
  63. #==============================================================================
  64. # ■ Game_Interpreter fix
  65. #==============================================================================
  66. class Game_Interpreter
  67.   alias molegato_command_111 command_111
  68.   def command_111
  69.     result = false
  70.     if @params[0]==6
  71.       character = get_character(@params[1])
  72.       if character
  73.         case @params[2]
  74.         when 8 #up
  75.           result = (character.direction == 8 or character.angledirection==9 or character.angledirection==7)
  76.         when 4 #left
  77.           result = (character.direction == 4 or character.angledirection==1 or character.angledirection==7)
  78.         when 6 #right
  79.           result = (character.direction == 6 or character.angledirection==9 or character.angledirection==3)
  80.         when 2 #down
  81.           result = (character.direction == 2 or character.angledirection==1 or character.angledirection==3)
  82.         end
  83.       end
  84.       @branch[@indent] = result
  85.       command_skip if !@branch[@indent]
  86.     else
  87.       molegato_command_111
  88.     end
  89.   end
  90. end
  91.  
  92. #==============================================================================
  93. # ■ Game_Character
  94. #==============================================================================
  95. class Game_Character
  96.   def move_forward
  97.     if @direction==2 or @direction==4 or @direction==6 or @direction==8
  98.       move_straight(@direction)
  99.     else
  100.       if @direction==1
  101.         move_diagonal(4, 2)
  102.       end
  103.       if @direction==3
  104.         move_diagonal(6, 2)
  105.       end
  106.       if @direction==7
  107.         move_diagonal(4, 8)
  108.       end
  109.       if @direction==9
  110.         move_diagonal(6, 8)
  111.       end
  112.     end
  113.   end
  114.  
  115.   def move_backward
  116.     last_direction_fix = @direction_fix
  117.     @direction_fix = true
  118.     if @direction==2 or @direction==4 or @direction==6 or @direction==8
  119.       move_straight(reverse_dir(@direction), false)
  120.     else
  121.       if @direction==9
  122.         move_diagonal(4, 2)
  123.       end
  124.       if @direction==7
  125.         move_diagonal(6, 2)
  126.       end
  127.       if @direction==3
  128.         move_diagonal(4, 8)
  129.       end
  130.       if @direction==1
  131.         move_diagonal(6, 8)
  132.       end
  133.     end
  134.     @direction_fix = last_direction_fix
  135.   end
  136.  
  137. end
  138.  
  139. #==============================================================================
  140. # ■ Game_CharacterBase
  141. #==============================================================================
  142.  
  143. class Game_CharacterBase
  144.   attr_reader   :direction
  145.   attr_reader   :angledirection
  146.      
  147.   def movedirection
  148.     return @angledirection
  149.   end
  150.  
  151.   alias diagofix_direction set_direction
  152.   def set_direction(d)
  153.     diagofix_direction(d)
  154.     @angledirection = @direction
  155.   end
  156.  
  157.   alias diagofix_straight move_straight
  158.   def move_straight(d, turn_ok = true)
  159.     diagofix_straight(d, turn_ok)
  160.     @angledirection = @direction
  161.   end
  162.  
  163.   alias diagofix_diagonal move_diagonal
  164.   def move_diagonal(horz, vert)
  165.     diagofix_diagonal(horz, vert)
  166.     if horz==4 and vert==2
  167.       @angledirection=1
  168.     end
  169.     if horz==6 and vert==2
  170.       @angledirection=3
  171.     end
  172.     if horz==4 and vert==8
  173.       @angledirection=7
  174.     end
  175.     if horz==6 and vert==8
  176.       @angledirection=9
  177.     end
  178.   end
  179.  
  180. end
Advertisement
Add Comment
Please, Sign In to add comment