Class: GamePlay::Base

Inherits:
Object show all
Includes:
DisplayMessage, Input
Defined in:
scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb,
scripts/01450 Systems/00004 Message/00003 GamePlay/00001 DisplayMessage.rb

Overview

The base class of every GamePlay scene interface

Add some usefull functions like message display and scene switch and perform the most of the task for you.

Generic Process of a GamePlay::Base
  1. initialize
    1.1 Create the message box (if called by super(false) or super())
  2. main
  2.1 main_begin
    2.1.1 create_graphics
    2.1.2 Graphics.transition (fade in)
  2.2 loop { update }
  2.3 main_end
    2.3.1 Graphics.freeze (fade out)
    2.3.2 dispose : grep all the /viewport/ ivar and dispose them
  3. update (in GamePlay::BaseCleanUpdate)
    3.1 update message
    3.2 update inputs (if not locked by message)
    3.3 update mouse (if not locked by inputs)
    3.4 update graphics (always)

This class is inherited by GamePlay::BaseCleanUpdate

You usually will define your Scene the following way : “`ruby

class Scene < BaseCleanUpdate
  # Create a new scene
  # @param args [Array] input arguments (do something better than *args)
  def initialize(*args)
    super() # <= the () force super to be called without argument because by `super` alone use the method arguments!
    # Initialize only the logic here (instance variable used for the state or data used by the UI)
  end

  # Called when input can be updated (put your input related code inside)
  # @return [Boolean] if the update can continue
  def update_inputs
    # ...
    return true
  end

  # Called when mouse can be updated (put your mouse related code inside, optional)
  # @param moved [Boolean] boolean telling if the mouse moved
  # @return [Boolean] if the update can continue
  def update_mouse(moved)
    return unless moved
    # ...
    return true
  end

  # Called each frame after message update and eventual mouse/input update
  # @return [Boolean] if the update can continue
  def update_graphics
    # ...
    return true
  end

  private

  # Create all the UI and thing related to graphics (super create the viewport)
  def create_graphics
    create_viewport # Necessary to make the scene work properly
    # ...
  end

  # (optional) Create the viewport (called by create_graphics from Base)
  def create_viewport
    super # < if you still use main with default settings, otherwise don't call super
    @sub_viewport = Viewport.create(...) # < Sub viewport for other stuff
  end
end

“`

Note : You don't have to define the dispose function with this. All the viewport that are stored inside ivar will be

automatically disposed if the variable name contains viewport.

Author:

  • Nuri Yuri

Constant Summary collapse

DEFAULT_TRANSITION =

Default fade type used to switch between interfaces

Returns:

  • (Symbol)

    :transition (for Graphics.freeze/transition), :fade_bk (for fade through black)

:transition
DEFAULT_TRANSITION_PARAMETER =

Parameters of the transition

Returns:

  • (Integer, Array)

    (usually the number of frame for the transition)

16

Constants included from Input

Input::ALIAS_KEYS, Input::AXIS_MAPPING, Input::AXIS_SENSITIVITY, Input::DEAD_ZONE, Input::Keyboard, Input::Keys, Input::NON_TRIGGER_ZONE, Input::REPEAT_COOLDOWN, Input::REPEAT_SPACE

Constants included from DisplayMessage

DisplayMessage::MESSAGE_ERROR, DisplayMessage::MESSAGE_PROCESS_ERROR

Instance Attribute Summary collapse

Attributes included from DisplayMessage

#message_window

Instance Method Summary collapse

Methods included from Input

dir4, dir8, get_text, joy_axis_position, press?, register_events, released?, repeat?, swap_states, trigger?

Methods included from DisplayMessage

#can_display_message_be_called?, #close_message_window, #display_message, #display_message_and_wait, #message_class, #message_processing?, #message_visible, #message_visible=

Constructor Details

#initialize(no_message = false, message_z = 20_000, *message_viewport_args) ⇒ Base

rubocop: disable Style/OptionalBooleanParameter Create a new GamePlay scene

Parameters:

  • no_message (Boolean) (defaults to: false)

    if the scene is created wihout the message management

  • message_z (Integer) (defaults to: 20_000)

    the z superiority of the message

  • message_viewport_args (Array)

    if empty : [:main, message_z] will be used.



103
104
105
106
107
108
109
110
111
112
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 103

def initialize(no_message = false, message_z = 20_000, *message_viewport_args)
  # List of object to dispose in #dispose
  @object_to_dispose = []
  # Force the message window of the map to be closed
  Scene_Map.from($scene).window_message_close(true) if $scene.instance_of?(Scene_Map)
  message_initialize(no_message, message_z, message_viewport_args)
  # Store the current scene
  @__last_scene = $scene
  message_soft_lock_prevent
end

Instance Attribute Details

#__last_sceneBase (readonly)

The scene that called this scene (usefull when this scene needs to return to the last scene)

Returns:



90
91
92
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 90

def __last_scene
  @__last_scene
end

#__result_processProc?

The process that is called when the call_scene method returns

Returns:

  • (Proc, nil)


93
94
95
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 93

def __result_process
  @__result_process
end

#runningBoolean

If the current scene is still running

Returns:

  • (Boolean)


96
97
98
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 96

