ippa-chingu 0.4.2 → 0.4.4

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/chingu.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{chingu}
5
- s.version = "0.4.2"
5
+ s.version = "0.4.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["ippa"]
9
- s.date = %q{2009-08-20}
9
+ s.date = %q{2009-08-21}
10
10
  s.description = %q{Game framework built on top of the OpenGL accelerated game lib Gosu. It adds simple yet powerfull game states, prettier inputhandling, deploymentsafe asset-handling, a basic re-usable game object and automation of common task.}
11
11
  s.email = ["ippa@rubylicio.us"]
12
12
  s.extra_rdoc_files = ["History.txt", "Manifest.txt"]
data/examples/example2.rb CHANGED
@@ -31,7 +31,7 @@ class Game < Chingu::Window
31
31
  #
32
32
  def draw
33
33
  # Raw Gosu Image.draw(x,y,zorder)-call
34
- Image["background1.png"].draw(0, 0, 0)
34
+ Image["background1.png"].draw(0, 0, 0)
35
35
  super
36
36
  end
37
37
 
@@ -43,8 +43,8 @@ class Game < Chingu::Window
43
43
  def update
44
44
 
45
45
  ### Your own gamelogic here
46
-
47
46
  super
47
+ self.caption = "FPS: #{self.fps} milliseconds_since_last_tick: #{self.milliseconds_since_last_tick}"
48
48
  end
49
49
 
50
50
  end
data/examples/example5.rb CHANGED
@@ -15,19 +15,23 @@ class Game < Gosu::Window
15
15
  # Create our game state manager and start out with State1
16
16
  @manager = Chingu::GameStateManager.new
17
17
  @manager.switch_game_state(State1)
18
+
19
+ # Insert FadeTo state between every push, pop and switch
20
+ # @manager.transitional_game_state(Chingu::GameStates::FadeTo, :speed => 10)
18
21
  end
19
22
 
20
23
  def button_down(id)
24
+ # This makes sure button_down(id) is called on the active game state
25
+ # Enables input-handling in game states, you might wanna do the same with button_up()
26
+ @manager.button_down(id)
27
+
21
28
  @manager.push_game_state(State1) if(id==Button::Kb1)
22
29
  @manager.push_game_state(State2) if(id==Button::Kb2)
23
30
  @manager.push_game_state(State3) if(id==Button::Kb3)
31
+ @manager.push_game_state(Chingu::GameStates::Pause) if(id==Button::KbP)
24
32
  @manager.pop_game_state if(id==Button::KbBackspace)
25
- close if(id==Button::KbEscape)
26
-
27
- # This makes sure button_down(id) is called on the active game state
28
- # Enables input-handling in game states, you might wanna do the same with button_up()
29
- @manager.button_down(id)
30
- end
33
+ exit if(id==Button::KbEscape)
34
+ end
31
35
 
32
36
  def update
33
37
  # This makes sure update() is called on the active game state
@@ -61,7 +65,7 @@ class State1 < Chingu::GameState
61
65
  end
62
66
  end
63
67
 
64
- class State2 < Chingu::GameState
68
+ class State2 < Chingu::GameState
65
69
  def setup
66
70
  @factor = 0.0
67
71
  @ticks = 0.0
@@ -69,7 +73,7 @@ class State2 < Chingu::GameState
69
73
 
70
74
  def update(dt)
71
75
  @ticks += 0.01
72
- @factor = 1.5 + Math.sin(@ticks)
76
+ @factor = 1.5 + Math.sin(@ticks).to_f
73
77
  end
74
78
 
75
79
  def draw
@@ -86,7 +90,7 @@ class State3 < Chingu::GameState
86
90
 
87
91
  def update(dt)
88
92
  @ticks += 0.01
89
- @factor = 1.5 + Math.sin(@ticks)
93
+ @factor = 1.5 + Math.sin(@ticks).to_f
90
94
  end
91
95
  def draw
92
96
  $window.font.draw("Inside State3 - factor_x: #{@factor.to_s}", 100, 100, 0, @factor, 1.0)
data/examples/example6.rb CHANGED
@@ -5,21 +5,44 @@ include Gosu
5
5
  #
6
6
  # Example of using a special GameState to fade between game states
7
7
  #
8
+ # Using a simple game state, Chingu::GameStates::FadeTo, shipped with Chingu.
9
+ #
8
10
  class Game < Chingu::Window
