Class: Arcade::GameWindow

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/arcade/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ GameWindow

Returns a new instance of GameWindow.



16
17
18
19
20
21
22
23
# File 'lib/arcade/base.rb', line 16

def initialize width, height
  @current_time = Gosu::milliseconds
  @objects      = []

  @keypress_listeners  = Hash.new { |h,k| h[k] = [] }

  super width, height, false
end

Instance Method Details

#add_listener(key, object) ⇒ Object



38
39
40
# File 'lib/arcade/base.rb', line 38

def add_listener key, object
  @keypress_listeners[key] << object
end

#add_object(game_object) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/arcade/base.rb', line 25

def add_object game_object
  game_object.keypress_listeners.each do |key, proc|
    self.add_listener key, game_object
  end

  @objects << game_object
end

#button_down(id) ⇒ Object



91
92
# File 'lib/arcade/base.rb', line 91

def button_down id
end

#drawObject



50
51
52
53
54
# File 'lib/arcade/base.rb', line 50

def draw
  @objects.each do |game_object|
    game_object.draw
  end
end

#draw_square(upper_left, width, height, color = Arcade::Color::WHITE) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/arcade/base.rb', line 94

def draw_square upper_left, width, height, color = Arcade::Color::WHITE
  x, y = upper_left

  draw_quad x,       y,        color,
            x+width, y,        color,
            x+width, y+height, color,
            x,       y+height,  color
end

#listened_keysObject



42
43
44
# File 'lib/arcade/base.rb', line 42

def listened_keys
  @keypress_listeners.keys
end

#listeners_for_key(key) ⇒ Object



46
47
48
# File 'lib/arcade/base.rb', line 46

def listeners_for_key key
  @keypress_listeners[key]
end

#register(game_object) ⇒ Object



33
34
35
36
# File 'lib/arcade/base.rb', line 33

def register game_object
  game_object.window = self
  self.add_object(game_object)
end

#updateObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/arcade/base.rb', line 56

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

  self.listened_keys.each do |key|
    self.listeners_for_key(key).each do |listener|
      listener.key_pressed(key) if button_down?(key)
    end
  end

  @objects.each do |object|
    (@objects - [object]).each do |other|
      if object.collides_with?(other)
        object.collided_with(other)
      end
    end

    edge = if object.top < 0
             :top
           elsif object.bottom > self.height
             :bottom
           elsif object.left < 0
             :left
           elsif object.right > self.width
             :right
           end

    object.hit_edge(edge) if edge
  end

  @objects.each do |object|
    object._update(dt)
  end
end