The RPG Maker Resource Kit

RMRK RPG Maker Creation => RPG Maker General => General Scripting => Topic started by: Tsunokiette on October 05, 2006, 12:45:36 AM

Title: Scene_Base, Like Window_Base, for Scenes
Post by: Tsunokiette on October 05, 2006, 12:45:36 AM
This is a resource which I hope to develope to the point it is used... A lot. (wishful thinking)

At the moment what it does is allow you to create scenes, while only having to initialize the objects in the scene and writing the seperate update methods. (for interaction)

Don't forget to make the scene a sub-class of Scene_Base, ie - (class Scene_Name < Scene_Base)

In the future I hope to include introduction animation methods and closing animation methods, as well as a variety of other methods to make writing scenes easier.

The only real difference, is that when you create an object in a scene, instead of doing -

Code: [Select]
@playtime_window = Window_PlayTime.new

You'd do -

Code: [Select]
@objects['time'] = Window_PlayTime.new

And when refering to the object, you'd obviously use -

@objects['time']

as opposed to the prior.

You can also play music in the scene, simply by adding -

Code: [Select]
super('Song Name Here')

Into the scenes initialize method.

Example -

Code: [Select]
#==============================================================================
# ** Scene_Menu
#------------------------------------------------------------------------------
#  This class performs menu screen processing.
#==============================================================================

class Scene_Menu < Scene_Base
  #--------------------------------------------------------------------------
  # * Object Initialization
  # menu_index : command cursor's initial position
  #--------------------------------------------------------------------------
  def initialize(menu_index = 0)
@menu_index = menu_index
super('015-Theme04')
  end
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
# Make command window
s1 = $data_system.words.item
s2 = $data_system.words.skill
s3 = $data_system.words.equip
s4 = "Status"
s5 = "Save"
s6 = "End Game"
@objects['command'] = Window_Command.new(160, [s1, s2, s3, s4, s5, s6])
@objects['command'].index = @menu_index
# If number of party members is 0
if $game_party.actors.size == 0
  # Disable items, skills, equipment, and status
  @objects['command'].disable_item(0)
  @objects['command'].disable_item(1)
  @objects['command'].disable_item(2)
  @objects['command'].disable_item(3)
end
# If save is forbidden
if $game_system.save_disabled
  # Disable save
  @objects['command'].disable_item(4)
end
# Make play time window
@objects['time'] = Window_PlayTime.new
@objects['time'].x = 0
@objects['time'].y = 224
# Make steps window
@objects['steps'] = Window_Steps.new
@objects['steps'].x = 0
@objects['steps'].y = 320
# Make gold window
@objects['gold'] = Window_Gold.new
@objects['gold'].x = 0
@objects['gold'].y = 416
# Make status window
@objects['status'] = Window_MenuStatus.new
@objects['status'].x = 160
@objects['status'].y = 0
super
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
super
# If command window is active: call update_command
if @objects['command'].active
  update_command
  return
end
# If status window is active: call update_status
if @objects['status'].active
  update_status
  return
end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (when command window is active)
  #--------------------------------------------------------------------------
  def update_command
# If B button was pressed
if Input.trigger?(Input::B)
  # Play cancel SE
  $game_system.se_play($data_system.cancel_se)
  # Switch to map screen
  $scene = Scene_Map.new
  return
end
# If C button was pressed
if Input.trigger?(Input::C)
  # If command other than save or end game, and party members = 0
  if $game_party.actors.size == 0 and @command_window.index < 4
# Play buzzer SE
$game_system.se_play($data_system.buzzer_se)
return
  end
  # Branch by command window cursor position
  case @objects['command'].index
  when 0  # item
# Play decision SE
$game_system.se_play($data_system.decision_se)
# Switch to item screen
$scene = Scene_Item.new
  when 1  # skill
# Play decision SE
$game_system.se_play($data_system.decision_se)
# Make status window active
@objects['command'].active = false
@objects['status'].active = true
@objects['status'].index = 0
  when 2  # equipment
