Skip to content

Commit

Permalink
remove some game state cache magic, internal rename in game state man…
Browse files Browse the repository at this point in the history
…ager of state to game_state. more tab-fixes.
  • Loading branch information
ippa committed Aug 19, 2009
1 parent 5f64bfa commit 31b2b88
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 85 deletions.
4 changes: 1 addition & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ You can activate the above game state in 2 ways

#
# 2) This leaves the actual object-creation to the game state manager.
# This results in only 1 object is ever created of class 'Intro'.
# The second time 'push_game_state(Intro)' is called, it will re-use the last one.
# This means code in Intro#initialize() is only called once, Intro#setup() is called everytime Intro is activated though.
# Intro#initialize() is called, then Intro#setup()
#
push_game_state(Intro)
end
Expand Down
87 changes: 43 additions & 44 deletions lib/chingu/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,54 @@
# Quick 'n easy access to sprites, sounds and tiles!
#
module Chingu

def media_path(file)
File.join($window.root, "media", file)
end

def image_path(file)
File.join($window.root, "gfx", file)
end

class ImagePath
include Chingu::NamedResource
def media_path(file)
File.join($window.root, "media", file)
end

def image_path(file)
File.join($window.root, "gfx", file)
end

class ImagePath
include Chingu::NamedResource

def self.autoload(name)
find_file(name)
end
end
def self.autoload(name)
find_file(name)
end
end
end

module Gosu
class Image
include Chingu::NamedResource
def self.autoload(name)
(path = find_file(name)) ? Gosu::Image.new($window, path, true) : nil
end
end
class Image
include Chingu::NamedResource
def self.autoload(name)
(path = find_file(name)) ? Gosu::Image.new($window, path, true) : nil
end
end

class Song
include Chingu::NamedResource

def self.autoload(name)
(path = find_file(name)) ? Gosu::Song.new($window, path) : nil
end
end

class Sample
include Chingu::NamedResource
def self.autoload(name)
(path = find_file(name)) ? Gosu::Sample.new($window, path) : nil
end
end
Sound = Sample
class Song
include Chingu::NamedResource
def self.autoload(name)
(path = find_file(name)) ? Gosu::Song.new($window, path) : nil
end
end
class Sample
include Chingu::NamedResource
def self.autoload(name)
(path = find_file(name)) ? Gosu::Sample.new($window, path) : nil
end
end
Sound = Sample

class Tile
include Chingu::NamedResource
class Tile
include Chingu::NamedResource

def self.autoload(name)
(path = find_file(name)) ? Gosu::Image.load_tiles($window, path, 32, 32, true) : nil
end
end
def self.autoload(name)
(path = find_file(name)) ? Gosu::Image.load_tiles($window, path, 32, 32, true) : nil
end
end
end
56 changes: 25 additions & 31 deletions lib/chingu/game_state_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@ module Chingu
#
class GameStateManager
attr_accessor :inside_state
attr_reader :states, :created_states
attr_reader :states

def initialize
@inside_state = nil
@states = []
@created_states = {}
end

#
# Gets the currently active gamestate (top of stack)
#
def current_state
def current_game_state
@states.last
end
alias :current current_game_state

#
# Switch to a given game state, _replacing_ the current active one.
#
def switch_state(state, options = {})
def switch_game_state(state, options = {})
options = {:setup => true, :finalize => true}.merge(options)

new_state = object_to_state(state)
new_state = game_state_instance(state)

if new_state
# Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
current_state.finalize if current_state.respond_to?(:finalize) && options[:finalize]
current_game_state.finalize if current_game_state.respond_to?(:finalize) && options[:finalize]

# Call setup
new_state.setup if new_state.respond_to?(:setup) && options[:setup]
new_state.setup if new_state.respond_to?(:setup) && options[:setup]

# Replace last (active) state with new one
@states[-1] = new_state
Expand All @@ -47,17 +47,17 @@ def switch_state(state, options = {})
#
# Adds a state to the game state-stack and activates it
#
def push_state(state, options = {})
def push_game_state(state, options = {})
options = {:setup => true, :finalize => true}.merge(options)

