moonpxi-nimo 0.1.0

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.
Files changed (57) hide show
  1. data/.document +5 -0
  2. data/.gitignore +5 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +15 -0
  5. data/Rakefile +47 -0
  6. data/VERSION +1 -0
  7. data/examples/bouncy.rb +101 -0
  8. data/examples/eventy.rb +19 -0
  9. data/examples/images/dg_classm32.png +0 -0
  10. data/examples/images/dg_dungeon32.png +0 -0
  11. data/examples/images/jeeklabs.png +0 -0
  12. data/examples/images/tcheco.png +0 -0
  13. data/examples/imagey.rb +82 -0
  14. data/examples/screeny.rb +80 -0
  15. data/examples/simplest.rb +23 -0
  16. data/examples/sounds/KDE-Sys-Log-Out.ogg +0 -0
  17. data/examples/sounds/k3b_error1.wav +0 -0
  18. data/examples/soundy.rb +40 -0
  19. data/examples/spritey.rb +90 -0
  20. data/lib/nimo.rb +35 -0
  21. data/lib/nimo/behavior/deflector.rb +39 -0
  22. data/lib/nimo/behavior/moveable.rb +33 -0
  23. data/lib/nimo/behavior/projectile.rb +23 -0
  24. data/lib/nimo/behavior/with_velocity.rb +28 -0
  25. data/lib/nimo/game_object.rb +58 -0
  26. data/lib/nimo/game_window.rb +59 -0
  27. data/lib/nimo/listeners/event_listener.rb +27 -0
  28. data/lib/nimo/listeners/input_listener.rb +70 -0
  29. data/lib/nimo/representations/image_representation.rb +18 -0
  30. data/lib/nimo/representations/object_representation.rb +67 -0
  31. data/lib/nimo/representations/quad_representation.rb +23 -0
  32. data/lib/nimo/representations/sprite_representation.rb +78 -0
  33. data/lib/nimo/representations/text_representation.rb +27 -0
  34. data/lib/nimo/screen.rb +103 -0
  35. data/lib/nimo/utils/game.rb +63 -0
  36. data/lib/nimo/utils/intersection.rb +30 -0
  37. data/lib/nimo/utils/object_extension.rb +17 -0
  38. data/lib/nimo/utils/resources.rb +51 -0
  39. data/nimo.gemspec +117 -0
  40. data/spec/nimo/behavior/deflector_spec.rb +51 -0
  41. data/spec/nimo/behavior/moveable_spec.rb +81 -0
  42. data/spec/nimo/behavior/projectile_spec.rb +34 -0
  43. data/spec/nimo/behavior/with_velocity_spec.rb +18 -0
  44. data/spec/nimo/game_object_spec.rb +169 -0
  45. data/spec/nimo/game_window_spec.rb +88 -0
  46. data/spec/nimo/listeners/event_listener_spec.rb +34 -0
  47. data/spec/nimo/listeners/input_listener_spec.rb +87 -0
  48. data/spec/nimo/representations/image_representation_spec.rb +41 -0
  49. data/spec/nimo/representations/object_representation_spec.rb +74 -0
  50. data/spec/nimo/representations/sprite_representation_spec.rb +73 -0
  51. data/spec/nimo/representations/text_representation_spec.rb +21 -0
  52. data/spec/nimo/screen_spec.rb +114 -0
  53. data/spec/nimo/utils/game_spec.rb +44 -0
  54. data/spec/nimo/utils/object_extension_spec.rb +21 -0
  55. data/spec/nimo/utils/resources_spec.rb +59 -0
  56. data/spec/spec_helper.rb +9 -0
  57. metadata +133 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Paulo Schneider
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.
data/README.rdoc ADDED
@@ -0,0 +1,15 @@
1
+ = nimo
2
+
3
+ Description goes here.
4
+
5
+ == TODO
6
+
7
+ - Sort out resource loading, specially for sounds.
8
+ - Create eventy.rb example to demonstrate how to do events (i.e. timers, screen transition hooks, etc).
9
+ - Finish soundy.rb.
10
+ - Create texty.rb to demonstrate 'system' and bitmap fonts.
11
+ - Add stuff to this README.
12
+
13
+ == Copyright
14
+
15
+ Copyright (c) 2009 moonpxi. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "nimo"
8
+ gem.summary = "Ruby game development library using Gosu."
9
+ gem.email = "paulo.schneider@gmail.com"
10
+ gem.homepage = "http://github.com/moonpxi/nimo"
11
+ gem.authors = ["moonpxi"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+ rescue LoadError
15
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
16
+ end
17
+
18
+ require 'spec/rake/spectask'
19
+ Spec::Rake::SpecTask.new(:spec) do |spec|
20
+ spec.libs << 'lib' << 'spec'
21
+ spec.spec_files = FileList['spec/**/*_spec.rb']
22
+ end
23
+
24
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.pattern = 'spec/**/*_spec.rb'
27
+ spec.rcov = true
28
+ end
29
+
30
+
31
+ task :default => :spec
32
+
33
+ require 'rake/rdoctask'
34
+ Rake::RDocTask.new do |rdoc|
35
+ if File.exist?('VERSION.yml')
36
+ config = YAML.load(File.read('VERSION.yml'))
37
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
38
+ else
39
+ version = ""
40
+ end
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "nimo #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
47
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,101 @@
1
+ # bouncy.rb
2
+ #
3
+ # Example using deflectors, projectiles and moveable behaviors. It demonstrates how to create interacting
4
+ # objects and representations
5
+ #
6
+ # TODO: collision behavior is still a bit wonky. Balls disappear at the edge of the screen sometimes.
7
+ #
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+ require 'nimo'
11
+ include Gosu::Button
12
+
13
+ WINDOW_WIDTH = 512
14
+ WINDOW_HEIGHT = 480
15
+
16
+ class Pad < Nimo::GameObject
17
+ include Nimo::Behavior::Deflector
18
+ include Nimo::Behavior::Moveable
19
+
20
+ def initialize
21
+ super(:x => 200, :y => 400, :width => 80, :height => 40, :speed => 5,
22
+ :boundary => Object.from_hash(:x => 0, :y => 0, :width => WINDOW_WIDTH, :height => WINDOW_HEIGHT))
23
+ when_deflect { |projectile| projectile.change_color }
24
+ end
25
+
26
+ def deflection_modifier(ball)
27
+ (ball.center.x - self.center.x)/(1.5*@width)
28
+ end
29
+
30
+ end
31
+
32
+
33
+ class Wall < Nimo::GameObject
34
+ include Nimo::Behavior::Deflector
35
+
36
+ def self.sections
37
+ [ Wall.new(:x => -10, :y => -10, :width => 10, :height => WINDOW_HEIGHT + 10),
38
+ Wall.new(:x => -10, :y => -10, :width => WINDOW_WIDTH + 10, :height => 10),
39
+ Wall.new(:x => WINDOW_WIDTH, :y => -10, :width => 10, :height => WINDOW_HEIGHT + 10),
40
+ Wall.new(:x => -10, :y => WINDOW_HEIGHT, :width => WINDOW_WIDTH, :height => 10) ]
41
+ end
42
+ end
43
+
44
+
45
+ class Ball < Nimo::GameObject
46
+ include Nimo::Behavior::Deflector
47
+ include Nimo::Behavior::Projectile
48
+
49
+ attr_accessor :speed, :old_speed
50
+
51
+ def initialize(*deflectors)
52
+ random_velocity = Nimo::Behavior::Velocity.new(0, 1)
53
+ random_velocity.adjust(rand)
54
+
55
+ super(:x => 200, :y => 200, :width => 10, :height => 10, :velocity => random_velocity, :speed => 10, :old_speed => 0)
56
+ with_deflectors(deflectors)
57
+
58
+ @colors = [Gosu::red, Gosu::green, Gosu::yellow, Gosu::blue, Gosu::white]
59
+ @color_index = 0
60
+ end
61
+
62
+ def color
63
+ @colors[@color_index]
64
+ end
65
+
66
+ def change_color
67
+ @color_index = @color_index == @colors.size - 1 ? 0 : @color_index + 1
68
+ notify(:color_change)
69
+ end
70
+
71
+ end
72
+
73
+ if __FILE__ == $PROGRAM_NAME
74
+ pad = Pad.new
75
+ balls = (0..19).collect { Ball.new(pad, Wall.sections) }
76
+
77
+ Nimo::Game("Bouncy", WINDOW_WIDTH, WINDOW_HEIGHT) do
78
+
79
+ screen :game do
80
+ balls.each do |ball|
81
+ quad :for => ball, :with => {:color => ball.color} do
82
+ always { move }
83
+ listen_to(:color_change) { self.color = game_object.color }
84
+ end
85
+ end
86
+
87
+ quad :for => pad, :with => {:color => Gosu::white} do
88
+ when_key(KbLeft) { move_left }
89
+ when_key(KbRight) { move_right }
90
+ when_key(KbUp) { move_up }
91
+ when_key(KbDown) { move_down }
92
+
93
+ when_key(KbSpace) { balls.each { |ball| ball.speed, ball.old_speed = ball.old_speed, ball.speed } }
94
+ end
95
+
96
+ when_key(KbEscape) { exit }
97
+ end
98
+
99
+ end
100
+
101
+ end
@@ -0,0 +1,19 @@
1
+ #
2
+ # eventy.rb
3
+ #
4
+ # Demonstrate how to use on enter and timer events. Not very detailed.
5
+ #
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ require 'nimo'
9
+
10
+ THREE_SECONDS = 3
11
+
12
+ Nimo::Game("Eventy", 512, 480) do
13
+ fonts :main => { :type => "Helvetica", :size => 20 }
14
+
15
+ screen :main do
16
+ text :with => { :text => "Will exit in a three seconds", :font => :main, :x => 10, :y => 200, :color => Gosu::white }
17
+ listen_to(:on_enter) { timer_for(THREE_SECONDS) { exit } }
18
+ end
19
+ end
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,82 @@
1
+ #
2
+ # imagey.rb
3
+ #
4
+ # Demonstrate how to load and use whole images and sub-images as representations.
5
+ # The character and dungeon images were retrieved from: http://www.molotov.nu/?page=graphics
6
+ #
7
+ # TODO Add better boundary check for trees
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
10
+ require 'nimo'
11
+ include Gosu::Button
12
+
13
+ WINDOW_WIDTH = 512
14
+ WINDOW_HEIGHT = 480
15
+ WINDOW = {:x => 0, :y => 0, :width => WINDOW_WIDTH, :height => WINDOW_HEIGHT}
16
+
17
+ class Player < Nimo::GameObject
18
+ include Nimo::Behavior::Moveable
19
+
20
+ def initialize
21
+ super(:x => 32, :y => 32, :width => 32, :height => 32, :speed => 32,
22
+ :boundary => Object.from_hash(:x => 32, :y => 32, :width => WINDOW_WIDTH - 32, :height => WINDOW_HEIGHT - 32))
23
+ end
24
+ end
25
+
26
+ class Dungeon
27
+ # A bit hacktastic!
28
+ def self.representation
29
+ map_config = {:image => :map_tiles}
30
+ representations = []
31
+
32
+ 16.times do |x|
33
+ 15.times do |y|
34
+ if x == 0 || x == 15 || y == 0 || y == 14
35
+ representations << map_config.merge(:x => x*32, :y => y*32, :index => 2)
36
+ else
37
+ representations << map_config.merge(:x => x*32, :y => y*32, :index => 79)
38
+ end
39
+ end
40
+ end
41
+ representations << map_config.merge(:x => 7*32, :y => 0, :index => 1)
42
+ representations << map_config.merge(:x => 8*32, :y => 0, :index => 20)
43
+ representations << map_config.merge(:x => 9*32, :y => 0, :index => 1)
44
+
45
+ representations << map_config.merge(:x => 7*32, :y => 4*32, :index => 85)
46
+ representations << map_config.merge(:x => 3*32, :y => 13*32, :index => 85)
47
+ representations << map_config.merge(:x => 14*32, :y => 3*32, :index => 85)
48
+
49
+ return representations
50
+ end
51
+ end
52
+
53
+
54
+ if __FILE__ == $PROGRAM_NAME
55
+ Nimo::Game("Imagey", WINDOW_WIDTH, WINDOW_HEIGHT) do
56
+ images :jeeklabs => { :filename => "examples/images/jeeklabs.png" },
57
+ :char_tiles => { :filename => "examples/images/dg_classm32.png", :tile_dimension => [32, 32] },
58
+ :map_tiles => { :filename => "examples/images/dg_dungeon32.png", :tile_dimension => [32, 32] }
59
+
60
+ screen :title do
61
+ quad :with => WINDOW.merge(:color => Gosu::white)
62
+ image :with => {:x => 116, :y => 190, :image => :jeeklabs}
63
+
64
+ any_key { go_to :game }
65
+ end
66
+
67
+ screen :game do
68
+ Dungeon.representation.each { |params| image :with => params }
69
+
70
+ image :for => Player.new, :with => {:image => :char_tiles, :index => 85} do
71
+ when_key(KbLeft, :repeatable => false) { self.move_left }
72
+ when_key(KbRight, :repeatable => false) { self.move_right }
73
+ when_key(KbUp, :repeatable => false) { move_up }
74
+ when_key(KbDown, :repeatable => false) { move_down }
75
+ end
76
+
77
+ when_key(KbEscape) { exit }
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,80 @@
1
+ #
2
+ # screeny.rb
3
+ #
4
+ # Example demonstrates how to navigate between screens and menus, and also some text output.
5
+ #
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ require 'nimo'
9
+ include Gosu::Button
10
+
11
+ WINDOW_WIDTH = 512
12
+ WINDOW_HEIGHT = 480
13
+
14
+ class Wall < Nimo::GameObject
15
+ include Nimo::Behavior::Deflector
16
+
17
+ def self.sections
18
+ [ Wall.new(:x => -10, :y => -10, :width => 10, :height => WINDOW_HEIGHT + 10),
19
+ Wall.new(:x => -10, :y => -10, :width => WINDOW_WIDTH + 10, :height => 10),
20
+ Wall.new(:x => WINDOW_WIDTH, :y => -10, :width => 10, :height => WINDOW_HEIGHT + 10),
21
+ Wall.new(:x => -10, :y => WINDOW_HEIGHT, :width => WINDOW_WIDTH, :height => 10) ]
22
+ end
23
+ end
24
+
25
+
26
+ class Ball < Nimo::GameObject
27
+ include Nimo::Behavior::Deflector
28
+ include Nimo::Behavior::Projectile
29
+
30
+ def initialize(*deflectors)
31
+ random_velocity = Nimo::Behavior::Velocity.new(0, 1)
32
+ random_velocity.adjust(rand + -rand)
33
+
34
+ super(:x => 200, :y => 200, :width => 10, :height => 10, :velocity => random_velocity, :speed => 10)
35
+ with_deflectors(deflectors)
36
+ end
37
+
38
+ end
39
+
40
+ if __FILE__ == $PROGRAM_NAME
41
+ Nimo::Game("Screeny", WINDOW_WIDTH, WINDOW_HEIGHT) do
42
+ fonts :main => { :type => "Helvetica", :size => 20 }
43
+
44
+ screen :start do
45
+ text :with => {:text => "StartScreen: press any key to go to the GameScreen",
46
+ :x => 10, :y => 200, :font => :main, :color => Gosu::white}
47
+ any_key { go_to :game }
48
+ end
49
+
50
+ screen :game do
51
+ text :with => {:text => "GameScreen: press any key to open the MenuScreen",
52
+ :x => 10, :y => 10, :font => :main, :color => Gosu::white}
53
+
54
+ balls = (0..19).collect { Ball.new(Wall.sections) }
55
+ balls.each do |ball|
56
+ ball.with_deflectors(balls.find_all { |other_ball| other_ball != ball })
57
+ quad :for => ball, :with => {:color => Gosu::red} do
58
+ always { move }
59
+ end
60
+ end
61
+
62
+ any_key { open_menu :menu }
63
+ end
64
+
65
+ screen :menu do
66
+ text :with => {:text => "MenuScreen:", :x => 100, :y => 200, :font => :main, :color => Gosu::white, :size => 15}
67
+ text :with => {:text => "- <ESC> to go to the EndScreen", :x => 100, :y => 215, :font => :main, :color => Gosu::white, :size => 15}
68
+ text :with => {:text => "- <ENTER> to go to the GameScreen", :x => 100, :y => 230, :font => :main, :color => Gosu::white, :size => 15}
69
+
70
+ when_key(KbReturn) { close_menu }
71
+ when_key(KbEscape) { close_menu and go_to :end }
72
+ end
73
+
74
+ screen :end do
75
+ text :with => {:text => "EndScreen: you've reached the end of this example!",
76
+ :x => 10, :y => 200, :font => :main, :color => Gosu::white}
77
+ any_key { exit }
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,23 @@
1
+ # simplest.rb
2
+ #
3
+ # Simplest example ever! A white box controlled by the arrow keys.
4
+ #
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'nimo'
8
+ include Gosu::Button
9
+
10
+ Nimo::Game("Simplest", 640, 480) do
11
+
12
+ screen :game do
13
+ quad :with => { :width => 20, :height => 20, :color => Gosu::white } do
14
+ when_key(KbLeft) { self.x -= 5 }
15
+ when_key(KbRight) { self.x += 5 }
16
+ when_key(KbUp) { self.y -= 5 }
17
+ when_key(KbDown) { self.y += 5 }
18
+ end
19
+
20
+ when_key(KbEscape) { exit }
21
+ end
22
+
23
+ end
Binary file
Binary file
@@ -0,0 +1,40 @@
1
+ #
2
+ # soundy.rb
3
+ #
4
+ # Demonstrate how to play sounds and background music.
5
+ # Copyright (sort of): the sounds used here came from my Ubuntu installation (probably from the KDE distribution).
6
+ #
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ require 'nimo'
10
+
11
+ WINDOW_WIDTH = 512
12
+ WINDOW_HEIGHT = 480
13
+
14
+ Nimo::Game("Soundy", WINDOW_WIDTH, WINDOW_HEIGHT) do
15
+ sounds :startup => { :filename => "examples/sounds/KDE-Sys-Log-Out.ogg" },
16
+ :effect => { :filename => "examples/sounds/k3b_error1.wav" }
17
+ fonts :main => { :type => "Helvetica", :size => 15}
18
+
19
+ screen :main do
20
+ listen_to(:on_enter) { sounds[:startup].play(true) }
21
+
22
+ text :with => {:x => 10, :y => 10, :font => :main, :color => Gosu::white,
23
+ :text => "This screen will play a background sound."}
24
+ text :with => {:x => 10, :y => 28, :font => :main, :color => Gosu::white,
25
+ :text => "Press <esc> to quit or <enter> to go to the next screen."}
26
+
27
+ when_key(Gosu::Button::KbEscape) { exit }
28
+ when_key(Gosu::Button::KbReturn) { sounds[:startup].stop; go_to :another }
29
+ end
30
+
31
+ screen :another do
32
+ text :with => {:x => 10, :y => 10, :font => :main, :color => Gosu::white,
33
+ :text => "This screen will play a sound effect."}
34
+ text :with => {:x => 10, :y => 28, :font => :main, :color => Gosu::white,
35
+ :text => "Press <esc> to go back or <enter> to play the sound."}
36
+
37
+ when_key(Gosu::Button::KbEscape) { go_to :main }
38
+ when_key(Gosu::Button::KbReturn) { sounds[:effect].play }
39
+ end
40
+ end