Sha256: 5b1b9675824f211be0fb78af4c0a358e429735f35430b308502b4b35b886e982

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Gamefic
  class Rulebook
    # Blocks of code to be executed for various narrative events, such as
    # on_ready and on_update.
    #
    class Events
      attr_reader :player_output_blocks

      attr_reader :player_conclude_blocks

      attr_reader :ready_blocks

      attr_reader :update_blocks

      attr_reader :conclude_blocks

      def initialize
        @ready_blocks = []
        @update_blocks = []
        @conclude_blocks = []
        @player_conclude_blocks = []
        @player_output_blocks = []
      end

      def empty?
        [player_output_blocks, player_conclude_blocks, ready_blocks, update_blocks, conclude_blocks].all?(&:empty?)
      end

      def freeze
        super
        instance_variables.each { |k| instance_variable_get(k).freeze }
        self
      end

      # @return [void]
      def on_ready callback
        @ready_blocks.push callback
      end

      def on_update callback
        @update_blocks.push callback
      end

      # @return [void]
      def on_conclude callback
        @conclude_blocks.push callback
      end

      # @yieldparam [Actor]
      # @return [void]
      def on_player_conclude callback
        @player_conclude_blocks.push callback
      end

      # @yieldparam [Actor]
      # @yieldparam [Hash]
      # @return [void]
      def on_player_output callback
        @player_output_blocks.push callback
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gamefic-3.6.0 lib/gamefic/rulebook/events.rb
gamefic-3.5.0 lib/gamefic/rulebook/events.rb
gamefic-3.4.0 lib/gamefic/rulebook/events.rb