arcade 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/arcade.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
+ require 'ostruct'
2
3
  require 'matrix'
3
4
  require 'gosu'
4
5
 
data/lib/arcade/base.rb CHANGED
@@ -69,7 +69,7 @@ class Arcade::GameWindow < Gosu::Window
69
69
  object.collided_with(other)
70
70
  end
71
71
  end
72
-
72
+
73
73
  edge = if object.top < 0
74
74
  :top
75
75
  elsif object.bottom > self.height
@@ -102,47 +102,61 @@ class Arcade::GameWindow < Gosu::Window
102
102
  end
103
103
 
104
104
  class Arcade::GameObject
105
- PROPERTIES = [:x, :y, :height, :width, :color, :name, :velocity]
106
- DEFAULTS = {:color => Arcade::Color::WHITE, :velocity => Arcade::Velocity::ZERO}
107
-
108
- PROPERTIES.each do |prop|
109
- attr_accessor prop
110
- alias_method :"set_#{prop}", :"#{prop}="
111
- end
105
+ PROPERTIES = [:x, :y, :height, :width, :color, :name, :velocity, :score]
106
+ DEFAULTS = {:color => Arcade::Color::WHITE,
107
+ :velocity => Arcade::Velocity::ZERO}
112
108
 
113
- alias_method :x_position, :x
114
- alias_method :y_position, :y
115
- alias_method :set_x_position, :x=
116
- alias_method :set_y_position, :y=
109
+ ALIASES = {:x_position => :x,
110
+ :y_position => :y,
111
+ :set_x_position => :x=,
112
+ :set_y_position => :y=}
117
113
 
118
114
  attr_reader :window
119
115
  attr_reader :keypress_listeners
120
116
 
117
+ def method_missing method, *args
118
+ method = ALIASES.fetch(method) { method }
119
+
120
+ mname = method.id2name
121
+
122
+ if prop = mname.scan(/^set_(.+)/).flatten.first
123
+ @state.send(:"#{prop}=", *args)
124
+ else
125
+ @state.send(method, *args)
126
+ end
127
+ end
128
+
121
129
  class << self
122
- PROPERTIES.each do |prop|
123
- attr_accessor prop
124
- alias_method :"set_#{prop}", :"#{prop}="
130
+ def set_defaults &block
131
+ @default_block = block
125
132
  end
126
133
 
127
- def config
128
- self
134
+ def has_defaults?
135
+ !@default_block.nil?
129
136
  end
130
137
 
131
- def set_defaults &block
132
- instance_eval &block
138
+ def defaults
139
+ @default_block
133
140
  end
134
141
  end
135
142
 
136
- def initialize &block
137
- PROPERTIES.each do |prop|
138
- val = self.class.send(prop) || Arcade::GameObject::DEFAULTS[prop] || 0
143
+ def setup_defaults
144
+ DEFAULTS.each do |prop, val|
139
145
  self.send(:"#{prop}=", val)
140
146
  end
141
147
 
148
+ if self.class.has_defaults?
149
+ instance_exec &self.class.defaults
150
+ end
151
+ end
152
+
153
+ def initialize &block
154
+ @state = OpenStruct.new
142
155
  @keypress_listeners = {}
143
156
  @collision_listeners = {}
144
157
  @edge_callback = nil
145
158
 
159
+ setup_defaults
146
160
  instance_exec &block
147
161
  end
148
162
 
@@ -168,37 +182,37 @@ class Arcade::GameObject
168
182
  self.update(dt)
169
183
  end
170
184
  end
171
-
185
+
172
186
  def top
173
- @y
187
+ self.y
174
188
  end
175
189
 
176
190
  def bottom
177
- @y + height
191
+ self.y + height
178
192
  end
179
193
 
180
194
  def left
181
- @x
195
+ self.x
182
196
  end
183
197
 
184
198
  def right
185
- @x + width
199
+ self.x + width
186
200
  end
187
201
 
188
202
  def move_up pixels
189
- @y -= pixels
203
+ self.y -= pixels
190
204
  end
191
205
 
192
206
  def move_down pixels
193
- @y += pixels
207
+ self.y += pixels
194
208
  end
195
209
 
196
210
  def move_left pixels
197
- @x -= pixels
211
+ self.x -= pixels
198
212
  end
199
213
 
200
214
  def move_right pixels
201
- @x += pixels
215
+ self.x += pixels
202
216
  end
203
217
 
204
218
  def on_keypress key, &block
@@ -235,15 +249,15 @@ class Arcade::GameObject
235
249
  left > other.right)
236
250
  end
237
251
  end
238
-
252
+
239
253
  def on_hit_edge &block
240
254
  @edge_callback = block
241
255
  end
242
-
256
+
243
257
  # One of :top, :bottom, :left, or :right
244
258
  def hit_edge edge
245
259
  if @edge_callback
246
260
  instance_exec edge, &@edge_callback
247
261
  end
248
262
  end
249
- end
263
+ end
@@ -1,4 +1,4 @@
1
1
  module Arcade
2
2
  # The current version of arcade
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arcade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-25 00:00:00.000000000Z
12
+ date: 2012-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70194030751000 !ruby/object:Gem::Requirement
16
+ requirement: &70248389339500 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70194030751000
24
+ version_requirements: *70248389339500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: gosu
27
- requirement: &70194030721540 !ruby/object:Gem::Requirement
27
+ requirement: &70248389338680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70194030721540
35
+ version_requirements: *70248389338680
36
36
  description: ! 'Arcade is a simple Ruby-vased DSL for creating old-school arcade games
37
37
  like Pong. The philosophy is similar to GameMaker, except expressed as a Ruby library
38
38
  instead of a crazy GUI.
@@ -63,15 +63,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
63
63
  - - ! '>='
64
64
  - !ruby/object:Gem::Version
65
65
  version: '0'
66
+ segments:
67
+ - 0
68
+ hash: 2415246944967182854
66
69
  required_rubygems_version: !ruby/object:Gem::Requirement
67
70
  none: false
68
71
  requirements:
69
72
  - - ! '>='
70
73
  - !ruby/object:Gem::Version
71
74
  version: '0'
75
+ segments:
76
+ - 0
77
+ hash: 2415246944967182854
72
78
  requirements: []
73
79
  rubyforge_project:
74
- rubygems_version: 1.8.15
80
+ rubygems_version: 1.8.17
75
81
  signing_key:
76
82
  specification_version: 3
77
83
  summary: A Ruby-based DSL for building simple arcade-style games like Pong, etc.