dare 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OWY5MzQ5MzBmNTE3YzExNTNmZTU3YmI0NjBiOTBkOTMxZWMwOGFhMA==
4
+ NjhhODdlYTdmMTk1MTg0YzI1YTI4NjUwNmY1OWFlZTViMzk0OWJkNA==
5
5
  data.tar.gz: !binary |-
6
- YTkwYTc2OWI5NGM4NjVkN2FiOGIzNjk5ZjkwNTQ5Nzg5ZmMxZTcyOA==
6
+ YzhhMzYxZGNlOTBlNjg2MTMzYWFlZmIxYjRhZWI5YTg3YWM0ZmQxYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTUzYjQwMDM4Njc0ZjM2NTIxZGJlNGY4MDIxN2FkMjk3ZTg2YjAwNTM1N2Qz
10
- NDAyMDNlM2JhMjk5MDViODA4NDNiM2JlN2M0M2QxOTE0YmQ3ZTI2ZWZmZTYx
11
- N2I0YzM1ZDE0MjdiM2UwZWE4ZTI2MDAzMjhlZDY1ZDk4ZTZlNzA=
9
+ M2E5OTU3YmQ0NjU1MDFlZjNlOTAxMTQ2N2NjZTg3Y2M5OTk3MzE3NzM3Yzg4
10
+ ODcxMDE1ODIzN2ZmZTk4MjQ3ZjU5ZTdiOGVkZTMyZWNkMjVhMmIwMmFmOTFj
11
+ MDc4NzRiN2Q0YzIwYjY5MjdmZWEzMGNkOTNmNjc5ZjQ5YWY3NTU=
12
12
  data.tar.gz: !binary |-
13
- YmI0NzllYzU0NjJmZTdmMTBmNzRiMDExMTA1ZThkMTc0MmI1NTMyZWQ3ZjA5
14
- NDkxOTkwMzUzNzk5Y2NlNzcwM2I4ZDZjY2U1MTA5YzJhOTI5MmQzYTNkMGJk
15
- ODlkNjZiNjhmZWY4YTE0OWJlNWY1NzUyM2VjM2U5YzQyZjUyOTQ=
13
+ YWJjMDRhMjYzMzMzNWI4MmZkMWRlNTVmY2M2ODI2ODVkZDMxYjg0ZGE2NDI1
14
+ MzJjYzY1NTBhOThlYThlMDlhZTE0MjNlNmMwM2UyOWMzODRlNDY1ZmU2MWMy
15
+ ZmRlYmM2YWZlN2U3NWQ5Yzc5ZGVjZTUxZmYxNGIzNjE4N2M4YTg=
data/bin/dare CHANGED
@@ -6,19 +6,20 @@ class DareCLI < Thor
6
6
  desc "new", "creates a new app"
7
7
  def new(app_name)
8
8
  add_file("#{app_name}/Gemfile") do
9
- "gem 'dare', '0.1.0'
9
+ "gem 'dare', '0.2.0'
10
10
  gem 'opal', '0.7.0beta3'
11
11
  gem 'opal-jquery', '0.3.0beta1'"
12
12
  end
13
13
  add_file "#{app_name}/Rakefile" do
14
14
  "desc \"Build #{app_name}.js\"
15
15
  task :build do
16
- gem 'dare', '0.1.0'
16
+ gem 'dare', '0.2.0'
17
17
  gem 'opal', '0.7.0beta3'
18
18
  gem 'opal-jquery', '0.3.0beta1'
19
19
  require 'opal'
20
20
  require 'opal-jquery'
21
21
  Opal::Processor.source_map_enabled = false
22
+ Opal::Processor.inline_operators_enabled = true
22
23
  env = Sprockets::Environment.new
23
24
  Opal.paths.each do |path|
24
25
  env.append_path path
@@ -32,9 +33,7 @@ task :build do
32
33
  end"
33
34
  end
34
35
  add_file "#{app_name}/#{app_name}.rb" do
35
- "require 'opal' #this will be removed when 0.7.0 is released
36
- require 'opal-jquery'
37
- require 'dare'
36
+ "require 'dare'
38
37
 
