Class: LZRTag::Hook::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/lzrtag/hooks/base_hook.rb

Overview

Base class for all game hooks, implements DSL. This class shall be used as a base class for any DIY game hooks. The purpose of any hook is to implement a specific element of a game, such as damaging players, regenerating them, handing out teams, etc.

This hook base class implements a DSL that makes it very easy for the application to implement their own behavior in a modular fashion.

@example class MyHook < LZRTag::Base::Hook # Hooks can register which parameters are configurable, and how # This will be used to reconfigure them on the fly, but it is not mandatory # to register options. describe_option :myValue, “A description of my Value”;

def initialize(handler, **options) super(handler);

@myValue = options || “default”; end

# The “on” DSL makes it easy to perform tasks on # any arbitrary event on :playerKilled do |player| puts “Player #playerplayer.name was killed :C”; end end

class MyGame < LZRTag::Game::Base # A game can register that it wants to use this hook, and # even which options to use for it. hook :aHook, MyHook, “Not a default!”; end

# Alternatively, the hook can be added to the game directly handler.add_hook(MyHook);

Direct Known Subclasses

Game::Base, Damager, Debug, Regenerator, TeamSelector

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ Base

Returns a new instance of Base.



52
53
54
55
56
# File 'lib/lzrtag/hooks/base_hook.rb', line 52

def initialize(handler)
  @localCBList = Hash.new();

  @handler = handler
end

Class Method Details

.describe_option(optionSymbol, descString, extraDetails = {}) ⇒ Object

DSL function to describe an option of this hook. The application can use this DSL to describe a given option, identified by optionSymbol. The extraDetails hash is optional, but in the future will allow this hook to be reconfigured remotely via MQTT!

Parameters:

  • optionSymbol (Symbol)

    The Symbol used for this option

  • descString (String)

    String description of this option.

  • extraDetails (Hash) (defaults to: {})

    Optional hash to provide further details of this option, such as “min”, “max”, “type”, etc.

Raises:

  • (ArgumentError)


67
68
69
70
71
72
# File 'lib/lzrtag/hooks/base_hook.rb', line 67

def self.describe_option(optionSymbol, descString, extraDetails = {})
  raise ArgumentError, "Option shall be a symbol!" unless optionSymbol.is_a? Symbol
  raise ArgumentError, "Description should be a string!" unless descString.is_a? String
  getOptionDescriptions()[optionSymbol] = extraDetails;
  getOptionDescriptions()[optionSymbol][:desc] = descString;
end

.getCBsObject



43
44
45
46
# File 'lib/lzrtag/hooks/base_hook.rb', line 43

def self.getCBs()
  @globalCBList ||= Hash.new();
  return @globalCBList;
end

.getOptionDescriptionsObject



47
48
49
50
# File 'lib/lzrtag/hooks/base_hook.rb', line 47

def self.getOptionDescriptions()
  @globalOptionDescriptions ||= Hash.new();
  return @globalOptionDescriptions
end

.on(evtName, &block) ⇒ Object

DSL function to add a block on an event. The application can provide a block to this function that will be executed for every “evtName” game event

Parameters:

  • evtName (Symbol)

    Event name to trigger this block on

Raises:

  • (ArgumentError)


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/lzrtag/hooks/base_hook.rb', line 78

def self.on(evtName, &block)
  raise ArgumentError, "Block needs to be given!" unless block_given?

  evtName = [evtName].flatten
  evtName.each do |evt|
    unless (evt.is_a? Symbol)
      raise ArgumentError, "Event needs to be a symbol or array of symbols!"
    end
    getCBs()[evt] ||= Array.new();
    getCBs()[evt] << block;
  end
end

Instance Method Details

#consume_event(evtName, data) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/lzrtag/hooks/base_hook.rb', line 109

def consume_event(evtName, data)
  if(cbList = self.class.getCBs()[evtName])
    cbList.each do |cb|
      begin
        instance_exec(*data, &cb);
      rescue StandardError => e
        puts e.message
        puts e.backtrace.inspect
      end
    end
  end
  if(cbList = @localCBList[evtName]) then
    cbList.each do |cb|
      begin
        cb.call(*data);
      rescue StandardError => e
        puts e.message
        puts e.backtrace.inspect
      end
    end
  end
end

#on(evtName, &block) ⇒ Object

Function to add hooks to the already instantiated hook. This function works exactly like LZRTag::Hook::Base.selfself.on, except that it acts on an instance of hook, and allows the app to extend a standard hook by extending it afterwards.

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lzrtag/hooks/base_hook.rb', line 95

def on(evtName, &block)
  raise ArgumentError, "Block needs to be given!" unless block_given?

  evtName = [evtName].flatten
  evtName.each do |evt|
    unless (evt.is_a? Symbol)
      raise ArgumentError, "Event needs to be a symbol or array of symbols!"
    end
    @localCBList[evt] ||= Array.new();
    @localCBList[evt] << block;
  end
end

#on_hookin(handler) ⇒ Object



132
133
134
# File 'lib/lzrtag/hooks/base_hook.rb', line 132

def on_hookin(handler)
  @handler = handler;
end

#on_hookoutObject



135
136
# File 'lib/lzrtag/hooks/base_hook.rb', line 135

def on_hookout()
end

#process_raw_hitObject



138
139
140
# File 'lib/lzrtag/hooks/base_hook.rb', line 138

def process_raw_hit(*)
  return true;
end