9
11
  def initialize
10
12
  super(640,800)
11
13
  switch_game_state(State1)
12
- self.input = {:space => :next_game_state, :esc => :exit}
13
- self.caption = "Example of fading game state to switch between 2 other game states"
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)
14
17
  end
15
18
 
16
- def next_game_state
17
- push_game_state(FadeTo.new(State2.new)) if current_game_state.is_a?(State1)
18
- push_game_state(FadeTo.new(State1.new)) if current_game_state.is_a?(State2)
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
19
42
  end
20
43
  end
21
44
 
22
- class State1 < Chingu::GameState
45
+ class State1 < Chingu::GameState
23
46
  def draw
24
47
  Image["ruby.png"].draw(0,0,0)
25
48
  end
@@ -31,40 +54,43 @@ class State2 < Chingu::GameState
31
54
  end
32
55
  end
33
56
 
57
+ Game.new.show
34
58
 
35
- class FadeTo < Chingu::GameState
36
- def initialize(game_state)
37
- @new_game_state = game_state
38
- end
39
-
40
- def setup
41
- @color = Gosu::Color.new(0,0,0,0)
42
- @alpha = 0.0
43
- @fading_in = false
44
- end
45
-
46
- def update(dt)
47
- @alpha += (@fading_in ? -2 : 2)
48
- if @alpha >= 255
49
- @fading_in = true
50
- else
51
- @color.alpha = @alpha.to_i
52
- end
53
- end
54
-
55
- def draw
56
- previous_game_state.draw if @fading_in == false
57
- @new_game_state.draw if @fading_in == true
58
-
59
- $window.draw_quad( 0,0,@color,
60
- $window.width,0,@color,
61
- $window.width,$window.height,@color,
62
- 0,$window.height,@color,999)
63
-
64
- if @fading_in == true && @alpha == 0
65
- switch_game_state(@new_game_state)
66
- end
67
- end
68
- end
69
-
70
- Game.new.show
59
+ #
60
+ # The has now become premade game state shippet with Chingu.
61
+ # See chingu\game_states\fade_to.rb
62
+ #
63
+ #class FadeTo < Chingu::GameState
64
+ # def initialize(game_state)
65
+ # @new_game_state = game_state
66
+ # end
67
+ #
68
+ # def setup
69
+ # @color = Gosu::Color.new(0,0,0,0)
70
+ # @alpha = 0.0
71
+ # @fading_in = false
72
+ # end
73
+ #
74
+ # def update(dt)
75
+ # @alpha += (@fading_in ? -2 : 2)
76
+ # if @alpha >= 255
77
+ # @fading_in = true
78
+ # else
79
+ # @color.alpha = @alpha.to_i
80
+ # end
81
+ # end
82
+ #
83
+ # def draw
84
+ # previous_game_state.draw if @fading_in == false
85
+ # @new_game_state.draw if @fading_in == true
86
+ #
87
+ # $window.draw_quad( 0,0,@color,
88
+ # $window.width,0,@color,
89
+ # $window.width,$window.height,@color,
90
+ # 0,$window.height,@color,999)
91
+ #
92
+ # if @fading_in == true && @alpha == 0
93
+ # switch_game_state(@new_game_state)
94
+ # end
95
+ # end
96
+ #end
data/lib/chingu.rb CHANGED
@@ -25,6 +25,13 @@ require 'set'
25
25
  require File.join(root,"chingu",lib)
26
26
  end
27
27
 
28
+ %w{ pause
29
+ fade_to
30
+ }.each do |lib|
31
+ root ||= File.dirname(File.expand_path(__FILE__))
32
+ require File.join(root,"chingu","game_states",lib)
33
+ end
34
+
28
35
  module Chingu
29
- VERSION = "0.4.1"
36
+ VERSION = "0.4.3"
30
37
  end
@@ -94,8 +94,10 @@ module Chingu
94
94
  # A GameObject can either belong to a GameState or our mainwindow ($window)
95
95
  # .. or live in limbo with manual updates
96
96
  #
97
- @parent = $window.game_state_manager.inside_state || $window
98
- @parent.add_game_object(self) if @parent
97
+ if $window && $window.respond_to?(:game_state_manager)
98
+ @parent = $window.game_state_manager.inside_state || $window
99
+ @parent.add_game_object(self) if @parent
100
+ end
99
101
  end
