arcade 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Jesse Farmer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ arcade
2
+ ======
3
+
4
+ A Ruby-based DSL for building simple arcade-style games like pong, etc.
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Arcade'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task :default => :test
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'matrix'
3
+ require 'gosu'
4
+
5
+ require 'arcade/version'
6
+ require 'arcade/vector'
7
+ require 'arcade/base'
@@ -0,0 +1,249 @@
1
+ module Arcade
2
+ module Keyboard
3
+ Gosu::Button.constants.each do |cons|
4
+ if cons =~ /^Kb/
5
+ self.const_set "Key" + cons.to_s.scan(/Kb(.*)/).flatten.first.to_s,
6
+ Gosu::Button.const_get(cons)
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ class Arcade::Color < Gosu::Color
13
+ end
14
+
15
+ class Arcade::GameWindow < Gosu::Window
16
+ def initialize width, height
17
+ @current_time = Gosu::milliseconds
18
+ @objects = []
19
+
20
+ @keypress_listeners = Hash.new { |h,k| h[k] = [] }
21
+
22
+ super width, height, false
23
+ end
24
+
25
+ def add_object game_object
26
+ game_object.keypress_listeners.each do |key, proc|
27
+ self.add_listener key, game_object
28
+ end
29
+
30
+ @objects << game_object
31
+ end
32
+
33
+ def register game_object
34
+ game_object.window = self
35
+ self.add_object(game_object)
36
+ end
37
+
38
+ def add_listener key, object
39
+ @keypress_listeners[key] << object
40
+ end
41
+
42
+ def listened_keys
43
+ @keypress_listeners.keys
44
+ end
45
+
46
+ def listeners_for_key key
47
+ @keypress_listeners[key]
48
+ end
49
+
50
+ def draw
51
+ @objects.each do |game_object|
52
+ game_object.draw
53
+ end
54
+ end
55
+
56
+ def update
57
+ dt = (Gosu::milliseconds - @current_time) / 1000.0
58
+ @current_time = Gosu::milliseconds
59
+
60
+ self.listened_keys.each do |key|
61
+ self.listeners_for_key(key).each do |listener|
62
+ listener.key_pressed(key) if button_down?(key)
63
+ end
64
+ end
65
+
66
+ @objects.each do |object|
67
+ (@objects - [object]).each do |other|
68
+ if object.collides_with?(other)
69
+ object.collided_with(other)
70
+ end
71
+ end
72
+
73
+ edge = if object.top < 0
74
+ :top
75
+ elsif object.bottom > self.height
76
+ :bottom
77
+ elsif object.left < 0
78
+ :left
79
+ elsif object.right > self.width
80
+ :right
81
+ end
82
+
83
+ object.hit_edge(edge) if edge
84
+ end
85
+
86
+ @objects.each do |object|
87
+ object._update(dt)
88
+ end
89
+ end
90
+
91
+ def button_down id
92
+ end
93
+
94
+ def draw_square upper_left, width, height, color = Arcade::Color::WHITE
95
+ x, y = upper_left
96
+
97
+ draw_quad x, y, color,
98
+ x+width, y, color,
99
+ x+width, y+height, color,
100
+ x, y+height, color
101
+ end
102
+ end
103
+
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
112
+
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=
117
+
118
+ attr_reader :window
119
+ attr_reader :keypress_listeners
120
+
121
+ class << self
122
+ PROPERTIES.each do |prop|
123
+ attr_accessor prop
124
+ alias_method :"set_#{prop}", :"#{prop}="
125
+ end
126
+
127
+ def config
128
+ self
129
+ end
130
+
131
+ def set_defaults &block
132
+ instance_eval &block
133
+ end
134
+ end
135
+
136
+ def initialize &block
137
+ PROPERTIES.each do |prop|
138
+ val = self.class.send(prop) || Arcade::GameObject::DEFAULTS[prop] || 0
139
+ self.send(:"#{prop}=", val)
140
+ end
141
+
142
+ @keypress_listeners = {}
143
+ @collision_listeners = {}
144
+ @edge_callback = nil
145
+
146
+ instance_exec &block
147
+ end
148
+
149
+ def config
150
+ self
151
+ end
152
+
153
+ def draw
154
+ window.draw_square [self.x, self.y], self.width, self.height, self.color
155
+ end
156
+
157
+ def window= game_window
158
+ @window = game_window
159
+ end
160
+
161
+ def _update dt
162
+ unless self.velocity.zero?
163
+ self.x += self.velocity.element(0)
164
+ self.y += self.velocity.element(1)
165
+ end
166
+
167
+ if self.respond_to? :update
168
+ self.update(dt)
169
+ end
170
+ end
171
+
172
+ def top
173
+ @y
174
+ end
175
+
176
+ def bottom
177
+ @y + height
178
+ end
179
+
180
+ def left
181
+ @x
182
+ end
183
+
184
+ def right
185
+ @x + width
186
+ end
187
+
188
+ def move_up pixels
189
+ @y -= pixels
190
+ end
191
+
192
+ def move_down pixels
193
+ @y += pixels
194
+ end
195
+
196
+ def move_left pixels
197
+ @x -= pixels
198
+ end
199
+
200
+ def move_right pixels
201
+ @x += pixels
202
+ end
203
+
204
+ def on_keypress key, &block
205
+ @keypress_listeners[key] = block
206
+ end
207
+
208
+ def key_pressed key
209
+ instance_eval &@keypress_listeners[key]
210
+ end
211
+
212
+ def can_collide_with? klass
213
+ @collision_listeners.has_key? klass
214
+ end
215
+
216
+ def on_collides_with klass, &block
217
+ @collision_listeners[klass] = block
218
+ end
219
+
220
+ def collided_with other
221
+ klass = other.class
222
+
223
+ if self.can_collide_with?(klass)
224
+ instance_exec other, &@collision_listeners[klass]
225
+ end
226
+ end
227
+
228
+ def collides_with? other
229
+ if !other.kind_of?(Arcade::GameObject) || other == self
230
+ false
231
+ else
232
+ !(bottom < other.top ||
233
+ top > other.bottom ||
234
+ right < other.left ||
235
+ left > other.right)
236
+ end
237
+ end
238
+
239
+ def on_hit_edge &block
240
+ @edge_callback = block
241
+ end
242
+
243
+ # One of :top, :bottom, :left, or :right
244
+ def hit_edge edge
245
+ if @edge_callback
246
+ instance_exec edge, &@edge_callback
247
+ end
248
+ end
249
+ end
@@ -0,0 +1,18 @@
1
+ class Arcade::Velocity < Vector
2
+ HORIZ_REFLECT = Matrix[[-1,0], [0,1]]
3
+ VERT_REFLECT = Matrix[[1,0], [0,-1]]
4
+
5
+ ZERO = Arcade::Velocity[0,0]
6
+
7
+ def reflect_horizontally
8
+ Arcade::Velocity[*(HORIZ_REFLECT * self).to_a]
9
+ end
10
+
11
+ def reflect_vertically
12
+ Arcade::Velocity[*(VERT_REFLECT * self).to_a]
13
+ end
14
+
15
+ def zero?
16
+ self.eql? ZERO
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ module Arcade
2
+ # The current version of arcade
3
+ VERSION = "0.1.0"
4
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arcade
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jesse Farmer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-25 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70194030751000 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70194030751000
25
+ - !ruby/object:Gem::Dependency
26
+ name: gosu
27
+ requirement: &70194030721540 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70194030721540
36
+ description: ! 'Arcade is a simple Ruby-vased DSL for creating old-school arcade games
37
+ like Pong. The philosophy is similar to GameMaker, except expressed as a Ruby library
38
+ instead of a crazy GUI.
39
+
40
+ '
41
+ email:
42
+ - jesse@20bits.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/arcade/base.rb
48
+ - lib/arcade/vector.rb
49
+ - lib/arcade/version.rb
50
+ - lib/arcade.rb
51
+ - MIT-LICENSE
52
+ - Rakefile
53
+ - README.md
54
+ homepage: http://github.com/jfarmer/arcade
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.15
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: A Ruby-based DSL for building simple arcade-style games like Pong, etc.
78
+ test_files: []