gosu_wrapper 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +182 -0
- data/lib/gosu_wrapper/grid_helpers.rb +63 -0
- data/lib/gosu_wrapper/util.rb +1 -27
- data/lib/gosu_wrapper.rb +7 -0
- data/lib/version.rb +1 -1
- metadata +3 -2
- data/readme.md +0 -126
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4714b462fb1c471cf994ff6dc847b3e7b1685b44
|
4
|
+
data.tar.gz: 06dbe58ba8eb7d0a895a05e2f33927f5fc37a6b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed5952d37fd52deff3aa3136469684b71b2737e61c1976a29d7105b6c79192ecc946fd93754dfa0ab16f1fb8b5fad43010a57e27f4289c225aec24ee8563c3f9
|
7
|
+
data.tar.gz: 8e1744ea198b03d390025a3970551903e5b80ddbcc15ebb1e42c040c55bb444b62551a7541d0da8503d14adea3b24d9ff53e875deff9c0cebe284bb49fc1a7d0
|
data/README.md
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
### Installing
|
2
|
+
|
3
|
+
_gosu and activesupport will be loaded as dependencies_
|
4
|
+
|
5
|
+
`gem install gosu_wrapper` or in a gemfile: `gem 'gosu_wrapper'`
|
6
|
+
|
7
|
+
### Usage
|
8
|
+
|
9
|
+
One public class is exposed, `GosuWrapper`. It's essentially a wrapper over an
|
10
|
+
anonymous `Gosu::Window` class that gets functionality added through metaprogramming.
|
11
|
+
|
12
|
+
In lieue of a "getting started" snippet, I'm going to delegate that to the
|
13
|
+
[gosu_wrapper_snake_example](http://github.com/maxpleaner/gosu_wrapper_snake_example)
|
14
|
+
codebase and run through the API step-by-step here.
|
15
|
+
|
16
|
+
---
|
17
|
+
|
18
|
+
**`#initialize(width:, height:, attributes:)`**
|
19
|
+
|
20
|
+
- This defines `#window` which is an instance of `Gosu::Window`.
|
21
|
+
|
22
|
+
- width & height are num pixels (of the total window).
|
23
|
+
|
24
|
+
- attributes is an array of symbols.
|
25
|
+
- It must at least include `:window_height` and `:window_width`.
|
26
|
+
- All of the keys in your app's state hash should be given here.
|
27
|
+
- Each of them get attr_accessors defined on `#window` and shorthand accessors
|
28
|
+
using `method_missing` (see next section).
|
29
|
+
|
30
|
+
---
|
31
|
+
|
32
|
+
**There are four instance methods added using `method_missing`**
|
33
|
+
|
34
|
+
1. `#get_<attr>`
|
35
|
+
delegates the getter to `window`
|
36
|
+
|
37
|
+
2. `#set_<attr>(val)`
|
38
|
+
delegates the setter to `window`
|
39
|
+
|
40
|
+
3. `#change_<attr>(sym, arg)`
|
41
|
+
a shorthand for some update operations on non-mutable objects.
|
42
|
+
For example `change_score(:+, 1)`
|
43
|
+
|
44
|
+
4. `#get_or_set_<attr>(&blk)`
|
45
|
+
returns the val if it's truthy, and otherwise sets the val equal to the
|
46
|
+
block result (returning it as well).
|
47
|
+
|
48
|
+
---
|
49
|
+
|
50
|
+
**`#define_method_on_window(name, &blk)`**
|
51
|
+
|
52
|
+
- this should be a private method, but is used by `#add_hook/#add_helper` so
|
53
|
+
it's worth explaining.
|
54
|
+
|
55
|
+
- It defines the method named `name` on `window` with `blk` as its body.
|
56
|
+
|
57
|
+
- Arguments are determined by the given block
|
58
|
+
|
59
|
+
- The block is always invoked with the `GosuWrapper` instance as the value of `self`
|
60
|
+
|
61
|
+
---
|
62
|
+
|
63
|
+
**`#scope(*args, **keywords, &blk)`**
|
64
|
+
|
65
|
+
- another method that should be private; this is what handles calling a proc
|
66
|
+
with the `GosuWrapper` instance as the value of `self`.
|
67
|
+
|
68
|
+
---
|
69
|
+
|
70
|
+
**`#config(&blk)`**
|
71
|
+
|
72
|
+
- mostly the same thing as `#scope` but is meant to be used publically.
|
73
|
+
|
74
|
+
- the only functional difference is this always returns `self`
|
75
|
+
(the `GosuWrapper` instance) whereas `#scope` returns the result of the
|
76
|
+
block.
|
77
|
+
|
78
|
+
- Like `#scope`, this is a classic DSL-style method which exists for the
|
79
|
+
sole purpose of saving keystrokes (using `instance_exec`)
|
80
|
+
|
81
|
+
---
|
82
|
+
|
83
|
+
**`#add_hook(name, &blk)`**
|
84
|
+
|
85
|
+
- a "hook" conceptually is a method internally defined by `Gosu::Window`
|
86
|
+
that we're tapping into by overriding. Hooking into events `update`,
|
87
|
+
`button_down`, and `draw` are how the app works fundamentally.
|
88
|
+
|
89
|
+
- the `button_down` proc is passed an `id` which can be checked against
|
90
|
+
the values in `#buttons` to determine which key was pressed.
|
91
|
+
|
92
|
+
- `update` and `draw` are run every tick. Conceptually, `update`
|
93
|
+
is used to change the state and `draw` to represent it.
|
94
|
+
|
95
|
+
---
|
96
|
+
|
97
|
+
**`#add_helper(name, &blk)`**
|
98
|
+
|
99
|
+
- functionally speaking this is the exact same thing as `add_hook` -
|
100
|
+
it defines a method on `Gosu::Window`.
|
101
|
+
|
102
|
+
- It exists to distinguish between built-in hooks like `update` from
|
103
|
+
custom methods.
|
104
|
+
|
105
|
+
- These can be called with `#invoke` or its alias `#call_helper`.
|
106
|
+
|
107
|
+
---
|
108
|
+
|
109
|
+
**`#show`**
|
110
|
+
|
111
|
+
- Starts the game. Needs to be called only once.
|
112
|
+
|
113
|
+
---
|
114
|
+
|
115
|
+
**`#draw_rect(start_x:, start_y:, end_x:, end_y:, color)`**
|
116
|
+
|
117
|
+
- Should be called from `draw` only. Fills the rectangle with `color`
|
118
|
+
- Needs to run every `draw` tick.
|
119
|
+
|
120
|
+
---
|
121
|
+
|
122
|
+
**`#colors`**
|
123
|
+
|
124
|
+
- a hash of name => color object pairs
|
125
|
+
- e.g. `colors[:red]`
|
126
|
+
- it's also possible to make hexademical colors with Gosu
|
127
|
+
|
128
|
+
---
|
129
|
+
|
130
|
+
**`#buttons`**
|
131
|
+
|
132
|
+
- a hash of name => button id pairs
|
133
|
+
- e.g. `buttons[:right]` or `buttons[:escape]`
|
134
|
+
- not all the gosu buttons are mapped here but I'd welcome a PR to add more.
|
135
|
+
|
136
|
+
---
|
137
|
+
|
138
|
+
**`#invoke/#call_helper/#dispatch/#call_hook`**
|
139
|
+
|
140
|
+
- all aliases for the same simple thing: calling a function on
|
141
|
+
`window` (the `Gosu::Window instance`).
|
142
|
+
|
143
|
+
---
|
144
|
+
|
145
|
+
**default helpers**
|
146
|
+
|
147
|
+
There are a few predefined helpers.
|
148
|
+
|
149
|
+
They can be called with `#call_helper(name_sym, *args, **keywords, &blk)`
|
150
|
+
|
151
|
+
The required parameters differ between the helpers.
|
152
|
+
|
153
|
+
1. `div_window_into(num_cols:, num_rows:, margin:)`
|
154
|
+
- splits the window into a grid but doesn't draw it
|
155
|
+
- returns a matrix-like hash which can be passed to `draw_grid`
|
156
|
+
as the `sections` argument
|
157
|
+
- uses `div_section_into` internally
|
158
|
+
|
159
|
+
2. `div_setion_into(start_x: start_y:, end_x:, end_y:, num_cols:, num_rows:, margin:)`
|
160
|
+
- splits a rectangle into a grid but doesn't draw it
|
161
|
+
- returns a matrix-like hash which can be passed to `draw_grid`
|
162
|
+
- `margin` is an optional reduction of the size of each column.
|
163
|
+
It's passed as `1` in `div_window_into` and the columns won't be
|
164
|
+
distinguishable (there will be no visible border) unless this is positive.
|
165
|
+
- specifically, the return value looks like this:
|
166
|
+
|
167
|
+
# many rows and columns can be stored in this structure
|
168
|
+
{
|
169
|
+
<row_idx> => {
|
170
|
+
start_x: <num>, start_y: <num>, end_x: <num>, end_y: <num>,
|
171
|
+
cols: {
|
172
|
+
<col_idx> => {
|
173
|
+
start_x: <num>, start_y: <num>, end_x: <num>, end_y: <num>
|
174
|
+
}
|
175
|
+
}
|
176
|
+
}
|
177
|
+
}
|
178
|
+
|
179
|
+
3. `draw_grid(sections:, row_color:, col_color:)`
|
180
|
+
- The result of `div_window_into` can be passed to this.
|
181
|
+
- Internally it uses `GridHelpers#draw_rect`
|
182
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
class GosuWrapper
|
2
|
+
class GridHelpers
|
3
|
+
|
4
|
+
def self.div_windo_into
|
5
|
+
Proc.new do |num_cols:, num_rows:, margin:|
|
6
|
+
call_helper :div_section_into, {
|
7
|
+
start_x: 0,
|
8
|
+
start_y: 0,
|
9
|
+
end_x: get_window_width,
|
10
|
+
end_y: get_window_height,
|
11
|
+
margin: margin,
|
12
|
+
num_rows: num_rows,
|
13
|
+
num_cols: num_cols
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.div_section_into
|
19
|
+
Proc.new do |
|
20
|
+
start_x:, start_y:, end_x:, end_y:, num_cols:, num_rows:, margin:
|
21
|
+
|
|
22
|
+
total_height = end_y - start_y
|
23
|
+
total_width = end_x - start_x
|
24
|
+
row_height = total_height / num_rows
|
25
|
+
col_width = total_width / num_cols
|
26
|
+
(num_rows).times.reduce({}) do |rows, row_idx|
|
27
|
+
row_start_y = (row_height * row_idx)
|
28
|
+
row_end_y = (row_start_y + row_height)
|
29
|
+
rows[row_idx] = {
|
30
|
+
start_y: row_start_y,
|
31
|
+
end_y: row_end_y,
|
32
|
+
start_x: start_x,
|
33
|
+
end_x: end_x,
|
34
|
+
cols: (num_cols).times.reduce({}) do |cols, col_idx|
|
35
|
+
col_start_x = col_width * col_idx
|
36
|
+
col_end_x = col_start_x + col_width
|
37
|
+
cols[col_idx] = {
|
38
|
+
start_x: col_start_x + margin,
|
39
|
+
end_x: col_end_x - margin,
|
40
|
+
start_y: row_start_y + margin,
|
41
|
+
end_y: row_end_y - margin,
|
42
|
+
}
|
43
|
+
cols
|
44
|
+
end
|
45
|
+
}
|
46
|
+
rows
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.draw_grid
|
52
|
+
Proc.new do |sections:, row_color:, col_color:|
|
53
|
+
sections.each do |row_num, row|
|
54
|
+
draw_rect row.merge(color: row_color)
|
55
|
+
row[:cols].each do |col_num, col|
|
56
|
+
draw_rect col.merge(color: col_color)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
data/lib/gosu_wrapper/util.rb
CHANGED
@@ -1,32 +1,7 @@
|
|
1
1
|
class GosuWrapper
|
2
2
|
module Util
|
3
|
-
|
4
|
-
# ================================================
|
3
|
+
|
5
4
|
def method_missing_for(regex, type:, &definition_blk)
|
6
|
-
# Regex should include a match group
|
7
|
-
# type should be either :instance or :class
|
8
|
-
# Definition blk is run unless there's an existing method with the same name.
|
9
|
-
# if caller_blk is provided, it will be passed to definition_blk's invocation
|
10
|
-
#
|
11
|
-
# Example:
|
12
|
-
#
|
13
|
-
# class Animal
|
14
|
-
# extend Util
|
15
|
-
# attr_reader :noises
|
16
|
-
# def initialize
|
17
|
-
# @noises = { cat: "meow" }
|
18
|
-
# end
|
19
|
-
# method_missing_for /^(.+)_goes$/ do |match, arg|
|
20
|
-
# puts @noises[match]
|
21
|
-
# end
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# Animal.new.cat_goes # => "meow"
|
25
|
-
#
|
26
|
-
# Through the use of an anonymous modules (adding method_missing to the
|
27
|
-
# inheritance chain vs overwriting it with define_method), this can be
|
28
|
-
# used multiple times.
|
29
|
-
#
|
30
5
|
anon_module = Module.new do |mod|
|
31
6
|
define_method(:method_missing) do |name, *args, **keywords, &caller_blk|
|
32
7
|
match = name.to_s.scan(regex).flatten[0]
|
@@ -59,7 +34,6 @@ class GosuWrapper
|
|
59
34
|
base.prepend anon_module
|
60
35
|
|
61
36
|
end
|
62
|
-
# ================================================
|
63
37
|
|
64
38
|
end
|
65
39
|
end
|
data/lib/gosu_wrapper.rb
CHANGED
@@ -21,6 +21,13 @@ class GosuWrapper
|
|
21
21
|
@window.window_width = width
|
22
22
|
@hooks = []
|
23
23
|
@helpers = []
|
24
|
+
add_default_helpers
|
25
|
+
end
|
26
|
+
|
27
|
+
def add_default_helpers
|
28
|
+
add_helper :div_window_into, GridHelpers.div_window_into
|
29
|
+
add_helper :div_section_into, GridHelpers.div_section_into
|
30
|
+
add_helper :draw_grid, GridHelpers.draw_grid
|
24
31
|
end
|
25
32
|
|
26
33
|
# Delegate "set_<attr>" setters to window
|
data/lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gosu_wrapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maxpleaner
|
@@ -45,13 +45,14 @@ executables:
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
+
- README.md
|
48
49
|
- bin/gosu_wrapper
|
49
50
|
- lib/gosu_wrapper.rb
|
50
51
|
- lib/gosu_wrapper/buttons.rb
|
51
52
|
- lib/gosu_wrapper/colors.rb
|
53
|
+
- lib/gosu_wrapper/grid_helpers.rb
|
52
54
|
- lib/gosu_wrapper/util.rb
|
53
55
|
- lib/version.rb
|
54
|
-
- readme.md
|
55
56
|
homepage: http://github.com/maxpleaner/gosu_wrapper
|
56
57
|
licenses:
|
57
58
|
- MIT
|
data/readme.md
DELETED
@@ -1,126 +0,0 @@
|
|
1
|
-
### Installing
|
2
|
-
|
3
|
-
_gosu and activesupport will be loaded as dependencies_
|
4
|
-
|
5
|
-
`gem install gosu_wrapper`
|
6
|
-
|
7
|
-
or in a gemfile: `gem 'gosu_wrapper'`
|
8
|
-
|
9
|
-
### Usage
|
10
|
-
|
11
|
-
One public class is exposed, `GosuWrapper`. It's essentially a wrapper
|
12
|
-
over an anonymous `Gosu::Window` class that gets functionality added through
|
13
|
-
metaprogramming.
|
14
|
-
|
15
|
-
In lieue of a "getting started" snippet, I'm going to delegate that to the
|
16
|
-
[gosu_wrapper_snake_example](http://github.com/maxpleaner/gosu_wrapper_snake_example)
|
17
|
-
codebase and run through the API step-by-step here.
|
18
|
-
|
19
|
-
---
|
20
|
-
|
21
|
-
**`#initialize(width:, height:, attributes:)`**
|
22
|
-
|
23
|
-
- This defines `#window` which is an instance of `Gosu::Window`.
|
24
|
-
- width & height are num pixels (of the total window).
|
25
|
-
- attributes is an array of symbols.
|
26
|
-
It must at least include `:window_height` and `:window_width`.
|
27
|
-
All of the keys in your app's state hash should be given here.
|
28
|
-
Each of them get attr_accessors defined on `#window` and shorthand
|
29
|
-
accessors using `method_missing` (see next section).
|
30
|
-
|
31
|
-
---
|
32
|
-
|
33
|
-
**There are four instance methods added using `method_missing`**
|
34
|
-
|
35
|
-
1. `#get_<attr>`
|
36
|
-
- delegates the getter to `window`
|
37
|
-
2. `#set_<attr>(val)`
|
38
|
-
- delegates the setter to `window`
|
39
|
-
3. `#change_<attr>(sym, arg)`
|
40
|
-
- a shorthand for some update operations
|
41
|
-
on non-mutable objects. For example `change_score(:+, 1)`
|
42
|
-
4. `#get_or_set_<attr>(&blk)`
|
43
|
-
- returns the val if it's truthy, and otherwise sets the val equal
|
44
|
-
to the block result (returning it as well).
|
45
|
-
|
46
|
-
---
|
47
|
-
|
48
|
-
**`#define_method_on_window(name, &blk)`**
|
49
|
-
|
50
|
-
- this should be a private method, but is used by a number of others so
|
51
|
-
it's worth explaing.
|
52
|
-
- It defines the method named `name` on `window` with `blk` as its body.
|
53
|
-
- Arguments are determined by the block
|
54
|
-
- The block is always invoked with the `GosuWrapper` instance as
|
55
|
-
the value of `self`
|
56
|
-
|
57
|
-
---
|
58
|
-
|
59
|
-
**`#scope(*args, **keywords, &blk)`**
|
60
|
-
|
61
|
-
- another method that should be private; this is what handles
|
62
|
-
calling a proc with the `GosuWrapper` instance as the value of `self`.
|
63
|
-
|
64
|
-
---
|
65
|
-
|
66
|
-
**`#config(&blk)`**
|
67
|
-
|
68
|
-
- mostly the same thing as `#scope` but is meant to be used publically.
|
69
|
-
- the only functional difference is this always returns `self`
|
70
|
-
(the `GosuWrapper` instance) whereas `#scope` returns the result of the
|
71
|
-
block.
|
72
|
-
- Like `#scope`, this is a classic DSL-style method which exists for the
|
73
|
-
sole purpose of saving keystrokes (using `instance_exec`)
|
74
|
-
|
75
|
-
---
|
76
|
-
|
77
|
-
**`#add_hook(name, &blk)`**
|
78
|
-
|
79
|
-
- a "hook" conceptually is a method internally defined by `Gosu::Window`
|
80
|
-
that we're tapping into by overriding. Hooking into events `update`,
|
81
|
-
`button_down`, and `draw` are how the app works at a basic level.
|
82
|
-
- the `button_down` proc is passed an `id` which can be checked against
|
83
|
-
the values in `#buttons` to determine which key was pressed.
|
84
|
-
- `update` and `draw` are run every tick. Conceptually, `update`
|
85
|
-
is used to change the state and `draw` to represent it.
|
86
|
-
|
87
|
-
---
|
88
|
-
|
89
|
-
**`#add_helper(name, &blk)`**
|
90
|
-
|
91
|
-
- functionally speaking this is the exact same thing as `add_hook` -
|
92
|
-
it defines a method on `Gosu::Window`.
|
93
|
-
- It exists to distinguish between built-in hooks like `update` from
|
94
|
-
custom methods.
|
95
|
-
- These can be called with `#invoke` or its alias `#call_helper`.
|
96
|
-
|
97
|
-
---
|
98
|
-
|
99
|
-
**`#show`**
|
100
|
-
|
101
|
-
- Starts the game. Needs to be called only once.
|
102
|
-
|
103
|
-
---
|
104
|
-
|
105
|
-
**`#draw_rect(start_x:, start_y:, end_x:, end_y:, color)`**
|
106
|
-
|
107
|
-
- Can be called from `draw` only. Fills the rectangle with `color`
|
108
|
-
|
109
|
-
---
|
110
|
-
|
111
|
-
**`#colors`**
|
112
|
-
|
113
|
-
- a hash of name => color object pairs
|
114
|
-
|
115
|
-
---
|
116
|
-
|
117
|
-
**`#buttons`**
|
118
|
-
|
119
|
-
- a hash of name => button id pairs
|
120
|
-
|
121
|
-
---
|
122
|
-
|
123
|
-
**`#invoke/#call_helper/#dispatch/#call_hook`**
|
124
|
-
|
125
|
-
- all aliases for the same simple thing: calling a function on
|
126
|
-
`window` (the `Gosu::Window instance`).
|