new_state = object_to_state(state)
new_state = game_state_instance(state)

if new_state
# Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
current_state.finalize if current_state.respond_to?(:finalize) && options[:finalize]
current_game_state.finalize if current_game_state.respond_to?(:finalize) && options[:finalize]

# Call setup
new_state.setup if new_state.respond_to?(:setup) && options[:setup]
new_state.setup if new_state.respond_to?(:setup) && options[:setup]

# Push new state on top of stack and therefore making it active
@states.push(new_state)
Expand All @@ -67,44 +67,38 @@ def push_state(state, options = {})
#
# Pops a state off the game state-stack, activating the previous one.
#
def pop_state(options = {})
def pop_game_state(options = {})
options = {:setup => true, :finalize => true}.merge(options)

#
# Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
#
current_state.finalize if current_state.respond_to?(:finalize) && options[:finalize]
current_game_state.finalize if current_game_state.respond_to?(:finalize) && options[:finalize]

#
# Activate the game state "bellow" current one with a simple Array.pop
#
@states.pop

# Call setup on the new current state
current_state.setup if current_state.respond_to?(:setup) && options[:setup]
current_game_state.setup if current_game_state.respond_to?(:setup) && options[:setup]
end

#
# Returns a GameState-instance from either a class or object
#
def object_to_state(state)
def game_state_instance(state)
new_state = nil
#
# If state is a GameState-instance, just queue it
#
if state.is_a? Chingu::GameState
new_state = state
#
# If state is a GameState-class, create/initialize it once (@created_states keeps track of this)
# If state is a GameState-class, create it.
#
elsif state.superclass == Chingu::GameState

if @created_states[state.to_s]
new_state = @created_states[state.to_s]
else
new_state = state.new({})
@created_states[state.class.to_s] = new_state
end
new_state = state.new({})
end

return new_state
Expand All @@ -114,15 +108,15 @@ def object_to_state(state)
#
# Returns the previous game state
#
def previous_state
@states[@states.index(current_state)-1]
def previous_game_state
@states[@states.index(current_game_state)-1]
end
alias :prev_state previous_state
alias :previous previous_game_state

#
# Remove all game states from stack
#
def clear_states
def clear_game_states
@states.clear
end

Expand All @@ -146,27 +140,27 @@ def pop_until_game_state(new_state)
# Called before #update when the user pressed a button while the window had the focus.
#
def button_down(id)
current_state.button_down(id) if current_state
current_game_state.button_down(id) if current_game_state
end

#
# Called when the user released a button.
#
def button_up(id)
current_state.button_up(id) if current_state
current_game_state.button_up(id) if current_game_state
end

#
# Calls #update on the current gamestate, if there is one.
#
def update(time = 1)
current_state.update(time) if current_state
current_game_state.update(time) if current_game_state
end
#
# Calls draw() on the current gamestate, if there is one.
#
def draw
current_state.draw if current_state
current_game_state.draw if current_game_state
end
end
end
12 changes: 6 additions & 6 deletions lib/chingu/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,27 @@ def dispatch_action(action, object)
#
module GameStateHelpers
def push_game_state(state, options = {})
$window.game_state_manager.push_state(state, options)
$window.game_state_manager.push_game_state(state, options)
end

def pop_game_state(options = {})
$window.game_state_manager.pop_state(options)
$window.game_state_manager.pop_game_state(options)
end

def switch_game_state(state, options = {})
$window.game_state_manager.switch_state(state, options)
$window.game_state_manager.switch_game_state(state, options)
end

def current_game_state
$window.game_state_manager.current_state
$window.game_state_manager.current_game_state
end

def previous_game_state
$window.game_state_manager.previous_state
$window.game_state_manager.previous_game_state
end

def clear_game_states
$window.game_state_manager.clear_states
$window.game_state_manager.clear_game_states
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/chingu/window.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Chingu
class Window < Gosu::Window
class Window < Gosu::Window
# adds push_game_state, pop_game_state, current_game_state and previous_game_state
include Chingu::GameStateHelpers

Expand Down

0 comments on commit 31b2b88

Please sign in to comment.