triggerful 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/example/game-logic.rb +107 -0
- data/example/methods.rb +82 -0
- data/example/usage.rb +34 -0
- data/lib/triggerful.rb +19 -45
- metadata +15 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 61ac309ae45567aa35b123196c6668b619884283
|
4
|
+
data.tar.gz: efe1ceb742a31cc93022ce6d5c68a7390d4344be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8725c277b0204333e982a4d2a24de50f41b4323157311ddd0bc7d4a6cb4eb4fa22e7615a4aad8df5c26fa1948b02e79d9f924d115a19551e79ffe3691645b593
|
7
|
+
data.tar.gz: 74119b4bb875d04155dfdd80a8cf87a94c3cce59cf5012247084d66aaaaeb41b3d127c9dd0d3c53b413d0ffdf70d48f14d2bbb617975bbb2329e098a357681d8
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# Example game logic that could be used
|
2
|
+
require 'triggerful'
|
3
|
+
|
4
|
+
class Player
|
5
|
+
def initialize name, power, toughness, crit
|
6
|
+
@name, @power, @toughness, @crit = name, power, toughness, crit
|
7
|
+
@health = 100
|
8
|
+
@gear = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def name(*set)
|
12
|
+
if set[0]
|
13
|
+
@name = set[0]
|
14
|
+
end
|
15
|
+
@name
|
16
|
+
end
|
17
|
+
|
18
|
+
def power(*set)
|
19
|
+
if set[0]
|
20
|
+
@power += set[0]
|
21
|
+
end
|
22
|
+
@power
|
23
|
+
end
|
24
|
+
|
25
|
+
def toughness(*set)
|
26
|
+
if set[0]
|
27
|
+
@toughness += set[0]
|
28
|
+
end
|
29
|
+
@toughness
|
30
|
+
end
|
31
|
+
|
32
|
+
def crit(*set)
|
33
|
+
if set[0]
|
34
|
+
@crit += set[0]
|
35
|
+
end
|
36
|
+
@crit
|
37
|
+
end
|
38
|
+
|
39
|
+
def health(*set)
|
40
|
+
if set[0]
|
41
|
+
@health += set[0]
|
42
|
+
end
|
43
|
+
@health
|
44
|
+
end
|
45
|
+
|
46
|
+
def gear
|
47
|
+
@gear
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_s(*args)
|
51
|
+
puts "#{@name} health(#{@health}) power(#{@power}) toughness(#{@toughness}) crit(#{@crit}) gear(#{@gear})"
|
52
|
+
end
|
53
|
+
|
54
|
+
def equip(*items)
|
55
|
+
@gear += items
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Create two players
|
60
|
+
will = Player.new('Will', 15, 5, 20)
|
61
|
+
fred = Player.new('Fred', 10, 10, 10)
|
62
|
+
|
63
|
+
# Create an attack method (this should be attached to the Player class)
|
64
|
+
def attack(attacker, target)
|
65
|
+
puts "#{attacker.name} is attacking #{target.name}."
|
66
|
+
if rand(0..99) < attacker.crit
|
67
|
+
attacker.power*2
|
68
|
+
else
|
69
|
+
attacker.power
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Again, this is example logic. Bad code, do not follow.
|
74
|
+
def attacked(attacker, target, damage)
|
75
|
+
total_damage = damage - target.toughness
|
76
|
+
puts "#{target.name} has taken #{total_damage} points of damage"
|
77
|
+
target.health(-total_damage)
|
78
|
+
total_damage
|
79
|
+
end
|
80
|
+
|
81
|
+
def barbed_shield(attacker, target, legacy_damage, total_damage)
|
82
|
+
attacker.health(-(total_damage*0.2).floor) if target.gear.include?(:barbed_shield)
|
83
|
+
end
|
84
|
+
|
85
|
+
def display_stats(*players)
|
86
|
+
return_val = ""
|
87
|
+
players.each do |player|
|
88
|
+
if player.instance_of? Player
|
89
|
+
return_val += "{#{player.name}: health (#{player.health})} "
|
90
|
+
end
|
91
|
+
end
|
92
|
+
puts return_val
|
93
|
+
end
|
94
|
+
|
95
|
+
attacked_event = Trigger.new(:attacked, true, :barbed_shield, fred.public_method(:to_s), will.public_method(:to_s))
|
96
|
+
attack_event = Trigger.new(:attack, true, attacked_event)
|
97
|
+
|
98
|
+
|
99
|
+
# Arbitrarily equip fred with barbed_shield.
|
100
|
+
# Also, do not use equipment like this. You should really use classes.
|
101
|
+
fred.equip(:barbed_shield)
|
102
|
+
|
103
|
+
# Fight to the death!
|
104
|
+
while fred.health > 0 && will.health > 0 do
|
105
|
+
attack_event.trigger(fred, will) if fred.health > 0 && will.health > 0
|
106
|
+
attack_event.trigger(will, fred) if fred.health > 0 && will.health > 0
|
107
|
+
end
|
data/example/methods.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# A more comprehensive list of methods that Triggerful supports
|
2
|
+
require '~/Test/triggerful/lib/triggerful.rb'
|
3
|
+
|
4
|
+
$foobar = 0
|
5
|
+
|
6
|
+
def foo
|
7
|
+
amount_of_change = rand(0..100)
|
8
|
+
$foobar += amount_of_change
|
9
|
+
|
10
|
+
# The return value will be appended to the arguments sent
|
11
|
+
# to the callback.
|
12
|
+
amount_of_change
|
13
|
+
end
|
14
|
+
|
15
|
+
def say_foobar(amount_of_change)
|
16
|
+
puts "$foobar has increased by #{amount_of_change} and is now #{$foobar}"
|
17
|
+
end
|
18
|
+
|
19
|
+
# Set the second argument to true to append the return value
|
20
|
+
# from the trigger to the callback.
|
21
|
+
_foo = Trigger.new(:foo, true, :say_foobar)
|
22
|
+
_foo.trigger
|
23
|
+
|
24
|
+
# Sometimes you don't care about creating a new method the
|
25
|
+
# normal way and you just want to use a Proc instead.
|
26
|
+
_bar = Trigger.new(Proc.new{ $foobar += 1; 1 }, true, :say_foobar)
|
27
|
+
_bar.trigger
|
28
|
+
|
29
|
+
# You might even want to have a class's public method be
|
30
|
+
# an event. To get a hold of that, use instance.public_method(:symbol)
|
31
|
+
class FooBar
|
32
|
+
def initialize amount
|
33
|
+
@amount = amount
|
34
|
+
end
|
35
|
+
|
36
|
+
def add
|
37
|
+
$foobar += @amount
|
38
|
+
@amount
|
39
|
+
end
|
40
|
+
|
41
|
+
def subtract
|
42
|
+
$foobar -= @amount
|
43
|
+
@amount
|
44
|
+
end
|
45
|
+
|
46
|
+
def amount
|
47
|
+
@amount
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
twenty = FooBar.new(20)
|
52
|
+
|
53
|
+
# Note that FooBar.subtract has a return value, but you don't
|
54
|
+
# need to turn progression on to use the callback.
|
55
|
+
_twenty_add = Trigger.new(twenty.public_method(:add), true, :say_foobar)
|
56
|
+
_twenty_subtract = Trigger.new(twenty.public_method(:add), Proc.new{ puts "$foobar has decreased by #{twenty.amount} and is now #{$foobar}" })
|
57
|
+
|
58
|
+
_twenty_add.trigger
|
59
|
+
_twenty_subtract.trigger
|
60
|
+
|
61
|
+
# Triggers support a few methods that regular arrays support,
|
62
|
+
# such as remove, delete_at, length, and index(symbol). If you
|
63
|
+
# want an actual array of callbacks, then call Trigger.list
|
64
|
+
_twenty_subtract.add(:foo)
|
65
|
+
puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}\n----"
|
66
|
+
|
67
|
+
_twenty_subtract.insert(1, _twenty_add)
|
68
|
+
puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}\n----"
|
69
|
+
|
70
|
+
_twenty_subtract.delete_at(0)
|
71
|
+
puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}\n----"
|
72
|
+
|
73
|
+
_twenty_subtract.delete_at(_twenty_subtract.index(:foo))
|
74
|
+
puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}\n----"
|
75
|
+
|
76
|
+
_twenty_subtract.remove_all
|
77
|
+
puts "(#{_twenty_subtract.length}) #{_twenty_subtract.list}\n----"
|
78
|
+
|
79
|
+
# If you're ever unsure of what your event is called,
|
80
|
+
# such as when you're using a Proc, then you may call
|
81
|
+
# the Trigger.event to fetch that data
|
82
|
+
puts "Proc: #{_bar.event}\nSymbol: #{_foo.event}\nPublic Method: #{_twenty_add.event}"
|
data/example/usage.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# Triggerful usage
|
2
|
+
require 'triggerful'
|
3
|
+
|
4
|
+
$foobar = 0
|
5
|
+
|
6
|
+
def foo
|
7
|
+
$foobar += 1
|
8
|
+
end
|
9
|
+
|
10
|
+
def bar
|
11
|
+
puts $foobar
|
12
|
+
end
|
13
|
+
|
14
|
+
# create Trigger and callback, then trigger the Trigger
|
15
|
+
_foo = Trigger.new(:foo, :bar)
|
16
|
+
_foo.trigger
|
17
|
+
|
18
|
+
# create a method to determine if bar has been called before
|
19
|
+
$bar_called? = false
|
20
|
+
def bar_called
|
21
|
+
$bar_called = true
|
22
|
+
end
|
23
|
+
|
24
|
+
# create a callback for the callback
|
25
|
+
_bar = Trigger.new(:bar, :bar_called)
|
26
|
+
|
27
|
+
# replace old callback with new one (note that no symbol was used for the callback)
|
28
|
+
_foo.remove(:bar)
|
29
|
+
_foo.add(_bar)
|
30
|
+
|
31
|
+
# methods are not called when added to Trigger callbacks, only when triggered
|
32
|
+
puts $bar_called?
|
33
|
+
_foo.trigger
|
34
|
+
puts $bar_called?
|
data/lib/triggerful.rb
CHANGED
@@ -1,39 +1,3 @@
|
|
1
|
-
=begin
|
2
|
-
# USAGE:
|
3
|
-
|
4
|
-
$foobar = 0
|
5
|
-
|
6
|
-
def foo
|
7
|
-
$foobar += 1
|
8
|
-
end
|
9
|
-
|
10
|
-
def bar
|
11
|
-
puts $foobar
|
12
|
-
end
|
13
|
-
|
14
|
-
# create Trigger and callback, then trigger the Trigger
|
15
|
-
_foo = Trigger.new(:foo, :bar)
|
16
|
-
_foo.trigger
|
17
|
-
|
18
|
-
# create a method to determine if bar has been called before
|
19
|
-
bar_called? = false
|
20
|
-
def bar_called
|
21
|
-
bar_called = true
|
22
|
-
end
|
23
|
-
|
24
|
-
# create a callback for the callback
|
25
|
-
_bar = Trigger.new(:bar, :bar_called)
|
26
|
-
|
27
|
-
# replace old callback with new one (note that no symbol was used for the callback)
|
28
|
-
_foo.remove(:bar)
|
29
|
-
_foo.add(_bar)
|
30
|
-
|
31
|
-
# methods are not called when added to Trigger callbacks, only when triggered
|
32
|
-
puts bar_called?
|
33
|
-
_foo.trigger
|
34
|
-
puts bar_called?
|
35
|
-
=end
|
36
|
-
|
37
1
|
class Trigger
|
38
2
|
def initialize event, *callbacks
|
39
3
|
@callbacks = callbacks
|
@@ -51,10 +15,10 @@ class Trigger
|
|
51
15
|
end
|
52
16
|
|
53
17
|
def trigger(*args)
|
54
|
-
case @event
|
55
|
-
when
|
18
|
+
case @event
|
19
|
+
when Proc
|
56
20
|
event_data = @event.call
|
57
|
-
when
|
21
|
+
when Method
|
58
22
|
event_data = @event.call
|
59
23
|
else
|
60
24
|
event_data = self.method(@event).call(*args)
|
@@ -67,14 +31,14 @@ class Trigger
|
|
67
31
|
callback.trigger(*args)
|
68
32
|
end
|
69
33
|
else
|
70
|
-
case callback
|
71
|
-
when
|
34
|
+
case callback
|
35
|
+
when Proc
|
72
36
|
if @progression
|
73
37
|
callback.call(*args, event_data)
|
74
38
|
else
|
75
39
|
callback.call(*args)
|
76
40
|
end
|
77
|
-
when
|
41
|
+
when Method
|
78
42
|
if @progression
|
79
43
|
callback.call(*args, event_data)
|
80
44
|
else
|
@@ -97,8 +61,10 @@ class Trigger
|
|
97
61
|
if callback.instance_of? Trigger
|
98
62
|
callback.trigger(*args)
|
99
63
|
else
|
100
|
-
case callback
|
101
|
-
when
|
64
|
+
case callback
|
65
|
+
when Proc
|
66
|
+
callback.call
|
67
|
+
when Method
|
102
68
|
callback.call
|
103
69
|
else
|
104
70
|
method(callback).call(*args)
|
@@ -136,13 +102,21 @@ class Trigger
|
|
136
102
|
@callbacks.index(callback)
|
137
103
|
end
|
138
104
|
|
139
|
-
def
|
105
|
+
def event
|
140
106
|
@event
|
141
107
|
end
|
142
108
|
|
109
|
+
def length
|
110
|
+
@callbacks.length
|
111
|
+
end
|
112
|
+
|
143
113
|
def list
|
144
114
|
@callbacks
|
145
115
|
end
|
116
|
+
|
117
|
+
def callbacks
|
118
|
+
@callbacks
|
119
|
+
end
|
146
120
|
end
|
147
121
|
|
148
122
|
=begin
|
metadata
CHANGED
@@ -1,24 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: triggerful
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Bob Garrett
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: Create callbacks to methods that you can easily remove.
|
14
|
-
for lightweight event based programming. Even the medic agrees.
|
15
|
-
email: andrew@
|
13
|
+
description: Create callbacks to methods that you can easily and dynamically create/remove.
|
14
|
+
Quite useful for lightweight event based programming. Even the medic agrees.
|
15
|
+
email: andrew@karbon.tk
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- lib/triggerful.rb
|
21
|
-
|
21
|
+
- example/usage.rb
|
22
|
+
- example/methods.rb
|
23
|
+
- example/game-logic.rb
|
24
|
+
homepage: https://rubygems.org/gems/triggerful
|
22
25
|
licenses:
|
23
26
|
- MIT
|
24
27
|
metadata: {}
|
@@ -41,5 +44,9 @@ rubyforge_project:
|
|
41
44
|
rubygems_version: 2.0.6
|
42
45
|
signing_key:
|
43
46
|
specification_version: 4
|
44
|
-
summary: Triggerful is a gem used for creating modular callbacks
|
45
|
-
|
47
|
+
summary: Triggerful is a gem used for creating modular callbacks to be used for event
|
48
|
+
based programming.
|
49
|
+
test_files:
|
50
|
+
- example/usage.rb
|
51
|
+
- example/methods.rb
|
52
|
+
- example/game-logic.rb
|