class Gamefic::Scene::Base

An abstract class for building different types of scenes. It can be extended either through concrete subclasses or by creating anonymous subclasses through a scene helper method like `Gamefic::World::Scenes#custom`.

Attributes

tracked[W]
actor[R]

The scene's primary actor.

@return [Gamefic::Actor]

data[R]

@return [Hash{Symbol => Object}]

input[R]

The input received from the actor.

@return [String]

prompt[W]

The text to display when requesting input.

@return [String]

type[W]

A human-readable string identifying the type of scene.

@return [String]

Public Class Methods

new(actor, **data) click to toggle source
# File lib/gamefic/scene/base.rb, line 34
def initialize actor, **data
  @actor = actor
  @data = data
  post_initialize
end
on_start(&block) click to toggle source

@yieldparam [Class<Gamefic::Scene::Base>]

# File lib/gamefic/scene/base.rb, line 118
def self.on_start &block
  @start_block = block
end
start_block() click to toggle source
# File lib/gamefic/scene/base.rb, line 133
def start_block
  @start_block
end
subclass(&block) click to toggle source

@yieldparam [Class<Gamefic::Actor>] @return [Class<Gamefic::Scene::Base>]

# File lib/gamefic/scene/base.rb, line 96
def self.subclass &block
  c = Class.new(self) do
    on_start &block
  end
  c
end
tracked?() click to toggle source
# File lib/gamefic/scene/base.rb, line 137
def tracked?
  @tracked ||= false
end

Public Instance Methods

[](key) click to toggle source

A shortcut for the data hash.

@param key [Symbol] @return [Object]

# File lib/gamefic/scene/base.rb, line 44
def [] key
  data[key]
end
finish() click to toggle source

Finish the scene.

# File lib/gamefic/scene/base.rb, line 73
def finish
  @finish_block.call @actor, self unless @finish_block.nil?
  @finished = true
end
finished?() click to toggle source

Determine whether the scene's execution is finished.

@return [Boolean]

# File lib/gamefic/scene/base.rb, line 81
def finished?
  @finished ||= false
end
on_finish(&block) click to toggle source

Set a proc to be executed at the end of the scene.

# File lib/gamefic/scene/base.rb, line 53
def on_finish &block
  @finish_block = block
end
post_initialize() click to toggle source
# File lib/gamefic/scene/base.rb, line 48
def post_initialize
end
prompt() click to toggle source

Get the prompt to be displayed to the user when accepting input.

@return [String] The text to be displayed.

# File lib/gamefic/scene/base.rb, line 106
def prompt
  @prompt ||= '>'
end
start() click to toggle source

Start the scene.

# File lib/gamefic/scene/base.rb, line 66
def start
  self.class.start_block.call @actor, self unless self.class.start_block.nil?
  @actor.entered self if tracked?
end
state() click to toggle source

Get a hash that describes the current state of the scene.

@return [Hash]

# File lib/gamefic/scene/base.rb, line 88
def state
  {
    scene: type, prompt: prompt
  }
end
tracked=(bool) click to toggle source
# File lib/gamefic/scene/base.rb, line 126
def tracked= bool
  self.class.tracked = bool
end
tracked?() click to toggle source
# File lib/gamefic/scene/base.rb, line 122
def tracked?
  self.class.tracked?
end
type() click to toggle source

Get a String that describes the type of scene.

@return [String]

# File lib/gamefic/scene/base.rb, line 113
def type
  @type ||= 'Scene'
end
update() click to toggle source

Update the scene.

# File lib/gamefic/scene/base.rb, line 59
def update
  @input = actor.queue.shift
  finish
end