Class: Chingu::Console

Inherits:
Object
  • Object
show all
Includes:
Helpers::FPSCounter, Helpers::GameObject, Helpers::GameState
Defined in:
lib/chingu/console.rb

Overview

Console is the non-gfx variant of

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::GameObject

#game_objects_of_class, #load_game_objects, #save_game_objects

Constructor Details

#initialize(update_interval = 16.666666) ⇒ Console

Returns a new instance of Console



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chingu/console.rb', line 34

def initialize(update_interval = 16.666666)			
  $window = self
  @update_interval = update_interval
  @root = File.dirname(File.expand_path($0))
  @game_objects = GameObjectList.new      
  @fps_counter = FPSCounter.new
  @game_state_manager = GameStateManager.new
  @milliseconds_since_last_tick = 0
  @factor = 1
  setup
end

Instance Attribute Details

#factorObject (readonly)

Returns the value of attribute factor



32
33
34
# File 'lib/chingu/console.rb', line 32

def factor
  @factor
end

#game_objectsObject (readonly)

Returns the value of attribute game_objects



32
33
34
# File 'lib/chingu/console.rb', line 32

def game_objects
  @game_objects
end

#game_state_managerObject (readonly)

Returns the value of attribute game_state_manager



32
33
34
# File 'lib/chingu/console.rb', line 32

def game_state_manager
  @game_state_manager
end

#milliseconds_since_last_tickObject (readonly)

Returns the value of attribute milliseconds_since_last_tick



32
33
34
# File 'lib/chingu/console.rb', line 32

def milliseconds_since_last_tick
  @milliseconds_since_last_tick
end

#rootObject (readonly)

Returns the value of attribute root



32
33
34
# File 'lib/chingu/console.rb', line 32

def root
  @root
end

Instance Method Details

#current_scopeObject

Returns self inside GameState.initialize (a game state is not 'active' inside initialize()) Or returns current active game state (as in a switched to or pushed game state) … Falls back to returning $window

current_scope is used to make GameObject.all and friends work everywhere.



73
74
75
# File 'lib/chingu/console.rb', line 73

def current_scope
  game_state_manager.inside_state || game_state_manager.current_game_state || self
end

#intermediate_updateObject

“game logic” update that is safe to call even between Gosus update-calls



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chingu/console.rb', line 95

def intermediate_update
  #
  # Call update() on all game objects belonging to the main window.
  #
  @game_objects.update
  
  #
  # Call update() on all game objects belonging to the current game state.
  #

  #
  # Call update() on our game_state_manger
  # -> call update on active states
  # -> call update on all game objects in that state
  #
  @game_state_manager.update
end

#setupObject

Placeholder to be overwritten



64
# File 'lib/chingu/console.rb', line 64

def setup; end

#startObject Also known as: show

This is our “game-loop”. Will loop forever, with framerate specified and call update() The idea is to be very simular to how a Chingu::Window works.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chingu/console.rb', line 50

def start
  loop do
    t1 = Time.now
    update
    t2 = Time.now
    update_duration = t2 - t1
    
    milliseconds = (@update_interval/1000 - update_duration)
    sleep(milliseconds)  if milliseconds > 0
  end
end

#updateObject

Chingus core-logic / loop. Gosu will call this each game-iteration.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chingu/console.rb', line 80

def update
  #
  # Register a tick with our rather standard tick/framerate counter. 
  # Returns the amount of milliseconds since last tick. This number is used in all update()-calls.
  # 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
  
  intermediate_update
end