def running
  @running
end

#viewportViewport? (readonly)

The viewport in which the scene is shown

Returns:



87
88
89
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 87

def viewport
  @viewport
end

Instance Method Details

#add_disposable(*args)

Add a disposable object to the “object_to_dispose” array

Parameters:



138
139
140
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 138

def add_disposable(*args)
  @object_to_dispose.concat(args)
end

#call_scene(name, *args, fade_out_params: nil, fade_in_params: nil, **kwarg, &result_process) ⇒ Boolean

Call an other scene

Parameters:

  • name (Class)

    the scene to call

  • args (Array)

    the parameter of the initialize method of the scene to call

  • fade_out_params (Array, nil) (defaults to: nil)

    params to send to the fade_out function (when this scene hides to call the next scene)

  • fade_in_params (Array, nil) (defaults to: nil)

    params to send to the fade_in function (when this scene comes back)

Returns:

  • (Boolean)

    if this scene can still run



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 182

def call_scene(name, *args, fade_out_params: nil, fade_in_params: nil, **kwarg, &result_process)
  fade_out(*(fade_out_params || [@cfo_type || DEFAULT_TRANSITION, @cfo_param || DEFAULT_TRANSITION_PARAMETER]))
  # Make the current scene invisible
  self.visible = false
  result_process ||= @__result_process
  @__result_process = nil
  # @type [GamePlay::Base]
  scene = name.new(*args, **kwarg)
  scene.main { Scheduler.start(:on_scene_switch, self.class) }
  # Call the result process if any
  result_process&.call(scene)
  # If the scene has changed we stop this one
  return @running = false if $scene != self || !@running

  self.visible = true
  fade_in(*(fade_in_params || [@cfi_type || DEFAULT_TRANSITION, @cfi_param || DEFAULT_TRANSITION_PARAMETER]))
  return true
end

#dispose

Note:

@viewport and @message_window will be disposed.

Dispose the scene graphics.



126
127
128
129
130
131
132
133
134
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 126

def dispose
  message_soft_lock_prevent
  Scheduler.start(:on_dispose, self.class)
  message_dispose
  @object_to_dispose.each { |object| object.dispose unless object.disposed? }
  instance_variables.grep(/viewport/).collect { |ivar| instance_variable_get(ivar) }.each do |vp|
    vp.dispose if vp.is_a?(Viewport) && !vp.disposed?
  end
end

#find_parent(klass, fallback = self)

Find a parent scene

Parameters:

  • klass (Class<GamePlay::Base>)

    criteria passed to .is_a?()

  • fallback (GamePlay::Base) (defaults to: self)

    result if the scene was not found



235
236
237
238
239
240
241
242
243
244
245
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 235

def find_parent(klass, fallback = self)
  scene = self
  while scene.is_a?(Base)
    scene = scene.__last_scene
    break if scene == self
    next unless scene.is_a?(klass)

    return scene
  end
  return false
end

#main

The GamePlay entry point (Must not be overridden).



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 143

def main
  raise 'You forgot to call super in initialize of your scene' unless @object_to_dispose
  # Store the last scene and store self in $scene
  @__last_scene = $scene if $scene != self
  $scene = self
  yield if block_given? # Ensure we call the on_scene_switch in call_scene
  # Tell the interface is running
  @running = true
  # Main processing
  main_begin
  main_process
  main_end
  # Reset $scene unless it was already done
  $scene = @__last_scene if $scene == self
  Scheduler.start(:on_scene_switch, self.class)
end

#return_to_scene(name, *args) ⇒ Boolean

Note:

This scene will stop running

Return to an other scene, create the scene if args.size > 0

Parameters:

  • name (Class)

    the scene to return to

  • args (Array)

    the parameter of the initialize method of the scene to call

Returns:

  • (Boolean)

    if the scene has successfully returned to the desired scene



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 206

def return_to_scene(name, *args)
  if args.empty?
    scene = self
    while scene.is_a?(Base)
      scene = scene.__last_scene
      break if scene == self
      next unless scene.class == name

      $scene = scene
      @running = false
      return true
    end
    return false
  end
  $scene = name.new(*args)
  @running = false
  return true
end

#snap_to_bitmapTexture

Note:

You have to dispose the bitmap you got from this function

Take a snapshot of the scene

Returns:



228
229
230
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 228

def snap_to_bitmap
  @viewport&.snap_to_bitmap || Texture.new(16, 16)
end

#updateBoolean

Scene update process

Returns:

  • (Boolean)

    if the scene should continue the update process or abort it (message/animation etc…)



117
118
119
120
121
122
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 117

def update
  message_update
  return false if message_processing?

  return true
end

#visibleBoolean

Tell if the scene is visible

Returns:

  • (Boolean)


169
170
171
172
173
174
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 169

def visible
  return @viewport.visible if @viewport
  return message_visible if @message_window

  return true
end

#visible=(value)

Change the viewport visibility of the scene

Parameters:

  • value (Boolean)


162
163
164
165
# File 'scripts/01450 Systems/00000 General/00003 GamePlay__Base/00000 GamePlay__Base.rb', line 162

def visible=(value)
  @viewport.visible = value if @viewport
  self.message_visible = value
end