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.
@@ -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
- class Color
8
- def average(other, weight)
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
@@ -8,7 +8,7 @@ module Chingu
8
8
  @trail = options[:trail] || 10
9
9
  end
10
10
 
11
- def update(time)
11
+ def update
12
12
  end
13
13
  end
14
14
  end
@@ -31,7 +31,6 @@ module Chingu
31
31
 
32
32
  #
33
33
  # Calculate how many milliseconds passed since last game loop iteration.
34
- # useful in update()-calls
35
34
  #
36
35
  @milliseconds_since_last_tick = Gosu::milliseconds - @last_value
37
36
  @last_value = Gosu::milliseconds
@@ -1,197 +1,127 @@
1
1
  module Chingu
2
2
  #
3
- # A basic class, all your ingame objects should be built on this. Encapsulates
4
- # Gosus draw_rot and it's parameters.
3
+ # GameObject is our BasisGameObject (class with framespecific stuff)
5
4
  #
6
- # All objects that inherits from this class will automaticly be updated and drawn.
5
+ # On top of that, it encapsulates GOSUs Image#draw_rot and all its parameters.
7
6
  #
8
- class GameObject
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
- # Class-level default values.
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
- @@x = nil
23
- @@y = nil
24
- @@zorder = 100
25
- @@center_x = 0.5
26
- @@center_y = 0.5
27
- @@factor_x = 1.0
28
- @@factor_y = 1.0
29
-
30
- def self.x; @@x; end
31
- def self.x=(value); @@x = value; end
32
-
33
- def self.y; @@y; end
34
- def self.y=(value); @@y = value; end
35
-
36
- def self.zorder; @@zorder; end
37
- def self.zorder=(value); @@zorder = value; end
38
-
39
- def self.center_x; @@center_x; end
40
- def self.center_x=(value); @@center_x = value; end
41
-
42
- def self.center_y; @@center_y; end
43
- def self.center_y=(value); @@center_y = value; end
44
-
45
- def self.factor_x; @@factor_x; end
46
- def self.factor_x=(value); @@factor_x = value; end
47
-
48
- def self.factor_y; @@factor_y; end
49
- def self.factor_y=(value); @@factor_y = value; end
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
- # Create a new GameObject. Arguments are given in hash-format:
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
- # :update [true|false] Automaticly call #update on object each gameloop. Default +true+.
65
- # :draw [true|false] Automaticly call #update on object each gameloop. Default +true+.
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
- @options = options
69
-
70
- # draw_rot arguments
71
- @image = options[:image] if options[:image].is_a? Gosu::Image
72
- @image = Image[options[:image]] if options[:image].is_a? String
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] || @@x || 0
75
- @y = options[:y] || @@y || 0
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
- # Shortcuts for draw_rot arguments
89
- @factor = 1
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
- # gameloop/framework logic
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
- # The core of the gameclass, the draw_rot encapsulation. Draws the sprite on screen.
191
- # Calling #to_i on @x and @y enables thoose to be Float's, for subpixel slow movement in #update
192
- #
118
+ def distance_to(object)
119
+ distance(self.x, self.y, object.x, object.y)
120
+ end
121
+
193
122
  def draw
194
- @image.draw_rot(@x.to_i, @y.to_i, @zorder, @angle, @center_x, @center_y, @factor_x, @factor_y, @color, @mode)
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
@@ -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(time = 1)
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(time) }
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(dt), draw, button_up(id) and button_down(id) on the active state.
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(time = nil)
233
- current_game_state.update(time) if current_game_state
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(0) # Make sure states game logic is run Once (for a correct draw())
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(dt)
26
+ def update
27
27
  @alpha += (@fading_in ? -@options[:speed] : @options[:speed])
28
28
  if @alpha >= 255
29
29
  @fading_in = true
@@ -12,6 +12,8 @@ module Chingu
12
12
  end
13
13
 
14
14
  module InputDispatcher
15
+ attr_reader :input_clients
16
+
15
17
  def add_input_client(object)
16
18
  @input_clients << object
17
19
  end