Class: Chingu::Console

Inherits:
Object
  • Object
show all
Includes:
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

#add_game_object, #game_objects_of_class, #hide_game_object, #load_game_objects, #pause_game_object, #remove_game_object, #save_game_objects, #show_game_object, #unpause_game_object

Methods included from Helpers::GameState

#clear_game_states, #current_game_state, #game_states, #pop_game_state, #push_game_state, #switch_game_state, #transitional_game_state

Constructor Details

#initialize(update_interval = 16.666666) ⇒ Console

Returns a new instance of Console.



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

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.



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

def factor
  @factor
end

#game_objectsObject (readonly)

Returns the value of attribute game_objects.



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

def game_objects
  @game_objects
end

#game_state_managerObject (readonly)

Returns the value of attribute game_state_manager.



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

def game_state_manager
  @game_state_manager
end

#milliseconds_since_last_tickObject (readonly)

Returns the value of attribute milliseconds_since_last_tick.



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

def milliseconds_since_last_tick
  @milliseconds_since_last_tick
end

#rootObject (readonly)

Returns the value of attribute root.



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

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.



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

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

#dtObject

Mathematical short name for "milliseconds since last tick"



94
95
96
# File 'lib/chingu/console.rb', line 94

def dt
  @milliseconds_since_last_tick
end

#fpsObject Also known as: framerate

Frames per second, access with $window.fps or $window.framerate



79
80
81
# File 'lib/chingu/console.rb', line 79

def fps
  @fps_counter.fps
end

#intermediate_updateObject

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/chingu/console.rb', line 116

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



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

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.



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

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

#ticksObject

Total amount of game iterations (ticks)



87
88
89
# File 'lib/chingu/console.rb', line 87

def ticks
  @fps_counter.ticks
end

#updateObject

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



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chingu/console.rb', line 101

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