Browse files

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...
1 parent 039c507 commit 2c10db0952e06043c671db51cfbf10f24a3c94cd @ippa committed Sep 1, 2009
View
80 examples/example2.rb
@@ -6,6 +6,8 @@
# A little more complicated example where we do our own #update and #draw code.
# We also add another Actor - a bullet fired with space.
#
+# Also tests out the Debug game state.
+#
class Game < Chingu::Window
def initialize
#
@@ -14,39 +16,8 @@ def initialize
#
super
- @player = Player.new(:x => 200, :y => 200, :image => Image["spaceship.png"])
- @player.input = { :holding_left => :move_left,
- :holding_right => :move_right,
- :holding_up => :move_up,
- :holding_down => :move_down,
- :space => :fire,
- :escape => :exit
- }
+ push_game_state(Play.new)
end
-
- #
- # If we want to add extra graphics drawn just define your own draw.
- # Be sure to call #super for enabling Chingus autodrawing of Actors.
- # Putting #super before or after the background-draw-call really doesn't matter since Gosu work with "zorder".
- #
- def draw
- # Raw Gosu Image.draw(x,y,zorder)-call
- Image["background1.png"].draw(0, 0, 0)
- super
- end
-
- #
- # Gosus place for gamelogic is #update in the mainwindow
- #
- # A #super call here would call #update on all Chingu::Actors and check their inputs, and call the specified method.
- #
- def update
-
- ### Your own gamelogic here
- super
- self.caption = "FPS: #{self.fps} milliseconds_since_last_tick: #{self.milliseconds_since_last_tick}"
- end
-
end
#
@@ -78,10 +49,53 @@ def initialize(options)
end
# Move the bullet forward
- def update(time)
+ def update
@y -= 2
end
end
+class Play < Chingu::GameState
+ def initialize
+ super
+
+ @player = Player.new(:x => 200, :y => 200, :image => Image["spaceship.png"])
+ @player.input = { :holding_left => :move_left,
+ :holding_right => :move_right,
+ :holding_up => :move_up,
+ :holding_down => :move_down,
+ :space => :fire,
+ :escape => :exit,
+ }
+ self.input = { :f1 => :debug }
+ end
+
+ def debug
+ push_game_state(Chingu::GameStates::Debug)
+ end
+
+ #
+ # If we want to add extra graphics drawn just define your own draw.
+ # Be sure to call #super for enabling Chingus autodrawing of Actors.
+ # Putting #super before or after the background-draw-call really doesn't matter since Gosu work with "zorder".
+ #
+ def draw
+ # Raw Gosu Image.draw(x,y,zorder)-call
+ Image["background1.png"].draw(0, 0, 0)
+ super
+ end
+
+ #
+ # Gosus place for gamelogic is #update in the mainwindow
+ #
+ # A #super call here would call #update on all Chingu::Actors and check their inputs, and call the specified method.
+ #
+ def update
+
+ ### Your own gamelogic here
+ super
+ $window.caption = "FPS: #{$window.fps} milliseconds_since_last_tick: #{$window.milliseconds_since_last_tick}"
+ end
+end
+
Game.new.show
View
2 examples/example4.rb
@@ -62,7 +62,7 @@ def initialize(options)
@image = Image["fire_bullet.png"]
end
- def update(time)
+ def update
@y -= 2
end
end
View
6 examples/example5.rb
@@ -55,7 +55,7 @@ def setup
@spinner_index = 0.0
end
- def update(dt)
+ def update
@spinner_index += 0.1
@spinner_index = 0 if @spinner_index >= @spinner.size
end
@@ -71,7 +71,7 @@ def setup
@ticks = 0.0
end
- def update(dt)
+ def update
@ticks += 0.01
@factor = 1.5 + Math.sin(@ticks).to_f
end
@@ -88,7 +88,7 @@ def setup
@ticks = 0.0
end
- def update(dt)
+ def update
@ticks += 0.01
@factor = 1.5 + Math.sin(@ticks).to_f
end
View
41 examples/example6.rb
@@ -54,43 +54,4 @@ def draw
end
end
-Game.new.show
-
-#
-# The has now become premade game state shippet with Chingu.
-# See chingu\game_states\fade_to.rb
-#
-#class FadeTo < Chingu::GameState
-# def initialize(game_state)
-# @new_game_state = game_state
-# end
-#
-# def setup
-# @color = Gosu::Color.new(0,0,0,0)
-# @alpha = 0.0
-# @fading_in = false
-# end
-#
-# def update(dt)
-# @alpha += (@fading_in ? -2 : 2)
-# if @alpha >= 255
-# @fading_in = true
-# else
-# @color.alpha = @alpha.to_i
-# end
-# end
-#
-# def draw
-# previous_game_state.draw if @fading_in == false
-# @new_game_state.draw if @fading_in == true
-#
-# $window.draw_quad( 0,0,@color,
-# $window.width,0,@color,
-# $window.width,$window.height,@color,
-# 0,$window.height,@color,999)
-#
-# if @fading_in == true && @alpha == 0
-# switch_game_state(@new_game_state)
-# end
-# end
-#end
+Game.new.show
View
2 examples/example7.rb
@@ -82,7 +82,7 @@ def setup
@ground_y = $window.height * 0.95
end
- def update(time)
+ def update
#
# Fire 1. Dies quickly (big :fade). Small in size (small :zoom)
#
View
7 lib/chingu.rb
@@ -1,7 +1,9 @@
#
#
#
-require 'rubygems'
+unless RUBY_VERSION =~ /1\.9/
+ require 'rubygems'
+end
require 'gosu'
require 'set'
@@ -28,11 +30,12 @@
%w{ pause
fade_to
+ debug
}.each do |lib|
root ||= File.dirname(File.expand_path(__FILE__))
require File.join(root,"chingu","game_states",lib)
end
module Chingu
- VERSION = "0.4.8"
+ VERSION = "0.4.9"
end
View
2 lib/chingu/effects.rb
@@ -8,7 +8,7 @@ def initialize(options)
@trail = options[:trail] || 10
end
- def update(time)
+ def update
end
end
end
View
1 lib/chingu/fpscounter.rb
@@ -31,7 +31,6 @@ def register_tick
#
# Calculate how many milliseconds passed since last game loop iteration.
- # useful in update()-calls
#
@milliseconds_since_last_tick = Gosu::milliseconds - @last_value
@last_value = Gosu::milliseconds
View
2 lib/chingu/game_object.rb
@@ -182,7 +182,7 @@ def outside_window?(x = @x, y = @y)
not inside_window?(x,y)
end
- def update(time = 0)
+ def update
# Objects gamelogic here, 'time' is the time passed between 2 iterations of the main game loop
end
View
7 lib/chingu/game_state.rb
@@ -95,11 +95,12 @@ def button_up(id)
#
# Calls update on each game object that has current game state as parent (created inside that game state)
#
- def update(time = 1)
+ def update
dispatch_input_for(self)
+
@input_clients.each { |game_object| dispatch_input_for(game_object) }
- @game_objects.each { |object| object.update(time) }
+ @game_objects.each { |object| object.update }
end
#
@@ -108,7 +109,7 @@ def update(time = 1)
def draw
@game_objects.each { |object| object.draw }
end
-
+
#
# Closes game state by poping it off the stack (and activating the game state below)
#
View
7 lib/chingu/game_state_manager.rb
@@ -7,7 +7,7 @@ module Chingu
# http://www.gamedev.net/community/forums/topic.asp?topic_id=477320
#
# Chingu::Window automatically creates a @game_state_manager and makes it accessible in our game loop.
- # By default the game loop calls update(dt), draw, button_up(id) and button_down(id) on the active state.
+ # By default the game loop calls update, draw, button_up(id) and button_down(id) on the active state.
#
# ==== Chingu Examples
#
@@ -180,7 +180,6 @@ def pop_game_state(options = {})
# Returns the previous game state. Shortcut: "previous"
#
def previous_game_state
- ##@game_states[@game_states.index(current_game_state)-1]
@previous_game_state
end
alias :previous previous_game_state
@@ -229,8 +228,8 @@ def button_up(id)
#
# If you're using Chingu::Window instead of Gosu::Window this will automaticly be called.
#
- def update(time = nil)
- current_game_state.update(time) if current_game_state
+ def update
+ current_game_state.update if current_game_state
end
#
View
43 lib/chingu/game_states/debug.rb
@@ -0,0 +1,43 @@
+#
+# Debug game state (F1 is default key to start/exit debug win, 'p' to pause game)
+#
+module Chingu
+ module GameStates
+ class Debug < Chingu::GameState
+ def initialize(options)
+ super
+ @white = Color.new(255,255,255,255)
+ @fade_color = Gosu::Color.new(100,255,255,255)
+
+ @font = Gosu::Font.new($window, default_font_name, 15)
+ @paused = true
+
+ self.input = {:p => :pause, :f1 => :return_to_game, :esc => :return_to_game}
+ end
+
+ def return_to_game
+ game_state_manager.pop_game_state
+ end
+
+ def pause
+ @paused = @paused ? false : true
+ end
+
+ def update
+ game_state_manager.previous_game_state.update unless @paused
+ end
+
+ def draw
+ game_state_manager.previous_game_state.draw
+
+ $window.draw_quad( 0,0,@fade_color,
+ $window.width,0,@fade_color,
+ $window.width,$window.height,@fade_color,
+ 0,$window.height,@fade_color,10)
+
+ text = "DEBUG CONSOLE"
+ @font.draw(text, $window.width - @font.text_width(text), @font.height, 999)
+ end
+ end
+ end
+end
View
2 lib/chingu/game_states/fade_to.rb
@@ -23,7 +23,7 @@ def setup
@new_game_state.update(0) # Make sure states game logic is run Once (for a correct draw())
end
- def update(dt)
+ def update
@alpha += (@fading_in ? -@options[:speed] : @options[:speed])
if @alpha >= 255
@fading_in = true
View
4 lib/chingu/parallax.rb
@@ -30,9 +30,9 @@ def <<(arg)
end
#
- # TODO: make use of time here!
+ # TODO: make use of $window.milliseconds_since_last_update here!
#
- def update(time)
+ def update
@backgrounds.each do |background|
background.x = -@x / background.damping
background.y = @y / background.damping
View
2 lib/chingu/particle.rb
@@ -12,7 +12,7 @@ def initialize(options)
@animation = options[:animation] || nil
end
- def update(time)
+ def update
self.image = @animation.next! if @animation
self.rotate(@rotation)
self.zoom(@zoom)
View
20 lib/chingu/window.rb
@@ -67,6 +67,13 @@ def fps
def ticks
@fps_counter.ticks
end
+
+ #
+ # Mathematical short name for "milliseconds since last tick"
+ #
+ def dt
+ @milliseconds_since_last_tick
+ end
#
# Chingus core-logic / loop. Gosu will call this each game-iteration.
@@ -78,8 +85,15 @@ def update
# Without this self.fps would return an incorrect value.
# If you override this in your Chingu::Window class, make sure to call super.
#
- @milliseconds_since_last_tick = @fps_counter.register_tick
+ @milliseconds_since_last_tick = @fps_counter.register_tick
+ intermediate_update
+ end
+
+ #
+ # "game logic" update that is safe to call even between Gosus update-calls
+ #
+ def intermediate_update
#
# Dispatch inputmap for main window
#
@@ -123,7 +137,7 @@ def draw
# Call update() on all game objects in main game window.
#
def update_game_objects
- @game_objects.each { |object| object.update(@milliseconds_since_last_tick) }
+ @game_objects.each { |object| object.update }
end
#
@@ -132,7 +146,7 @@ def update_game_objects
# -> call update on all game objects in that state
#
def update_game_state_manager
- @game_state_manager.update(@milliseconds_since_last_tick)
+ @game_state_manager.update
end
#

0 comments on commit 2c10db0

Please sign in to comment.