# Play decision SE
$game_system.se_play($data_system.decision_se)
# Make status window active
@objects['command'].active = false
@objects['status'].active = true
@objects['status'].index = 0
  when 3  # status
# Play decision SE
$game_system.se_play($data_system.decision_se)
# Make status window active
@objects['command'].active = false
@objects['status'].active = true
@objects['status'].index = 0
  when 4  # save
# If saving is forbidden
if $game_system.save_disabled
  # Play buzzer SE
  $game_system.se_play($data_system.buzzer_se)
  return
end
# Play decision SE
$game_system.se_play($data_system.decision_se)
# Switch to save screen
$scene = Scene_Save.new
  when 5  # end game
# Play decision SE
$game_system.se_play($data_system.decision_se)
# Switch to end game screen
$scene = Scene_End.new
  end
  return
end
  end
  #--------------------------------------------------------------------------
  # * Frame Update (when status window is active)
  #--------------------------------------------------------------------------
  def update_status
# If B button was pressed
if Input.trigger?(Input::B)
  # Play cancel SE
  $game_system.se_play($data_system.cancel_se)
  # Make command window active
  @objects['command'].active = true
@objects['status'].active = false
@objects['status'].index = -1
  return
end
# If C button was pressed
if Input.trigger?(Input::C)
  # Branch by command window cursor position
  case @objects['command'].index
  when 1  # skill
# If this actor's action limit is 2 or more
if $game_party.actors[@objects['status'].index].restriction >= 2
  # Play buzzer SE
  $game_system.se_play($data_system.buzzer_se)
  return
end
# Play decision SE
$game_system.se_play($data_system.decision_se)
# Switch to skill screen
$scene = Scene_Skill.new(@objects['status'].index)
  when 2  # equipment
# Play decision SE
$game_system.se_play($data_system.decision_se)
# Switch to equipment screen
$scene = Scene_Equip.new(@objects['status'].index)
  when 3  # status
# Play decision SE
$game_system.se_play($data_system.decision_se)
# Switch to status screen
$scene = Scene_Status.new(@objects['status'].index)
  end
  return
end
  end
end

C & C please.

Script -

Code: [Select]
class Scene_Base
def initialize(bgm = nil)
# Create Objects Hash
@objects = {}
# Store bgm value
@bgm = bgm
end
def main
# Start playing bgm unless one is not provided
$game_system.bgm_play(RPG::AudioFile.new(@bgm)) unless @bgm.nil?
# Execute transition
Graphics.transition
# Main loop
loop do
# Update Scene
update
# Abort loop if screen is changed
break if $scene != self
end
# Prepare for transition
Graphics.freeze
# Dispose of Scene
dispose
end
def update
# Update Objects unless none exist
unless @objects.empty?
  for key in @objects.keys
@objects[key].update
  end
end
# Update Graphics
Graphics.update
# Update input information
Input.update
end
def dispose
# Dispose of Objects unless none exist
unless @objects.empty?
for key in @objects.keys
@objects[key].dispose
  end
end
# Stop playing bgm unless one is not playing
$game_system.bgm_stop unless @bgm.nil?
end
end
Title: Re: Scene_Base, Like Window_Base, for Scenes
Post by: Tsunokiette on October 09, 2006, 08:44:43 PM
*update*

Not done yet, but version 2 will include animation capabilities.


Code: [Select]
#==============================================================================
# ** Scene_Base
#------------------------------------------------------------------------------
# Tsunokiette
# 0.5
# October 9, 2006
#==============================================================================
class Scene_Base
#--------------------------------------------------------------------------
# * Initialization
#--------------------------------------------------------------------------
def initialize(bgm = nil)
@bgm = bgm
@intro = []
@ending = []
@animation = []
end
#--------------------------------------------------------------------------
# * Main
#--------------------------------------------------------------------------
def main
unless @bgm.nil?
file = RPG::AudioFile.new(@bgm)
$game_system.bgm_play(file)
end
Graphics.transition
@animation = @intro
animate
loop do
update
break if $scene != self
end
@animation = @ending
animate
Graphics.freeze
dispose
end
#--------------------------------------------------------------------------
# * Update
#--------------------------------------------------------------------------
def update(input = true)
self.instance_variables.each.do(|item|
item.update if update_include?(item))
Graphics.update
Input.update if input
end
#--------------------------------------------------------------------------
# * Dispose
#--------------------------------------------------------------------------
def dispose
self.instance_variables.each.do(|item|
item.dispose if dispose_include?(item))
$game_system.bgm_stop unless @bgm.nil?
end
#--------------------------------------------------------------------------
# * List of Updatable Object Classes
#--------------------------------------------------------------------------
def updatable_objects
[Window]
end
#--------------------------------------------------------------------------
# * Returns if a given Object is listed as an Updatable Object Class
#--------------------------------------------------------------------------
def update_include?(object)
updatable_objects.each.do(|item|
return true if object.is_a?(item))
return false
end
#--------------------------------------------------------------------------
# * List of Disposable Object Classes
#--------------------------------------------------------------------------
def disposable_objects
[Window,Sprite]
end
#--------------------------------------------------------------------------
# * Returns if a given Object is listed as a Disposable Object Class
#--------------------------------------------------------------------------
def dispose_include?(object)
disposable_objects.each.do(|item|
return true if object.is_a?(item))
return false
end
#--------------------------------------------------------------------------
# * Add to Intro Animation
#--------------------------------------------------------------------------
def intro(type,window,*args)
@intro.push([type,window,*args])
end
#--------------------------------------------------------------------------
# * Add to Ending Animation
#-------------------------------------------------------------------------
def ending(type,window,*args)
@ending.push([type,window,*args])
end
#--------------------------------------------------------------------------
# * Add Move Window Command To Animation
#--------------------------------------------------------------------------
def move(window,x,y,frames)
@animation.push([0,window,x,y,frames])
end
#--------------------------------------------------------------------------
# * Add Fade Window Command To Animation
#--------------------------------------------------------------------------
def fade(window,opacity,frames)
@animation.push([1,window,opacity,frames])
end
#--------------------------------------------------------------------------
# * Add Stretch Window Command To Animation
#--------------------------------------------------------------------------
def stretch(window,w,h,frames)
@animation.push([2,window,w,h,frames])
end
#--------------------------------------------------------------------------
# * Wait a given number of frames
#--------------------------------------------------------------------------
def wait(frames)
time = Graphics.frame_count + frames
while Graphics.frame_count < time
update(false)
end
end
#--------------------------------------------------------------------------
# * Run Animation and Reset it
#-----------------------------=--------------------------------------------
def animate
return if @animation.empty?
tempa = []
@animation.each.do(|item|
if (item[0] == 0) or (item[0] == 2)
tempa.push(item)
end)
tempb = {}
tempa.each.do(|item|
if tempb[item].nil?
tempb[item] = 0
end
tempb[item] += 1)
tempc = []
tempb.each.do(|item|
tempc.push(item) if tempb[item] > 1)
@animation.each.do(|item|
if tempc.include?(item)
item[0] = 3
end)
# Code
@animation = []
end
end

Anything else you guys would like in it?
Title: Re: Scene_Base, Like Window_Base, for Scenes
Post by: Arrow on October 09, 2006, 08:58:39 PM
Maybe moving the window around? Like, you could make it bounce in or something.
Title: Re: Scene_Base, Like Window_Base, for Scenes
Post by: Chaos of Destruction on October 09, 2006, 08:59:33 PM
For me...No. it's great for now until the Animations are done. Well Done.
Title: Re: Scene_Base, Like Window_Base, for Scenes
Post by: Tsunokiette on October 09, 2006, 09:02:30 PM
Well, theoreticly, you could create a bounce like effect using the move command.

Oh, and a word of warning, *somewhat*, the strectch command creates an empty window with the same dimensions as the given window, and stretches itself to the given dimentions, the dimentions of the window itself won't change.