mcgluszak

[Author: fukuyama] Caterpillar

Jul 31st, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 19.05 KB | None | 0 0
  1. # train_actor
  2. =begin
  3.  
  4. Caterpillar walking script
  5.  
  6. Copyright © 2005 fukuyama
  7.  
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12.  
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. Lesser General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21.  
  22. http://www.gnu.org/licenses/lgpl.html
  23. http://www.opensource.gr.jp/lesser/lgpl.ja.html
  24.  
  25. =end
  26.  
  27. # Config.rb
  28. #==============================================================================
  29. # ■ Train_Actor::Config
  30. #------------------------------------------------------------------------------
  31. # Caterpillar movement of actor is carried out on map
  32. #==============================================================================
  33.  
  34. module Train_Actor
  35.  
  36. # ●Switch setup for transparent status
  37. # When true, switch control is used
  38. # TRANSPARENT_SWITCH = true
  39. TRANSPARENT_SWITCH = false
  40.  
  41. # ●Switch number for transparent status
  42. # When TRANSPARENT_SWITCH is true, transparency will be activated when the switch of this number is on
  43. TRANSPARENT_SWITCHES_INDEX = 20
  44.  
  45. # ●Maximum number of actors
  46. # There will be support for a large number of people in a party in the future...
  47. TRAIN_ACTOR_SIZE_MAX = 7
  48.  
  49. # Constants
  50. DOWN_LEFT = 1
  51. DOWN_RIGHT = 3
  52. UP_LEFT = 7
  53. UP_RIGHT = 9
  54. JUMP = 5
  55.  
  56. end
  57.  
  58. # rgss
  59.  
  60. # Spriteset_Map_Module.rb
  61. #==============================================================================
  62. # ■ Spriteset_Map_Module
  63. #------------------------------------------------------------------------------
  64. # Caterpillar movement of actor is carried out on map
  65. #==============================================================================
  66.  
  67. module Train_Actor
  68.  
  69.   module Spriteset_Map_Module
  70.    
  71.     def setup_actor_character_sprites?
  72.       return @setup_actor_character_sprites_flag != nil
  73.     end
  74.    
  75.     def setup_actor_character_sprites(characters)
  76.       if !setup_actor_character_sprites?
  77.         for character in characters.reverse
  78.           @character_sprites.unshift(
  79.           Sprite_Character.new(@viewport1, character)
  80.           )
  81.         end
  82.       @setup_actor_character_sprites_flag = true
  83.       end
  84.     end
  85.    
  86.   end
  87.  
  88. end
  89.  
  90. class Spriteset_Map
  91.   include Train_Actor::Spriteset_Map_Module
  92. end
  93.  
  94. # Scene_Map_Module.rb
  95. #==============================================================================
  96. # ■ Scene_Map_Module
  97. #------------------------------------------------------------------------------
  98. # Caterpillar movement of actor is carried out on map
  99. #==============================================================================
  100.  
  101. module Train_Actor
  102.  
  103.   module Scene_Map_Module
  104.     def setup_actor_character_sprites(characters)
  105.       @spriteset.setup_actor_character_sprites(characters)
  106.     end
  107.   end
  108.  
  109. end
  110.  
  111. class Scene_Map
  112.   include Train_Actor::Scene_Map_Module
  113. end
  114.  
  115. # Game_Party_Module.rb
  116. #==============================================================================
  117. # ■ Game_Party_Module
  118. #------------------------------------------------------------------------------
  119. # Caterpillar movement of actor is carried out on map
  120. #==============================================================================
  121.  
  122. module Train_Actor
  123.  
  124.   module Game_Party_Module
  125.   attr_reader :characters
  126.  
  127.     def actors_dead?
  128.       for actor in actors
  129.         if actor.dead?
  130.           return true
  131.         end
  132.       end
  133.       return false
  134.     end
  135.  
  136.     def update_party_order
  137.       if not actors_dead?
  138.         return actors
  139.       end
  140.       alive_actors = []
  141.       dead_actors = []
  142.       for actor in actors
  143.         if actor.dead?
  144.           dead_actors.push actor
  145.         else
  146.           alive_actors.push actor
  147.         end
  148.       end
  149.       return alive_actors + dead_actors
  150.     end
  151.    
  152.     def setup_actor_character_sprites
  153.       if @characters.nil?
  154.         @characters = []
  155.         for i in 1 ... TRAIN_ACTOR_SIZE_MAX
  156.           @characters.push(Game_Party_Actor.new)
  157.         end
  158.       end
  159.       setup_actors = update_party_order
  160.       for i in 1 ... TRAIN_ACTOR_SIZE_MAX
  161.         @characters[i - 1].setup(setup_actors[i])
  162.       end
  163.       if $scene.class.method_defined?('setup_actor_character_sprites')
  164.         $scene.setup_actor_character_sprites(@characters)
  165.       end
  166.     end
  167.    
  168.     def update_party_actors
  169.       update_party_order
  170.       setup_actor_character_sprites
  171.       transparent = $game_player.transparent
  172.       if transparent == false
  173.         if TRANSPARENT_SWITCH
  174.           transparent = $game_switches[TRANSPARENT_SWITCHES_INDEX]
  175.         end
  176.       end
  177.       for character in @characters
  178.         character.transparent = transparent
  179.         character.move_speed = $game_player.move_speed
  180.         character.step_anime = $game_player.step_anime
  181.         character.update
  182.       end
  183.     end
  184.    
  185.     def moveto_party_actors( x, y )
  186.       setup_actor_character_sprites
  187.       for character in @characters
  188.         character.moveto( x, y )
  189.       end
  190.       if @move_list == nil
  191.         @move_list = []
  192.       end
  193.       move_list_setup
  194.     end
  195.    
  196.     def move_party_actors
  197.       if @move_list == nil
  198.         @move_list = []
  199.         move_list_setup
  200.       end
  201.       @move_list.each_index do |i|
  202.       if @characters[i] != nil
  203.         case @move_list[i].type
  204.         when Input::DOWN
  205.           @characters[i].move_down(@move_list[i].args[0])
  206.         when Input::LEFT
  207.           @characters[i].move_left(@move_list[i].args[0])
  208.         when Input::RIGHT
  209.           @characters[i].move_right(@move_list[i].args[0])
  210.         when Input::UP
  211.           @characters[i].move_up(@move_list[i].args[0])
  212.         when DOWN_LEFT
  213.           @characters[i].move_lower_left
  214.         when DOWN_RIGHT
  215.           @characters[i].move_lower_right
  216.         when UP_LEFT
  217.           @characters[i].move_upper_left
  218.         when UP_RIGHT
  219.           @characters[i].move_upper_right
  220.         when JUMP
  221.           @characters[i].jump(@move_list[i].args[0],@move_list[i].args[1])
  222.         end
  223.       end
  224.     end
  225.   end
  226.  
  227.   class Move_List_Element
  228.    
  229.     def initialize(type,args)
  230.       @type = type
  231.       @args = args
  232.     end
  233.    
  234.     def type
  235.       return @type
  236.     end
  237.    
  238.     def args
  239.       return @args
  240.     end
  241.   end
  242.  
  243.   def move_list_setup
  244.     for i in 0 .. TRAIN_ACTOR_SIZE_MAX
  245.       @move_list[i] = nil
  246.     end
  247.   end
  248.  
  249.   def add_move_list(type,*args)
  250.     @move_list.unshift(Move_List_Element.new(type,args)).pop
  251.   end
  252.  
  253.   def move_down_party_actors(turn_enabled = true)
  254.     move_party_actors
  255.     add_move_list(Input::DOWN,turn_enabled)
  256.   end
  257.  
  258.   def move_left_party_actors(turn_enabled = true)
  259.     move_party_actors
  260.     add_move_list(Input::LEFT,turn_enabled)
  261.   end
  262.  
  263.   def move_right_party_actors(turn_enabled = true)
  264.     move_party_actors
  265.     add_move_list(Input::RIGHT,turn_enabled)
  266.   end
  267.  
  268.   def move_up_party_actors(turn_enabled = true)
  269.     move_party_actors
  270.     add_move_list(Input::UP,turn_enabled)
  271.   end
  272.  
  273.   def move_lower_left_party_actors
  274.     move_party_actors
  275.     add_move_list(DOWN_LEFT)
  276.   end
  277.  
  278.   def move_lower_right_party_actors
  279.     move_party_actors
  280.     add_move_list(DOWN_RIGHT)
  281.   end
  282.  
  283.   def move_upper_left_party_actors
  284.     move_party_actors
  285.     add_move_list(UP_LEFT)
  286.   end
  287.  
  288.   def move_upper_right_party_actors
  289.     move_party_actors
  290.     add_move_list(UP_RIGHT)
  291.   end
  292.  
  293.   def jump_party_actors(x_plus, y_plus)
  294.     move_party_actors
  295.     add_move_list(JUMP,x_plus, y_plus)
  296.   end
  297.  
  298. end
  299.  
  300. end
  301.  
  302. class Game_Party
  303.   include Train_Actor::Game_Party_Module
  304. end
  305.  
  306. # Game_Player_Module.rb
  307. #==============================================================================
  308. # ■ Game_Player_Module
  309. #------------------------------------------------------------------------------
  310. # Caterpillar movement of actor is carried out on map
  311. #==============================================================================
  312.  
  313. module Train_Actor
  314.  
  315.   module Game_Player_Module
  316.   attr_reader :move_speed
  317.   attr_reader :step_anime
  318.  
  319.     def update_party_actors
  320.       $game_party.update_party_actors
  321.       $game_party.actors.each do |actor|
  322.       if actor.dead?
  323.         next
  324.       end
  325.       @character_name = actor.character_name
  326.       @character_hue = actor.character_hue
  327.       break
  328.     end
  329.   end
  330.  
  331.   def update
  332.     update_party_actors
  333.     super
  334.   end
  335.  
  336.   def moveto( x, y )
  337.     $game_party.moveto_party_actors( x, y )
  338.     super( x, y )
  339.   end
  340.  
  341.   def move_down(turn_enabled = true)
  342.     if passable?(@x, @y, Input::DOWN)
  343.       $game_party.move_down_party_actors(turn_enabled)
  344.     end
  345.     super(turn_enabled)
  346.   end
  347.  
  348.   def move_left(turn_enabled = true)
  349.     if passable?(@x, @y, Input::LEFT)
  350.       $game_party.move_left_party_actors(turn_enabled)
  351.     end
  352.     super(turn_enabled)
  353.   end
  354.  
  355.   def move_right(turn_enabled = true)
  356.     if passable?(@x, @y, Input::RIGHT)
  357.       $game_party.move_right_party_actors(turn_enabled)
  358.     end
  359.     super(turn_enabled)
  360.   end
  361.  
  362.   def move_up(turn_enabled = true)
  363.     if passable?(@x, @y, Input::UP)
  364.       $game_party.move_up_party_actors(turn_enabled)
  365.     end
  366.     super(turn_enabled)
  367.   end
  368.  
  369.   def move_lower_left
  370.     # When possible to move from down→left or from left→down
  371.     if (passable?(@x, @y, Input::DOWN) and passable?(@x, @y + 1, Input::LEFT)) or
  372.     (passable?(@x, @y, Input::LEFT) and passable?(@x - 1, @y, Input::DOWN))
  373.       $game_party.move_lower_left_party_actors
  374.     end
  375.     super
  376.   end
  377.  
  378.   def move_lower_right
  379.     # When possible to move from down→right or from right→down
  380.     if (passable?(@x, @y, Input::DOWN) and passable?(@x, @y + 1, Input::RIGHT)) or
  381.     (passable?(@x, @y, Input::RIGHT) and passable?(@x + 1, @y, Input::DOWN))
  382.       $game_party.move_lower_right_party_actors
  383.     end
  384.     super
  385.   end
  386.  
  387.   def move_upper_left
  388.     # When possible to move from up→left or from left→up
  389.     if (passable?(@x, @y, Input::UP) and passable?(@x, @y - 1, Input::LEFT)) or
  390.     (passable?(@x, @y, Input::LEFT) and passable?(@x - 1, @y, Input::UP))
  391.        $game_party.move_upper_left_party_actors
  392.     end
  393.     super
  394.   end
  395.  
  396.   def move_upper_right
  397.     # When possible to move from up→right or from right→up
  398.     if (passable?(@x, @y, Input::UP) and passable?(@x, @y - 1, Input::RIGHT)) or
  399.     (passable?(@x, @y, Input::RIGHT) and passable?(@x + 1, @y, Input::UP))
  400.       $game_party.move_upper_right_party_actors
  401.     end
  402.     super
  403.   end
  404.  
  405.   def jump(x_plus, y_plus)
  406.   # New coordinates are calculated
  407.     new_x = @x + x_plus
  408.     new_y = @y + y_plus
  409.     # When addition values are (0,0), it is possible to jump to the destination
  410.     if (x_plus == 0 and y_plus == 0) or passable?(new_x, new_y, 0)
  411.       $game_party.jump_party_actors(x_plus, y_plus)
  412.     end
  413.     super(x_plus, y_plus)
  414.   end
  415. end
  416.  
  417. end
  418.  
  419. class Game_Player
  420.   include Train_Actor::Game_Player_Module
  421. end
  422.  
  423. # Game_Event_Module.rb
  424. #==============================================================================
  425. # ■ Game_Event_Module
  426. #------------------------------------------------------------------------------
  427. # Caterpillar movement of actor is carried out on map
  428. #==============================================================================
  429.  
  430. module Train_Actor
  431.  
  432.   module Game_Event_Module
  433. #--------------------------------------------------------------------------
  434. # ● Judgement determined
  435. # x : X coordinates
  436. # y : Y coordinates
  437. # d : Direction (0,2,4,6,8) ※ 0 = Checks if all directions are not able to be passed (for a jump)
  438. # return : Passing is impossible (false), possible (true)
  439. #--------------------------------------------------------------------------
  440.     def passable?(x, y, d)
  441.       result = super(x, y, d)
  442.       if result
  443.       # New coordinates are searched for
  444.         new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0)
  445.         new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0)
  446.       # Loops for actor in train
  447.       for actor in $game_party.characters
  448. # When displayed
  449.         if not actor.character_name.empty?
  450. # When actor's coordinates correspond to the destination
  451.           if actor.x == new_x and actor.y == new_y
  452. # When event
  453.             if self != $game_player
  454. # Passing is impossible
  455.               return false
  456.             end
  457.           end
  458.         end
  459.       end
  460.     end
  461.     return result
  462.   end
  463. end
  464.  
  465. end
  466.  
  467. class Game_Event
  468.   include Train_Actor::Game_Event_Module
  469. end
  470.  
  471. # Game_Party_Actor.rb
  472. #==============================================================================
  473. # ■ Game_Party_Actor
  474. #------------------------------------------------------------------------------
  475. # Caterpillar movement of actor is carried out on map
  476. #==============================================================================
  477.  
  478. module Train_Actor
  479.  
  480.   class Game_Party_Actor < Game_Character
  481.   attr_writer :move_speed
  482.   attr_writer :step_anime
  483.  
  484.     def initialize
  485.       super()
  486.       @through = true
  487.     end
  488.    
  489.     def setup(actor)
  490.     # The file name and hue of the character are set
  491.       if actor != nil and (not actor.dead?) # When dead, it is erased for the time being...
  492.         @character_name = actor.character_name
  493.         @character_hue = actor.character_hue
  494.       else
  495.         @character_name = ""
  496.         @character_hue = 0
  497.       end
  498.       # Opacity and blending method are initialized
  499.       @opacity = 255
  500.       @blend_type = 0
  501.     end
  502.  
  503.     def screen_z(height = 0)
  504.       if $game_player.x == @x and $game_player.y == @y
  505.         return $game_player.screen_z(height) - 1
  506.       end
  507.       super(height)
  508.     end
  509.    
  510. #--------------------------------------------------------------------------
  511. # ● Move down
  512. # turn_enabled : Flag that permits direction change on the spot
  513. #--------------------------------------------------------------------------
  514.       def move_down(turn_enabled = true)
  515.       # Face down
  516.         if turn_enabled
  517.           turn_down
  518.         end
  519.         # When possible to pass
  520.         if passable?(@x, @y, Input::DOWN)
  521.         # Face down
  522.           turn_down
  523.         # Update coordinates
  524.           @y += 1
  525.         end
  526.       end
  527. #--------------------------------------------------------------------------
  528. # ● Move left
  529. # turn_enabled : Flag that permits direction change on the spot
  530. #--------------------------------------------------------------------------
  531.       def move_left(turn_enabled = true)
  532.         # Face left
  533.         if turn_enabled
  534.           turn_left
  535.         end
  536.         # When possible to pass
  537.         if passable?(@x, @y, Input::LEFT)
  538.         # Face left
  539.           turn_left
  540.         # Update coordinates
  541.           @x -= 1
  542.         end
  543.       end
  544. #--------------------------------------------------------------------------
  545. # ● Move right
  546. # turn_enabled : Flag that permits direction change on the spot
  547. #--------------------------------------------------------------------------
  548.       def move_right(turn_enabled = true)
  549.         # Face right
  550.         if turn_enabled
  551.           turn_right
  552.         end
  553.         # When possible to pass
  554.         if passable?(@x, @y, Input::RIGHT)
  555.           # Face right
  556.           turn_right
  557.           # Update coordinates
  558.           @x += 1
  559.         end
  560.       end
  561. #--------------------------------------------------------------------------
  562. # ● Move up
  563. # turn_enabled : Flag that permits direction change on the spot
  564. #--------------------------------------------------------------------------
  565.       def move_up(turn_enabled = true)
  566.         # Face up
  567.         if turn_enabled
  568.           turn_up
  569.         end
  570.         # When possible to pass
  571.         if passable?(@x, @y, Input::UP)
  572.           # Face up
  573.           turn_up
  574.           # Update coordinates
  575.           @y -= 1
  576.         end
  577.       end
  578. #--------------------------------------------------------------------------
  579. # ● Move lower left
  580. #--------------------------------------------------------------------------
  581.       def move_lower_left
  582.         # When no direction fixation
  583.         unless @direction_fix
  584.           # Turn left when facing right, turn down when facing up
  585.           @direction = (@direction == Input::RIGHT ? Input::LEFT : @direction == Input::UP ? Input::DOWN : @direction)
  586.         end
  587.         # When possible to move from down→left or from left→down
  588.         if (passable?(@x, @y, Input::DOWN) and passable?(@x, @y + 1, Input::LEFT)) or
  589.         (passable?(@x, @y, Input::LEFT) and passable?(@x - 1, @y, Input::DOWN))
  590.         # Update coordinates
  591.           @x -= 1
  592.           @y += 1
  593.         end
  594.       end
  595. #--------------------------------------------------------------------------
  596. # ● Move lower right
  597. #--------------------------------------------------------------------------
  598.       def move_lower_right
  599.       # When no direction fixation
  600.         unless @direction_fix
  601.         # Turn right when facing left, turn down when facing up
  602.           @direction = (@direction == Input::LEFT ? Input::RIGHT : @direction == Input::UP ? Input::DOWN : @direction)
  603.         end
  604.         # When possible to move from down→right or from right→down
  605.         if (passable?(@x, @y, Input::DOWN) and passable?(@x, @y + 1, Input::RIGHT)) or
  606.         (passable?(@x, @y, Input::RIGHT) and passable?(@x + 1, @y, Input::DOWN))
  607.           # Update coordinates
  608.           @x += 1
  609.           @y += 1
  610.         end
  611.       end
  612. #--------------------------------------------------------------------------
  613. # ● move upper left
  614. #--------------------------------------------------------------------------
  615.       def move_upper_left
  616.         # When no direction fixation
  617.         unless @direction_fix
  618.         # Turn left when facing right, turn up when facing down
  619.           @direction = (@direction == Input::RIGHT ? Input::LEFT : @direction == Input::DOWN ? Input::UP : @direction)
  620.         end
  621.         # When possible to move from up→left or from left→up
  622.         if (passable?(@x, @y, Input::UP) and passable?(@x, @y - 1, Input::LEFT)) or
  623.         (passable?(@x, @y, Input::LEFT) and passable?(@x - 1, @y, Input::UP))
  624.           # Update coordinates
  625.           @x -= 1
  626.           @y -= 1
  627.         end
  628.       end
  629. #--------------------------------------------------------------------------
  630. # ● move upper right
  631. #--------------------------------------------------------------------------
  632.   def move_upper_right
  633.     # When no direction fixation
  634.     unless @direction_fix
  635.     # Turn right when facing left, turn up when facing down
  636.       @direction = (@direction == Input::LEFT ? Input::RIGHT : @direction == Input::DOWN ? Input::UP : @direction)
  637.     end
  638.     # When possible to move from up→right or from right→up
  639.     if (passable?(@x, @y, Input::UP) and passable?(@x, @y - 1, Input::RIGHT)) or
  640.     (passable?(@x, @y, Input::RIGHT) and passable?(@x + 1, @y, Input::UP))
  641.     # Update coordinates
  642.       @x += 1
  643.       @y -= 1
  644.     end
  645.   end
  646. end
  647.  
  648. end
Advertisement
Add Comment
Please, Sign In to add comment