Sha256: 77bf72626e749d936d87f4551a37b45a8eb991368162c1939a21b6738cb805ca

Contents?: true

Size: 1.23 KB

Versions: 14

Compression:

Stored size: 1.23 KB

Contents

# This is a convenience trait.
#
# Instead of calling
#   @controls << Control.new(self, @player1, :some_key_code => :action)
# you can define the controls in the object itself.
#
module Controllable extend Trait
  
  def self.included controllable
    controllable.extend ClassMethods
  end
  
  module ClassMethods
    
    # Enables to define the controls in the object itself, as in the Example:
    # 
    # class Spaceship < Thing
    #   it_is Controllable
    #   controls Gosu::Button::KbA     => Turnable::Left,
    #            Gosu::Button::KbD     => Turnable::Right,
    #            Gosu::Button::KbW     => Moveable::Accelerate,
    #            Gosu::Button::KbS     => Moveable::Backwards,
    #            Gosu::Button::KbSpace => Shooter::Shoot
    #
    def controls mapping
      attr_accessor :controls_mapping
      InitializerHooks.register self do
        self.controls_mapping = mapping
        self.controls
      end
    end
    
  end
  
  # You can add controls dynamically.
  #
  # Provide a hash of mappings:
  #   player_one.controls Gosu::Button::KbA => Turnable::Left,
  #                       Gosu::Button::KbD => Turnable::Right
  #
  def controls mapping = nil
    self.window.add_controls_for self, mapping
  end
  
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
gosu_extensions-0.3.8 lib/traits/controllable.rb
gosu_extensions-0.3.7 lib/traits/controllable.rb
gosu_extensions-0.3.6 lib/traits/controllable.rb
gosu_extensions-0.3.5 lib/traits/controllable.rb
gosu_extensions-0.3.4 lib/traits/controllable.rb
gosu_extensions-0.3.3 lib/traits/controllable.rb
gosu_extensions-0.3.2 lib/traits/controllable.rb
gosu_extensions-0.3.1 lib/traits/controllable.rb
gosu_extensions-0.3.0 lib/traits/controllable.rb
gosu_extensions-0.2.9 lib/traits/controllable.rb
gosu_extensions-0.2.8 lib/traits/controllable.rb
gosu_extensions-0.2.7 lib/traits/controllable.rb
gosu_extensions-0.2.6 lib/traits/controllable.rb
gosu_extensions-0.2.5 lib/traits/controllable.rb