dare 0.0.1 → 0.0.4

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
- NGYxMmFhYmY5ZTczZDViMjVkMzI3YzhkYzYwZGRiZDkyOTc0MzcyNQ==
4
+ MDc0ZGMwZTJkMmQ0NDk5NmJiM2E5ZTc4YmJkZjg1OGVlNWUxOTgxNQ==
5
5
  data.tar.gz: !binary |-
6
- MjM4NTBkM2IwNWI5OTgzZTY5ZGIzNGExY2E2MDQ1MjkyYjExMzM4MQ==
6
+ YTU1MGE4MzIwYzliM2I5MGNjY2ExMjkzZmMzZDhiMTRjN2RiYzJhMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NzFiMTg4OGIwNzczMjBhMGE3MTNmZjM5ZmQxMWQzNDZjN2Q4OThlYWE1NmJj
10
- MzhkMzg5NGY4ODhiZmZjZTE5Njg4ZTU0M2ZmNzRhZDIwODRlMmQzMDVjZmFm
11
- YWQzMWY0NzRkMGQwYzYwMTliMTA4MWFiNzEwZTJiNmQxYjFkNWQ=
9
+ Y2Q5MTEwYzg2YmJmNWYyMjBkNWRiNTc1ZWRjOWNhNjdjMGM2MWY2MGMxNGE1
10
+ ZGVkZmViNWZjZTFmZTYxNjdkZGIyYThkYWZkODlkY2I2ZjUyNTgzYzI3NDE2
11
+ YTRjMGY2N2YwMmI4M2M5MWExZjY5NmI5NTk0ZGM4MDgzZTRkMzI=
12
12
  data.tar.gz: !binary |-
