drtarsus

Ship battle script 0.3

Aug 28th, 2014
306
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #  v0.3 (28/08/14)
  2. #  Created by Chris Swire (Tarsus)
  3. #  All work is my own
  4.  
  5. # not required but recommended
  6. Graphics.resize_screen(640,480)
  7.  
  8. ##### global ship variables #####
  9. module SHIP
  10.  
  11. ##### editable options #####
  12.  
  13.   SHIPID = 10            # the variable id holding the actor id of your ship
  14.   PARTYSIZE = 4          # size of normal party while in battle
  15.   MENULABEL = "Ship"     # the text you want to appear on main menu
  16.   MENUWIDTH = 160        # 160/216 is the default size for game 640x480
  17.   MENUHEIGHT = 216       # these 2 options are for people with custom menus
  18.   SHIPSKILL = 3          # the id of the ship skill type
  19.   SHIPCOL = 1            # the amout of columns to show for skills and items
  20.   SHIPBACK = "Clouds"    # custom battleback1 to use in ship battles, 2 is empty
  21.  
  22.   # replacment labels for the ships equip slots
  23.   SHIPSLOTS = ["Cannons","Deck Gun","Figurehead","Armour","Decoration"]    
  24.  
  25.   ##### edit after here at own risk #####
  26.    
  27.   NEWITEMS = {
  28.   # "Option Label"  => [Event Switch ID,Common Event ID]
  29.   MENULABEL => [0,0],}
  30.  
  31.   ## returns the actor set as ship ##
  32.   def self.shipactor
  33.     id = $game_variables[SHIPID].to_i
  34.     return $game_actors[id]
  35.   end
  36.  
  37.   def self.shipactorid
  38.     return $game_variables[SHIPID]
  39.   end
  40.  
  41.   def self.shipactive
  42.     id = $game_variables[SHIPID].to_i
  43.     return id >= 1
  44.   end
  45.      
  46.   def self.shipbattle
  47.     return shipactive && $data_enemies[$game_troop.members[0].enemy_id].note =~ /<shipbattle>/i
  48.   end
  49.  
  50.   def self.captainbattle
  51.     if shipactive && $data_enemies[$game_troop.members[0].enemy_id].note =~ /<captain (.*)>/i
  52.       return $1.to_i
  53.     else
  54.       return 0
  55.     end    
  56.   end  
  57.  
  58. end
  59.  
  60. ##### Small window in bottom left to preview ship #####
  61. class Window_Ship_Preview < Window_Base
  62.   ## creates window ##  
  63.   def initialize
  64.     super(0, y, window_width, window_height)
  65.     refresh
  66.   end
  67.   ## returns width taken from variables at top ##
  68.   def window_width
  69.     return SHIP::MENUWIDTH
  70.   end
  71.   ## same for height ##
  72.   def window_height
  73.     return SHIP::MENUHEIGHT
  74.   end
  75.   ## moves window so it matchs existing windows ##
  76.   def y
  77.     return Graphics.height - fitting_height(1) - window_height
  78.   end
  79.   ## refresh ##
  80.   def refresh
  81.     contents.clear
  82.     @actor = SHIP.shipactor
  83.     xcentre = window_width/2 - standard_padding - 48
  84.     barwidth = SHIP::MENUWIDTH - standard_padding*2
  85.     #draw_actor_bust(@actor, xcentre - 24, 0)
  86.     draw_actor_icons(@actor, 0, 0)
  87.     draw_actor_face(@actor, xcentre, 15)
  88.     draw_actor_name(@actor, xcentre, 120 + line_height * 0)    
  89.     draw_actor_hp(@actor, 0, 120 + line_height * 1, barwidth)
  90.     draw_actor_mp(@actor, 0, 120 + line_height * 2, barwidth)
  91.        
  92.   end  
  93.   ## open ##
  94.   def open
  95.     refresh
  96.     super
  97.   end  
  98. end
  99.  
  100. ##### New main menu command for ship options #####
  101. class Window_MenuCommand < Window_Command  
  102.   alias ship_commands add_original_commands
  103.   ## adds command using label from variable list ##
  104.   def add_original_commands
  105.     ship_commands()  
  106.     SHIP::NEWITEMS.each_key do |label|
  107.       switch_id = SHIP::NEWITEMS[label][0]
  108.       enabled   = (switch_id > 0) ? $game_switches[switch_id] : SHIP.shipactive
  109.       add_command(label, label.to_sym, enabled)
  110.     end
  111.   end
  112. end
  113.  
  114. ##### Rewrote main menu to add ship command #####
  115. class Scene_Menu < Scene_MenuBase  
  116.   alias ship_window create_command_window  
  117.   ## rewrote ##
  118.   def start
  119.     super
  120.     create_command_window
  121.     create_gold_window
  122.     create_status_window
  123.     if SHIP.shipactive
  124.       create_ship_window
  125.     end
  126.   end
  127.   def create_command_window
  128.     ship_window()    
  129.     SHIP::NEWITEMS.each_key do |label|
  130.       @command_window.set_handler(label.to_sym, method(:command_ship))
  131.     end    
  132.   end    
  133.   def create_ship_window
  134.     @ship_window = Window_Ship_Preview.new
  135.   end  
  136.   def command_ship
  137.     SceneManager.call(Scene_Ship)
  138.   end  
  139. end
  140.  
  141. ##### Ship scene basic processing #####
  142. class Scene_Ship < Scene_MenuBase
  143.   ## starts scene ##
  144.   def start
  145.     super
  146.     create_help_window
  147.     create_status_window
  148.     create_command_window
  149.     create_slot_window
  150.     create_item_window
  151.     create_skill_window    
  152.     @item_window.hide
  153.     @skill_window.hide
  154.     @skill_window.stype_id = SHIP::SHIPSKILL    
  155.     @actor = SHIP.shipactor
  156.     @status_window.actor = @actor
  157.     @slot_window.actor = @actor
  158.     @item_window.actor = @actor
  159.     @command_window.activate
  160.   end
  161.   ## creates window ##
  162.   def create_status_window
  163.     @status_window = Window_ShipStatus.new(0, @help_window.height)
  164.     @status_window.viewport = @viewport
  165.     @status_window.actor = SHIP.shipactor
  166.   end
  167.   ## creates window ##
  168.   def create_command_window
  169.     wx = @status_window.width
  170.     wy = @help_window.height
  171.     ww = Graphics.width - @status_window.width
  172.     @command_window = Window_ShipCommand.new(wx, wy, ww)
  173.     @command_window.viewport = @viewport
  174.     @command_window.help_window = @help_window
  175.     @command_window.set_handler(:equip,    method(:command_equip))
  176.     @command_window.set_handler(:repair,    method(:command_repair))
  177.     @command_window.set_handler(:skills,    method(:command_skills))
  178.     @command_window.set_handler(:cancel,   method(:return_scene))
  179.   end
  180.   ## creates window ##
  181.   def create_slot_window
  182.     wx = @status_window.width
  183.     wy = @command_window.y + @command_window.height
  184.     ww = Graphics.width - @status_window.width
  185.     @slot_window = Window_ShipEquipSlot.new(wx, wy, ww)
  186.     @slot_window.viewport = @viewport
  187.     @slot_window.help_window = @help_window
  188.     @slot_window.status_window = @status_window
  189.     @slot_window.set_handler(:ok,       method(:on_slot_ok))
  190.     @slot_window.set_handler(:cancel,   method(:on_slot_cancel))
  191.   end
  192.   ## creates window ##
  193.   def create_item_window
  194.     wx = @status_window.width
  195.     wy = @slot_window.y + @slot_window.height
  196.     ww = Graphics.width - @status_window.width
  197.     wh = Graphics.height - wy
  198.     @item_window = Window_ShipEquipItem.new(wx, wy, ww, wh)
  199.     @item_window.viewport = @viewport
  200.     @item_window.help_window = @help_window
  201.     @item_window.status_window = @status_window
  202.     @item_window.set_handler(:ok,     method(:on_item_ok))
  203.     @item_window.set_handler(:cancel, method(:on_item_cancel))
  204.     @slot_window.item_window = @item_window
  205.   end
  206.   ## creates window ##
  207.   def create_skill_window
  208.     wx = @status_window.width
  209.     wy = @slot_window.y + @slot_window.height
  210.     ww = Graphics.width - @status_window.width
  211.     wh = Graphics.height - wy
  212.     @skill_window = Window_ShipSkillList.new(wx, wy, ww, wh)
  213.     @item_window.viewport = @viewport
  214.     @skill_window.help_window = @help_window
  215.     @skill_window.set_handler(:ok,     method(:on_skill_ok))
  216.     @skill_window.set_handler(:cancel, method(:on_skill_cancel))
  217.   end
  218.  
  219.   ## command processing for all new windows ##
  220.   def command_equip
  221.     @item_window.show
  222.     @slot_window.activate
  223.     @slot_window.select(0)
  224.   end
  225.   def command_repair
  226.      Sound.play_equip
  227.      @command_window.activate
  228.   end
  229.   def command_skills
  230.     @skill_window.show
  231.     @skill_window.activate
  232.     @skill_window.select(0)
  233.   end
  234.   def on_slot_ok
  235.     @item_window.activate
  236.     @item_window.select(0)
  237.   end
  238.   def on_slot_cancel
  239.     @slot_window.unselect
  240.     @command_window.activate
  241.     @item_window.hide
  242.   end
  243.   def on_item_ok
  244.     Sound.play_equip
  245.     @actor.change_equip(@slot_window.index, @item_window.item)
  246.     @slot_window.activate
  247.     @slot_window.refresh
  248.     @item_window.unselect
  249.     @item_window.refresh
  250.   end
  251.   def on_item_cancel
  252.     @slot_window.activate
  253.     @item_window.unselect
  254.   end
  255.   def on_skill_ok
  256.     @actor.last_skill.object = item
  257.     determine_item
  258.   end
  259.   def on_skill_cancel
  260.     @skill_window.unselect
  261.     @command_window.activate
  262.     @skill_window.hide
  263.   end
  264. end
  265.  
  266. ##### rewrote #####
  267. class Game_System  
  268.   attr_accessor :battle_member_size  
  269.   alias :gamesysteminit :initialize
  270.   def initialize
  271.     gamesysteminit
  272.     @battle_member_size = SHIP::PARTYSIZE
  273.   end
  274. end
  275.  
  276. ##### rewrote #####
  277. class Game_Party
  278.   def max_battle_members
  279.     return $game_system.battle_member_size
  280.   end
  281. end
  282.  
  283. ##### Rewrote to use custom ship battleback #####
  284. class Spriteset_Battle
  285.   def battleback1_bitmap
  286.     if SHIP.shipbattle
  287.       Cache.battleback1(SHIP::SHIPBACK)
  288.     elsif battleback1_name
  289.       Cache.battleback1(battleback1_name)
  290.     else
  291.       create_blurry_background_bitmap
  292.     end
  293.   end
  294.  
  295.   def battleback2_bitmap
  296.     if SHIP.shipbattle
  297.       Bitmap.new(1, 1)
  298.     elsif battleback2_name
  299.       Cache.battleback2(battleback2_name)
  300.     else
  301.       Bitmap.new(1, 1)
  302.     end
  303.   end
  304. end
  305.  
  306.  
  307. ##### Rewrote battle scene to swap in/out ship #####
  308. class Scene_Battle < Scene_Base
  309.   ## start battle ##
  310.   alias ship_start start
  311.   def start
  312.     @partysize = $game_party.all_members
  313.     @partylast = @partysize.size
  314.     if SHIP.shipbattle #$data_enemies[$game_troop.members[0].enemy_id].note =~ /<shipbattle>/i
  315.       $game_party.add_actor(SHIP.shipactorid)
  316.       $game_system.battle_member_size = 1
  317.       $game_party.swap_order(0,@partylast)
  318.     end
  319.     ship_start    
  320.   end
  321.   ## end battle ##
  322.   def terminate
  323.     super
  324.     dispose_spriteset
  325.     @info_viewport.dispose
  326.     RPG::ME.stop
  327.     if SHIP.shipbattle #$data_enemies[$game_troop.members[0].enemy_id].note =~ /<shipbattle>/i
  328.       $game_system.battle_member_size = SHIP::PARTYSIZE
  329.       $game_party.swap_order(0,@partylast)
  330.       $game_party.remove_actor(SHIP.shipactorid)
  331.     end
  332.     if SHIP.captainbattle >= 1
  333.       BattleManager.setup(SHIP.captainbattle, true, true)
  334.       BattleManager.save_bgm_and_bgs
  335.       BattleManager.play_battle_bgm
  336.       SceneManager.call(Scene_Battle)
  337.     end  
  338.   end
  339. end
  340.  
  341.  
  342. ##### ship version of the normal status screen #####
  343. class Window_ShipStatus < Window_Selectable
  344.   ## Initialization ##
  345.   def initialize(x, y)
  346.     super(x, y, 204, Graphics.height-y)    
  347.     @actor = SHIP.shipactor
  348.     @temp_actor = nil
  349.     refresh
  350.   end
  351.   ## get lines to show ##
  352.   def visible_line_number
  353.     return 7
  354.   end
  355.   ## sets actor ##
  356.   def actor=(actor)
  357.     return if @actor == actor
  358.     @actor = actor
  359.     refresh
  360.   end
  361.   ## refresh ##
  362.   def refresh
  363.     contents.clear
  364.     #draw_actor_bust(@actor, 4, 0)    
  365.     draw_actor_icons(@actor, 0, 0)
  366.     draw_actor_face(@actor, 40, 15)
  367.     draw_actor_name(@actor, 40, 120 + line_height * 0)    
  368.     draw_actor_hp(@actor, 0, 120 + line_height * 1, 180)
  369.     draw_actor_mp(@actor, 0, 120 + line_height * 2, 180)
  370.     6.times {|i| draw_item(0, line_height * (1 + i) + 120 + line_height * 2, 2 + i) }
  371.   end
  372.   ## temp actor to show changes ##
  373.   def set_temp_actor(temp_actor)
  374.     return if @temp_actor == temp_actor
  375.     @temp_actor = temp_actor
  376.     refresh
  377.   end
  378.   ## draw item ##
  379.   def draw_item(x, y, param_id)
  380.     draw_param_name(x + 4, y, param_id)
  381.     draw_current_param(x + 94, y, param_id) if @actor
  382.     draw_right_arrow(x + 126, y)
  383.     draw_new_param(x + 150, y, param_id) if @temp_actor
  384.   end
  385.   ## draw name, current, arrow and new ##
  386.   def draw_param_name(x, y, param_id)
  387.     change_color(system_color)
  388.     draw_text(x, y, 80, line_height, Vocab::param(param_id))
  389.   end
  390.   def draw_current_param(x, y, param_id)
  391.     change_color(normal_color)
  392.     draw_text(x, y, 32, line_height, @actor.param(param_id), 2)
  393.   end
  394.   def draw_right_arrow(x, y)
  395.     change_color(system_color)
  396.     draw_text(x, y, 22, line_height, "→", 1)
  397.   end
  398.   def draw_new_param(x, y, param_id)
  399.     new_value = @temp_actor.param(param_id)
  400.     change_color(param_change_color(new_value - @actor.param(param_id)))
  401.     draw_text(x, y, 32, line_height, new_value, 2)
  402.   end
  403. end
  404.  
  405. ##### new window with ship commands #####
  406. class Window_ShipCommand < Window_HorzCommand
  407.   ## Initialization##
  408.   def initialize(x, y, width)
  409.     @window_width = width
  410.     super(x, y)
  411.   end
  412.   ## Window Width ##
  413.   def window_width
  414.     @window_width
  415.   end
  416.   ## column count ##
  417.   def col_max
  418.     return 3
  419.   end
  420.   ## adds commands ##
  421.   def make_command_list
  422.     add_command("Equipment", :equip)
  423.     add_command("Repair", :repair)
  424.     add_command("Skills", :skills)
  425.   end
  426. end
  427.  
  428. ##### creates new window showing ship skills #####
  429. class Window_ShipSkillList < Window_SkillList
  430.   ## Initialization ##
  431.   def initialize(x, y, width, height)
  432.     super
  433.     @actor = SHIP.shipactor
  434.     @stype_id = 0
  435.     @data = []
  436.   end
  437.   ## column count ##
  438.   def col_max
  439.     return SHIP::SHIPCOL
  440.   end
  441. end
  442.  
  443. ##### rewrote to reduce column amount #####
  444. class Window_ShipEquipItem < Window_EquipItem
  445.   def col_max
  446.     return SHIP::SHIPCOL
  447.   end
  448. end
  449.  
  450.  
  451. ##### creates new window whowing ships equipment slots #####
  452. class Window_ShipEquipSlot < Window_EquipSlot
  453.   ## Initialization ##
  454.   def initialize(x, y, width)
  455.     super(x, y, width)
  456.     @actor = SHIP.shipactor
  457.     refresh
  458.   end
  459.   ## draws new ship slots ##
  460.   def draw_item(index)
  461.     rect = item_rect_for_text(index)
  462.     change_color(system_color, enable?(index))
  463.     draw_text(rect.x, rect.y, 120, line_height, slot_name(index))
  464.     draw_item_name(@actor.equips[index], rect.x + 120, rect.y, enable?(index))
  465.   end
  466.   ## gets new ship slots form variables ##
  467.   def slot_name(index)
  468.     return SHIP::SHIPSLOTS[index]  
  469.   end  
  470. end
RAW Paste Data