chingu 0.5.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data.tar.gz.sig +0 -0
  2. data/History.txt +21 -0
  3. data/LICENSE +504 -0
  4. data/Manifest.txt +72 -0
  5. data/README.rdoc +588 -0
  6. data/Rakefile +19 -0
  7. data/benchmarks/README.txt +1 -0
  8. data/benchmarks/benchmark.rb +6 -0
  9. data/benchmarks/benchmark3.rb +23 -0
  10. data/benchmarks/benchmark4.rb +71 -0
  11. data/benchmarks/benchmark5.rb +91 -0
  12. data/benchmarks/benchmark6.rb +23 -0
  13. data/benchmarks/meta_benchmark.rb +67 -0
  14. data/benchmarks/meta_benchmark2.rb +39 -0
  15. data/chingu.gemspec +34 -0
  16. data/examples/example1.rb +37 -0
  17. data/examples/example10.rb +75 -0
  18. data/examples/example11.rb +51 -0
  19. data/examples/example12.rb +67 -0
  20. data/examples/example2.rb +115 -0
  21. data/examples/example3.rb +40 -0
  22. data/examples/example4.rb +175 -0
  23. data/examples/example5.rb +107 -0
  24. data/examples/example6.rb +57 -0
  25. data/examples/example7.rb +133 -0
  26. data/examples/example8.rb +109 -0
  27. data/examples/example9.rb +106 -0
  28. data/examples/media/Parallax-scroll-example-layer-0.png +0 -0
  29. data/examples/media/Parallax-scroll-example-layer-1.png +0 -0
  30. data/examples/media/Parallax-scroll-example-layer-2.png +0 -0
  31. data/examples/media/Parallax-scroll-example-layer-3.png +0 -0
  32. data/examples/media/background1.png +0 -0
  33. data/examples/media/fire_bullet.png +0 -0
  34. data/examples/media/fireball.png +0 -0
  35. data/examples/media/particle.png +0 -0
  36. data/examples/media/ruby.png +0 -0
  37. data/examples/media/spaceship.png +0 -0
  38. data/examples/media/stickfigure.bmp +0 -0
  39. data/examples/media/stickfigure.png +0 -0
  40. data/examples/media/video_games.png +0 -0
  41. data/lib/chingu.rb +32 -0
  42. data/lib/chingu/actor.rb +17 -0
  43. data/lib/chingu/animation.rb +142 -0
  44. data/lib/chingu/assets.rb +64 -0
  45. data/lib/chingu/basic_game_object.rb +132 -0
  46. data/lib/chingu/core_extensions.rb +53 -0
  47. data/lib/chingu/effects.rb +36 -0
  48. data/lib/chingu/fpscounter.rb +62 -0
  49. data/lib/chingu/game_object.rb +127 -0
  50. data/lib/chingu/game_object_list.rb +91 -0
  51. data/lib/chingu/game_state.rb +137 -0
  52. data/lib/chingu/game_state_manager.rb +284 -0
  53. data/lib/chingu/game_states/debug.rb +65 -0
  54. data/lib/chingu/game_states/fade_to.rb +91 -0
  55. data/lib/chingu/game_states/pause.rb +57 -0
  56. data/lib/chingu/gfx_helpers.rb +89 -0
  57. data/lib/chingu/helpers.rb +166 -0
  58. data/lib/chingu/inflector.rb +34 -0
  59. data/lib/chingu/input.rb +100 -0
  60. data/lib/chingu/named_resource.rb +254 -0
  61. data/lib/chingu/parallax.rb +83 -0
  62. data/lib/chingu/particle.rb +21 -0
  63. data/lib/chingu/rect.rb +612 -0
  64. data/lib/chingu/require_all.rb +133 -0
  65. data/lib/chingu/text.rb +56 -0
  66. data/lib/chingu/traits/collision_detection.rb +172 -0
  67. data/lib/chingu/traits/effect.rb +113 -0
  68. data/lib/chingu/traits/input.rb +38 -0
  69. data/lib/chingu/traits/retrofy.rb +53 -0
  70. data/lib/chingu/traits/rotation_center.rb +84 -0
  71. data/lib/chingu/traits/timer.rb +90 -0
  72. data/lib/chingu/traits/velocity.rb +67 -0
  73. data/lib/chingu/window.rb +170 -0
  74. metadata +162 -0
  75. metadata.gz.sig +1 -0
