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.



22
23
24
25
26
27
28
29
# File 'lib/arcade/base.rb', line 22

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



48
49
50
# File 'lib/arcade/base.rb', line 48

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

#add_object(game_object) ⇒ Object



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

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

  @objects.merge! game_object.name => game_object
end

#button_down(id) ⇒ Object



101
102
# File 'lib/arcade/base.rb', line 101

def button_down id
end

#drawObject



60
61
62
63
64
# File 'lib/arcade/base.rb', line 60

def draw
  each_object do |game_object|
    game_object.draw
  end
end

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



104
105
106
107
108
109
110
111
# File 'lib/arcade/base.rb', line 104

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

#get_object(object_name) ⇒ Object



44
45
46
# File 'lib/arcade/base.rb', line 44

def get_object object_name
  @objects[object_name]
end

#listened_keysObject



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

def listened_keys
  @keypress_listeners.keys
end

#listeners_for_key(key) ⇒ Object



56
57
58
# File 'lib/arcade/base.rb', line 56

def listeners_for_key key
  @keypress_listeners[key]
end

#register(game_object) ⇒ Object



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

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

#updateObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/arcade/base.rb', line 66

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

  each_object do |object|
    (@objects.values - [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

  each_object do |object|
    object._update(dt)
  end
end