100
102
 
101
103
  #
@@ -34,10 +34,11 @@ module Chingu
34
34
  include Chingu::InputClient
35
35
 
36
36
  attr_reader :options # so jlnr can access his :level-number
37
- attr_reader :game_objects
37
+ attr_accessor :game_state_manager
38
38
 
39
39
  def initialize(options = {})
40
40
  @options = options
41
+ ## @game_state_manager = options[:game_state_manager] || $window.game_state_manager
41
42
  @game_objects = Set.new
42
43
  @input_clients = Set.new # Set is like a unique Array with Hash lookupspeed
43
44
 
@@ -47,6 +48,11 @@ module Chingu
47
48
  end
48
49
  end
49
50
 
51
+ def game_objects
52
+ return [] unless defined?(@game_objects)
53
+ return @game_objects
54
+ end
55
+
50
56
  #
51
57
  # An unique identifier for the GameState-class,
52
58
  # Used in game state manager to keep track of created states.
@@ -25,6 +25,9 @@ module Chingu
25
25
  def initialize
26
26
  @inside_state = nil
27
27
  @game_states = []
28
+ @transitional_game_state = nil
29
+ @transitional_game_state_options = {}
30
+ @previous_game_state = nil
28
31
  end
29
32
 
30
33
  #
@@ -42,29 +45,66 @@ module Chingu
42
45
  @game_states.reverse
43
46
  end
44
47
 
48
+ #
49
+ # Sets a game state to be called between the old and the new game state
50
+ # whenever a game state is switched,pushed or popped.
51
+ #
52
+ # The transitional game state is responsible for switching to the "new game state".
53
+ # It should do so with ":transitional => false" not to create an infinite loop.
54
+ #
55
+ # The new game state is the first argument to the transitional game states initialize().
56
+ #
57
+ # Example:
58
+ # transitional_game_state(FadeIn)
59
+ # push_game_state(Level2)
60
+ #
61
+ # would in practice become:
62
+ #
63
+ # push_game_state(FadeIn.new(Level2))
64
+ #
65
+ # This would be the case for every game state change until the transitional game state is removed:
66
+ # transitional_game_state(nil) # or false
67
+ #
68
+ # Very useful for fading effect between scenes.
69
+ #
70
+ def transitional_game_state(game_state, options = {})
71
+ @transitional_game_state = game_state
72
+ @transitional_game_state_options = options
73
+ end
74
+
45
75
  #
46
76
  # Switch to a given game state, _replacing_ the current active one.
47
77
  # By default setup() is called on the game state we're switching _to_.
48
78
  # .. and finalize() is called on the game state we're switching _from_.
49
79
  #
50
80
  def switch_game_state(state, options = {})
51
- options = {:setup => true, :finalize => true}.merge(options)
81
+ options = {:setup => true, :finalize => true, :transitional => true}.merge(options)
52
82
 
53
83
  new_state = game_state_instance(state)
54
84
 
55
85
  if new_state
86
+ # Make sure the game state knows about the manager
87
+ new_state.game_state_manager = self
88
+
89
+
56
90
  # Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
91
+ @previous_game_state = current_game_state
57
92
  current_game_state.finalize if current_game_state.respond_to?(:finalize) && options[:finalize]
58
93
 
59
94
  # Call setup
60
95
  new_state.setup if new_state.respond_to?(:setup) && options[:setup]
61
96
 
62
-
63
- if current_game_state.nil?
64
- @game_states << new_state
97
+ if @transitional_game_state && options[:transitional]
98
+ # If we have a transitional, switch to that instead, with new_state as first argument
99
+ transitional_game_state = @transitional_game_state.new(new_state, @transitional_game_state_options)
100
+ self.switch_game_state(transitional_game_state, :transitional => false)
65
101
  else
66
- # Replace last (active) state with new one
67
- @game_states[-1] = new_state
102
+ if current_game_state.nil?
103
+ @game_states << new_state
104
+ else
105
+ # Replace last (active) state with new one
106
+ @game_states[-1] = new_state
107
+ end
68
108
  end
69
109
  end
70
110
  end
@@ -76,19 +116,29 @@ module Chingu
76
116
  # .. and finalize() is called on the game state we're leaving.
77
117
  #
78
118
  def push_game_state(state, options = {})
79
- options = {:setup => true, :finalize => true}.merge(options)
119
+ options = {:setup => true, :finalize => true, :transitional => true}.merge(options)
80
120
 
81
121
  new_state = game_state_instance(state)
82
-
122
+
83
123
  if new_state
84
- # Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
85
- current_game_state.finalize if current_game_state.respond_to?(:finalize) && options[:finalize]
86
-
87
124
  # Call setup
88
125
  new_state.setup if new_state.respond_to?(:setup) && options[:setup]
89
126
 
90
- # Push new state on top of stack and therefore making it active
91
- @game_states.push(new_state)
127
+ # Make sure the game state knows about the manager
128
+ new_state.game_state_manager = self
129
+
130
+ # Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
131
+ @previous_game_state = current_game_state
132
+ current_game_state.finalize if current_game_state.respond_to?(:finalize) && options[:finalize]
133
+
134
+ if @transitional_game_state && options[:transitional]
135
+ # If we have a transitional, push that instead, with new_state as first argument
136
+ transitional_game_state = @transitional_game_state.new(new_state, @transitional_game_state_options)
137
+ self.push_game_state(transitional_game_state, :transitional => false)
138
+ else
139
+ # Push new state on top of stack and therefore making it active
140
+ @game_states.push(new_state)
141
+ end
92
142
  end
93
143
  end
94
144
  alias :push :push_game_state
@@ -99,20 +149,27 @@ module Chingu
99
149
  # .. and finalize() is called on the game state we're leaving.
100
150
  #
101
151
  def pop_game_state(options = {})
102
- options = {:setup => true, :finalize => true}.merge(options)
152
+ options = {:setup => true, :finalize => true, :transitional => true}.merge(options)
103
153
 
104
154
  #
105
155
  # Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
106
156
  #
157
+ @previous_game_state = current_game_state
107
158
  current_game_state.finalize if current_game_state.respond_to?(:finalize) && options[:finalize]
108
-
159
+
109
160
  #
110
161
  # Activate the game state "bellow" current one with a simple Array.pop
111
162
  #
112
163
  @game_states.pop
113
-
164
+
114
165
  # Call setup on the new current state
115
166
  current_game_state.setup if current_game_state.respond_to?(:setup) && options[:setup]
167
+
168
+ if @transitional_game_state && options[:transitional]
169
+ # If we have a transitional, push that instead, with new_state as first argument
170
+ transitional_game_state = @transitional_game_state.new(current_game_state, @transitional_game_state_options)
171
+ self.switch_game_state(transitional_game_state, :transitional => false)
172
+ end
116
173
  end
117
174
  alias :pop :pop_game_state
118
175
 
@@ -120,7 +177,8 @@ module Chingu
120
177
  # Returns the previous game state. Shortcut: "previous"
121
178
  #
122
179
  def previous_game_state
123
- @game_states[@game_states.index(current_game_state)-1]
180
+ ##@game_states[@game_states.index(current_game_state)-1]
181
+ @previous_game_state
124
182
  end
125
183
  alias :previous previous_game_state
126
184
 
@@ -85,13 +85,7 @@ module Chingu
85
85
  #
86
86
  # push_game_state accepts either a class inherited from GameState or an object-instance from such a class.
87
87
  #
88
- # push_game_state(Intro):
89
- # game state mananger will create a new Intro-object first time called and cache it.
90
- #
91
- # push_game_state(Intro.new):
92
- # The first line ends up calling "new" to Intro before activating the newly created game state.
93
- # Each time 'push_game_state(Intro.new)' is called a new Intro-object will be created.
94
- # Usefull for stuff like: push_game_state(Level.new(:level_nr => 11))
88
+ # It will make call new() on a class, and just push an object.
95
89
  #
96
90
  module GameStateHelpers
97
91
  def push_game_state(state, options = {})
@@ -106,6 +100,10 @@ module Chingu
106
100
  $window.game_state_manager.switch_game_state(state, options)
107
101
  end
108
102
 
103
+ def transitional_game_state(state, options = {})
104
+ $window.game_state_manager.transitional_game_state(state, options)
105
+ end
106
+
109
107
  def current_game_state
110
108
  $window.game_state_manager.current_game_state
111
109
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ippa-chingu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - ippa
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-20 00:00:00 -07:00
12
+ date: 2009-08-21 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency