Class: Dare::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/dare/window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Window

creates a new window object to hold all your game goodness options include:

:width # (default 640) sets default canvas to a particular width in pixels
:height # (default (480) sets default canvas to a particular height in pixels
:update_interval # (default 16.666666) sets the update interval in milliseconds between updates
:border # true/false (default false) draws a border around the default canvas
:canvas # a canvas to refer to when drawing.  Just let the default do its thing
:mouse # true/false (default true) turn off mouse event listeners by setting to false


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dare/window.rb', line 15

def initialize(opts = {})
  opts[:width] ||= 640
  opts[:height] ||= 480
  opts[:update_interval] ||= 16.666666
  opts[:border] ||= false
  opts[:canvas] ||= Canvas.new(width: opts[:width], height: opts[:height], border: opts[:border])
  opts[:mouse] ||= true
  @width = opts[:width]
  @height = opts[:height]
  @update_interval = opts[:update_interval]
  @clock = opts[:clock]
  @canvas = opts[:canvas]
  Dare.default_canvas ||= @canvas
  @keys = []
  add_mouse_event_listener if opts[:mouse]
  add_keyboard_event_listeners
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



4
5
6
# File 'lib/dare/window.rb', line 4

def canvas
  @canvas
end

#heightObject (readonly)

Returns the value of attribute height.



4
5
6
# File 'lib/dare/window.rb', line 4

def height
  @height
end

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'lib/dare/window.rb', line 4

def key
  @key
end

#mouse_xObject (readonly)

Returns the value of attribute mouse_x.



4
5
6
# File 'lib/dare/window.rb', line 4

def mouse_x
  @mouse_x
end

#mouse_yObject (readonly)

Returns the value of attribute mouse_y.



4
5
6
# File 'lib/dare/window.rb', line 4

def mouse_y
  @mouse_y
end

#ticksObject (readonly)

Returns the value of attribute ticks.



4
5
6
# File 'lib/dare/window.rb', line 4

def ticks
  @ticks
end

#update_intervalObject (readonly)

Returns the value of attribute update_interval.



4
5
6
# File 'lib/dare/window.rb', line 4

def update_interval
  @update_interval
end

#widthObject (readonly)

Returns the value of attribute width.



4
5
6
# File 'lib/dare/window.rb', line 4

def width
  @width
end

Instance Method Details

#add_keyboard_event_listenersObject

adds keyboard event listeners to entire page



68
69
70
71
72
73
74
75
# File 'lib/dare/window.rb', line 68

def add_keyboard_event_listeners
  Element.find("html").on :keydown do |event|
    @keys[get_key_id(event)] = true
  end
  Element.find("html").on :keyup do |event|
    @keys[get_key_id(event)] = false
  end
end

#add_mouse_event_listenerObject

adds mousemove event listener to main canvas



59
60
61
62
63
64
65
# File 'lib/dare/window.rb', line 59

def add_mouse_event_listener
  Element.find("##{@canvas.id}").on :mousemove do |event|
    coords = get_cursor_position(event)
    @mouse_x = coords.x[:x]
    @mouse_y = coords.x[:y]
  end
end

#button_down?(button) ⇒ Boolean

checks to see if button passed is currently being pressed

Returns:

  • (Boolean)


78
79
80
# File 'lib/dare/window.rb', line 78

def button_down?(button)
  @keys[button]
end

#caption(title) ⇒ Object

sets the caption/title of the window to the string passed



119
120
121
# File 'lib/dare/window.rb', line 119

def caption(title)
  `document.getElementById('pageTitle').innerHTML = #{title}`
end

#drawObject

gets run every frame of animation override this in your subclass of Dare::Window



48
49
50
# File 'lib/dare/window.rb', line 48

def draw

end

#draw_rect(opts = {}) ⇒ Object

draws a rectangle starting at (top_left, top_left) down to (top_leftwidth, top_leftheight)



107
108
109
110
111
112
113
114
115
116
# File 'lib/dare/window.rb', line 107

def draw_rect(opts = {})
  x = opts[:top_left][0]
  y = opts[:top_left][1]
  width = opts[:width]
  height = opts[:height]
  color = opts[:color]

  `#{@canvas.context}.fillStyle = #{color}`
  `#{@canvas.context}.fillRect(#{x}, #{y}, #{width}, #{height})`
end

#fullscreen?Boolean

checks if game is fullscreen. currently not implemented.

Returns:

  • (Boolean)


124
125
126
# File 'lib/dare/window.rb', line 124

def fullscreen?
  false
end

#get_cursor_position(event) ⇒ Object

sets mouse_x and mouse_y to current mouse positions relative to the main canvas



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dare/window.rb', line 84

def get_cursor_position(event)
  if (event.page_x && event.page_y)
    x = event.page_x
    y = event.page_y
  else
    doc = Opal.Document[0]
    x = event[:clientX] + doc.scrollLeft +
          doc.documentElement.scrollLeft
    y = event[:clientY] + doc.body.scrollTop +
          doc.documentElement.scrollTop
  end
  x -= `#{@canvas.canvas}.offsetLeft`
  y -= `#{@canvas.canvas}.offsetTop`
  Coordinates.new(x: x, y: y)
end

#get_key_id(event) ⇒ Object

retrieves key code of current pressed key for keydown or keyup event



101
102
103
# File 'lib/dare/window.rb', line 101

def get_key_id(event)
  event[:keyCode]
end

#run!Object

starts the game loop for the window.



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

def run!
  %x{
    function anim_loop() {
      requestAnimationFrame(anim_loop);
      #{update};
      #{@canvas.context}.clearRect(0, 0, #{width}, #{height});
      #{draw};
    }
    requestAnimationFrame(anim_loop);
  }
end

#text_inputObject

this is here for Gosu API compatability



129
# File 'lib/dare/window.rb', line 129

def text_input; end

#updateObject

gets run every update_interval if it can override this in your subclass of Dare::Window



54
55
56
# File 'lib/dare/window.rb', line 54

def update

end