@@ -0,0 +1,107 @@
1
+ require 'rubygems'
2
+ require File.join(File.dirname($0), "..", "lib", "chingu")
3
+ include Gosu
4
+
5
+ #
6
+ # Using Chingus game state mananger in pure Gosu.
7
+ #
8
+ class Game < Gosu::Window
9
+ attr_reader :font
10
+ def initialize
11
+ $window = super(800,600,false)
12
+
13
+ @font = Font.new($window, default_font_name(), 20)
14
+
15
+ # Create our game state manager
16
+ @manager = Chingu::GameStateManager.new
17
+
18
+ # Switch to first state
19
+ @manager.switch_game_state(State1)
20
+
21
+ # Insert FadeTo state between every push, pop and switch
22
+ @manager.transitional_game_state(Chingu::GameStates::FadeTo, :speed => 10, :game_state_manager => @manager)
23
+ end
24
+
25
+ def button_down(id)
26
+ # This makes sure button_down(id) is called on the active game state
27
+ # Enables input-handling in game states, you might wanna do the same with button_up()
28
+ @manager.button_down(id)
29
+
30
+ if @manager.current_game_state.class != Chingu::GameStates::FadeTo
31
+
32
+ @manager.push_game_state(State1) if(id==Button::Kb1)
33
+ @manager.push_game_state(State2) if(id==Button::Kb2)
34
+ @manager.push_game_state(State3) if(id==Button::Kb3)
35
+ @manager.push_game_state(Chingu::GameStates::Pause) if(id==Button::KbP)
36
+ @manager.pop_game_state if(id==Button::KbBackspace)
37
+ end
38
+
39
+ exit if(id==Button::KbEscape)
40
+ end
41
+
42
+ def update
43
+ # This makes sure update() is called on the active game state
44
+ @manager.update
45
+ end
46
+
47
+ def draw
48
+ @font.draw("Game State Stack. 1-3 to push a game state. Backspace to pop.", 100, 200, 0)
49
+ @manager.game_states.each_with_index do |game_state, index|
50
+ @font.draw("#{index+1}) #{game_state.to_s}", 100, 220+index*20, 0)
51
+ end
52
+
53
+ # This makes sure draw() is called on the active game state
54
+ @manager.draw
55
+ end
56
+ end
57
+
58
+ class State1 < Chingu::GameState
59
+ def setup
60
+ @spinner = ["|", "/", "-", "\\", "|", "/", "-", "\\"]
61
+ @spinner_index = 0.0
62
+ end
63
+
64
+ def update
65
+ @spinner_index += 0.1
66
+ @spinner_index = 0 if @spinner_index >= @spinner.size
67
+ end
68
+
69
+ def draw
70
+ $window.font.draw("Inside State1: #{@spinner[@spinner_index.to_i]}", 100, 100, 0)
71
+ end
72
+ end
73
+
74
+ class State2 < Chingu::GameState
75
+ def setup
76
+ @factor = 0.0
77
+ @ticks = 0.0
78
+ end
79
+
80
+ def update
81
+ @ticks += 0.01
82
+ @factor = 1.5 + Math.sin(@ticks).to_f
83
+ end
84
+
85
+ def draw
86
+ $window.font.draw("Inside State2 - factor_y: #{@factor.to_s}", 100, 100, 0, 1.0, @factor)
87
+ end
88
+ end
89
+
90
+
91
+ class State3 < Chingu::GameState
92
+ def setup
93
+ @factor = 0.0
94
+ @ticks = 0.0
95
+ end
96
+
97
+ def update
98
+ @ticks += 0.01
99
+ @factor = 1.5 + Math.sin(@ticks).to_f
100
+ end
101
+ def draw
102
+ $window.font.draw("Inside State3 - factor_x: #{@factor.to_s}", 100, 100, 0, @factor, 1.0)
103
+ end
104
+ end
105
+
106
+
107
+ Game.new.show
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require File.join(File.dirname($0), "..", "lib", "chingu")
3
+ include Gosu
4
+
5
+ #
6
+ # Example of using a special GameState to fade between game states
7
+ #
8
+ # Using a simple game state, Chingu::GameStates::FadeTo, shipped with Chingu.
9
+ #
10
+ class Game < Chingu::Window
11
+ def initialize
12
+ super(640,800)
13
+ switch_game_state(State1)
14
+ self.input = {:space => :push, :return => :switch, :esc => :exit}
15
+ self.caption = "Example of transitional game state FadeTo when switchin between two game states"
16
+ transitional_game_state(Chingu::GameStates::FadeTo, :speed => 10)
17
+ end
18
+
19
+ def push
20
+ #
21
+ # Since we have a transitional game state set, the bellow code in practice become:
22
+ #
23
+ # if current_game_state.is_a?(State1)
24
+ # push_game_state(Chingu::GameStates::FadeTo.new(State2.new, :speed => 10))
25
+ # elsif current_game_state.is_a?(State2)
26
+ # push_game_state(Chingu::GameStates::FadeTo.new(State1.new, :speed => 10))
27
+ # end
28
+ #
29
+ if current_game_state.is_a?(State1)
30
+ push_game_state(State2.new)
31
+ elsif current_game_state.is_a?(State2)
32
+ push_game_state(State1.new)
33
+ end
34
+ end
35
+
36
+ def switch
37
+ if current_game_state.is_a?(State1)
38
+ switch_game_state(State2.new)
39
+ elsif current_game_state.is_a?(State2)
40
+ switch_game_state(State1.new)
41
+ end
42
+ end
43
+ end
44
+
45
+ class State1 < Chingu::GameState
46
+ def draw
47
+ Image["ruby.png"].draw(0,0,0)
48
+ end
49
+ end
50
+
51
+ class State2 < Chingu::GameState
52
+ def draw
53
+ Image["video_games.png"].draw(0,0,0)
54
+ end
55
+ end
56
+
57
+ Game.new.show
@@ -0,0 +1,133 @@
1
+ require 'rubygems'
2
+ require File.join(File.dirname($0), "..", "lib", "chingu")
3
+ include Gosu
4
+ include Chingu
5
+
6
+ #
7
+ # GFXHelpers example - demonstrating Chingus GFX
8
+ #
9
+ class Game < Chingu::Window
10
+ def initialize
11
+ super(640,400)
12
+ self.input = {:space => :next_effect, :esc => :exit}
13
+ self.caption = "Example of Chingus GFX Helpers"
14
+
15
+ push_game_state(Fill)
16
+ push_game_state(FillRect)
17
+ push_game_state(FillGradient)
18
+ push_game_state(FillGradientRect)
19
+ push_game_state(Particles)
20
+ end
21
+
22
+ def next_effect
23
+ pop_game_state
24
+ end
25
+ end
26
+
27
+
28
+ class Fill < Chingu::GameState
29
+ def setup
30
+ @white = Color.new(255,255,255,255)
31
+ end
32
+ def draw
33
+ $window.caption = "fill (space to continue)"
34
+ fill(@white)
35
+ end
36
+ end
37
+
38
+ class FillRect < Chingu::GameState
39
+ def setup
40
+ @white = Color.new(255,255,255,255)
41
+ end
42
+ def draw
43
+ $window.caption = "fill_rect (space to continue)"
44
+ fill_rect([10,10,100,100], @white)
45
+ end
46
+ end
47
+
48
+ class FillGradient < Chingu::GameState
49
+ def setup
50
+ @pinkish = Color.new(0xFFCE17B6)
51
+ @blueish = Color.new(0xFF6DA9FF)
52
+ end
53
+
54
+ def draw
55
+ $window.caption = "fill_gradient (space to continue)"
56
+ fill_gradient(:from => @pinkish, :to => @blueish, :orientation => :vertical)
57
+ end
58
+ end
59
+
60
+ class FillGradientRect < Chingu::GameState
61
+ def setup
62
+ @color1 = Color.new(0xFFFFEA02)
63
+ @color2 = Color.new(0xFF078B20)
64
+ end
65
+
66
+ def draw
67
+ $window.caption = "fill_gradient with :rect-option (space to continue)"
68
+ fill_gradient(:from => @color1, :to => @color2, :rect => [100,100,200,200], :orientation => :horizontal)
69
+ end
70
+ end
71
+
72
+ class Particles < Chingu::GameState
73
+ def setup
74
+ @color1 = Color.new(0xFFFFEA02)
75
+ @color2 = Color.new(0xFF078B20)
76
+ @blue_laserish = Color.new(0xFF86EFFF)
77
+ @red = Color.new(0xFFFF0000)
78
+ @white = Color.new(0xFFFFFFFF)
79
+ @yellow = Color.new(0xFFF9F120)
80
+
81
+ # Thanks jsb in #gosu of Encave-fame for fireball.png :)
82
+ @fireball_animation = Chingu::Animation.new(:file => media_path("fireball.png"), :width => 32, :height => 32)
83
+ @ground_y = $window.height * 0.95
84
+ end
85
+
86
+ def update
87
+ #
88
+ # Fire 1. Dies quickly (big :fade). Small in size (small :zoom)
89
+ #
90
+ Chingu::Particle.create( :x => 100,
91
+ :y => @ground_y,
92
+ :animation => @fireball_animation,
93
+ :zooming => +0.05,
94
+ :fading => -10,
95
+ :rotating => +1,
96
+ :mode => :default
97
+ )
98
+
99
+ #
100
+ # Fire 2. Higher flame, :fade only -4. Wide Flame with bigger :zoom.
101
+ #
102
+ Chingu::Particle.create( :x => 300,
103
+ :y => @ground_y,
104
+ :animation => @fireball_animation,
105
+ :zooming => +0.2,
106
+ :fading => -4,
107
+ :rotating => +3,
108
+ :mode => :default
109
+ )
110
+ #
111
+ # Fire 3. Blue plasma with smooth particle.png and color-overlay.
112
+ #
113
+ Chingu::Particle.create( :x => 500,
114
+ :y => @ground_y,
115
+ :image => "particle.png",
116
+ :color => @blue_laserish,
117
+ :mode => :additive
118
+ )
119
+
120
+ Particle.all.each { |particle| particle.y -= 5; particle.x += 2 - rand(4) }
121
+ game_objects.destroy_if { |object| object.outside_window? || object.color.alpha == 0 }
122
+ super
123
+ end
124
+
125
+ def draw
126
+ $window.caption = "particle example (space to continue) [particles#: #{game_objects.size} - framerate: #{$window.fps}]"
127
+ fill_gradient(:from => Color.new(255,0,0,0), :to => Color.new(255,60,60,80), :rect => [0,0,$window.width,@ground_y])
128
+ fill_gradient(:from => Color.new(255,100,100,100), :to => Color.new(255,50,50,50), :rect => [0,@ground_y,$window.width,$window.height-@ground_y])
129
+ super
130
+ end
131
+ end
132
+
133
+ Game.new.show
@@ -0,0 +1,109 @@
1
+ require 'rubygems'
2
+ require File.join(File.dirname($0), "..", "lib", "chingu")
3
+ include Gosu
4
+ include Chingu
5
+
6
+ #
7
+ # Demonstrating domponents "velocity" and "effect"
8
+ #
9
+ class Game < Chingu::Window
10
+ def initialize
11
+ super(640,400)
12
+ self.input = {:esc => :exit}
13
+ self.caption = "Example of game object traits 'velocity' and 'effect'"
14
+ push_game_state(Particles)
15
+ puts RUBY_VERSION
16
+ end
17
+
18
+ def next_effect; pop_game_state; end
19
+ end
20
+
21
+ class Plasma < Chingu::GameObject
22
+ has_traits :velocity, :effect
23
+
24
+ def initialize(options)
25
+ super
26
+ @image = Image["particle.png"]
27
+ @mode = :additive
28
+
29
+ # initialize with a rightwards velocity with some damping to look more realistic
30
+ @velocity_x = options[:velocity_x] || 10
31
+ @acceleration_x = -0.1
32
+
33
+ # Simulate gravity
34
+ @acceleration_y = 0.4
35
+ end
36
+ end
37
+
38
+ class Particles < Chingu::GameState
39
+ def setup
40
+ @color1 = Color.new(0xFFFFEA02)
41
+ @color2 = Color.new(0xFF078B20)
42
+
43
+ #
44
+ # +1 fps
45
+ #
46
+ #@ground_y = $window.height * 0.95
47
+ @ground_y = ($window.height * 0.95).to_i
48
+ end
49
+
50
+ def update
51
+
52
+ #
53
+ # old velocity.rb 350 particles, 49 fps
54
+ # first optimization: 490 particles, 47 fps (350 @ 60)
55
+ # optimized GameObject if/elsif: 490 particles, 50 fps
56
+ #
57
+ Plasma.create(:x => 0, :y => 0 + rand(5), :color => Color.new(0xFF86EFFF), :velocity_x => 10)
58
+ Plasma.create(:x => 0, :y => 50 + rand(5), :color => Color.new(0xFF86EFFF), :velocity_x => 14)
59
+ Plasma.create(:x => 0, :y => 100 + rand(5), :color => Color.new(0xFF86EFFF), :velocity_x => 7)
60
+ Plasma.create(:x => 0, :y => 200 + rand(5), :color => Color.new(0xFF86EFFF), :velocity_x => 6)
61
+
62
+ Plasma.all.each do |particle|
63
+ #
64
+ # +1 fps
65
+ #
66
+ # particle.x += 1 - rand(2)
67
+ # -just removed, not replaced-
68
+
69
+ #
70
+ # If particle hits the ground:
71
+ #
72
+ if particle.y >= @ground_y
73
+
74
+ # 1) "Bounce" it up particle by reversing velocity_y with damping
75
+ slower = particle.velocity_y/3
76
+ particle.velocity_y = -(slower + rand(slower))
77
+
78
+ # 2) "Bounce" it randomly to left and right
79
+ if rand(2) == 0
80
+ particle.velocity_x = particle.velocity_y/2 + rand(2) # Randomr.randomr / 50
81
+ particle.acceleration_x = -0.02
82
+ else
83
+ particle.velocity_x = -particle.velocity_y/2 - rand(2) # Randomr.randomr / 50
84
+ particle.acceleration_x = 0.02
85
+ end
86
+
87
+ # 3) Start fading the alphachannel
88
+ particle.fading = -3
89
+ end
90
+ end
91
+
92
+ #
93
+ # +4 fps
94
+ #
95
+ #self.game_objects.reject! { |object| object.outside_window? || object.color.alpha == 0 }
96
+ self.game_objects.destroy_if { |object| object.color.alpha == 0 }
97
+
98
+ super
99
+ end
100
+
101
+ def draw
102
+ $window.caption = "particle example (esc to quit) [particles#: #{game_objects.size} - framerate: #{$window.fps}]"
103
+ fill_gradient(:from => Color.new(255,0,0,0), :to => Color.new(255,60,60,80), :rect => [0,0,$window.width,@ground_y])
104
+ fill_gradient(:from => Color.new(255,100,100,100), :to => Color.new(255,50,50,50), :rect => [0,@ground_y,$window.width,$window.height-@ground_y])
105
+ super
106
+ end
107
+ end
108
+
109
+ Game.new.show
@@ -0,0 +1,106 @@
1
+ require 'rubygems'
2
+ require File.join(File.dirname($0), "..", "lib", "chingu")
3
+ include Gosu
4
+ include Chingu
5
+
6
+ #
7
+ # Demonstrating domponents "velocity" and "effect"
8
+ #
9
+ class Game < Chingu::Window
10
+ def initialize
11
+ super(800,800)
12
+ self.input = {:esc => :exit}
13
+ self.caption = "Example of game object traits 'velocity' and 'effect'"
14
+ push_game_state(ParticleState)
15
+ end
16
+
17
+ def next_effect; pop_game_state; end
18
+ end
19
+
20
+ class FireCube < Chingu::GameObject
21
+ has_trait :velocity
22
+ has_trait :effect
23
+ has_trait :collision_detection
24
+ #
25
+ # TODO:
26
+ # has_trait :collision_detection, :type => :bounding_box
27
+ # has_trait :collision_detection, :type => :radius
28
+ #
29
+
30
+ attr_accessor :color, :radius
31
+
32
+ def initialize(options)
33
+ super
34
+ @mode = :additive
35
+
36
+ # initialize with a rightwards velocity with some damping to look more realistic
37
+ @velocity_x = options[:velocity_x] || 1 + rand(2)
38
+ @velocity_y = options[:velocity_y] || 1 + rand(2)
39
+
40
+ @bounding_box = Rect.new([@x, @y, 10, 10])
41
+ @radius = 6
42
+
43
+ @blue = Color.new(255,100,255,255)
44
+ @red = Color.new(255,255,10,10)
45
+ @color = @blue
46
+ end
47
+
48
+ def draw
49
+ $window.fill_rect(@bounding_box, @color)
50
+ end
51
+
52
+ def update
53
+ @color = @blue
54
+ end
55
+
56
+ def collides?(object2)
57
+ radius_collision?(object2)
58
+ end
59
+
60
+ def die!
61
+ @color = @red
62
+ end
63
+
64
+ end
65
+
66
+ class ParticleState < Chingu::GameState
67
+ def setup
68
+ self.input = { :space => :new_fire_cube }
69
+ 100.times { new_fire_cube }
70
+ end
71
+
72
+ def new_fire_cube
73
+ FireCube.create(:x => rand($window.width), :y => rand($window.height))
74
+ end
75
+
76
+ def update
77
+ super
78
+
79
+ FireCube.all.each do |particle|
80
+ if particle.x < 0 || particle.x > $window.width
81
+ particle.velocity_x = -particle.velocity_x
82
+ end
83
+
84
+ if particle.y < 0 || particle.y > $window.height
85
+ particle.velocity_y = -particle.velocity_y
86
+ end
87
+ end
88
+
89
+ #
90
+ # GameObject.each_collsion / each_radius_collision wont collide an object with itself
91
+ #
92
+ FireCube.each_radius_collision(FireCube) do |cube1, cube2|
93
+ cube1.die!
94
+ cube2.die!
95
+ end
96
+
97
+ game_objects.destroy_if { |object| object.color.alpha == 0 }
98
+ end
99
+
100
+ def draw
101
+ $window.caption = "radius based iterative collision detection. Particles#: #{game_objects.size}, Collisionchecks each gameloop: ~#{game_objects.size**2} - FPS: #{$window.fps}"
102
+ super
103
+ end
104
+ end
105
+
106
+ Game.new.show