Class: Lotu::Game

Inherits:
Gosu::Window
  • Object
show all
Extended by:
Behavior
Includes:
Helpers::Util
Defined in:
lib/lotu/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Behavior

behave_like, inherited

Methods included from Helpers::Util

#class_debug_info, #instance_debug_info, #parse_cli_options

Constructor Details

#initialize(opts = {}) ⇒ Game

Returns a new instance of Game.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lotu/game.rb', line 16

def initialize(opts={})
  # parse and merge options passed from
  # the CLI (Command Line Interface)
  # CLI options have the greatest precedence
  if opts[:parse_cli_options] || opts[:parse_cli_options].nil?
    opts.merge!(parse_cli_options)
  end

  # set some sane default opts
  default_opts = {
    :width => 1024,
    :height => 768,
    :fullscreen => false,
    :debug => false
  }
  # fill in any missing options using the defaults
  opts = default_opts.merge!(opts)

  # Handy global window variable
  $lotu = self

  # Game setup
  @debug = opts[:debug]
  @pause = false
  setup_containers

  # if debug is set, print out class info
  class_debug_info if @debug

  # call the Gosu::Window constructor
  super(opts[:width], opts[:height], opts[:fullscreen])

  # For timer initialization
  @last_time = Gosu::milliseconds

  # start behaving as
  init_behavior opts

  # Call hook methods
  load_resources
  setup_actors
  setup_input
  setup_events

  # if debug is set, print out instance info
  instance_debug_info if @debug
end

Instance Attribute Details

#debugObject (readonly)

Accessors for elapsed time since last update (time delta) and debug



12
13
14
# File 'lib/lotu/game.rb', line 12

def debug
  @debug
end

#draw_queueObject

Accessors for queues



14
15
16
# File 'lib/lotu/game.rb', line 14

def draw_queue
  @draw_queue
end

#dtObject (readonly)

Accessors for elapsed time since last update (time delta) and debug



12
13
14
# File 'lib/lotu/game.rb', line 12

def dt
  @dt
end

#input_listenersObject

Accessors for queues



14
15
16
# File 'lib/lotu/game.rb', line 14

def input_listeners
  @input_listeners
end

#update_queueObject

Accessors for queues



14
15
16
# File 'lib/lotu/game.rb', line 14

def update_queue
  @update_queue
end

Instance Method Details

#button_down(id) ⇒ Object

These are for managing input



138
139
140
141
142
# File 'lib/lotu/game.rb', line 138

def button_down(id)
  @input_register[id].each do |item|
    item.button_down(id)
  end
end

#button_up(id) ⇒ Object



144
145
146
147
148
# File 'lib/lotu/game.rb', line 144

def button_up(id)
  @input_register[id].each do |item|
    item.button_up(id)
  end
end

#debug!Object



76
77
78
# File 'lib/lotu/game.rb', line 76

def debug!
  @debug = !@debug
end

#debug?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/lotu/game.rb', line 80

def debug?
  @debug
end

#drawObject

Main draw loop



115
116
117
118
119
120
121
122
123
124
# File 'lib/lotu/game.rb', line 115

def draw
  # to call draw on behaviors (that in turn will call
  # draw on systems, for example)
  super

  # Draw each actor in queue
  @draw_queue.each do |actor|
    actor.draw
  end
end

#fpsObject



64
65
66
# File 'lib/lotu/game.rb', line 64

def fps
  Gosu::fps
end

#kill_me(actor) ⇒ Object



132
133
134
135
# File 'lib/lotu/game.rb', line 132

def kill_me(actor)
  @draw_queue.delete(actor)
  @update_queue.delete(actor)
end

#load_resourcesObject

Hook methods, these are meant to be replaced by subclasses



85
# File 'lib/lotu/game.rb', line 85

def load_resources; end

#manage_me(actor) ⇒ Object

For actor management



127
128
129
130
# File 'lib/lotu/game.rb', line 127

def manage_me(actor)
  @draw_queue << actor
  @update_queue << actor
end

#pause!Object



68
69
70
# File 'lib/lotu/game.rb', line 68

def pause!
  @pause = !@pause
end

#paused?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/lotu/game.rb', line 72

def paused?
  @pause
end

#register_for_input(controller) ⇒ Object



150
151
152
153
154
155
# File 'lib/lotu/game.rb', line 150

def register_for_input(controller)
  controller.keys.each_key do |key|
    @input_register[key] << controller
  end
  @update_queue << controller
end

#setup_actorsObject



86
# File 'lib/lotu/game.rb', line 86

def setup_actors; end

#setup_containersObject

Setup various containers



91
92
93
94
95
96
# File 'lib/lotu/game.rb', line 91

def setup_containers
  # For queues
  @update_queue = []
  @draw_queue = []
  @input_register = Hash.new{|hash,key| hash[key] = []}
end

#setup_eventsObject



88
# File 'lib/lotu/game.rb', line 88

def setup_events; end

#setup_inputObject



87
# File 'lib/lotu/game.rb', line 87

def setup_input; end

#updateObject

Main update loop



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lotu/game.rb', line 99

def update
  new_time = Gosu::milliseconds
  @dt = (new_time - @last_time)/1000.0
  @last_time = new_time

  # to call update on behaviors (that in turn wil call
  # update on systems, for example)
  super

  # Update each actor
  @update_queue.each do |actor|
    actor.update
  end unless paused?
end