39
38
  class #{app_name[0].upcase + app_name[1..-1]} < Dare::Window
40
39
 
@@ -43,11 +42,11 @@ class #{app_name[0].upcase + app_name[1..-1]} < Dare::Window
43
42
  end
44
43
 
45
44
  def draw
46
- #code that runs every frame
45
+ #code that draws every frame
47
46
  end
48
47
 
49
48
  def update
50
- #code that runs every frame
49
+ #code that runs every update_interval
51
50
  end
52
51
 
53
52
  end
@@ -1,17 +1,11 @@
1
+ require 'opal' #this will be removed when 0.7.0 is released
1
2
  require 'opal-jquery'
2
- # Dir["dare/*.rb"].each {|f| require f } # can't do this because Opal
3
- require "dare/canvas.rb"
4
- require "dare/font.rb"
5
- require "dare/image.rb"
6
- require "dare/keyboard_constants.rb"
7
- require "dare/sound.rb"
8
- require "dare/sprite.rb"
9
- require "dare/window.rb"
3
+ require_tree 'dare'
10
4
 
11
5
  module Dare
12
6
 
13
7
  # returns the current version of this gem
14
- VERSION = "0.1.0"
8
+ VERSION = "0.2.0"
15
9
 
16
10
  # returns the magnitude of the horizontal component of a vector
17
11
  # at some angle and some magnitude where the angle is in degrees
@@ -22,7 +16,7 @@ module Dare
22
16
  # Dare.offset_x(45, 10) # returns 10 times the square root of 2
23
17
  #
24
18
  def self.offset_x(angle, magnitude)
25
- `#{magnitude}*Math.cos(#{angle}*Math.PI/180.0)`
19
+ `#{magnitude}*Math.cos(-#{angle}*Math.PI/180.0)`
26
20
  end
27
21
 
28
22
  # returns the magnitude of the vertical component of a vector
@@ -34,7 +28,7 @@ module Dare
34
28
  # Dare.offset_y(45, 10) # returns 10 times the square root of 2
35
29
  #
36
30
  def self.offset_y(angle, magnitude)
37
- `#{magnitude}*Math.sin(#{angle}*Math.PI/180.0)`
31
+ `#{magnitude}*Math.sin(-#{angle}*Math.PI/180.0)`
38
32
  end
39
33
 
40
34
  # returns the number of milliseconds since the Unix epoch
@@ -43,6 +37,11 @@ module Dare
43
37
  def self.ms
44
38
  `(new Date()).getTime()`
45
39
  end
40
+
41
+ def self.distance(x1, y1, x2, y2)
42
+ `Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))`
43
+ end
44
+
46
45
  class << self
47
46
  attr_accessor :default_canvas
48
47
  end
@@ -1,18 +1,23 @@
1
1
  module Dare
2
2
  class Font
3
- def initialize(window = Dare.default_canvas, font_type = "30px Arial")
4
- @window = window
5
- @font = font_type
6
- @color = "red"
7
- @x = 50
8
- @y = 50
3
+ def initialize(opts = {})
4
+ opts[:font] ||= "Arial"
5
+ opts[:canvas] ||= Dare.default_canvas
6
+ opts[:size] ||= 30
7
+ opts[:color] ||= "black"
8
+
9
+ @font = opts[:size].to_s + "px" + " " + opts[:font]
10
+ @canvas = opts[:canvas]
11
+ @color = opts[:color]
9
12
  end
10
13
 
11
- def draw(string = "", x = @x, y = @y)
14
+ def draw(string = "", x = 0, y = 0, opts = {})
12
15
  %x{
13
- #{@window.canvas.context}.font = #{@font};
14
- #{@window.canvas.context}.fillStyle = #{@color};
15
- #{@window.canvas.context}.fillText(#{string}, #{x}, #{y});
16
+ #{@canvas.context}.font = #{@font} ;
17
+ #{@canvas.context}.textAlign = 'left';
18
+ #{@canvas.context}.textBaseline = 'top';
19
+ #{@canvas.context}.fillStyle = #{@color};
20
+ #{@canvas.context}.fillText(#{string}, #{x}, #{y});
16
21
  }
