class TyranoDsl::ExportGame::WritingContext

Context for writing

Attributes

current_scene_assets[R]

@return [Array<String>]

current_scene_content[R]

@return [Array<String>]

file_actions[R]

@return [Hash{String => Object}]

world[R]

@return [TyranoDsl::Elements::World]

Public Class Methods

new(world) click to toggle source

@param [TyranoDsl::Elements::World] world

# File lib/tyrano_dsl/export_game/writing_context.rb, line 21
def initialize(world)
  @logger = Logger.new(STDOUT)
  @world = world
  @file_actions = []
  @current_scene_content = nil
  @current_scene_name = nil
  @current_scene_assets = nil
  @current_scene_labels = nil
  @scene_writer = TyranoDsl::ExportGame::ElementsWriters::SceneWriter.new
end

Public Instance Methods

add_asset_loading(word_location, asset_content) click to toggle source

Add an asset to be loaded in the current scene

@param [Array<String>] word_location @param [String] asset_content @return [void] @raise [TyranoDsl::TyranoException]

# File lib/tyrano_dsl/export_game/writing_context.rb, line 49
def add_asset_loading(word_location, asset_content)
  check_in_scene(word_location)
  @current_scene_assets << asset_content
end
add_label(word_location, label_name) click to toggle source

Add an label in the current scene

@param [Array<String>] word_location @param [String] label_name @return [void] @raise [TyranoDsl::TyranoException]

# File lib/tyrano_dsl/export_game/writing_context.rb, line 60
def add_label(word_location, label_name)
  check_in_scene(word_location)
  if @current_scene_labels.include? label_name
    raise TyranoDsl::TyranoException, "Duplicated label [#{label_name}]"
  end
  @current_scene_labels << label_name
end
append_content(word_location, content) click to toggle source

Append some content to the current scene

@param [Array<String>] word_location @param [Array<String>] content @return [void] @raise [TyranoDsl::TyranoException]

# File lib/tyrano_dsl/export_game/writing_context.rb, line 38
def append_content(word_location, content)
  check_in_scene(word_location)
  @current_scene_content << content
end
end_writing() click to toggle source

Bookkeeping stuff to end the writing @raise [TyranoDsl::TyranoException] @return [void]

# File lib/tyrano_dsl/export_game/writing_context.rb, line 82
def end_writing
  write_current_scene
  @current_scene_content = nil
  @current_scene_name = nil
  @current_scene_assets = nil
  @current_scene_labels = nil
  @world.validate
  log {"Writing is over, #{@file_actions.length} actions created"}
end
init_new_scene(scene_name) click to toggle source

Initialize a new scene @param [String] scene_name the scene name @return [void]

# File lib/tyrano_dsl/export_game/writing_context.rb, line 71
def init_new_scene(scene_name)
  write_current_scene
  @current_scene_content = []
  @current_scene_name = scene_name
  @current_scene_assets = Set.new
  @current_scene_labels = []
end

Private Instance Methods

check_in_scene(word_location) click to toggle source

@param [Array<String>] word_location @return [void] @raise [TyranoDsl::TyranoException]

# File lib/tyrano_dsl/export_game/writing_context.rb, line 115
def check_in_scene(word_location)
  unless @current_scene_content
    exception = TyranoDsl::TyranoException.new('This action should take place in a scene')
    exception.set_backtrace word_location
    raise exception
  end
end
log() { || ... } click to toggle source
# File lib/tyrano_dsl/export_game/writing_context.rb, line 108
def log
  @logger.info(self.class) {yield}
end
write_current_scene() click to toggle source
# File lib/tyrano_dsl/export_game/writing_context.rb, line 94
def write_current_scene
  if @current_scene_name
    current_scene = @world.scenes[@current_scene_name]
    @file_actions.concat @scene_writer.write(
        current_scene,
        @current_scene_content,
        @current_scene_assets
    )
    @current_scene_labels.each do |label|
      current_scene.labels << label
    end
  end
end