Jacius' Custom Events

From Rubygame

Jump to: navigation, search

This wiki has moved! You can view and edit the new wiki on GitHub!

Jacius wrote this silly example after being annoyed by a certain Rubygame user...

# How to use custom event classes with Rubygame's EventHandler class:
# A guide for lazy, annoying people.
 
 
require "rubygame"
include Rubygame
 
 
# Custom event that occurs whenever someone does something annoying.
class AnnoyingEvent
  attr_reader :annoying_person
 
  def initialize( annoying_person )
    @annoying_person = annoying_person
  end
end
 
 
# A person who bothers other people with annoying stuff.
class AnnoyingPerson
 
  attr_reader :name
 
  def initialize( name )
    @name = name
  end
 
  # Bother the other person by making them handle an AnnoyingEvent.
  def bother( other_person )
    puts "#{@name}: #{other_person.name}: yo. You\'re there?"
    other_person.handle( AnnoyingEvent.new(self) )
  end
 
end
 
 
# Someone who has to endure annoying people.
class SoftwareMaintainer
  include EventHandler::HasEventHandler
 
  attr_reader :name
 
  def initialize( name )
    @name = name
    @annoyance_level = 0
    @patience = 10
    make_magic_hooks( AnnoyingEvent => :be_annoyed )
  end
 
  def be_annoyed( event )
    @annoyance_level += 1
 
    if(@annoyance_level > @patience)
      raise( "#{@name} gets annoyed and slaps " +
             "#{event.annoying_person.name} with a trout. STFU. >:P" )
    end
 
    puts "#{@name}: Sigh, not this again..."
  end
end
 
 
jacius = SoftwareMaintainer.new("jacius")
kiba = AnnoyingPerson.new("kiba")
 
 
loop do
  kiba.bother( jacius )
  sleep 1
end
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox