rubysketch 0.3.21 → 0.5.1
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 +4 -4
- data/.github/workflows/{release.yml → release-gem.yml} +21 -21
- data/.github/workflows/tag.yml +35 -0
- data/.github/workflows/test.yml +8 -9
- data/.github/workflows/utils.rb +55 -0
- data/ChangeLog.md +7 -150
- data/LICENSE +1 -1
- data/README.md +1 -1
- data/Rakefile +10 -34
- data/RubySketch.podspec +4 -3
- data/VERSION +1 -1
- data/examples/hello.rb +6 -6
- data/lib/rubysketch/all.rb +9 -0
- data/lib/rubysketch/context.rb +47 -0
- data/lib/rubysketch/{module.rb → extension.rb} +3 -3
- data/lib/rubysketch/graphics_context.rb +37 -0
- data/lib/rubysketch/helper.rb +5 -0
- data/lib/rubysketch/sprite.rb +101 -0
- data/lib/rubysketch.rb +30 -12
- data/rubysketch.gemspec +15 -8
- data/src/{RubySketch.h → RubyProcessing.h} +0 -4
- data/src/RubyProcessing.mm +25 -0
- data/test/helper.rb +3 -10
- data/test/test_sprite.rb +23 -0
- metadata +119 -29
- data/examples/breakout.rb +0 -212
- data/examples/camera.rb +0 -14
- data/examples/clock.rb +0 -57
- data/examples/delay_camera.rb +0 -33
- data/examples/glsl.rb +0 -14
- data/examples/image.rb +0 -13
- data/examples/shapes.rb +0 -121
- data/lib/rubysketch/app.rb +0 -13
- data/lib/rubysketch/glsl.rb +0 -38
- data/lib/rubysketch/processing.rb +0 -2910
- data/lib/rubysketch/window.rb +0 -205
- data/lib/rubysketch-glsl.rb +0 -11
- data/lib/rubysketch-processing.rb +0 -25
- data/src/RubySketch.mm +0 -55
- data/test/processing/test_utility.rb +0 -34
- data/test/processing/test_vector.rb +0 -394
@@ -0,0 +1,101 @@
|
|
1
|
+
module RubySketch
|
2
|
+
|
3
|
+
|
4
|
+
class Sprite
|
5
|
+
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def initialize(x = 0, y = 0, w = nil, h = nil, image: nil, offset: nil, dynamic: false)
|
9
|
+
w ||= image&.width || 0
|
10
|
+
h ||= image&.height || 0
|
11
|
+
raise 'invalid size' unless w >= 0 && h >= 0
|
12
|
+
|
13
|
+
@image__, @offset__ = image, offset
|
14
|
+
@view__ = View.new self, x: x, y: y, w: w, h: h, back: :white, dynamic: dynamic
|
15
|
+
end
|
16
|
+
|
17
|
+
def_delegators :@view__,
|
18
|
+
:x, :x=,
|
19
|
+
:y, :y=,
|
20
|
+
:w, :h,
|
21
|
+
:width, :height,
|
22
|
+
:dynamic?, :dynamic=,
|
23
|
+
:density, :density=,
|
24
|
+
:friction, :friction=,
|
25
|
+
:restitution, :restitution=
|
26
|
+
|
27
|
+
def update(&block)
|
28
|
+
@view__.update = block
|
29
|
+
end
|
30
|
+
|
31
|
+
def contact(&block)
|
32
|
+
@view__.contact = block
|
33
|
+
end
|
34
|
+
|
35
|
+
def position()
|
36
|
+
@view__.position.toVector
|
37
|
+
end
|
38
|
+
|
39
|
+
def position=(vector)
|
40
|
+
@view__.position = vector.getInternal__
|
41
|
+
end
|
42
|
+
|
43
|
+
def size()
|
44
|
+
@view__.size.toVector
|
45
|
+
end
|
46
|
+
|
47
|
+
def image()
|
48
|
+
@image__
|
49
|
+
end
|
50
|
+
|
51
|
+
def offset()
|
52
|
+
@offset__
|
53
|
+
end
|
54
|
+
|
55
|
+
def velocity()
|
56
|
+
@view__.velocity.toVector
|
57
|
+
end
|
58
|
+
|
59
|
+
def velocity=(vector)
|
60
|
+
@view__.velocity = vector.getInternal__
|
61
|
+
end
|
62
|
+
|
63
|
+
alias pos position
|
64
|
+
alias pos= position=
|
65
|
+
alias vel velocity
|
66
|
+
alias vel= velocity=
|
67
|
+
alias dens density
|
68
|
+
alias dens= density=
|
69
|
+
alias fric friction
|
70
|
+
alias fric= friction
|
71
|
+
alias rest restitution
|
72
|
+
alias rest= restitution=
|
73
|
+
|
74
|
+
# @private
|
75
|
+
def getInternal__()
|
76
|
+
@view__
|
77
|
+
end
|
78
|
+
|
79
|
+
class View < Reflex::View
|
80
|
+
attr_accessor :update, :contact
|
81
|
+
attr_reader :sprite
|
82
|
+
|
83
|
+
def initialize(sprite, *a, **k, &b)
|
84
|
+
@sprite = sprite
|
85
|
+
super(*a, **k, &b)
|
86
|
+
end
|
87
|
+
|
88
|
+
def on_update(e)
|
89
|
+
@update.call if @update
|
90
|
+
end
|
91
|
+
|
92
|
+
def on_contact(e)
|
93
|
+
v = e.view
|
94
|
+
@contact.call v.sprite, e.action if @contact && v.is_a?(View)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end# Sprite
|
99
|
+
|
100
|
+
|
101
|
+
end# RubySketch
|
data/lib/rubysketch.rb
CHANGED
@@ -1,12 +1,30 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
require 'rubysketch/all'
|
2
|
+
|
3
|
+
|
4
|
+
module RubySketch
|
5
|
+
WINDOW = Processing::Window.new {start}
|
6
|
+
CONTEXT = RubySketch::Context.new WINDOW
|
7
|
+
|
8
|
+
refine Object do
|
9
|
+
(CONTEXT.methods - Object.instance_methods).each do |method|
|
10
|
+
define_method method do |*args, **kwargs, &block|
|
11
|
+
CONTEXT.__send__ method, *args, **kwargs, &block
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end# RubySketch
|
16
|
+
|
17
|
+
|
18
|
+
begin
|
19
|
+
w, c = RubySketch::WINDOW, RubySketch::CONTEXT
|
20
|
+
|
21
|
+
c.class.constants.each do |const|
|
22
|
+
self.class.const_set const, c.class.const_get(const)
|
23
|
+
end
|
24
|
+
|
25
|
+
w.__send__ :begin_draw
|
26
|
+
at_exit do
|
27
|
+
w.__send__ :end_draw
|
28
|
+
Processing::App.new {w.show}.start if c.hasDrawBlock__ && !$!
|
29
|
+
end
|
30
|
+
end
|
data/rubysketch.gemspec
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
File.expand_path('lib', __dir__)
|
5
5
|
.tap {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
|
6
6
|
|
7
|
-
require 'rubysketch/
|
7
|
+
require 'rubysketch/extension'
|
8
8
|
|
9
9
|
|
10
10
|
Gem::Specification.new do |s|
|
@@ -12,24 +12,31 @@ Gem::Specification.new do |s|
|
|
12
12
|
patterns.map {|pat| Dir.glob(pat).to_a}.flatten
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
name =
|
15
|
+
ext = RubySketch::Extension
|
16
|
+
name = ext.name.downcase
|
17
17
|
rdocs = glob.call *%w[README]
|
18
18
|
|
19
19
|
s.name = name
|
20
|
-
s.summary = '
|
21
|
-
s.description = '
|
22
|
-
s.version =
|
20
|
+
s.summary = 'A game engine based on the Processing API.'
|
21
|
+
s.description = 'A game engine based on the Processing API.'
|
22
|
+
s.version = ext.version
|
23
23
|
|
24
24
|
s.authors = %w[xordog]
|
25
25
|
s.email = 'xordog@gmail.com'
|
26
26
|
s.homepage = "https://github.com/xord/rubysketch"
|
27
27
|
|
28
28
|
s.platform = Gem::Platform::RUBY
|
29
|
-
s.required_ruby_version = '>= 2.
|
29
|
+
s.required_ruby_version = '>= 2.7.0'
|
30
30
|
|
31
|
-
s.add_runtime_dependency '
|
31
|
+
s.add_runtime_dependency 'xot', '~> 0.1.32'
|
32
|
+
s.add_runtime_dependency 'rucy', '~> 0.1.32'
|
33
|
+
s.add_runtime_dependency 'beeps', '~> 0.1.32'
|
34
|
+
s.add_runtime_dependency 'rays', '~> 0.1.32'
|
35
|
+
s.add_runtime_dependency 'reflexion', '~> 0.1.32'
|
36
|
+
s.add_runtime_dependency 'processing', '~> 0.5.1'
|
32
37
|
|
38
|
+
s.add_development_dependency 'rake'
|
39
|
+
s.add_development_dependency 'test-unit'
|
33
40
|
s.add_development_dependency 'yard'
|
34
41
|
|
35
42
|
s.files = `git ls-files`.split $/
|
@@ -0,0 +1,25 @@
|
|
1
|
+
// -*- mode: objc -*-
|
2
|
+
#import <CRuby.h>
|
3
|
+
#import "RubyProcessing.h"
|
4
|
+
#import "RubySketch.h"
|
5
|
+
|
6
|
+
|
7
|
+
@implementation RubySketch
|
8
|
+
|
9
|
+
+ (void) setup
|
10
|
+
{
|
11
|
+
static BOOL done = NO;
|
12
|
+
if (done) return;
|
13
|
+
done = YES;
|
14
|
+
|
15
|
+
[RubyProcessing setup];
|
16
|
+
[CRuby addLibrary:@"RubySketch" bundle:[NSBundle bundleForClass:RubySketch.class]];
|
17
|
+
}
|
18
|
+
|
19
|
+
+ (void) start: (NSString*) path
|
20
|
+
{
|
21
|
+
[CRuby evaluate:@"raise 'already started' unless require 'rubysketch'\n"];
|
22
|
+
[RubyProcessing start: path];
|
23
|
+
}
|
24
|
+
|
25
|
+
@end
|
data/test/helper.rb
CHANGED
@@ -1,19 +1,12 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
3
|
|
4
|
-
%w[../xot ../rucy ../rays ../reflex .]
|
5
|
-
.map {|s| File.expand_path "
|
4
|
+
%w[../xot ../rucy ../rays ../reflex ../processing .]
|
5
|
+
.map {|s| File.expand_path "../#{s}/lib", __dir__}
|
6
6
|
.each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
|
7
7
|
|
8
8
|
require 'test/unit'
|
9
9
|
require 'xot/test'
|
10
|
-
require 'rubysketch'
|
10
|
+
require 'rubysketch/all'
|
11
11
|
|
12
12
|
include Xot::Test
|
13
|
-
|
14
|
-
|
15
|
-
def assert_equal_vector(v1, v2, delta = 0.000001)
|
16
|
-
assert_in_delta v1.x, v2.x, delta
|
17
|
-
assert_in_delta v1.y, v2.y, delta
|
18
|
-
assert_in_delta v1.z, v2.z, delta
|
19
|
-
end
|
data/test/test_sprite.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
|
4
|
+
require_relative 'helper'
|
5
|
+
|
6
|
+
|
7
|
+
class TestSprite < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def sprite(*args, **kwargs)
|
10
|
+
RubySketch::Sprite.new(*args, **kwargs)
|
11
|
+
end
|
12
|
+
|
13
|
+
def vec(*args, **kwargs)
|
14
|
+
Processing::Vector.new(*args, **kwargs)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_initialize()
|
18
|
+
assert_equal 0, sprite.x
|
19
|
+
assert_equal 0, sprite.y
|
20
|
+
assert_equal vec(0, 0), sprite.pos
|
21
|
+
end
|
22
|
+
|
23
|
+
end# TestSprite
|
metadata
CHANGED
@@ -1,29 +1,127 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubysketch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- xordog
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: xot
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.32
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.32
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rucy
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.1.32
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.1.32
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: beeps
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.1.32
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.1.32
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rays
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.1.32
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.1.32
|
13
69
|
- !ruby/object:Gem::Dependency
|
14
70
|
name: reflexion
|
15
71
|
requirement: !ruby/object:Gem::Requirement
|
16
72
|
requirements:
|
17
73
|
- - "~>"
|
18
74
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.1.
|
75
|
+
version: 0.1.32
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.1.32
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: processing
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.5.1
|
20
90
|
type: :runtime
|
21
91
|
prerelease: false
|
22
92
|
version_requirements: !ruby/object:Gem::Requirement
|
23
93
|
requirements:
|
24
94
|
- - "~>"
|
25
95
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.1
|
96
|
+
version: 0.5.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: test-unit
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
27
125
|
- !ruby/object:Gem::Dependency
|
28
126
|
name: yard
|
29
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,14 +136,16 @@ dependencies:
|
|
38
136
|
- - ">="
|
39
137
|
- !ruby/object:Gem::Version
|
40
138
|
version: '0'
|
41
|
-
description:
|
139
|
+
description: A game engine based on the Processing API.
|
42
140
|
email: xordog@gmail.com
|
43
141
|
executables: []
|
44
142
|
extensions: []
|
45
143
|
extra_rdoc_files: []
|
46
144
|
files:
|
47
|
-
- ".github/workflows/release.yml"
|
145
|
+
- ".github/workflows/release-gem.yml"
|
146
|
+
- ".github/workflows/tag.yml"
|
48
147
|
- ".github/workflows/test.yml"
|
148
|
+
- ".github/workflows/utils.rb"
|
49
149
|
- ".gitignore"
|
50
150
|
- ".yardopts"
|
51
151
|
- ChangeLog.md
|
@@ -55,28 +155,19 @@ files:
|
|
55
155
|
- Rakefile
|
56
156
|
- RubySketch.podspec
|
57
157
|
- VERSION
|
58
|
-
- examples/breakout.rb
|
59
|
-
- examples/camera.rb
|
60
|
-
- examples/clock.rb
|
61
|
-
- examples/delay_camera.rb
|
62
|
-
- examples/glsl.rb
|
63
158
|
- examples/hello.rb
|
64
|
-
- examples/image.rb
|
65
|
-
- examples/shapes.rb
|
66
|
-
- lib/rubysketch-glsl.rb
|
67
|
-
- lib/rubysketch-processing.rb
|
68
159
|
- lib/rubysketch.rb
|
69
|
-
- lib/rubysketch/
|
70
|
-
- lib/rubysketch/
|
71
|
-
- lib/rubysketch/
|
72
|
-
- lib/rubysketch/
|
73
|
-
- lib/rubysketch/
|
160
|
+
- lib/rubysketch/all.rb
|
161
|
+
- lib/rubysketch/context.rb
|
162
|
+
- lib/rubysketch/extension.rb
|
163
|
+
- lib/rubysketch/graphics_context.rb
|
164
|
+
- lib/rubysketch/helper.rb
|
165
|
+
- lib/rubysketch/sprite.rb
|
74
166
|
- rubysketch.gemspec
|
75
|
-
- src/
|
76
|
-
- src/
|
167
|
+
- src/RubyProcessing.h
|
168
|
+
- src/RubyProcessing.mm
|
77
169
|
- test/helper.rb
|
78
|
-
- test/
|
79
|
-
- test/processing/test_vector.rb
|
170
|
+
- test/test_sprite.rb
|
80
171
|
homepage: https://github.com/xord/rubysketch
|
81
172
|
licenses: []
|
82
173
|
metadata: {}
|
@@ -88,18 +179,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
179
|
requirements:
|
89
180
|
- - ">="
|
90
181
|
- !ruby/object:Gem::Version
|
91
|
-
version: 2.
|
182
|
+
version: 2.7.0
|
92
183
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
184
|
requirements:
|
94
185
|
- - ">="
|
95
186
|
- !ruby/object:Gem::Version
|
96
187
|
version: '0'
|
97
188
|
requirements: []
|
98
|
-
rubygems_version: 3.
|
189
|
+
rubygems_version: 3.4.6
|
99
190
|
signing_key:
|
100
191
|
specification_version: 4
|
101
|
-
summary:
|
192
|
+
summary: A game engine based on the Processing API.
|
102
193
|
test_files:
|
103
194
|
- test/helper.rb
|
104
|
-
- test/
|
105
|
-
- test/processing/test_vector.rb
|
195
|
+
- test/test_sprite.rb
|
data/examples/breakout.rb
DELETED
@@ -1,212 +0,0 @@
|
|
1
|
-
%w[xot rays reflex rubysketch]
|
2
|
-
.map {|s| File.expand_path "../../#{s}/lib", __dir__}
|
3
|
-
.each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
|
4
|
-
|
5
|
-
require 'rubysketch-processing'
|
6
|
-
|
7
|
-
|
8
|
-
PADDING = 50
|
9
|
-
BRICK_COUNT = 10
|
10
|
-
|
11
|
-
$objs = []
|
12
|
-
$bar = nil
|
13
|
-
$gameover = false
|
14
|
-
|
15
|
-
setup do
|
16
|
-
addWalls
|
17
|
-
addBricks
|
18
|
-
|
19
|
-
colorMode HSB, 360, 100, 100
|
20
|
-
ellipseMode CORNER
|
21
|
-
background 0
|
22
|
-
noStroke
|
23
|
-
fill 0, 0, 100
|
24
|
-
end
|
25
|
-
|
26
|
-
draw do
|
27
|
-
background 0
|
28
|
-
$objs.each do |o|
|
29
|
-
o.update
|
30
|
-
o.draw
|
31
|
-
end
|
32
|
-
drawTexts
|
33
|
-
end
|
34
|
-
|
35
|
-
mousePressed do
|
36
|
-
start unless started?
|
37
|
-
end
|
38
|
-
|
39
|
-
mouseDragged do
|
40
|
-
$bar.pos.x = mouseX - $bar.w / 2 if $bar
|
41
|
-
end
|
42
|
-
|
43
|
-
def start()
|
44
|
-
$bar = Bar.new
|
45
|
-
$objs += [$bar, Ball.new($bar)]
|
46
|
-
end
|
47
|
-
|
48
|
-
def started?()
|
49
|
-
$bar
|
50
|
-
end
|
51
|
-
|
52
|
-
def gameover?()
|
53
|
-
started? &&
|
54
|
-
$objs.count {|o| o.kind_of? Ball} == 0
|
55
|
-
end
|
56
|
-
|
57
|
-
def cleared?()
|
58
|
-
started? && !gameover? &&
|
59
|
-
$objs.count {|o| o.kind_of? Brick} == 0
|
60
|
-
end
|
61
|
-
|
62
|
-
def addWalls()
|
63
|
-
left = Obj.new 0, 0, 10, height
|
64
|
-
top = Obj.new 0, 0, width, 10
|
65
|
-
right = Obj.new width - 10, 0, 10, height
|
66
|
-
bottom = Bottom.new 0, height - 10, width, 10
|
67
|
-
$objs += [top, bottom, left, right]
|
68
|
-
end
|
69
|
-
|
70
|
-
def addBricks()
|
71
|
-
brickW = (width - PADDING * 2) / BRICK_COUNT
|
72
|
-
5.times do |y|
|
73
|
-
BRICK_COUNT.times do |x|
|
74
|
-
xx = PADDING + brickW * x
|
75
|
-
yy = PADDING + 30 * y
|
76
|
-
$objs.push Brick.new(xx, yy, brickW - 5, 20, y * 60)
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def drawTexts()
|
82
|
-
push do
|
83
|
-
textAlign CENTER, CENTER
|
84
|
-
|
85
|
-
if !started?
|
86
|
-
fill 50, 100, 100
|
87
|
-
textSize 50
|
88
|
-
text "BREAKOUT", 0, 0, width, height
|
89
|
-
textSize 20
|
90
|
-
translate 0, 100
|
91
|
-
text "Tap to start!", 0, 0, width, height
|
92
|
-
elsif cleared?
|
93
|
-
fill 100, 80, 100
|
94
|
-
textSize 50
|
95
|
-
text "CLEAR!", 0, 0, width, height
|
96
|
-
elsif gameover?
|
97
|
-
fill 0, 20, 100
|
98
|
-
textSize 50
|
99
|
-
text "GAMEOVER", 0, 0, width, height
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
class Obj
|
105
|
-
attr_reader :pos, :w, :h, :vel
|
106
|
-
|
107
|
-
def initialize(x, y, w, h, vx = 0, vy = 0)
|
108
|
-
@pos = createVector x, y
|
109
|
-
@w, @h = w, h
|
110
|
-
@vel = createVector vx, vy
|
111
|
-
end
|
112
|
-
|
113
|
-
def update()
|
114
|
-
@pos.add @vel
|
115
|
-
end
|
116
|
-
|
117
|
-
def draw()
|
118
|
-
rect @pos.x, @pos.y, @w, @h
|
119
|
-
end
|
120
|
-
|
121
|
-
def bounds()
|
122
|
-
x, y = @pos.x, @pos.y
|
123
|
-
return x, y, x + @w, y + @h
|
124
|
-
end
|
125
|
-
|
126
|
-
def center()
|
127
|
-
createVector @pos.x + @w / 2, @pos.y + @h / 2
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
class Ball < Obj
|
132
|
-
def initialize(bar)
|
133
|
-
super bar.pos.x, bar.pos.y - bar.h, 20, 20
|
134
|
-
self.vel = createVector random(-1, 1), -1
|
135
|
-
end
|
136
|
-
|
137
|
-
def vel=(v)
|
138
|
-
@vel = v.dup.normalize.mult 8
|
139
|
-
end
|
140
|
-
|
141
|
-
def update()
|
142
|
-
b = bounds.dup
|
143
|
-
super
|
144
|
-
checkHit b
|
145
|
-
end
|
146
|
-
|
147
|
-
def checkHit(prevBounds)
|
148
|
-
x1, y1, x2, y2 = prevBounds
|
149
|
-
hitH = hitV = false
|
150
|
-
hits = []
|
151
|
-
$objs.each do |o|
|
152
|
-
next if o == self
|
153
|
-
next unless intersect? o
|
154
|
-
hits.push o
|
155
|
-
ox1, oy1, ox2, oy2 = o.bounds
|
156
|
-
hitH ||= !overlap?(x1, x2, ox1, ox2)
|
157
|
-
hitV ||= !overlap?(y1, y2, oy1, oy2)
|
158
|
-
end
|
159
|
-
vel.x *= -1 if hitH
|
160
|
-
vel.y *= -1 if hitV
|
161
|
-
|
162
|
-
hits.each {|o| hit o}
|
163
|
-
end
|
164
|
-
|
165
|
-
def intersect?(o)
|
166
|
-
x1, y1, x2, y2 = bounds
|
167
|
-
ox1, oy1, ox2, oy2 = o.bounds
|
168
|
-
overlap?(x1, x2, ox1, ox2) && overlap?(y1, y2, oy1, oy2)
|
169
|
-
end
|
170
|
-
|
171
|
-
def overlap?(a1, a2, b1, b2)
|
172
|
-
a1 <= b2 && b1 <= a2
|
173
|
-
end
|
174
|
-
|
175
|
-
def hit(o)
|
176
|
-
case o
|
177
|
-
when Bar then self.vel = center.sub o.center
|
178
|
-
when Brick then $objs.delete o
|
179
|
-
when Bottom then $objs.delete self unless cleared?
|
180
|
-
end
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
class Bar < Obj
|
185
|
-
def initialize()
|
186
|
-
w = 100
|
187
|
-
super (width - w) / 2, height - 50, w, 20
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
class Brick < Obj
|
192
|
-
def initialize(x, y, w, h, hue)
|
193
|
-
super x, y, w, h
|
194
|
-
@hue = hue
|
195
|
-
end
|
196
|
-
|
197
|
-
def draw()
|
198
|
-
push do
|
199
|
-
fill @hue, 50, 100
|
200
|
-
super
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
class Bottom < Obj
|
206
|
-
def draw()
|
207
|
-
push do
|
208
|
-
fill 0, 0, 50
|
209
|
-
super
|
210
|
-
end
|
211
|
-
end
|
212
|
-
end
|