17
22
  end
18
23
  end
@@ -1,24 +1,218 @@
1
1
  module Dare
2
2
 
3
- # loads an image which can be drawn to a canvas
3
+ # Represents an image which can be drawn to a canvas
4
4
  #
5
5
  class Image
6
6
 
7
- # loads a new image resource
8
- # image = Dare::Image.new('some_image.png')
7
+ # Loads a new image resource from an absolute URL or relative path
9
8
  #
10
- def initialize(path = "", window = Dare.default_canvas)
9
+ # @param [String] path ("") the path to the resource
10
+ # @param [Hash] opts ({}) the options to create an image.
11
+ # @option opts [Dare::Canvas] :canvas (Dare.default_canvas) a canvas to refer to when drawing.
12
+ #
13
+ # @example
14
+ # Dare::Image.new('some_image.png')
15
+ #
16
+ # @example
17
+ # Dare::Image.new('https://www.google.com/images/srpr/logo11w.png', canvas: Dare::Canvas.new)
18
+ #
19
+ def initialize(path = "", opts = {})
20
+ opts[:canvas] ||= Dare.default_canvas
21
+ @path = path
11
22
  @img = `new Image()`
12
23
  `#{@img}.src = #{path}`
13
- @window = window
24
+ @canvas = opts[:canvas]
25
+ end
26
+
27
+ # The path or URL of the image
28
+ #
29
+ # @return [String]
30
+ #
31
+ def path
32
+ @path
33
+ end
34
+
35
+ def img
36
+ @img
37
+ end
38
+
39
+ # The width of the image in pixels
40
+ #
41
+ # @return [Integer]
42
+ #
43
+ def width
44
+ `#{@img}.width`
45
+ end
46
+
47
+ # The height of the image in pixels
48
+ #
49
+ # @return [Integer]
50
+ #
51
+ def height
52
+ `#{@img}.height`
53
+ end
54
+
55
+ # Draws image to a canvas at an `x` and `y` position. `x` and `y` represent the top-left corner of the image
56
+ #
57
+ # @param [Integer] x (0) x coordinate of top-left of image
58
+ # @param [Integer] y (0) y coordinate of top-left of image
59
+ # @param [Hash] opts ({}) the options to draw an image.
60
+ # @option opts [Dare::Canvas] :canvas (Dare.default_canvas) a canvas to refer to when drawing.
61
+ #
62
+ # @example
63
+ # image.draw(100, 200)
64
+ #
65
+ # @example
66
+ # image.draw(10, 10, canvas: Dare::Canvas.new(width: 100, height: 100))
67
+ #
68
+ def draw(x = 0, y = 0, opts = {})
69
+
70
+ opts[:canvas] ||= @canvas
71
+ %x{
72
+ #{opts[:canvas].context}.drawImage(
73
+ #{@img},
74
+ #{x},
75
+ #{y}
76
+ );
77
+ }
78
+ end
79
+
80
+ # (see #draw)
81
+ # @note Used by Dare::ImageTile to draw a portion of an image to a canvas.
82
+ #
83
+ def draw_tile(x = 0, y = 0, opts = {})
84
+
85
+ opts[:canvas] ||= @canvas
86
+ opts[:sx] ||= 0
87
+ opts[:sy] ||= 0
88
+ opts[:swidth] ||= width - opts[:sx]
89
+ opts[:sheight] ||= height - opts[:sy]
90
+ opts[:dwidth] ||= opts[:swidth]
91
+ opts[:dheight] ||= opts[:sheight]
92
+
93
+ %x{
94
+ #{opts[:canvas].context}.drawImage(
95
+ #{@img},
96
+ #{opts[:sx]},
97
+ #{opts[:sy]},
98
+ #{opts[:swidth]},
99
+ #{opts[:sheight]},
100
+ #{x},
101
+ #{y},
102
+ #{opts[:dwidth]},
103
+ #{opts[:dheight]}
104
+ );
105
+ }
106
+ end
107
+
108
+ # Draws image to a canvas at an x and y position and rotated at some angle
109
+ # x and y represent the center of the image
110
+ # angle is in degrees starting by pointing to the right and increasing counterclockwise
111
+ #
112
+ # @param [Integer] x (0) x coordinate of center of image
113
+ # @param [Integer] y (0) y coordinate of center of image
114
+ # @param [Float] angle (90.0) angle of image
115
+ # @param [Hash] opts ({}) the options to draw an image.
116
+ # @option opts [Dare::Canvas] :canvas (Dare.default_canvas) a canvas to refer to when drawing.
117
+ #
118
+ # @example
119
+ # image.draw_rot(100, 200, 45) # this will point the top of the image up and to the right
120
+ #
121
+ # @example
122
+ # image.draw_rot(50, 75, 270, canvas: some_other_canvas)
123
+ #
124
+ def draw_rot(x = 0, y = 0, angle = 90, opts = {})
125
+ opts[:canvas] ||= @canvas
126
+ %x{
127
+ var context = #{opts[:canvas].context};
128
+ var width = #{@img}.width;
129
+ var height = #{@img}.height;
130
+ context.translate(#{x}, #{y});
131
+ context.rotate(-#{angle-90}*Math.PI/180.0);
132
+ context.drawImage(#{@img}, -width/2, -height/2, width, height);
133
+ context.rotate(#{angle-90}*Math.PI/180.0);
134
+ context.translate(-#{x}, -#{y});
135
+ }
136
+ end
137
+
138
+ # (see #draw_rot)
139
+ # @note Used by Dare::ImageTile to draw a portion of an image to a canvas at an angle.
140
+ #
141
+ def draw_tile_rot(x = 0, y = 0, angle = 90, opts = {})
142
+ opts[:canvas] ||= @canvas
143
+ opts[:sx] ||= 0
144
+ opts[:sy] ||= 0
145
+ opts[:swidth] ||= width - opts[:sx]
146
+ opts[:sheight] ||= height - opts[:sy]
147
+ opts[:dwidth] ||= opts[:swidth]
148
+ opts[:dheight] ||= opts[:sheight]
149
+ %x{
150
+ var context = #{opts[:canvas].context};
151
+ context.translate(#{x}, #{y});
152
+ context.rotate(-#{angle-90}*Math.PI/180.0);
153
+ context.drawImage(
154
+ #{@img},
155
+ #{opts[:sx]},
156
+ #{opts[:sy]},
157
+ #{opts[:swidth]},
158
+ #{opts[:sheight]},
159
+ -#{opts[:swidth]}/2,
160
+ -#{opts[:sheight]}/2,
161
+ #{opts[:swidth]},
162
+ #{opts[:sheight]});
163
+ context.rotate(#{angle-90}*Math.PI/180.0);
164
+ context.translate(-#{x}, -#{y});
165
+ }
14
166
  end
15
167
 
16
- # draws image to a canvas at an x and y position
17
- # x and y represent the top-left corner of the image
18
- # image.draw(100, 200)
168
+ # Loads image and cuts it into tiles
19
169
  #
20
- def draw(x = 0, y = 0)
21
- `#{@window.canvas.context}.drawImage(#{@img},#{x},#{y})`
170
+ # @param [String] path ("") the path to the resource
171
+ # @param [Hash] opts ({}) the options to create an image.
172
+ # @option opts [Integer] :width (Image.new(path).width) width of a single tile from the image
173
+ # @option opts [Integer] :height (Image.new(path).height) height of a single tile from the image
174
+ #
175
+ # @return [Array] An array of images each of which are individual tiles of the original
176
+ #
177
+ def self.load_tiles(path = "", opts = {})
178
+
179
+ image = Image.new(path)
180
+ @tiles = []
181
+
182
+ %x{
183
+ #{image.img}.onload = function() {
184
+ #{opts[:width] ||= image.width};
185
+ #{opts[:height] ||= image.height};
186
+ #{columns = image.width/opts[:width]};
187
+ #{rows = image.height/opts[:height]};
188
+
189
+ #{rows.times do |row|
190
+ columns.times do |column|
191
+ @tiles << ImageTile.new(image, column*opts[:width].to_i, row*opts[:height].to_i, opts[:width].to_i, opts[:height].to_i)
192
+ end
193
+ end};
194
+ }
195
+ }
196
+ @tiles
197
+ end
198
+ end
199
+
200
+ class ImageTile
201
+ attr_reader :width, :height
202
+ def initialize(image = Image.new, x = 0, y = 0, width = 0, height = 0)
203
+ @image = image
204
+ @x = x
205
+ @y = y
206
+ @width = width
207
+ @height = height
208
+ end
209
+
210
+ def draw(x = 0, y = 0, opts = {})
211
+ @image.draw_tile(x, y, opts.merge({sx: @x, sy: @y, swidth: @width, sheight: @height}))
212
+ end
213
+
214
+ def draw_rot(x = 0, y = 0, angle = 90, opts = {})
215
+ @image.draw_tile_rot(x, y, angle, opts.merge({sx: @x, sy: @y, swidth: @width, sheight: @height}))
22
216
  end
23
217
  end
24
218
  end
@@ -8,14 +8,24 @@ module Dare
8
8
  class Sound
9
9
 
10
10
  # loads an audio resource from a path
11
- # Sound.new('http://www.some-site.com/song.mp3')
11
+ # Sound.new('http://www.google.com/song.mp3')
12
12
  # Sound.new('local_song_in_same_directory_of_app_js_file.mp3')
13
- # a predefined volume may be passed as a second parameter
14
- # Sound.new('file.mp3', 0.5)
15
- def initialize(path, volume = 1)
16
- `var snd = new Audio(#{path})`
17
- `snd.volume = #{volume}`
18
- @sound = `snd`
13
+ # a predefined volume may be passed as an option
14
+ # Sound.new('file.mp3', volume: 0.5)
15
+ #
16
+ def initialize(path, opts = {})
17
+ opts[:overlap] ||= 1
18
+ opts[:volume] ||= 1
19
+ opts[:overlap] = 1 if opts[:overlap].to_i < 1
20
+ opts[:overlap] = 10 if opts[:overlap].to_i > 10
21
+ @overlap = opts[:overlap]
22
+ @sounds = []
23
+ @overlap.times do
24
+ `var snd = new Audio(#{path})`
25
+ `snd.volume = #{opts[:volume]}`
26
+ @sounds << `snd`
27
+ end
28
+ @sound = 0
19
29
  end
20
30
 
21
31
  # set the volume of the audio resource to value between 0 and 1
@@ -34,7 +44,9 @@ module Dare
34
44
  # if resource was paused, it will start playing from where it left off
35
45
  #
36
46
  def play
37
- `#{@sound}.play()`
47
+ @sound += 1
48
+ @sound %= @overlap
49
+ `#{@sounds[@sound]}.play()`
38
50
  end
39
51
 
40
52
  # pause the audio resource, halting playback
@@ -1,16 +1,22 @@
1
1
  module Dare
2
+ class Color
3
+ attr_accessor :red, :green, :blue
4
+ def initialize(color)
5
+
6
+ end
7
+ end
2
8
  class Window
3
9
 
4
10
  attr_reader :width, :height, :ticks, :mouse_x, :mouse_y, :canvas, :key, :update_interval
5
11
 
6
- # creates a new window object to hold all your game goodness
7
- # options include:
8
- # :width # (default 640) sets default canvas to a particular width in pixels
9
- # :height # (default (480) sets default canvas to a particular height in pixels
10
- # :update_interval # (default 16.666666) sets the update interval in milliseconds between updates
11
- # :border # true/false (default false) draws a border around the default canvas
12
- # :canvas # a canvas to refer to when drawing. Just let the default do its thing
13
- # :mouse # true/false (default true) turn off mouse event listeners by setting to false
12
+ # Creates a new window object to hold all your game goodness
13
+ # @param [Hash] opts the options to create a window.
14
+ # @option opts [Integer] :width (640) sets default canvas to a particular width in pixels
15
+ # @option opts [Integer] :height (480) sets default canvas to a particular height in pixels
16
+ # @option opts [Float] :update_interval (16.666666) sets the update interval in milliseconds between updates
17
+ # @option opts [Boolean] :border (false) draws a border around the default canvas
18
+ # @option opts [Dare::Canvas] :canvas (Dare.default_canvas) a canvas to refer to when drawing.
19
+ # @option opts [Boolean] :mouse (true) turn off mouse event listeners by setting to false
14
20
  #
15
21
  def initialize(opts = {})
16
22
  opts[:width] ||= 640
@@ -33,12 +39,15 @@ module Dare
33
39
  # starts the game loop for the window.
34
40
  def run!
35
41
  %x{
42
+ function update_loop() {
43
+ #{update};
44
+ }
36
45
  function anim_loop() {
37
46
  requestAnimationFrame(anim_loop);
38
- #{update};
39
- #{@canvas.context}.clearRect(0, 0, #{width}, #{height});
47
+ #{@canvas.canvas}.width = #{@canvas.canvas}.width;
40
48
  #{draw};
41
49
  }
50
+ setInterval(update_loop, #{@update_interval});
42
51
  requestAnimationFrame(anim_loop);
43
52
  }
44
53
  end
@@ -72,6 +81,9 @@ module Dare
72
81
  Element.find("html").on :keyup do |event|
73
82
  @keys[get_key_id(event)] = false
74
83
  end
84
+ ::Window.on :blur do |event|
85
+ @keys.fill false
86
+ end
75
87
  end
76
88
 
77
89
  # checks to see if button passed is currently being pressed
@@ -115,11 +127,44 @@ module Dare
115
127
  `#{@canvas.context}.fillRect(#{x}, #{y}, #{width}, #{height})`
116
128
  end
117
129
 
130
+ #works the same as Gosu::Window.draw_quad
131
+ def draw_quad(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, z)
132
+ %x{
133
+ var QUALITY = 256;
134
+
135
+ var canvas_colors = document.createElement( 'canvas' );
136
+ canvas_colors.width = 2;
137
+ canvas_colors.height = 2;
138
+
139
+ var context_colors = canvas_colors.getContext( '2d' );
140
+ context_colors.fillStyle = 'rgba(0,0,0,1)';
141
+ context_colors.fillRect( 0, 0, 2, 2 );
142
+
143
+ var image_colors = context_colors.getImageData( 0, 0, 2, 2 );
144
+ var data = image_colors.data;
145
+
146
+ var canvas_render = #{@canvas};
147
+
148
+ var context_render = #{@canvas.context};
149
+ context_render.translate( - QUALITY / 2, - QUALITY / 2 );
150
+ context_render.scale( QUALITY, QUALITY );
151
+
152
+ data[ 0 ] = 255; // Top-left, red component
153
+ data[ 5 ] = 255; // Top-right, green component
154
+ data[ 10 ] = 255; // Bottom-left, blue component
155
+
156
+ context_colors.putImageData( image_colors, 0, 0 );
157
+ context_render.drawImage( canvas_colors, 0, 0 );
158
+ }
159
+ end
160
+
118
161
  # sets the caption/title of the window to the string passed
119
- def caption(title)
162
+ def caption=(title)
120
163
  `document.getElementById('pageTitle').innerHTML = #{title}`
121
164
  end
122
165
 
166
+ alias :title= :caption=
167
+
123
168
  # checks if game is fullscreen. currently not implemented.
124
169
  def fullscreen?
125
170
  false
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dare
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dominic Muller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-11 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -108,6 +108,10 @@ extensions: []
108
108
  extra_rdoc_files: []
109
109
  files:
110
110
  - bin/dare
111
+ - examples/tutorial/assets/Beep.wav
112
+ - examples/tutorial/assets/Space.png
113
+ - examples/tutorial/assets/Star.png
114
+ - examples/tutorial/assets/Starfighter.png
111
115
  - lib/dare.rb
112
116
  - lib/dare/canvas.rb
113
117
  - lib/dare/font.rb