Skip to content
This repository
Browse code

All automatic update-calls are now update() and Not update(time) as b…

…efore, was just confusing anyhow. Use $window.dt or $window.milliseconds_since_last_update if you need the value. Started on a debug game state.
  • Loading branch information...
commit 2c10db0952e06043c671db51cfbf10f24a3c94cd 1 parent 039c507
ippa authored
80 examples/example2.rb
@@ -6,6 +6,8 @@
6 6 # A little more complicated example where we do our own #update and #draw code.
7 7 # We also add another Actor - a bullet fired with space.
8 8 #
  9 +# Also tests out the Debug game state.
  10 +#
9 11 class Game < Chingu::Window
10 12 def initialize
11 13 #
@@ -14,39 +16,8 @@ def initialize
14 16 #
15 17 super
16 18
17   - @player = Player.new(:x => 200, :y => 200, :image => Image["spaceship.png"])
18   - @player.input = { :holding_left => :move_left,
19   - :holding_right => :move_right,
20   - :holding_up => :move_up,
21   - :holding_down => :move_down,
22   - :space => :fire,
23   - :escape => :exit
24   - }
  19 + push_game_state(Play.new)
25 20 end
26   -
27   - #
28   - # If we want to add extra graphics drawn just define your own draw.
29   - # Be sure to call #super for enabling Chingus autodrawing of Actors.
30   - # Putting #super before or after the background-draw-call really doesn't matter since Gosu work with "zorder".
31   - #
32   - def draw
33   - # Raw Gosu Image.draw(x,y,zorder)-call
34   - Image["background1.png"].draw(0, 0, 0)
35   - super
36   - end
37   -
38   - #
39   - # Gosus place for gamelogic is #update in the mainwindow
40   - #
41   - # A #super call here would call #update on all Chingu::Actors and check their inputs, and call the specified method.
42   - #
43   - def update
44   -
45   - ### Your own gamelogic here
46   - super
47   - self.caption = "FPS: #{self.fps} milliseconds_since_last_tick: #{self.milliseconds_since_last_tick}"
48   - end
49   -
50 21 end
51 22
52 23 #
@@ -78,10 +49,53 @@ def initialize(options)
78 49 end
79 50
80 51 # Move the bullet forward
81   - def update(time)
  52 + def update
82 53 @y -= 2
83 54 end
84 55
85 56 end
86 57
  58 +class Play < Chingu::GameState
  59 + def initialize
  60 + super
  61 +
  62 + @player = Player.new(:x => 200, :y => 200, :image => Image["spaceship.png"])
  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 + push_game_state(Chingu::GameStates::Debug)
  75 + end
  76 +
  77 + #
  78 + # If we want to add extra graphics drawn just define your own draw.
  79 + # Be sure to call #super for enabling Chingus autodrawing of Actors.
  80 + # Putting #super before or after the background-draw-call really doesn't matter since Gosu work with "zorder".
  81 + #
  82 + def draw
  83 + # Raw Gosu Image.draw(x,y,zorder)-call
  84 + Image["background1.png"].draw(0, 0, 0)
  85 + super
  86 + end
  87 +
  88 + #
  89 + # Gosus place for gamelogic is #update in the mainwindow
  90 + #
  91 + # A #super call here would call #update on all Chingu::Actors and check their inputs, and call the specified method.
  92 + #
  93 + def update
  94 +
  95 + ### Your own gamelogic here
  96 + super
  97 + $window.caption = "FPS: #{$window.fps} milliseconds_since_last_tick: #{$window.milliseconds_since_last_tick}"
  98 + end
  99 +end
  100 +
87 101 Game.new.show
2  examples/example4.rb
@@ -62,7 +62,7 @@ def initialize(options)
62 62 @image = Image["fire_bullet.png"]
63 63 end
64 64
65   - def update(time)
  65 + def update
66 66 @y -= 2
67 67 end
68 68 end
6 examples/example5.rb
@@ -55,7 +55,7 @@ def setup
55 55 @spinner_index = 0.0
56 56 end
57 57
58   - def update(dt)
  58 + def update
59 59 @spinner_index += 0.1
60 60 @spinner_index = 0 if @spinner_index >= @spinner.size
61 61 end
@@ -71,7 +71,7 @@ def setup
71 71 @ticks = 0.0
72 72 end
73 73
74   - def update(dt)
  74 + def update
75 75 @ticks += 0.01
76 76 @factor = 1.5 + Math.sin(@ticks).to_f
77 77 end
@@ -88,7 +88,7 @@ def setup
88 88 @ticks = 0.0
89 89 end
90 90
91   - def update(dt)
  91 + def update
92 92 @ticks += 0.01
93 93 @factor = 1.5 + Math.sin(@ticks).to_f
94 94 end
41 examples/example6.rb
@@ -54,43 +54,4 @@ def draw
54 54 end
55 55 end
56 56
57   -Game.new.show
58   -
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
  57 +Game.new.show
