ippa-chingu 0.4.8 → 0.5
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/History.txt +5 -0
- data/README.rdoc +43 -23
- data/chingu.gemspec +4 -4
- data/examples/example1.rb +2 -0
- data/examples/example2.rb +63 -33
- data/examples/example4.rb +16 -3
- data/examples/example5.rb +3 -3
- data/examples/example6.rb +1 -40
- data/examples/example7.rb +9 -8
- data/examples/example8.rb +109 -0
- data/examples/example9.rb +83 -0
- data/lib/chingu.rb +17 -2
- data/lib/chingu/actor.rb +15 -0
- data/lib/chingu/animation.rb +27 -2
- data/lib/chingu/basic_game_object.rb +140 -0
- data/lib/chingu/core_extensions.rb +15 -5
- data/lib/chingu/effects.rb +1 -1
- data/lib/chingu/fpscounter.rb +0 -1
- data/lib/chingu/game_object.rb +85 -155
- data/lib/chingu/game_state.rb +4 -3
- data/lib/chingu/game_state_manager.rb +3 -4
- data/lib/chingu/game_states/debug.rb +43 -0
- data/lib/chingu/game_states/fade_to.rb +2 -2
- data/lib/chingu/helpers.rb +2 -0
- data/lib/chingu/parallax.rb +2 -2
- data/lib/chingu/particle.rb +5 -4
- data/lib/chingu/traits/collision_detection.rb +33 -0
- data/lib/chingu/traits/deprecated_module_visual.rb +108 -0
- data/lib/chingu/traits/deprecated_visual.rb +100 -0
- data/lib/chingu/traits/effect.rb +80 -0
- data/lib/chingu/traits/input.rb +15 -0
- data/lib/chingu/traits/velocity.rb +59 -0
- data/lib/chingu/window.rb +18 -3
- metadata +16 -4
@@ -1,11 +1,21 @@
|
|
1
1
|
#
|
2
|
-
# Core extensions
|
3
|
-
#
|
4
|
-
# Extensions to the classes GOSU offers
|
2
|
+
# Core extensions to GOSU
|
3
|
+
# Some of these require the gem 'texplay'
|
5
4
|
#
|
6
5
|
module Gosu
|
7
|
-
|
8
|
-
|
6
|
+
|
7
|
+
class Image
|
8
|
+
#
|
9
|
+
# Returns true if the pixel at x, y is 100% transperant (good for collisiondetection)
|
10
|
+
# Requires texplay
|
11
|
+
#
|
12
|
+
def transparent_pixel?(x, y)
|
13
|
+
begin
|
14
|
+
self.get_pixel(x,y)[3] == 0
|
15
|
+
rescue
|
16
|
+
puts "Error in get_pixel at x/y: #{x}/#{y}"
|
17
|
+
end
|
9
18
|
end
|
19
|
+
|
10
20
|
end
|
11
21
|
end
|
data/lib/chingu/effects.rb
CHANGED
data/lib/chingu/fpscounter.rb
CHANGED
data/lib/chingu/game_object.rb
CHANGED
@@ -1,197 +1,127 @@
|
|
1
1
|
module Chingu
|
2
2
|
#
|
3
|
-
#
|
4
|
-
# Gosus draw_rot and it's parameters.
|
3
|
+
# GameObject is our BasisGameObject (class with framespecific stuff)
|
5
4
|
#
|
6
|
-
#
|
5
|
+
# On top of that, it encapsulates GOSUs Image#draw_rot and all its parameters.
|
7
6
|
#
|
8
|
-
|
7
|
+
|
8
|
+
class GameObject < Chingu::BasicGameObject
|
9
9
|
attr_accessor :image, :x, :y, :angle, :center_x, :center_y, :factor_x, :factor_y, :color, :mode, :zorder
|
10
|
-
attr_accessor :update, :draw
|
11
|
-
attr_reader :options, :parent
|
12
|
-
|
13
|
-
include Chingu::InputClient
|
14
10
|
|
15
11
|
#
|
16
|
-
#
|
17
|
-
# This allows you to set default-values that affect all created GameObjects after that.
|
18
|
-
# You might want to draw gameobjects from the top-left @ x/y instead of putting it's center there:
|
19
|
-
#
|
20
|
-
# in Gosu::Window#initialize: GameObject.center_x = GameObject.center_y = 0
|
12
|
+
# returns [center_x, center_y]
|
21
13
|
#
|
22
|
-
@@
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
14
|
+
@@rotation_centers = {
|
15
|
+
:top_left => [0,0],
|
16
|
+
:left_top => [0,0],
|
17
|
+
|
18
|
+
:center_left => [0,0.5],
|
19
|
+
:left_center => [0,0.5],
|
20
|
+
|
21
|
+
:bottom_left => [0,1],
|
22
|
+
:left_bottom => [0,1],
|
23
|
+
|
24
|
+
:top_center => [0.5,0],
|
25
|
+
:center_top => [0.5,0],
|
26
|
+
|
27
|
+
:center_center => [0.5,0.5],
|
28
|
+
:center => [0.5,0.5],
|
29
|
+
|
30
|
+
:bottom_center => [0.5,1],
|
31
|
+
:center_bottom => [0.5,1],
|
32
|
+
|
33
|
+
:top_right => [1,0],
|
34
|
+
:right_top => [1,0],
|
35
|
+
|
36
|
+
:center_right => [1,0.5],
|
37
|
+
:right_center => [1,0.5],
|
38
|
+
|
39
|
+
:bottom_right => [1,1],
|
40
|
+
:right_bottom => [1,1]
|
41
|
+
}
|
42
|
+
|
51
43
|
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
# :x screen x-coordinate (default 0, to the left)
|
55
|
-
# :y screen y-coordinate (default 0, top of screen)
|
56
|
-
# :angle angle of object, used in draw_rot, (default 0, no rotation)
|
57
|
-
# :zorder a gameclass "foo" with higher zorder then gameclass "bar" is drawn on top of "foo".
|
58
|
-
# :center_x relative horizontal position of the rotation center on the image.
|
59
|
-
# 0 is the left border, 1 is the right border, 0.5 is the center (default 0.5)
|
60
|
-
# :center_y see center_x. (default 0.5)
|
61
|
-
# :factor_x horizontal zoom-factor, use >1.0 to zoom in. (default 1.0, no zoom).
|
62
|
-
# :factor_y vertical zoom-factor, use >1.0 to zoom in. (default 1.0, no zoom).
|
44
|
+
# Sets @center_x and @center_y according to given alignment. Available alignments are:
|
63
45
|
#
|
64
|
-
# :
|
65
|
-
# :
|
46
|
+
# :top_left, :center_left, :bottom_left, :top_center,
|
47
|
+
# :center_center, :bottom_center, :top_right, :center_right and :bottom_right
|
66
48
|
#
|
49
|
+
# They're also available in the opposite order with the same meaning.
|
50
|
+
#
|
51
|
+
def rotation_center(alignment)
|
52
|
+
@center_x, @center_y = @@rotation_centers[alignment.to_sym]
|
53
|
+
end
|
54
|
+
|
67
55
|
def initialize(options = {})
|
68
|
-
|
69
|
-
|
70
|
-
# draw_rot arguments
|
71
|
-
|
72
|
-
|
56
|
+
super
|
57
|
+
|
58
|
+
# All encapsulated draw_rot arguments can be set with hash-options at creation time
|
59
|
+
if options[:image].is_a?(Gosu::Image)
|
60
|
+
@image = options[:image]
|
61
|
+
elsif options[:image].is_a? String
|
62
|
+
@image = Image[options[:image]]
|
63
|
+
end
|
73
64
|
|
74
|
-
@x = options[:x] ||
|
75
|
-
@y = options[:y] ||
|
65
|
+
@x = options[:x] || 0
|
66
|
+
@y = options[:y] || 0
|
76
67
|
@angle = options[:angle] || 0
|
77
|
-
@zorder = options[:zorder] || @@zorder
|
78
|
-
@center_x = options[:center_x] || options[:center] || @@center_x
|
79
|
-
@center_y = options[:center_y] || options[:center] || @@center_y
|
80
|
-
@factor_x = options[:factor_x] || options[:factor] || @@factor_x
|
81
|
-
@factor_y = options[:factor_y] || options[:factor] || @@factor_y
|
82
|
-
@color = Gosu::Color.new(options[:color]) if options[:color].is_a? Bignum
|
83
|
-
@color = options[:color] if options[:color].respond_to?(:alpha)
|
84
|
-
@color = Gosu::Color.new(0xFFFFFFFF) if @color.nil?
|
85
|
-
|
86
|
-
@mode = options[:mode] || :default # :additive is also available.
|
87
68
|
|
88
|
-
|
89
|
-
@
|
69
|
+
@center_x = options[:center_x] || options[:center] || 0.5
|
70
|
+
@center_y = options[:center_y] || options[:center] || 0.5
|
71
|
+
@factor_x = options[:factor_x] || options[:factor] || 1.0
|
72
|
+
@factor_y = options[:factor_y] || options[:factor] || 1.0
|
73
|
+
|
74
|
+
# faster?
|
75
|
+
#self.center = options[:center] || 0.5
|
76
|
+
#self.factor = options[:factor] || 1.0
|
77
|
+
#@center_x = options[:center_x] || 0.5
|
78
|
+
#@center_y = options[:center_y] || 0.5
|
79
|
+
#@factor_x = options[:factor_x] || 1.0
|
80
|
+
#@factor_y = options[:factor_y] || 1.0
|
81
|
+
|
82
|
+
if options[:color].is_a?(Gosu::Color)
|
83
|
+
@color = options[:color]
|
84
|
+
elsif options[:color].is_a? Bignum
|
85
|
+
@color = Gosu::Color.new(options[:color])
|
86
|
+
else
|
87
|
+
@color = Gosu::Color.new(0xFFFFFFFF)
|
88
|
+
end
|
90
89
|
|
91
|
-
#
|
90
|
+
@mode = options[:mode] || :default # :additive is also available.
|
91
|
+
@zorder = options[:zorder] || 100
|
92
|
+
|
93
|
+
# gameloop/framework logic (TODO: use or get rid of)
|
92
94
|
@update = options[:update] || true
|
93
95
|
@draw = options[:draw] || true
|
94
|
-
@input = options[:input] || nil
|
95
|
-
|
96
|
-
#
|
97
|
-
# A GameObject can either belong to a GameState or our mainwindow ($window)
|
98
|
-
# .. or live in limbo with manual updates
|
99
|
-
#
|
100
|
-
if $window && $window.respond_to?(:game_state_manager)
|
101
|
-
@parent = $window.game_state_manager.inside_state || $window
|
102
|
-
@parent.add_game_object(self) if @parent
|
103
|
-
end
|
104
96
|
end
|
105
97
|
|
106
|
-
#
|
107
98
|
# Quick way of setting both factor_x and factor_y
|
108
|
-
#
|
109
99
|
def factor=(factor)
|
110
100
|
@factor_x = @factor_y = factor
|
111
101
|
end
|
112
|
-
|
113
|
-
#
|
102
|
+
|
114
103
|
# Quick way of setting both center_x and center_y
|
115
|
-
#
|
116
104
|
def center=(factor)
|
117
105
|
@center_x = @center_y = factor
|
118
106
|
end
|
119
107
|
|
120
|
-
#
|
121
|
-
# Zoom - increase @factor_x and @factor_y at the same time.
|
122
|
-
#
|
123
|
-
def zoom(amount = 0.1)
|
124
|
-
@factor_x += amount
|
125
|
-
@factor_y += amount
|
126
|
-
end
|
127
|
-
|
128
|
-
#
|
129
|
-
# Zoom Out - decrease @factor_x and @factor_y at the same time.
|
130
|
-
#
|
131
|
-
def zoom_out(amount = 0.1)
|
132
|
-
@factor_x -= amount
|
133
|
-
@factor_y -= amount
|
134
|
-
end
|
135
|
-
|
136
|
-
#
|
137
|
-
# Rotate object 'amount' degrees
|
138
|
-
#
|
139
|
-
def rotate(amount = 1)
|
140
|
-
@angle += amount
|
141
|
-
end
|
142
|
-
|
143
|
-
#
|
144
|
-
# Fade object by decreasing/increasing color.alpha
|
145
|
-
#
|
146
|
-
def fade(amount = 1)
|
147
|
-
return if amount == 0
|
148
|
-
|
149
|
-
new_alpha = @color.alpha + amount
|
150
|
-
if amount < 0
|
151
|
-
@color.alpha = [0, new_alpha].max
|
152
|
-
else
|
153
|
-
@color.alpha = [0, new_alpha].min
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
#
|
158
|
-
# Fade out objects color by decreasing color.alpha
|
159
|
-
#
|
160
|
-
def fade_out(amount = 1)
|
161
|
-
fade(-amount)
|
162
|
-
end
|
163
|
-
|
164
|
-
#
|
165
|
-
# Fade in objects color by increasing color.alpha
|
166
|
-
#
|
167
|
-
def fade_in(amount = 1)
|
168
|
-
fade(amount)
|
169
|
-
end
|
170
|
-
|
171
|
-
#
|
172
108
|
# Returns true if object is inside the game window, false if outside
|
173
|
-
#
|
174
109
|
def inside_window?(x = @x, y = @y)
|
175
110
|
x >= 0 && x <= $window.width && y >= 0 && y <= $window.height
|
176
111
|
end
|
177
112
|
|
178
|
-
#
|
179
113
|
# Returns true object is outside the game window
|
180
|
-
#
|
181
114
|
def outside_window?(x = @x, y = @y)
|
182
115
|
not inside_window?(x,y)
|
183
116
|
end
|
184
|
-
|
185
|
-
def update(time = 0)
|
186
|
-
# Objects gamelogic here, 'time' is the time passed between 2 iterations of the main game loop
|
187
|
-
end
|
188
117
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
118
|
+
def distance_to(object)
|
119
|
+
distance(self.x, self.y, object.x, object.y)
|
120
|
+
end
|
121
|
+
|
193
122
|
def draw
|
194
|
-
|
123
|
+
super
|
124
|
+
@image.draw_rot(@x, @y, @zorder, @angle, @center_x, @center_y, @factor_x, @factor_y, @color, @mode)
|
195
125
|
end
|
196
|
-
end
|
126
|
+
end
|
197
127
|
end
|
data/lib/chingu/game_state.rb
CHANGED
@@ -95,11 +95,12 @@ module Chingu
|
|
95
95
|
#
|
96
96
|
# Calls update on each game object that has current game state as parent (created inside that game state)
|
97
97
|
#
|
98
|
-
def update
|
98
|
+
def update
|
99
99
|
dispatch_input_for(self)
|
100
|
+
|
100
101
|
@input_clients.each { |game_object| dispatch_input_for(game_object) }
|
101
102
|
|
102
|
-
@game_objects.each { |object| object.update
|
103
|
+
@game_objects.each { |object| object.update }
|
103
104
|
end
|
104
105
|
|
105
106
|
#
|
@@ -108,7 +109,7 @@ module Chingu
|
|
108
109
|
def draw
|
109
110
|
@game_objects.each { |object| object.draw }
|
110
111
|
end
|
111
|
-
|
112
|
+
|
112
113
|
#
|
113
114
|
# Closes game state by poping it off the stack (and activating the game state below)
|
114
115
|
#
|
@@ -7,7 +7,7 @@ module Chingu
|
|
7
7
|
# http://www.gamedev.net/community/forums/topic.asp?topic_id=477320
|
8
8
|
#
|
9
9
|
# Chingu::Window automatically creates a @game_state_manager and makes it accessible in our game loop.
|
10
|
-
# By default the game loop calls update
|
10
|
+
# By default the game loop calls update, draw, button_up(id) and button_down(id) on the active state.
|
11
11
|
#
|
12
12
|
# ==== Chingu Examples
|
13
13
|
#
|
@@ -180,7 +180,6 @@ module Chingu
|
|
180
180
|
# Returns the previous game state. Shortcut: "previous"
|
181
181
|
#
|
182
182
|
def previous_game_state
|
183
|
-
##@game_states[@game_states.index(current_game_state)-1]
|
184
183
|
@previous_game_state
|
185
184
|
end
|
186
185
|
alias :previous previous_game_state
|
@@ -229,8 +228,8 @@ module Chingu
|
|
229
228
|
#
|
230
229
|
# If you're using Chingu::Window instead of Gosu::Window this will automaticly be called.
|
231
230
|
#
|
232
|
-
def update
|
233
|
-
current_game_state.update
|
231
|
+
def update
|
232
|
+
current_game_state.update if current_game_state
|
234
233
|
end
|
235
234
|
|
236
235
|
#
|
@@ -0,0 +1,43 @@
|
|
1
|
+
#
|
2
|
+
# Debug game state (F1 is default key to start/exit debug win, 'p' to pause game)
|
3
|
+
#
|
4
|
+
module Chingu
|
5
|
+
module GameStates
|
6
|
+
class Debug < Chingu::GameState
|
7
|
+
def initialize(options)
|
8
|
+
super
|
9
|
+
@white = Color.new(255,255,255,255)
|
10
|
+
@fade_color = Gosu::Color.new(100,255,255,255)
|
11
|
+
|
12
|
+
@font = Gosu::Font.new($window, default_font_name, 15)
|
13
|
+
@paused = true
|
14
|
+
|
15
|
+
self.input = {:p => :pause, :f1 => :return_to_game, :esc => :return_to_game}
|
16
|
+
end
|
17
|
+
|
18
|
+
def return_to_game
|
19
|
+
game_state_manager.pop_game_state
|
20
|
+
end
|
21
|
+
|
22
|
+
def pause
|
23
|
+
@paused = @paused ? false : true
|
24
|
+
end
|
25
|
+
|
26
|
+
def update
|
27
|
+
game_state_manager.previous_game_state.update unless @paused
|
28
|
+
end
|
29
|
+
|
30
|
+
def draw
|
31
|
+
game_state_manager.previous_game_state.draw
|
32
|
+
|
33
|
+
$window.draw_quad( 0,0,@fade_color,
|
34
|
+
$window.width,0,@fade_color,
|
35
|
+
$window.width,$window.height,@fade_color,
|
36
|
+
0,$window.height,@fade_color,10)
|
37
|
+
|
38
|
+
text = "DEBUG CONSOLE"
|
39
|
+
@font.draw(text, $window.width - @font.text_width(text), @font.height, 999)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -20,10 +20,10 @@ module Chingu
|
|
20
20
|
@color = Gosu::Color.new(0,0,0,0)
|
21
21
|
@alpha = 0.0
|
22
22
|
@fading_in = false
|
23
|
-
@new_game_state.update
|
23
|
+
@new_game_state.update # Make sure states game logic is run Once (for a correct draw())
|
24
24
|
end
|
25
25
|
|
26
|
-
def update
|
26
|
+
def update
|
27
27
|
@alpha += (@fading_in ? -@options[:speed] : @options[:speed])
|
28
28
|
if @alpha >= 255
|
29
29
|
@fading_in = true
|