The Rubygame Book Part 7
From Kibabase
| Home | Beginning | Back | Next | End |
|---|---|---|---|---|
| Back to Table of Content | Jump to Beginning | Back | Next | Jump to End(Part 8) |
So far we have created a game loop and a few skeleton classes all for managing the flow of game logics. Since they're not completed, we need to develop them further. We start off with this task by developing the Registrar class.
Contents |
The Registrar Class
Earlier in the tutorial, we created an empty registrar class and named it lib/events/registrar_top.rb so that it will load first. So what is so improtant about registrar that it needed to be loaded first? Well, the rubygame event system doesn't lend itself to easiness for developers when it come to managing hooks. Since we're not in the business of submiting patches to the rubygame author different way of managing hooks, we gottach extend them for our codebase. That's the purpose of the registrar class, to extend the rubygame event system so our life will be easier.
So, what is a Rubygame HookEvent that we're dealing with? The Rubygame HookEvent, or hooks for short, are how we assign trigger to events. For example, you assign the key K to the shooting method. When you pressed K, you shot something. Thus, the registrar class is all about managing hooks and lot of them. That mean constructing the proper hook, adding them, and then removing them.
Our chief purpose in this class is not to duplicate functionality of the rubygame event system when it comes to managing hooks, but rather complement and if neccesary, replace a method with something that fit our needs.
file: lib/events/registrar_top.rb
class Registrar < Rubygame::EventHandler end
Constructing a Hook and Adding Them
Constructing a Rubygame hook requires a little bit of information to create. When you initialize a EventHook instance, it requires the following information:
- Object: The object of action.
- Trigger: Tells it to engage the hook based on certain conditions.
- Action: Tells the object to act.
- Consumes: When this hook is triggered, the rest of the hooks that follow after doesn't see it.
- Active: Tells whether to check the hook for engagement or not.
These informations are stored in hashes.
For our purpose, these is a set of default assignments that will do what we needed, most of the time.
For the most part, Trigger will be a Rubygame::EventTriggers::YesTrigger.new() unless otherwise indicated. This is because we're mostly dealing with pressing buttons although sometime it is also neccesary to know if a button is not pressed. The action will be assigned to Rubygame::EventActions::MethodAction.new(:handle). This action assignment make it convenient for us to chain together multiple hooks if needed be. Consumes will be set to false simply because I never found uses for it. Then we need an identifier so we can identify the hook.
Time to translate the last few paragraphs into test specifications.
file: test/events/test_registrar.rb
def test_construct_hook @nilObject = NilClass @registrar.construct_hook(@nilObject, :test) hooks = get_hooks() assert hooks[0][:trigger].class == Rubygame::EventTriggers::YesTrigger assert hooks[0][:action].class == Rubygame::EventActions::MethodAction assert hooks[0][:consumes] == false assert hooks[0][:active] == true assert hooks[0][:identifer] == :test end
Here's my solution to the test specification.
file: lib/events/registrar_top.rb
def construct_hook object, name, trigger = Rubygame::EventTriggers::YesTrigger.new() , action = Rubygame::EventActions::MethodAction.new(:handle) , consumes = false, active = true hook = { :owner => object, :trigger => trigger, :action => action, :consumes => consumes, :active => true, :identifer => name } append_hook(hook) end
Removing Hooks
This is where the :identifer hash name will come in handy. It helps us identify which hook we want to delete. So for our destroy unit test, we're going to create two objects and one to delete.
file: test/events/test_registrar.rb
def test_create_destroy object = NilClass another = NilClass #Add another object to know if we're deleting just one or all of them. @registrar.construct_hook(object, :test) @registrar.construct_hook(another, :hello) hooks = get_hooks() assert hooks.size() == 2 @registrar.remove_hook(:test) assert hooks.size() == 1 end
Here's my example.
file: lib/events/registrar_top.rb
def remove_hook name @event_handler.hooks.each do |hook| if hook[:identifer] == name @event_handler.remove_hook(hook) break end end end
Conclusion
The registrar class we constructed is just the tip of the iceberg. There's other parts of the events system we have to construct. Hence, part 8 handles the construction of the Mode class. Proceed to Part 8.