2  examples/example7.rb
@@ -82,7 +82,7 @@ def setup
82 82 @ground_y = $window.height * 0.95
83 83 end
84 84
85   - def update(time)
  85 + def update
86 86 #
87 87 # Fire 1. Dies quickly (big :fade). Small in size (small :zoom)
88 88 #
7 lib/chingu.rb
... ... @@ -1,7 +1,9 @@
1 1 #
2 2 #
3 3 #
4   -require 'rubygems'
  4 +unless RUBY_VERSION =~ /1\.9/
  5 + require 'rubygems'
  6 +end
5 7 require 'gosu'
6 8 require 'set'
7 9
@@ -28,11 +30,12 @@
28 30
29 31 %w{ pause
30 32 fade_to
  33 + debug
31 34 }.each do |lib|
32 35 root ||= File.dirname(File.expand_path(__FILE__))
33 36 require File.join(root,"chingu","game_states",lib)
34 37 end
35 38
36 39 module Chingu
37   - VERSION = "0.4.8"
  40 + VERSION = "0.4.9"
38 41 end
2  lib/chingu/effects.rb
@@ -8,7 +8,7 @@ def initialize(options)
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
1  lib/chingu/fpscounter.rb
@@ -31,7 +31,6 @@ def register_tick
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
2  lib/chingu/game_object.rb
@@ -182,7 +182,7 @@ def outside_window?(x = @x, y = @y)
182 182 not inside_window?(x,y)
183 183 end
184 184
185   - def update(time = 0)
  185 + def update
186 186 # Objects gamelogic here, 'time' is the time passed between 2 iterations of the main game loop
187 187 end
188 188
7 lib/chingu/game_state.rb
@@ -95,11 +95,12 @@ def button_up(id)
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 @@ def update(time = 1)
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 lib/chingu/game_state_manager.rb
@@ -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 @@ def pop_game_state(options = {})
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 @@ def button_up(id)
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 #
43 lib/chingu/game_states/debug.rb
... ... @@ -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
2  lib/chingu/game_states/fade_to.rb
@@ -23,7 +23,7 @@ def setup
23 23 @new_game_state.update(0) # 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
4 lib/chingu/parallax.rb
@@ -30,9 +30,9 @@ def <<(arg)
30 30 end
31 31
32 32 #
33   - # TODO: make use of time here!
  33 + # TODO: make use of $window.milliseconds_since_last_update here!
34 34 #
35   - def update(time)
  35 + def update
36 36 @backgrounds.each do |background|
37 37 background.x = -@x / background.damping
38 38 background.y = @y / background.damping
2  lib/chingu/particle.rb
@@ -12,7 +12,7 @@ def initialize(options)
12 12 @animation = options[:animation] || nil
13 13 end
14 14
15   - def update(time)
  15 + def update
16 16 self.image = @animation.next! if @animation
17 17 self.rotate(@rotation)
18 18 self.zoom(@zoom)
20 lib/chingu/window.rb
@@ -67,6 +67,13 @@ def fps
67 67 def ticks
68 68 @fps_counter.ticks
69 69 end
  70 +
  71 + #
  72 + # Mathematical short name for "milliseconds since last tick"
  73 + #
  74 + def dt
  75 + @milliseconds_since_last_tick
  76 + end
70 77
71 78 #
72 79 # Chingus core-logic / loop. Gosu will call this each game-iteration.
@@ -78,8 +85,15 @@ def update
78 85 # Without this self.fps would return an incorrect value.
79 86 # If you override this in your Chingu::Window class, make sure to call super.
80 87 #
81   - @milliseconds_since_last_tick = @fps_counter.register_tick
  88 + @milliseconds_since_last_tick = @fps_counter.register_tick
82 89
  90 + intermediate_update
  91 + end
  92 +
  93 + #
  94 + # "game logic" update that is safe to call even between Gosus update-calls
  95 + #
  96 + def intermediate_update
83 97 #
84 98 # Dispatch inputmap for main window
85 99 #
@@ -123,7 +137,7 @@ def draw
123 137 # Call update() on all game objects in main game window.
124 138 #
125 139 def update_game_objects
126   - @game_objects.each { |object| object.update(@milliseconds_since_last_tick) }
  140 + @game_objects.each { |object| object.update }
127 141 end
128 142
129 143 #
@@ -132,7 +146,7 @@ def update_game_objects
132 146 # -> call update on all game objects in that state
133 147 #
134 148 def update_game_state_manager
135   - @game_state_manager.update(@milliseconds_since_last_tick)
  149 + @game_state_manager.update
136 150 end
137 151
138 152 #

0 comments on commit 2c10db0

Please sign in to comment.
Something went wrong with that request. Please try again.