dxopal 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/.ignore +5 -0
  4. data/.nojekyll +4 -0
  5. data/CHANGELOG.md +90 -0
  6. data/DEVELOPMENT.md +57 -0
  7. data/Gemfile +2 -0
  8. data/Gemfile.lock +39 -0
  9. data/README.md +22 -0
  10. data/Rakefile +66 -0
  11. data/TODO.md +278 -0
  12. data/build/dxopal.js +46590 -0
  13. data/build/dxopal.min.js +1 -0
  14. data/config.ru +39 -0
  15. data/doc/api/DXOpal.html +129 -0
  16. data/doc/api/DXOpal/Font.html +485 -0
  17. data/doc/api/DXOpal/Image.html +2533 -0
  18. data/doc/api/DXOpal/Input.html +1086 -0
  19. data/doc/api/DXOpal/Input/MouseCodes.html +146 -0
  20. data/doc/api/DXOpal/RemoteResource.html +641 -0
  21. data/doc/api/DXOpal/Sound.html +568 -0
  22. data/doc/api/DXOpal/SoundEffect.html +444 -0
  23. data/doc/api/DXOpal/SoundEffect/WaveTypes.html +130 -0
  24. data/doc/api/DXOpal/Sprite.html +1419 -0
  25. data/doc/api/DXOpal/Window.html +1915 -0
  26. data/doc/api/_index.html +228 -0
  27. data/doc/api/class_list.html +51 -0
  28. data/doc/api/css/common.css +1 -0
  29. data/doc/api/css/full_list.css +58 -0
  30. data/doc/api/css/style.css +492 -0
  31. data/doc/api/file.CHANGELOG.html +162 -0
  32. data/doc/api/file.README.html +124 -0
  33. data/doc/api/file_list.html +61 -0
  34. data/doc/api/frames.html +17 -0
  35. data/doc/api/index.html +124 -0
  36. data/doc/api/js/app.js +248 -0
  37. data/doc/api/js/full_list.js +216 -0
  38. data/doc/api/js/jquery.js +4 -0
  39. data/doc/api/method_list.html +939 -0
  40. data/doc/api/top-level-namespace.html +110 -0
  41. data/doc/en/index.html +93 -0
  42. data/doc/ja/index.html +92 -0
  43. data/dxopal.gemspec +29 -0
  44. data/exe/dxopal +44 -0
  45. data/index.html +56 -0
  46. data/opal/dxopal.rb +54 -0
  47. data/opal/dxopal/constants/colors.rb +16 -0
  48. data/opal/dxopal/font.rb +20 -0
  49. data/opal/dxopal/image.rb +301 -0
  50. data/opal/dxopal/input.rb +170 -0
  51. data/opal/dxopal/input/key_codes.rb +168 -0
  52. data/opal/dxopal/remote_resource.rb +65 -0
  53. data/opal/dxopal/sound.rb +53 -0
  54. data/opal/dxopal/sound_effect.rb +84 -0
  55. data/opal/dxopal/sprite.rb +94 -0
  56. data/opal/dxopal/sprite/collision_area.rb +288 -0
  57. data/opal/dxopal/sprite/collision_check.rb +106 -0
  58. data/opal/dxopal/sprite/collision_checker.rb +169 -0
  59. data/opal/dxopal/sprite/physics.rb +82 -0
  60. data/opal/dxopal/version.rb +3 -0
  61. data/opal/dxopal/window.rb +173 -0
  62. data/template/index.html +13 -0
  63. data/template/main.rb +9 -0
  64. metadata +191 -0
@@ -0,0 +1,82 @@
1
+ module DXOpal
2
+ class Sprite
3
+ # Physics engine powered by Matter.js
4
+ module Physics
5
+ # Create Matter Body and register it to the World
6
+ # - type: :rectangle, etc.
7
+ def physical_body=(ary)
8
+ raise "Call Sprite#initialize before calling physical_body=" if self.x.nil?
9
+ type = ary[0]
10
+ case type
11
+ when :rectangle
12
+ _, width, height, opts = *ary
13
+ x = self.x + width/2
14
+ y = self.y + height/2
15
+ info = [width, height]
16
+ `opts.angle = opts.angle || #{self.angle * Math::PI / 180}`
17
+ @_matter_body = `Matter.Bodies[type](x, y, width, height, opts)`
18
+ else
19
+ raise "type #{type.inspect} is unknown or not supported yet"
20
+ end
21
+ Sprite._add_matter_body(@_matter_body, type, self, info)
22
+ end
23
+ attr_reader :_matter_body
24
+
25
+ def _move_matter_body
26
+ # TODO: support non-default center_x, center_y
27
+ `Matter.Body.setPosition(#{@_matter_body},
28
+ Matter.Vector.create(#{@x+@center_x}, #{@y+@center_y}))`
29
+ end
30
+
31
+ def _move_to_matter_body(mx, my)
32
+ @x = mx - @center_x
33
+ @y = my - @center_y
34
+ end
35
+ end
36
+
37
+ # (internal) Matter.Engine instance
38
+ def self._matter_engine
39
+ @matter_engine ||= `Matter.Engine.create()`
40
+ end
41
+
42
+ # (internal) Matter.Runner instance
43
+ def self._matter_runner
44
+ @matter_runner ||= `Matter.Runner.create()`
45
+ end
46
+
47
+ # (internal)
48
+ def self._matter_sprites
49
+ @matter_bodies ||= {}
50
+ end
51
+
52
+ def self._add_matter_body(body, type, sprite, info)
53
+ _matter_sprites[`body.id`] = [type, sprite, info]
54
+ `Matter.World.addBody(#{Sprite._matter_engine}.world, body)`
55
+ end
56
+
57
+ # Return true if `physical_body=` is ever called
58
+ def self.matter_enabled?
59
+ `!!#{@matter_engine}`
60
+ end
61
+
62
+ # Call Matter.Runner.tick
63
+ # - time: time given by requestAnimationFrame
64
+ def self.matter_tick(time)
65
+ %x{
66
+ Matter.Runner.tick(#{Sprite._matter_runner}, #{Sprite._matter_engine}, time);
67
+ Matter.Composite.allBodies(#{Sprite._matter_engine}.world).forEach((body) => {
68
+ var [type, sprite, info] = #{Sprite._matter_sprites[`body.id`]};
69
+ switch(type) {
70
+ case "rectangle":
71
+ var [width, height] = info;
72
+ sprite['$_move_to_matter_body'](body.position.x, body.position.y);
73
+ sprite['$angle='](body.angle / Math.PI * 180);
74
+ break;
75
+ default:
76
+ `#{raise "unknown type: #{type}"}`
77
+ }
78
+ });
79
+ }
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ module DXOpal
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,173 @@
1
+ require 'dxopal/constants/colors'
2
+
3
+ module DXOpal
4
+ module Window
5
+ @@fps = 60
6
+ @@real_fps = 0
7
+ @@real_fps_ct = 1
8
+ @@real_fps_t = Time.now
9
+ @@width = 640
10
+ @@height = 480
11
+ @@block = nil
12
+ @@paused = false
13
+
14
+ # Load resources specified with Image.register or Sound.register
15
+ # Call block when loaded
16
+ def self.load_resources(&block)
17
+ RemoteResource._load_resources(&block)
18
+ end
19
+
20
+ # Start main loop
21
+ #
22
+ # When called twice, previous loop is stopped (this is useful
23
+ # when implementing interactive game editor, etc.)
24
+ def self.loop(&block)
25
+ @@block = block
26
+ `window`.JS.requestAnimationFrame{|time| _loop(time) }
27
+ end
28
+
29
+ # (DXOpal original) Pause & resume
30
+ def self.pause
31
+ @@paused = true
32
+ @@draw_queue.clear
33
+ draw_pause_screen
34
+ end
35
+ def self.paused?; @@paused; end
36
+ def self.resume
37
+ raise "Window.resume is called before Window.loop" if @@block.nil?
38
+ @@paused = false; Window.loop(&@@block)
39
+ end
40
+ def self.draw_pause_screen
41
+ Window.draw_box_fill(0, 0, Window.width, Window.height, C_BLACK)
42
+ Window.draw_font(0, 0, "...PAUSE...", Font.default, color: C_WHITE)
43
+ end
44
+
45
+ # (internal) call @@block periodically
46
+ def self._loop(time)
47
+ @@img ||= _init(@@width, @@height)
48
+ t0 = Time.now
49
+
50
+ # Calculate fps
51
+ if t0 - @@real_fps_t >= 1.0
52
+ @@real_fps = @@real_fps_ct
53
+ @@real_fps_ct = 1
54
+ @@real_fps_t = t0
55
+ else
56
+ @@real_fps_ct += 1
57
+ end
58
+
59
+ # Update physics
60
+ Sprite.matter_tick(time) if Sprite.matter_enabled?
61
+
62
+ # Detect inputs
63
+ Input._on_tick
64
+
65
+ # Call user code
66
+ @@draw_queue = []
67
+ if @@paused
68
+ Window.draw_pause_screen
69
+ else
70
+ @@block.call
71
+ end
72
+
73
+ # Draw
74
+ @@img.box_fill(0, 0, @@width, @@height, @@bgcolor)
75
+ sorted = @@draw_queue.sort{|a, b| a[0] == b[0] ? a[1] <=> b[1] : a[0] <=> a[1] }
76
+ sorted.each do |item|
77
+ case item[2]
78
+ when :image then @@img.draw(*item.drop(3))
79
+ when :image_rot then @@img.draw_rot(*item.drop(3))
80
+ when :draw_ex then @@img.draw_ex(*item.drop(3))
81
+ when :font then @@img.draw_font(*item.drop(3))
82
+ when :pixel then @@img.[]=(*item.drop(3))
83
+ when :line then @@img.line(*item.drop(3))
84
+ when :box then @@img.box(*item.drop(3))
85
+ when :box_fill then @@img.box_fill(*item.drop(3))
86
+ when :circle then @@img.circle(*item.drop(3))
87
+ when :circle_fill then @@img.circle_fill(*item.drop(3))
88
+ when :triangle then @@img.triangle(*item.drop(3))
89
+ when :triangle_fill then @@img.triangle_fill(*item.drop(3))
90
+ end
91
+ end
92
+
93
+ `window`.JS.requestAnimationFrame{|time| _loop(time) }
94
+ end
95
+
96
+ def self._init(w, h)
97
+ canvas = `document.getElementById("dxopal-canvas")`
98
+ `canvas.width = w`
99
+ `canvas.height = h`
100
+ img = Image.new(w, h, canvas: canvas)
101
+ Input._init(canvas)
102
+ return img
103
+ end
104
+
105
+ # Return internal DXOpal::Image object (for experimental/hacking use)
106
+ def self._img; @@img; end
107
+
108
+ def self.fps; @@fps; end
109
+ def self.fps=(w); @@fps = w; end
110
+ def self.real_fps; @@real_fps; end
111
+ def self.width; @@width; end
112
+ def self.width=(w); @@width = w; end
113
+ def self.height; @@height; end
114
+ def self.height=(h); @@height = h; end
115
+ @@bgcolor = Constants::Colors::C_BLACK
116
+ def self.bgcolor; @@bgcolor; end
117
+ def self.bgcolor=(col); @@bgcolor = col; end
118
+
119
+ def self.draw(x, y, image, z=0)
120
+ enqueue_draw(z, :image, x, y, image)
121
+ end
122
+
123
+ def self.draw_rot(x, y, image, angle, center_x=nil, center_y=nil, z=0)
124
+ enqueue_draw(z, :image_rot, x, y, image, angle, center_x, center_y)
125
+ end
126
+
127
+ def self.draw_ex(x, y, image, options={})
128
+ enqueue_draw(options[:z] || 0, :draw_ex, x, y, image, options)
129
+ end
130
+
131
+ def self.draw_font(x, y, string, font, option={})
132
+ z = option[:z] || 0
133
+ color = option[:color] || [255, 255, 255]
134
+ enqueue_draw(z, :font, x, y, string, font, color)
135
+ end
136
+
137
+ def self.draw_pixel(x, y, color, z=0)
138
+ enqueue_draw(z, :pixel, x, y, color)
139
+ end
140
+
141
+ def self.draw_line(x1, y1, x2, y2, color, z=0)
142
+ enqueue_draw(z, :line, x1, y1, x2, y2, color)
143
+ end
144
+
145
+ def self.draw_box(x1, y1, x2, y2, color, z=0)
146
+ enqueue_draw(z, :box, x1, y1, x2, y2, color)
147
+ end
148
+
149
+ def self.draw_box_fill(x1, y1, x2, y2, color, z=0)
150
+ enqueue_draw(z, :box_fill, x1, y1, x2, y2, color)
151
+ end
152
+
153
+ def self.draw_circle(x, y, r, color, z=0)
154
+ enqueue_draw(z, :circle, x, y, r, color)
155
+ end
156
+
157
+ def self.draw_circle_fill(x, y, r, color, z=0)
158
+ enqueue_draw(z, :circle_fill, x, y, r, color)
159
+ end
160
+
161
+ def self.draw_triangle(x1, y1, x2, y2, x3, y3, color, z=0)
162
+ enqueue_draw(z, :triangle, x1, y1, x2, y2, x3, y3, color)
163
+ end
164
+
165
+ def self.draw_triangle_fill(x1, y1, x2, y2, x3, y3, color, z=0)
166
+ enqueue_draw(z, :triangle_fill, x1, y1, x2, y2, x3, y3, color)
167
+ end
168
+
169
+ def self.enqueue_draw(z, *args)
170
+ @@draw_queue.push([z, @@draw_queue.length, *args])
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset= "utf-8" />
5
+ <title>DXOpal game</title>
6
+ <script type="text/javascript" src="dxopal.min.js"></script>
7
+ <script type="text/ruby" src="main.rb"></script>
8
+ </head>
9
+
10
+ <body>
11
+ <canvas id="dxopal-canvas"></canvas>
12
+ </body>
13
+ </html>
data/template/main.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'dxopal'
2
+ include DXOpal
3
+ Window.load_resources do
4
+ Window.bgcolor = C_BLACK
5
+
6
+ Window.loop do
7
+ Window.draw_font(0, 0, "Hello!", Font.default, color: C_WHITE)
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dxopal
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yutaka HARA
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-27 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.10.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.19.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.19.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.9.9
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.9.9
97
+ description:
98
+ email:
99
+ - yutaka.hara+github@gmail.com
100
+ executables:
101
+ - dxopal
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".ignore"
107
+ - ".nojekyll"
108
+ - CHANGELOG.md
109
+ - DEVELOPMENT.md
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - README.md
113
+ - Rakefile
114
+ - TODO.md
115
+ - build/dxopal.js
116
+ - build/dxopal.min.js
117
+ - config.ru
118
+ - doc/api/DXOpal.html
119
+ - doc/api/DXOpal/Font.html
120
+ - doc/api/DXOpal/Image.html
121
+ - doc/api/DXOpal/Input.html
122
+ - doc/api/DXOpal/Input/MouseCodes.html
123
+ - doc/api/DXOpal/RemoteResource.html
124
+ - doc/api/DXOpal/Sound.html
125
+ - doc/api/DXOpal/SoundEffect.html
126
+ - doc/api/DXOpal/SoundEffect/WaveTypes.html
127
+ - doc/api/DXOpal/Sprite.html
128
+ - doc/api/DXOpal/Window.html
129
+ - doc/api/_index.html
130
+ - doc/api/class_list.html
131
+ - doc/api/css/common.css
132
+ - doc/api/css/full_list.css
133
+ - doc/api/css/style.css
134
+ - doc/api/file.CHANGELOG.html
135
+ - doc/api/file.README.html
136
+ - doc/api/file_list.html
137
+ - doc/api/frames.html
138
+ - doc/api/index.html
139
+ - doc/api/js/app.js
140
+ - doc/api/js/full_list.js
141
+ - doc/api/js/jquery.js
142
+ - doc/api/method_list.html
143
+ - doc/api/top-level-namespace.html
144
+ - doc/en/index.html
145
+ - doc/ja/index.html
146
+ - dxopal.gemspec
147
+ - exe/dxopal
148
+ - index.html
149
+ - opal/dxopal.rb
150
+ - opal/dxopal/constants/colors.rb
151
+ - opal/dxopal/font.rb
152
+ - opal/dxopal/image.rb
153
+ - opal/dxopal/input.rb
154
+ - opal/dxopal/input/key_codes.rb
155
+ - opal/dxopal/remote_resource.rb
156
+ - opal/dxopal/sound.rb
157
+ - opal/dxopal/sound_effect.rb
158
+ - opal/dxopal/sprite.rb
159
+ - opal/dxopal/sprite/collision_area.rb
160
+ - opal/dxopal/sprite/collision_check.rb
161
+ - opal/dxopal/sprite/collision_checker.rb
162
+ - opal/dxopal/sprite/physics.rb
163
+ - opal/dxopal/version.rb
164
+ - opal/dxopal/window.rb
165
+ - template/index.html
166
+ - template/main.rb
167
+ homepage: https://github.com/yhara/dxopal
168
+ licenses:
169
+ - MIT
170
+ metadata: {}
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubyforge_project:
187
+ rubygems_version: 2.6.13
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: Game development framework for Opal
191
+ test_files: []