13
- OTAzODY4ZDQwZjg5Yjc4ZDZjYWY0Zjg4MDQ3N2JhZmMwMzUyMjc1NzMxYzhh
14
- NjI2MzQxZDFmNjViYWQxZDVkZjE2NzViZGFiZDU0ZDk3Y2I0Zjc3ZWZjNTM5
15
- OGIzYTdlMDlkYWY3ZTYyNWVkZTJhNTExOGY3ZDQ5NWQ0NjZlZjY=
13
+ ZGE4NzY0ZmE1ODRiMzE2ZTVkNjBiMzBiZGYzNmE2MWEzNDQ4OTg4MmQ3M2Zh
14
+ NTc2MGM5NTMyYWM0OTY4MGIxN2JjYjYxZjQ4MmZkNzAzNmJlY2RhMjlmM2E3
15
+ MTcxYzU4YWNhNWFiZjc1ZTgzOWQyYTMyYzdmYzhiMmE0MDU5NDY=
data/bin/dare.rb ADDED
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env ruby
2
+ require 'thor'
3
+ require 'dare'
4
+
5
+ class DareCLI < Thor
6
+ include Thor::Actions
7
+ desc "new", "creates a new app"
8
+ def new(app_name)
9
+ add_file("#{app_name}/Gemfile") do
10
+ "gem 'dare', '#{Dare::VERSION}'"
11
+ end
12
+ add_file "#{app_name}/Rakefile" do
13
+ "desc \"Build #{app_name}.js\"
14
+ task :build do
15
+ require 'opal'
16
+ require 'opal-jquery'
17
+ env = Opal::Environment.new
18
+ env.append_path \".\"
19
+ env.append_path `bundle show dare`.chomp
20
+
21
+ File.open(\"#{app_name}.js\", \"w+\") do |out|
22
+ out << env[\"#{app_name}\"].to_s
23
+ end
24
+ end"
25
+ end
26
+ add_file "#{app_name}/#{app_name}.rb" do
27
+ "require 'dare'
28
+ require 'opal-jquery'
29
+
30
+ class #{app_name[0].upcase + app_name[1..-1]} < Dare::Window
31
+
32
+ def initialize
33
+ super width: 640, height: 480, border: true
34
+ end
35
+
36
+ def draw
37
+ #code that runs every frame
38
+ end
39
+
40
+ def update
41
+ #code that runs every frame
42
+ end
43
+
44
+ end
45
+
46
+ #{app_name[0].upcase + app_name[1..-1]}.new.run!"
47
+ end
48
+ add_file "#{app_name}/#{app_name}.html" do
49
+ "<!DOCTYPE html>
50
+ <html>
51
+ <head>
52
+ <script src=\"http://code.jquery.com/jquery-1.11.0.min.js\"></script>
53
+ <script src=\"http://cdn.opalrb.org/opal/0.6.3/opal.min.js\"></script>
54
+ </head>
55
+ <body>
56
+ <script src=\"#{app_name}.js\"></script>
57
+ </body>
58
+ </html>"
59
+ end
60
+ end
61
+ end
62
+
63
+ DareCLI.start
data/lib/dare.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'dare/clock.rb'
2
+ require 'dare/sound.rb'
3
+ require 'dare/font.rb'
4
+ require 'dare/sprite.rb'
5
+ require 'dare/window.rb'
6
+
7
+ module Dare
8
+ VERSION = "0.0.2"
9
+ def self.offset_x(angle, magnitude)
10
+ `#{magnitude}*Math.cos(#{angle}*Math.PI/180.0)`
11
+ end
12
+ def self.offset_y(angle, magnitude)
13
+ `#{magnitude}*Math.sin(#{angle}*Math.PI/180.0)`
14
+ end
15
+ def self.ms
16
+ `(new Date()).getTime()`
17
+ end
18
+ end
data/lib/dare/clock.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Dare
2
+ class Clock
3
+
4
+ def initialize(opts = {})
5
+ opts[:update_interval] ||= 16.666
6
+ @update_interval = opts[:update_interval]
7
+ end
8
+
9
+ def start(&block)
10
+ @interval = `setInterval(function(){#{block.call}}, #{@update_interval})`
11
+ end
12
+
13
+ def stop
14
+ `clearInterval(#@interval)`
15
+ end
16
+ end
17
+ end
data/lib/dare/font.rb ADDED
@@ -0,0 +1,19 @@
1
+ module Dare
2
+ class Font
3
+ def initialize(window, font_type = "30px Arial")
4
+ @window = window
5
+ @font = font_type
6
+ @color = "red"
7
+ @x = 50
8
+ @y = 50
9
+ end
10
+
11
+ def draw(string = "", x = @x, y = @y)
12
+ %x{
13
+ #{@window.canvas.context}.font = #{@font};
14
+ #{@window.canvas.context}.fillStyle = #{@color};
15
+ #{@window.canvas.context}.fillText(#{string}, #{x}, #{y});
16
+ }
17
+ end
18
+ end
19
+ end
data/lib/dare/sound.rb ADDED
@@ -0,0 +1,16 @@
1
+ module Dare
2
+ class Sound
3
+
4
+ def initialize(path)
5
+ `var snd = new Audio(#{path})`
6
+ `snd.volume = 0.3`
7
+ @file = `snd`
8
+ end
9
+
10
+ def play
11
+ %x{
12
+ #{@file}.play();
13
+ }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,58 @@
1
+ module Dare
2
+ class Sprite
3
+ attr_accessor :images
4
+ def initialize(window)
5
+ @window = window
6
+ @images = []
7
+ @current_image = 0
8
+ @ticks_on_current_image = 0
9
+ @x = 0
10
+ @y = 0
11
+ end
12
+
13
+ def draw(x = 0, y = 0)
14
+ @images[@current_image].draw(x, y)
15
+ end
16
+
17
+ def update
18
+ if @window.button_down? Dare::KbRight
19
+ walk
20
+ else
21
+ stand
22
+ end
23
+ end
24
+
25
+ def walk
26
+ if @current_image == 0
27
+ @current_image = 2
28
+ @ticks_on_current_image = 0
29
+ elsif @current_image == 2
30
+ if @ticks_on_current_image >= 5
31
+ @current_image = 3
32
+ @ticks_on_current_image = 0
33
+ else
34
+ @ticks_on_current_image += 1
35
+ end
36
+ elsif @current_image == 3
37
+ if @ticks_on_current_image >= 5
38
+ @current_image = 4
39
+ @ticks_on_current_image = 0
40
+ else
41
+ @ticks_on_current_image += 1
42
+ end
43
+ elsif @current_image == 4
44
+ if @ticks_on_current_image >= 5
45
+ @current_image = 2
46
+ @ticks_on_current_image = 0
47
+ else
48
+ @ticks_on_current_image += 1
49
+ end
50
+ end
51
+ end
52
+
53
+ def stand
54
+ @current_image = 0
55
+ @ticks_on_current_image = 0
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,124 @@
1
+ module Dare
2
+ KbRight = 39
3
+ KbLeft = 37
4
+ class Canvas
5
+ attr_reader :id, :canvas
6
+ def initialize(opts = {})
7
+ opts[:width] ||= 640
8
+ opts[:height] ||= 480
9
+ opts[:border] ||= false
10
+ `var my_canvas = document.createElement("canvas")`
11
+ @id = rand(36**8).to_s(36)
12
+ `my_canvas.setAttribute('id', #{@id})`
13
+ `my_canvas.width = #{opts[:width]}`
14
+ `my_canvas.height = #{opts[:height]}`
15
+ `my_canvas.style.border = "solid 1px black"` if opts[:border]
16
+ `document.body.appendChild(my_canvas)`
17
+ @canvas = `my_canvas`
18
+ end
19
+ def context
20
+ `#{@canvas}.getContext('2d')`
21
+ end
22
+ end
23
+ class Window
24
+ attr_reader :width, :height, :ticks, :mouse_x, :mouse_y, :canvas, :key
25
+ def initialize(opts = {})
26
+ opts[:width] ||= 640
27
+ opts[:height] ||= 480
28
+ opts[:update_interval] ||= 16.666666
29
+ opts[:border] ||= false
30
+ opts[:clock] ||= Clock.new(update_interval: opts[:update_interval])
31
+ opts[:canvas] ||= Canvas.new(width: opts[:width], height: opts[:height], border: opts[:border])
32
+ opts[:no_mouse] ||= false
33
+ @width = opts[:width]
34
+ @height = opts[:height]
35
+ @clock = opts[:clock]
36
+ @ticks = 0
37
+ @canvas = opts[:canvas]
38
+ @keys = Array.new(108)
39
+ add_mouse_event_listener unless opts[:no_mouse]
40
+ add_keyboard_event_listeners
41
+ end
42
+ def run!
43
+ %x{
44
+ function anim_loop() {
45
+ requestAnimationFrame(anim_loop);
46
+ #{update};
47
+ #{@canvas.context}.clearRect(0, 0, #{width}, #{height});
48
+ #{draw};
49
+ }
50
+ requestAnimationFrame(anim_loop);
51
+ }
52
+ end
53
+ def update
54
+ @ticks += 1
55
+ end
56
+ def stop!
57
+ @clock.stop
58
+ end
59
+
60
+ def add_mouse_event_listener
61
+ Element.find("##{@canvas.id}").on :mousemove do |event|
62
+ coords = get_cursor_position(event)
63
+ @mouse_x = coords.x[:x]
64
+ @mouse_y = coords.x[:y]
65
+ end
66
+ end
67
+
68
+ def add_keyboard_event_listeners
69
+ Element.find("html").on :keydown do |event|
70
+ @keys[get_key_id(event)] = true
71
+ end
72
+ Element.find("html").on :keyup do |event|
73
+ @keys[get_key_id(event)] = false
74
+ end
75
+ end
76
+
77
+ def button_down?(button)
78
+ @keys[button]
79
+ end
80
+
81
+ def get_cursor_position(event)
82
+ if (event.page_x && event.page_y)
83
+ x = event.page_x;
84
+ y = event.page_y;
85
+ else
86
+ doc = Opal.Document[0]
87
+ x = event[:clientX] + doc.scrollLeft +
88
+ doc.documentElement.scrollLeft;
89
+ y = event[:clientY] + doc.body.scrollTop +
90
+ doc.documentElement.scrollTop;
91
+ end
92
+ x -= `#{@canvas.canvas}.offsetLeft`
93
+ y -= `#{@canvas.canvas}.offsetTop`
94
+ Coordinates.new(x: x, y: y)
95
+ end
96
+
97
+ def get_key_id(event)
98
+ event[:keyCode]
99
+ end
100
+
101
+ def draw_rect(opts = {})
102
+ x = opts[:top_left][0]
103
+ y = opts[:top_left][1]
104
+ width = opts[:width]
105
+ height = opts[:height]
106
+ color = opts[:color]
107
+
108
+ `#{@canvas.context}.fillStyle = #{color}`
109
+ `#{@canvas.context}.fillRect(#{x}, #{y}, #{width}, #{height})`
110
+ end
111
+
112
+ end
113
+ class Coordinates < Struct.new(:x, :y); end
114
+ class Image
115
+ def initialize(window, path = "")
116
+ @img = `new Image()`
117
+ `#{@img}.src = #{path}`
118
+ @window = window
119
+ end
120
+ def draw(x = 0, y = 0)
121
+ `#{@window.canvas.context}.drawImage(#{@img},#{x},#{y})`
122
+ end
123
+ end
124
+ end
metadata CHANGED
@@ -1,21 +1,102 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dare
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
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-04 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2014-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: 3.0.0
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.0'
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 3.0.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: pry
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.10'
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: 0.10.1
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ~>
62
+ - !ruby/object:Gem::Version
63
+ version: '0.10'
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: 0.10.1
67
+ - !ruby/object:Gem::Dependency
68
+ name: coveralls
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ version: '0.7'
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: 0.7.2
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: '0.7'
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: 0.7.2
13
87
  description:
14
88
  email: nicklink483@gmail.com
15
89
  executables: []
16
90
  extensions: []
17
91
  extra_rdoc_files: []
18
- files: []
92
+ files:
93
+ - bin/dare.rb
94
+ - lib/dare.rb
95
+ - lib/dare/clock.rb
96
+ - lib/dare/font.rb
97
+ - lib/dare/sound.rb
98
+ - lib/dare/sprite.rb
99
+ - lib/dare/window.rb
19
100
  homepage: https://github.com/nicklink483/dare
20
101
  licenses:
21
102
  - MIT
@@ -39,6 +120,6 @@ rubyforge_project:
39
120
  rubygems_version: 2.2.2
40
121
  signing_key:
41
122
  specification_version: 4
42
- summary: Ruby Web Game library on top of Opal
123
+ summary: Ruby 2D Game library on top of Opal
43
124
  test_files: []
44
125
  has_rdoc: