chingu 0.5.5.3
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.
- data.tar.gz.sig +0 -0
- data/History.txt +21 -0
- data/LICENSE +504 -0
- data/Manifest.txt +72 -0
- data/README.rdoc +588 -0
- data/Rakefile +19 -0
- data/benchmarks/README.txt +1 -0
- data/benchmarks/benchmark.rb +6 -0
- data/benchmarks/benchmark3.rb +23 -0
- data/benchmarks/benchmark4.rb +71 -0
- data/benchmarks/benchmark5.rb +91 -0
- data/benchmarks/benchmark6.rb +23 -0
- data/benchmarks/meta_benchmark.rb +67 -0
- data/benchmarks/meta_benchmark2.rb +39 -0
- data/chingu.gemspec +34 -0
- data/examples/example1.rb +37 -0
- data/examples/example10.rb +75 -0
- data/examples/example11.rb +51 -0
- data/examples/example12.rb +67 -0
- data/examples/example2.rb +115 -0
- data/examples/example3.rb +40 -0
- data/examples/example4.rb +175 -0
- data/examples/example5.rb +107 -0
- data/examples/example6.rb +57 -0
- data/examples/example7.rb +133 -0
- data/examples/example8.rb +109 -0
- data/examples/example9.rb +106 -0
- data/examples/media/Parallax-scroll-example-layer-0.png +0 -0
- data/examples/media/Parallax-scroll-example-layer-1.png +0 -0
- data/examples/media/Parallax-scroll-example-layer-2.png +0 -0
- data/examples/media/Parallax-scroll-example-layer-3.png +0 -0
- data/examples/media/background1.png +0 -0
- data/examples/media/fire_bullet.png +0 -0
- data/examples/media/fireball.png +0 -0
- data/examples/media/particle.png +0 -0
- data/examples/media/ruby.png +0 -0
- data/examples/media/spaceship.png +0 -0
- data/examples/media/stickfigure.bmp +0 -0
- data/examples/media/stickfigure.png +0 -0
- data/examples/media/video_games.png +0 -0
- data/lib/chingu.rb +32 -0
- data/lib/chingu/actor.rb +17 -0
- data/lib/chingu/animation.rb +142 -0
- data/lib/chingu/assets.rb +64 -0
- data/lib/chingu/basic_game_object.rb +132 -0
- data/lib/chingu/core_extensions.rb +53 -0
- data/lib/chingu/effects.rb +36 -0
- data/lib/chingu/fpscounter.rb +62 -0
- data/lib/chingu/game_object.rb +127 -0
- data/lib/chingu/game_object_list.rb +91 -0
- data/lib/chingu/game_state.rb +137 -0
- data/lib/chingu/game_state_manager.rb +284 -0
- data/lib/chingu/game_states/debug.rb +65 -0
- data/lib/chingu/game_states/fade_to.rb +91 -0
- data/lib/chingu/game_states/pause.rb +57 -0
- data/lib/chingu/gfx_helpers.rb +89 -0
- data/lib/chingu/helpers.rb +166 -0
- data/lib/chingu/inflector.rb +34 -0
- data/lib/chingu/input.rb +100 -0
- data/lib/chingu/named_resource.rb +254 -0
- data/lib/chingu/parallax.rb +83 -0
- data/lib/chingu/particle.rb +21 -0
- data/lib/chingu/rect.rb +612 -0
- data/lib/chingu/require_all.rb +133 -0
- data/lib/chingu/text.rb +56 -0
- data/lib/chingu/traits/collision_detection.rb +172 -0
- data/lib/chingu/traits/effect.rb +113 -0
- data/lib/chingu/traits/input.rb +38 -0
- data/lib/chingu/traits/retrofy.rb +53 -0
- data/lib/chingu/traits/rotation_center.rb +84 -0
- data/lib/chingu/traits/timer.rb +90 -0
- data/lib/chingu/traits/velocity.rb +67 -0
- data/lib/chingu/window.rb +170 -0
- metadata +162 -0
- metadata.gz.sig +1 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.join(File.dirname($0), "..", "lib", "chingu")
|
3
|
+
include Gosu
|
4
|
+
|
5
|
+
#
|
6
|
+
# Parallax-example
|
7
|
+
# Images from http://en.wikipedia.org/wiki/Parallax_scrolling
|
8
|
+
#
|
9
|
+
class Game < Chingu::Window
|
10
|
+
def initialize
|
11
|
+
super
|
12
|
+
self.input = { :holding_left => :scroll_left,
|
13
|
+
:holding_right => :scroll_right,
|
14
|
+
:holding_up => :scroll_up,
|
15
|
+
:holding_down => :scroll_down,
|
16
|
+
:escape => :exit }
|
17
|
+
|
18
|
+
self.caption = "Chingu::Parallax example. Scroll with left/right arrows."
|
19
|
+
|
20
|
+
@parallax = Chingu::Parallax.create(:x => 0, :y => 0, :center_x => 0, :center_y => 0)
|
21
|
+
|
22
|
+
#
|
23
|
+
# If no :zorder is given to @parallax.add_background it defaults to first added -> lowest zorder
|
24
|
+
# Everywhere the :image argument is used, theese 2 values are the Same:
|
25
|
+
# 1) Image["foo.png"] 2) "foo.png"
|
26
|
+
#
|
27
|
+
# TODO: scrolling to left borks outm, fix. + get rid of center_x / center_y args in a clean way.
|
28
|
+
@parallax << {:image => "paralaxx2", :damping => 100, :center => 0)
|
29
|
+
@parallax << {:image => "parallax-scroll-example-layer-1.png", :damping => 10, :center => 0)
|
30
|
+
@parallax << {:image => "paralaxx2.png", :damping => 5, :center => 0)
|
31
|
+
end
|
32
|
+
|
33
|
+
def scroll_left
|
34
|
+
@parallax.x -= 2
|
35
|
+
end
|
36
|
+
|
37
|
+
def scroll_right
|
38
|
+
@parallax.x += 2
|
39
|
+
end
|
40
|
+
|
41
|
+
def scroll_up
|
42
|
+
@parallax.y -= 2
|
43
|
+
end
|
44
|
+
|
45
|
+
def scroll_down
|
46
|
+
@parallax.y += 2
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
Game.new.show
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.join(File.dirname($0), "..", "lib", "chingu")
|
3
|
+
include Gosu
|
4
|
+
include Chingu
|
5
|
+
|
6
|
+
class Game < Chingu::Window
|
7
|
+
def initialize
|
8
|
+
super(640,400)
|
9
|
+
self.input = {:esc => :exit}
|
10
|
+
self.caption = "Example of Chingus :timer trait (arrows,space,return sets timers that affects @x,@y & @color)"
|
11
|
+
|
12
|
+
Cube.create(:x => 200, :y => 200)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Cube < GameObject
|
17
|
+
has_trait :timer
|
18
|
+
|
19
|
+
def initialize(options)
|
20
|
+
super
|
21
|
+
@white = Color.new(0xFFFFFFFF)
|
22
|
+
@red = Color.new(0xFFFF0000)
|
23
|
+
@blue = Color.new(0xFF0000FF)
|
24
|
+
self.input = { :left => :left, :right => :right, :up => :up, :down => :down,
|
25
|
+
:space => :shake1, :return => :shake2 }
|
26
|
+
end
|
27
|
+
|
28
|
+
def left
|
29
|
+
during(500) { @x -= 1 }
|
30
|
+
end
|
31
|
+
|
32
|
+
def right
|
33
|
+
during(500) { @x += 1 }
|
34
|
+
end
|
35
|
+
|
36
|
+
def up
|
37
|
+
@color = @red
|
38
|
+
during(500) { @y -= 1 }.then { @color = @white }
|
39
|
+
end
|
40
|
+
|
41
|
+
def down
|
42
|
+
@color = @blue
|
43
|
+
during(500) { @y += 1 }.then { @color = @white }
|
44
|
+
end
|
45
|
+
|
46
|
+
def shake1
|
47
|
+
#
|
48
|
+
# Nesting works too!
|
49
|
+
#
|
50
|
+
during(50) {@y -= 4}.then { during(100) {@y += 4}.then { during(50) {@y -= 4} } }
|
51
|
+
end
|
52
|
+
|
53
|
+
def shake2
|
54
|
+
#
|
55
|
+
# Does the exact same as "shake1" but using between() instead of during()
|
56
|
+
#
|
57
|
+
between(0,50) {@y -= 4}
|
58
|
+
between(50,150) {@y += 4}
|
59
|
+
between(150,200) {@y -= 4}
|
60
|
+
end
|
61
|
+
|
62
|
+
def draw
|
63
|
+
$window.fill_rect([@x, @y, 40, 40], @color)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
Game.new.show
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.join(File.dirname($0), "..", "lib", "chingu")
|
3
|
+
include Gosu
|
4
|
+
|
5
|
+
#
|
6
|
+
# A little more complicated example where we do our own #update and #draw code.
|
7
|
+
# We also add another Actor - a bullet fired with space.
|
8
|
+
#
|
9
|
+
# Also tests out the Debug game state.
|
10
|
+
#
|
11
|
+
class Game < Chingu::Window
|
12
|
+
def initialize
|
13
|
+
#
|
14
|
+
# See http://www.libgosu.org/rdoc/classes/Gosu/Window.html#M000034 for options
|
15
|
+
# By default Chingu does 640 x 480 non-fullscreen.
|
16
|
+
#
|
17
|
+
super
|
18
|
+
|
19
|
+
push_game_state(Play)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Our Player
|
25
|
+
#
|
26
|
+
class Player < Chingu::GameObject
|
27
|
+
def initialize(options = {})
|
28
|
+
super
|
29
|
+
@image = Image["spaceship.png"]
|
30
|
+
end
|
31
|
+
|
32
|
+
def move_left; @x -= 1; end
|
33
|
+
def move_right; @x += 1; end
|
34
|
+
def move_up; @y -= 1; end
|
35
|
+
def move_down; @y += 1; end
|
36
|
+
|
37
|
+
def fire
|
38
|
+
Bullet.create(:x => @x, :y => @y)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Bullet < Chingu::GameObject
|
43
|
+
#
|
44
|
+
# If we need our own initialize, just call super and Chingu does it's thing.
|
45
|
+
# Here we merge in an extra argument, specifying the bullet-image.
|
46
|
+
#
|
47
|
+
def initialize(options)
|
48
|
+
super(options.merge(:image => Image["fire_bullet.png"]))
|
49
|
+
end
|
50
|
+
|
51
|
+
# Move the bullet forward
|
52
|
+
def update
|
53
|
+
@y -= 2
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
class Play < Chingu::GameState
|
59
|
+
|
60
|
+
def initialize
|
61
|
+
super
|
62
|
+
@player = Player.create(:x => 200, :y => 200)
|
63
|
+
@player.input = { :holding_left => :move_left,
|
64
|
+
:holding_right => :move_right,
|
65
|
+
:holding_up => :move_up,
|
66
|
+
:holding_down => :move_down,
|
67
|
+
:space => :fire,
|
68
|
+
:escape => :exit,
|
69
|
+
}
|
70
|
+
self.input = { :f1 => :debug }
|
71
|
+
end
|
72
|
+
|
73
|
+
def debug
|
74
|
+
#puts "--------"
|
75
|
+
#GameObject.all.each do |game_object|
|
76
|
+
# puts game_object.class
|
77
|
+
#end
|
78
|
+
#return
|
79
|
+
|
80
|
+
push_game_state(Chingu::GameStates::Debug)
|
81
|
+
end
|
82
|
+
|
83
|
+
#
|
84
|
+
# If we want to add extra graphics drawn just define your own draw.
|
85
|
+
# Be sure to call #super for enabling Chingus autodrawing of Actors.
|
86
|
+
# Putting #super before or after the background-draw-call really doesn't matter since Gosu work with "zorder".
|
87
|
+
#
|
88
|
+
def draw
|
89
|
+
# Raw Gosu Image.draw(x,y,zorder)-call
|
90
|
+
Image["background1.png"].draw(0, 0, 0)
|
91
|
+
super
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# Gosus place for gamelogic is #update in the mainwindow
|
96
|
+
#
|
97
|
+
# A #super call here would call #update on all Chingu::Actors and check their inputs, and call the specified method.
|
98
|
+
#
|
99
|
+
def update
|
100
|
+
Bullet.destroy_if { |bullet| bullet.outside_window? }
|
101
|
+
#Bullet.destroy_if(&:outside_window?)
|
102
|
+
|
103
|
+
#
|
104
|
+
# Bullet.each_collision(Enemy) do |bullet, enemy|
|
105
|
+
# enemy.collision_with(bullet)
|
106
|
+
# bullet.destroy!
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
|
110
|
+
super
|
111
|
+
$window.caption = "FPS: #{$window.fps} - milliseconds_since_last_tick: #{$window.milliseconds_since_last_tick} - game objects# #{current_game_state.game_objects.size} Bullets# #{Bullet.size}"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
Game.new.show
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.join(File.dirname($0), "..", "lib", "chingu")
|
3
|
+
include Gosu
|
4
|
+
|
5
|
+
#
|
6
|
+
# Parallax-example
|
7
|
+
# Images from http://en.wikipedia.org/wiki/Parallax_scrolling
|
8
|
+
#
|
9
|
+
class Game < Chingu::Window
|
10
|
+
def initialize
|
11
|
+
super
|
12
|
+
self.input = { :holding_left => :scroll_left, :holding_right => :scroll_right, :escape => :exit }
|
13
|
+
|
14
|
+
self.caption = "Chingu::Parallax example. Scroll with left/right arrows."
|
15
|
+
|
16
|
+
@parallax = Chingu::Parallax.create(:x => 0, :y => 0, :center_x => 0, :center_y => 0)
|
17
|
+
|
18
|
+
#
|
19
|
+
# If no :zorder is given to @parallax.add_background it defaults to first added -> lowest zorder
|
20
|
+
# Everywhere the :image argument is used, theese 2 values are the Same:
|
21
|
+
# 1) Image["foo.png"] 2) "foo.png"
|
22
|
+
#
|
23
|
+
# TODO: scrolling to left borks outm, fix. + get rid of center_x / center_y args in a clean way.
|
24
|
+
@parallax.add_background(:image => "Parallax-scroll-example-layer-0.png", :damping => 100, :center => 0)
|
25
|
+
@parallax.add_background(:image => "Parallax-scroll-example-layer-1.png", :damping => 10, :center => 0)
|
26
|
+
@parallax.add_background(:image => "Parallax-scroll-example-layer-2.png", :damping => 5, :center => 0)
|
27
|
+
@parallax << {:image => "Parallax-scroll-example-layer-3.png", :damping => 1, :center => 0} # you can also add like this
|
28
|
+
end
|
29
|
+
|
30
|
+
def scroll_left
|
31
|
+
@parallax.x -= 2
|
32
|
+
end
|
33
|
+
|
34
|
+
def scroll_right
|
35
|
+
@parallax.x += 2
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
Game.new.show
|
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.join(File.dirname($0), "..", "lib", "chingu")
|
3
|
+
include Gosu
|
4
|
+
|
5
|
+
#
|
6
|
+
# Example demonstrating jumping between 4 different game states.
|
7
|
+
#
|
8
|
+
# push_game_state, pop_game_state, current_game_state previous_game_state are 4 helper-methods that Chingu mixes in
|
9
|
+
# into Chingu::Window and Chingu::GameState
|
10
|
+
#
|
11
|
+
# Behind the scenes they work against @game_state_manager that's autocreated within Chingu::Window.
|
12
|
+
#
|
13
|
+
# Execution in example4 flows like this:
|
14
|
+
#
|
15
|
+
# 1) Core Gosu calls instancemethods draw / update in the class based on Gosu::Window
|
16
|
+
# In this example 'Game' since "Game < Chingu::Window" and "Chingu::Window < Gosu::Window"
|
17
|
+
#
|
18
|
+
# 2) In its turn Game (Chingu::Window) calls @game_state_manager.draw / update
|
19
|
+
#
|
20
|
+
# 3) @game_state_manager calls draw / update on the current active game state
|
21
|
+
#
|
22
|
+
# 4) Each game state keeps a collection @game_objects which it calls draw / update on.
|
23
|
+
# Any object based on Chingu::GameObject (In this example Player and Text) automatically
|
24
|
+
# gets added to the correct state or or main window.
|
25
|
+
#
|
26
|
+
|
27
|
+
#
|
28
|
+
# Our standard Chingu::Window that makes all the magic happen.
|
29
|
+
#
|
30
|
+
class Game < Chingu::Window
|
31
|
+
def initialize
|
32
|
+
super
|
33
|
+
|
34
|
+
push_game_state(Intro)
|
35
|
+
|
36
|
+
# Yes you can do crazy things like this :)
|
37
|
+
self.input = { :left_mouse_button => lambda{Chingu::Text.create(:text => "Woff!")}, :esc => :exit}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Our Player
|
42
|
+
class Player < Chingu::GameObject
|
43
|
+
def initialize(options = {})
|
44
|
+
super
|
45
|
+
@image = Image["spaceship.png"]
|
46
|
+
end
|
47
|
+
|
48
|
+
def move_left; @x -= 1; end
|
49
|
+
def move_right; @x += 1; end
|
50
|
+
def move_up; @y -= 1; end
|
51
|
+
def move_down; @y += 1; end
|
52
|
+
|
53
|
+
def fire
|
54
|
+
#puts $window.current_parent.to_s
|
55
|
+
#puts $window.game_state_manager.inside_state
|
56
|
+
Bullet.create(:x => @x - 20, :y => @y)
|
57
|
+
Bullet.create(:x => @x, :y => @y)
|
58
|
+
Bullet.create(:x => @x + 20, :y => @y)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# The bullet the Player fires
|
63
|
+
class Bullet < Chingu::GameObject
|
64
|
+
def initialize(options)
|
65
|
+
super
|
66
|
+
@image = Image["fire_bullet.png"]
|
67
|
+
end
|
68
|
+
|
69
|
+
def update
|
70
|
+
@y -= 2
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
#
|
76
|
+
# GAMESTATE #1 - INTRO
|
77
|
+
#
|
78
|
+
class Intro < Chingu::GameState
|
79
|
+
def initialize(options = {})
|
80
|
+
super
|
81
|
+
@title = Chingu::Text.create(:text=>"Press and release F1", :x=>200, :y=>50, :size=>30)
|
82
|
+
self.input = { :f1 => :pressed, :released_f1 => :released, :f2 => Menu}
|
83
|
+
end
|
84
|
+
|
85
|
+
def pressed
|
86
|
+
@title.text = "F1 pressed (F2 to continue)"
|
87
|
+
end
|
88
|
+
|
89
|
+
def released
|
90
|
+
@title.text = "F1 released (F2 to continue)"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# GAMESTATE #2 - MENU
|
96
|
+
#
|
97
|
+
class Menu < Chingu::GameState
|
98
|
+
def initialize(options = {})
|
99
|
+
super
|
100
|
+
@title = Chingu::Text.create(:text => "Press 'S' to Start game", :x=>100, :y=>50, :size=>30)
|
101
|
+
self.input = { :s => Level.new(:level => 10) }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
#
|
106
|
+
# GAMESTATE #3 - LEVEL (Gameplay, yay)
|
107
|
+
#
|
108
|
+
class Level < Chingu::GameState
|
109
|
+
#
|
110
|
+
# initialize() is called when you create the game state
|
111
|
+
#
|
112
|
+
def initialize(options = {})
|
113
|
+
super
|
114
|
+
@title = Chingu::Text.create(:text=>"Level #{options[:level].to_s}. P: pause R:restart", :x=>20, :y=>10, :size=>30)
|
115
|
+
@player = Player.create
|
116
|
+
@player.input = { :holding_left => :move_left,
|
117
|
+
:holding_right => :move_right,
|
118
|
+
:holding_up => :move_up,
|
119
|
+
:holding_down => :move_down,
|
120
|
+
:space => :fire}
|
121
|
+
|
122
|
+
#
|
123
|
+
# The input-handler understands gamestates. P is pressed --> push_gamegate(Pause)
|
124
|
+
# You can also give it Procs/Lambdas which it will execute when key is pressed.
|
125
|
+
#
|
126
|
+
self.input = {:p => Pause, :r => lambda{ current_game_state.setup } }
|
127
|
+
end
|
128
|
+
|
129
|
+
def update
|
130
|
+
super
|
131
|
+
Bullet.destroy_if {|bullet| bullet.outside_window? }
|
132
|
+
$window.caption = "FPS: #{$window.fps} - GameObjects: #{game_objects.size} - Bullets: #{Bullet.size}"
|
133
|
+
end
|
134
|
+
|
135
|
+
#
|
136
|
+
# setup() is called each time you switch to the game state (and on creation time).
|
137
|
+
# You can skip setup by switching with push_game_state(:setup => false) or pop_game_state(:setup => false)
|
138
|
+
#
|
139
|
+
# This can be useful if you want to display some kind of box above the gameplay (pause/options/info/... box)
|
140
|
+
#
|
141
|
+
def setup
|
142
|
+
# Destroy all created objects of class Bullet
|
143
|
+
#p Bullet.size
|
144
|
+
#puts Bullet.all
|
145
|
+
Bullet.destroy_all
|
146
|
+
|
147
|
+
# Place player in a good starting position
|
148
|
+
@player.x = $window.width/2
|
149
|
+
@player.y = $window.height - @player.image.height
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
#
|
154
|
+
# SPECIAL GAMESTATE - Pause
|
155
|
+
#
|
156
|
+
# NOTICE: Chingu now comes with a predefined Chingu::GameStates::Pause that works simular to this!
|
157
|
+
#
|
158
|
+
class Pause < Chingu::GameState
|
159
|
+
def initialize(options = {})
|
160
|
+
super
|
161
|
+
@title = Chingu::Text.create(:text=>"PAUSED (press 'u' to un-pause)", :x=>100, :y=>200, :size=>20, :color => Color.new(0xFF00FF00))
|
162
|
+
self.input = { :u => :un_pause }
|
163
|
+
end
|
164
|
+
|
165
|
+
def un_pause
|
166
|
+
pop_game_state(:setup => false) # Return the previous game state, dont call setup()
|
167
|
+
end
|
168
|
+
|
169
|
+
def draw
|
170
|
+
previous_game_state.draw # Draw prev game state onto screen (in this case our level)
|
171
|
+
super # Draw game objects in current game state, this includes Chingu::Texts
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
Game.new.show
|