glimmer-dsl-libui 0.2.15 → 0.2.19
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/CHANGELOG.md +22 -0
- data/README.md +597 -92
- data/VERSION +1 -1
- data/bin/girb +0 -0
- data/examples/area_gallery.rb +11 -18
- data/examples/area_gallery2.rb +31 -7
- data/examples/area_gallery3.rb +14 -21
- data/examples/area_gallery4.rb +34 -10
- data/examples/basic_transform.rb +8 -2
- data/examples/color_button.rb +1 -1
- data/examples/histogram.rb +6 -13
- data/examples/meta_example.rb +3 -1
- data/examples/tetris/model/block.rb +48 -0
- data/examples/tetris/model/game.rb +306 -0
- data/examples/tetris/model/past_game.rb +39 -0
- data/examples/tetris/model/tetromino.rb +329 -0
- data/examples/tetris.rb +262 -0
- data/glimmer-dsl-libui.gemspec +0 -0
- data/lib/glimmer/dsl/libui/shape_expression.rb +1 -0
- data/lib/glimmer/libui/control_proxy/area_proxy.rb +25 -0
- data/lib/glimmer/libui/control_proxy/path_proxy.rb +19 -7
- data/lib/glimmer/libui/shape/figure.rb +4 -2
- data/lib/glimmer/libui/shape/polybezier.rb +45 -0
- data/lib/glimmer/libui/shape/polygon.rb +46 -0
- data/lib/glimmer/libui/shape/polyline.rb +45 -0
- data/lib/glimmer/libui/shape.rb +9 -3
- metadata +10 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.19
|
data/bin/girb
CHANGED
File without changes
|
data/examples/area_gallery.rb
CHANGED
@@ -18,31 +18,24 @@ window('Area Gallery', 400, 400) {
|
|
18
18
|
fill x0: 10, y0: 10, x1: 350, y1: 350, stops: [{pos: 0.25, r: 204, g: 102, b: 204}, {pos: 0.75, r: 102, g: 102, b: 204}]
|
19
19
|
}
|
20
20
|
path { # declarative stable path
|
21
|
-
|
22
|
-
|
23
|
-
line(400, 100)
|
24
|
-
line(400, 400)
|
25
|
-
|
26
|
-
closed true
|
27
|
-
}
|
28
|
-
|
21
|
+
polygon(100, 100, 100, 400, 400, 100, 400, 400)
|
22
|
+
|
29
23
|
fill r: 202, g: 102, b: 104, a: 0.5
|
30
24
|
stroke r: 0, g: 0, b: 0
|
31
25
|
}
|
32
26
|
path { # declarative stable path
|
33
|
-
|
34
|
-
bezier(200, 100, 100, 200, 400, 100)
|
35
|
-
bezier(300, 100, 100, 300, 100, 400)
|
36
|
-
bezier(100, 300, 300, 100, 400, 400)
|
37
|
-
|
38
|
-
closed true
|
39
|
-
}
|
27
|
+
polybezier(0, 0, 200, 100, 100, 200, 400, 100, 300, 100, 100, 300, 100, 400, 100, 300, 300, 100, 400, 400)
|
40
28
|
|
41
29
|
fill r: 202, g: 102, b: 204, a: 0.5
|
42
30
|
stroke r: 0, g: 0, b: 0, thickness: 2, dashes: [50, 10, 10, 10], dash_phase: -50.0
|
43
31
|
}
|
44
32
|
path { # declarative stable path
|
45
|
-
|
33
|
+
polyline(100, 100, 400, 100, 100, 400, 400, 400, 0, 0)
|
34
|
+
|
35
|
+
stroke r: 0, g: 0, b: 0, thickness: 2
|
36
|
+
}
|
37
|
+
path { # declarative stable path
|
38
|
+
arc(404, 216, 190, 90, 90, false)
|
46
39
|
|
47
40
|
# radial gradient (has an outer_radius in addition to x0, y0, x1, y1, and stops)
|
48
41
|
fill outer_radius: 90, x0: 0, y0: 0, x1: 500, y1: 500, stops: [{pos: 0.25, r: 102, g: 102, b: 204, a: 0.5}, {pos: 0.75, r: 204, g: 102, b: 204}]
|
@@ -54,9 +47,9 @@ window('Area Gallery', 400, 400) {
|
|
54
47
|
fill r: 202, g: 102, b: 204, a: 0.5
|
55
48
|
stroke r: 0, g: 0, b: 0, thickness: 2
|
56
49
|
}
|
57
|
-
text(
|
50
|
+
text(161, 40, 100) { # x, y, width
|
58
51
|
string('Area Gallery') {
|
59
|
-
font family: '
|
52
|
+
font family: 'Arial', size: 14
|
60
53
|
color :black
|
61
54
|
}
|
62
55
|
}
|
data/examples/area_gallery2.rb
CHANGED
@@ -88,18 +88,42 @@ window('Area Gallery', 400, 400) {
|
|
88
88
|
end_x 400
|
89
89
|
end_y 400
|
90
90
|
}
|
91
|
-
|
92
|
-
closed true
|
93
91
|
}
|
94
92
|
|
95
93
|
fill r: 202, g: 102, b: 204, a: 0.5
|
96
94
|
stroke r: 0, g: 0, b: 0, thickness: 2, dashes: [50, 10, 10, 10], dash_phase: -50.0
|
97
95
|
}
|
96
|
+
path { # declarative stable path
|
97
|
+
polyline(100, 100, 400, 100, 100, 400, 400, 400, 0, 0)
|
98
|
+
figure {
|
99
|
+
x 100
|
100
|
+
y 100
|
101
|
+
|
102
|
+
line {
|
103
|
+
x 400
|
104
|
+
y 100
|
105
|
+
}
|
106
|
+
line {
|
107
|
+
x 100
|
108
|
+
y 400
|
109
|
+
}
|
110
|
+
line {
|
111
|
+
x 400
|
112
|
+
y 400
|
113
|
+
}
|
114
|
+
line {
|
115
|
+
x 0
|
116
|
+
y 0
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
stroke r: 0, g: 0, b: 0, thickness: 2
|
121
|
+
}
|
98
122
|
path { # declarative stable path
|
99
123
|
arc {
|
100
|
-
x_center
|
101
|
-
y_center
|
102
|
-
radius
|
124
|
+
x_center 404
|
125
|
+
y_center 216
|
126
|
+
radius 190
|
103
127
|
start_angle 90
|
104
128
|
sweep 90
|
105
129
|
is_negative false
|
@@ -120,12 +144,12 @@ window('Area Gallery', 400, 400) {
|
|
120
144
|
stroke r: 0, g: 0, b: 0, thickness: 2
|
121
145
|
}
|
122
146
|
text {
|
123
|
-
x
|
147
|
+
x 161
|
124
148
|
y 40
|
125
149
|
width 100
|
126
150
|
|
127
151
|
string {
|
128
|
-
font family: '
|
152
|
+
font family: 'Arial', size: 14
|
129
153
|
color :black
|
130
154
|
|
131
155
|
'Area Gallery'
|
data/examples/area_gallery3.rb
CHANGED
@@ -8,42 +8,35 @@ window('Area Gallery', 400, 400) {
|
|
8
8
|
path { # a dynamic path is added semi-declaratively inside on_draw block
|
9
9
|
square(0, 0, 100)
|
10
10
|
square(100, 100, 400)
|
11
|
-
|
11
|
+
|
12
12
|
fill r: 102, g: 102, b: 204
|
13
13
|
}
|
14
14
|
path { # a dynamic path is added semi-declaratively inside on_draw block
|
15
15
|
rectangle(0, 100, 100, 400)
|
16
16
|
rectangle(100, 0, 400, 100)
|
17
|
-
|
17
|
+
|
18
18
|
# linear gradient (has x0, y0, x1, y1, and stops)
|
19
19
|
fill x0: 10, y0: 10, x1: 350, y1: 350, stops: [{pos: 0.25, r: 204, g: 102, b: 204}, {pos: 0.75, r: 102, g: 102, b: 204}]
|
20
20
|
}
|
21
21
|
path { # a dynamic path is added semi-declaratively inside on_draw block
|
22
|
-
|
23
|
-
|
24
|
-
line(400, 100)
|
25
|
-
line(400, 400)
|
26
|
-
|
27
|
-
closed true
|
28
|
-
}
|
29
|
-
|
22
|
+
polygon(100, 100, 100, 400, 400, 100, 400, 400)
|
23
|
+
|
30
24
|
fill r: 202, g: 102, b: 104, a: 0.5
|
31
25
|
stroke r: 0, g: 0, b: 0
|
32
26
|
}
|
33
27
|
path { # a dynamic path is added semi-declaratively inside on_draw block
|
34
|
-
|
35
|
-
|
36
|
-
bezier(300, 100, 100, 300, 100, 400)
|
37
|
-
bezier(100, 300, 300, 100, 400, 400)
|
38
|
-
|
39
|
-
closed true
|
40
|
-
}
|
41
|
-
|
28
|
+
polybezier(0, 0, 200, 100, 100, 200, 400, 100, 300, 100, 100, 300, 100, 400, 100, 300, 300, 100, 400, 400)
|
29
|
+
|
42
30
|
fill r: 202, g: 102, b: 204, a: 0.5
|
43
31
|
stroke r: 0, g: 0, b: 0, thickness: 2, dashes: [50, 10, 10, 10], dash_phase: -50.0
|
44
32
|
}
|
45
33
|
path { # a dynamic path is added semi-declaratively inside on_draw block
|
46
|
-
|
34
|
+
polyline(100, 100, 400, 100, 100, 400, 400, 400, 0, 0)
|
35
|
+
|
36
|
+
stroke r: 0, g: 0, b: 0, thickness: 2
|
37
|
+
}
|
38
|
+
path { # a dynamic path is added semi-declaratively inside on_draw block
|
39
|
+
arc(404, 216, 190, 90, 90, false)
|
47
40
|
|
48
41
|
# radial gradient (has an outer_radius in addition to x0, y0, x1, y1, and stops)
|
49
42
|
fill outer_radius: 90, x0: 0, y0: 0, x1: 500, y1: 500, stops: [{pos: 0.25, r: 102, g: 102, b: 204, a: 0.5}, {pos: 0.75, r: 204, g: 102, b: 204}]
|
@@ -55,9 +48,9 @@ window('Area Gallery', 400, 400) {
|
|
55
48
|
fill r: 202, g: 102, b: 204, a: 0.5
|
56
49
|
stroke r: 0, g: 0, b: 0, thickness: 2
|
57
50
|
}
|
58
|
-
text(
|
51
|
+
text(161, 40, 100) { # x, y, width
|
59
52
|
string('Area Gallery') {
|
60
|
-
font family: '
|
53
|
+
font family: 'Arial', size: 14
|
61
54
|
color :black
|
62
55
|
}
|
63
56
|
}
|
data/examples/area_gallery4.rb
CHANGED
@@ -53,10 +53,10 @@ window('Area Gallery', 400, 400) {
|
|
53
53
|
x 400
|
54
54
|
y 400
|
55
55
|
}
|
56
|
-
|
56
|
+
|
57
57
|
closed true
|
58
58
|
}
|
59
|
-
|
59
|
+
|
60
60
|
fill r: 202, g: 102, b: 104, a: 0.5
|
61
61
|
stroke r: 0, g: 0, b: 0
|
62
62
|
}
|
@@ -89,18 +89,42 @@ window('Area Gallery', 400, 400) {
|
|
89
89
|
end_x 400
|
90
90
|
end_y 400
|
91
91
|
}
|
92
|
-
|
93
|
-
closed true
|
94
92
|
}
|
95
|
-
|
93
|
+
|
96
94
|
fill r: 202, g: 102, b: 204, a: 0.5
|
97
95
|
stroke r: 0, g: 0, b: 0, thickness: 2, dashes: [50, 10, 10, 10], dash_phase: -50.0
|
98
96
|
}
|
97
|
+
path { # a dynamic path is added semi-declaratively inside on_draw block
|
98
|
+
polyline(100, 100, 400, 100, 100, 400, 400, 400, 0, 0)
|
99
|
+
figure {
|
100
|
+
x 100
|
101
|
+
y 100
|
102
|
+
|
103
|
+
line {
|
104
|
+
x 400
|
105
|
+
y 100
|
106
|
+
}
|
107
|
+
line {
|
108
|
+
x 100
|
109
|
+
y 400
|
110
|
+
}
|
111
|
+
line {
|
112
|
+
x 400
|
113
|
+
y 400
|
114
|
+
}
|
115
|
+
line {
|
116
|
+
x 0
|
117
|
+
y 0
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
stroke r: 0, g: 0, b: 0, thickness: 2
|
122
|
+
}
|
99
123
|
path { # a dynamic path is added semi-declaratively inside on_draw block
|
100
124
|
arc {
|
101
|
-
x_center
|
102
|
-
y_center
|
103
|
-
radius
|
125
|
+
x_center 404
|
126
|
+
y_center 216
|
127
|
+
radius 190
|
104
128
|
start_angle 90
|
105
129
|
sweep 90
|
106
130
|
is_negative false
|
@@ -121,12 +145,12 @@ window('Area Gallery', 400, 400) {
|
|
121
145
|
stroke r: 0, g: 0, b: 0, thickness: 2
|
122
146
|
}
|
123
147
|
text {
|
124
|
-
x
|
148
|
+
x 161
|
125
149
|
y 40
|
126
150
|
width 100
|
127
151
|
|
128
152
|
string {
|
129
|
-
font family: '
|
153
|
+
font family: 'Arial', size: 14
|
130
154
|
color :black
|
131
155
|
|
132
156
|
'Area Gallery'
|
data/examples/basic_transform.rb
CHANGED
@@ -16,10 +16,16 @@ window('Basic Transform', 350, 350) {
|
|
16
16
|
fill r: [255 - n*5, 0].max, g: [n*5, 255].min, b: 0, a: 0.5
|
17
17
|
stroke :black, thickness: 2
|
18
18
|
transform {
|
19
|
-
|
20
|
-
|
19
|
+
unless OS.windows?
|
20
|
+
skew 0.15, 0.15
|
21
|
+
translate 50, 50
|
22
|
+
end
|
21
23
|
rotate 100, 100, -9 * n
|
22
24
|
scale 1.1, 1.1
|
25
|
+
if OS.windows?
|
26
|
+
skew 0.15, 0.15
|
27
|
+
translate 50, 50
|
28
|
+
end
|
23
29
|
}
|
24
30
|
}
|
25
31
|
end
|
data/examples/color_button.rb
CHANGED
data/examples/histogram.rb
CHANGED
@@ -31,20 +31,13 @@ end
|
|
31
31
|
|
32
32
|
# method-based custom control representing a graph path
|
33
33
|
def graph_path(width, height, should_extend, &block)
|
34
|
-
locations = point_locations(width, height)
|
34
|
+
locations = point_locations(width, height).flatten
|
35
35
|
path {
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
if should_extend
|
42
|
-
line(width, height)
|
43
|
-
line(0, height)
|
44
|
-
|
45
|
-
closed true
|
46
|
-
end
|
47
|
-
}
|
36
|
+
if should_extend
|
37
|
+
polygon(locations + [width, height, 0, height])
|
38
|
+
else
|
39
|
+
polyline(locations)
|
40
|
+
end
|
48
41
|
|
49
42
|
# apply a transform to the coordinate space for this path so (0, 0) is the top-left corner of the graph
|
50
43
|
transform {
|
data/examples/meta_example.rb
CHANGED
@@ -11,7 +11,7 @@ class MetaExample
|
|
11
11
|
|
12
12
|
def examples
|
13
13
|
if @examples.nil?
|
14
|
-
example_files = Dir.glob(File.join(File.expand_path('.', __dir__), '
|
14
|
+
example_files = Dir.glob(File.join(File.expand_path('.', __dir__), '*.rb'))
|
15
15
|
example_file_names = example_files.map { |f| File.basename(f, '.rb') }
|
16
16
|
example_file_names = example_file_names.reject { |f| f == 'meta_example' || f.match(/\d$/) }
|
17
17
|
@examples = example_file_names.map { |f| f.underscore.titlecase }
|
@@ -110,6 +110,8 @@ class MetaExample
|
|
110
110
|
FileUtils.mkdir_p(parent_dir)
|
111
111
|
example_file = File.join(parent_dir, "#{selected_example.underscore}.rb")
|
112
112
|
File.write(example_file, @code_entry.text)
|
113
|
+
example_supporting_directory = File.expand_path(selected_example.underscore, __dir__)
|
114
|
+
FileUtils.cp_r(example_supporting_directory, parent_dir) if Dir.exist?(example_supporting_directory)
|
113
115
|
FileUtils.cp_r(File.expand_path('../icons', __dir__), File.dirname(parent_dir))
|
114
116
|
FileUtils.cp_r(File.expand_path('../sounds', __dir__), File.dirname(parent_dir))
|
115
117
|
run_example(example_file)
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# Copyright (c) 2007-2021 Andy Maleh
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
# a copy of this software and associated documentation files (the
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
# the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be
|
12
|
+
# included in all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
class Tetris
|
23
|
+
module Model
|
24
|
+
class Block
|
25
|
+
COLOR_CLEAR = :white
|
26
|
+
|
27
|
+
attr_accessor :color
|
28
|
+
|
29
|
+
# Initializes with color. Default color (gray) signifies an empty block
|
30
|
+
def initialize(color = COLOR_CLEAR)
|
31
|
+
@color = color
|
32
|
+
end
|
33
|
+
|
34
|
+
# Clears block color. `quietly` option indicates if it should not notify observers by setting value quietly via variable not attribute writer.
|
35
|
+
def clear
|
36
|
+
self.color = COLOR_CLEAR unless self.color == COLOR_CLEAR
|
37
|
+
end
|
38
|
+
|
39
|
+
def clear?
|
40
|
+
self.color == COLOR_CLEAR
|
41
|
+
end
|
42
|
+
|
43
|
+
def occupied?
|
44
|
+
!clear?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,306 @@
|
|
1
|
+
# Copyright (c) 2007-2021 Andy Maleh
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
# a copy of this software and associated documentation files (the
|
5
|
+
# "Software"), to deal in the Software without restriction, including
|
6
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
# the following conditions:
|
10
|
+
#
|
11
|
+
# The above copyright notice and this permission notice shall be
|
12
|
+
# included in all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
22
|
+
require 'fileutils'
|
23
|
+
require 'json'
|
24
|
+
require 'glimmer/data_binding/observer'
|
25
|
+
require 'glimmer/config'
|
26
|
+
|
27
|
+
require_relative 'block'
|
28
|
+
require_relative 'tetromino'
|
29
|
+
require_relative 'past_game'
|
30
|
+
|
31
|
+
class Tetris
|
32
|
+
module Model
|
33
|
+
class Game
|
34
|
+
PLAYFIELD_WIDTH = 10
|
35
|
+
PLAYFIELD_HEIGHT = 20
|
36
|
+
PREVIEW_PLAYFIELD_WIDTH = 4
|
37
|
+
PREVIEW_PLAYFIELD_HEIGHT = 2
|
38
|
+
SCORE_MULTIPLIER = {1 => 40, 2 => 100, 3 => 300, 4 => 1200}
|
39
|
+
|
40
|
+
attr_reader :playfield_width, :playfield_height
|
41
|
+
attr_accessor :game_over, :paused, :preview_tetromino, :lines, :score, :level, :high_scores, :beeping, :added_high_score, :show_high_scores, :up_arrow_action
|
42
|
+
alias game_over? game_over
|
43
|
+
alias paused? paused
|
44
|
+
alias beeping? beeping
|
45
|
+
alias added_high_score? added_high_score
|
46
|
+
|
47
|
+
def initialize(playfield_width = PLAYFIELD_WIDTH, playfield_height = PLAYFIELD_HEIGHT)
|
48
|
+
@playfield_width = playfield_width
|
49
|
+
@playfield_height = playfield_height
|
50
|
+
@high_scores = []
|
51
|
+
@show_high_scores = false
|
52
|
+
@beeping = true
|
53
|
+
@up_arrow_action = :rotate_left
|
54
|
+
load_high_scores!
|
55
|
+
end
|
56
|
+
|
57
|
+
def configure_beeper(&beeper)
|
58
|
+
@beeper = beeper
|
59
|
+
end
|
60
|
+
|
61
|
+
def game_in_progress?
|
62
|
+
!game_over? && !paused?
|
63
|
+
end
|
64
|
+
|
65
|
+
def start!
|
66
|
+
self.show_high_scores = false
|
67
|
+
self.paused = false
|
68
|
+
self.level = 1
|
69
|
+
self.score = 0
|
70
|
+
self.lines = 0
|
71
|
+
reset_playfield
|
72
|
+
reset_preview_playfield
|
73
|
+
reset_tetrominoes
|
74
|
+
preview_next_tetromino!
|
75
|
+
consider_adding_tetromino
|
76
|
+
self.game_over = false
|
77
|
+
end
|
78
|
+
alias restart! start!
|
79
|
+
|
80
|
+
def game_over!
|
81
|
+
add_high_score!
|
82
|
+
beep
|
83
|
+
self.game_over = true
|
84
|
+
end
|
85
|
+
|
86
|
+
def clear_high_scores!
|
87
|
+
high_scores.clear
|
88
|
+
end
|
89
|
+
|
90
|
+
def add_high_score!
|
91
|
+
self.added_high_score = true
|
92
|
+
high_scores.prepend(PastGame.new("Player #{high_scores.count + 1}", score, lines, level))
|
93
|
+
end
|
94
|
+
|
95
|
+
def save_high_scores!
|
96
|
+
high_score_file_content = @high_scores.map {|past_game| past_game.to_a.join("\t") }.join("\n")
|
97
|
+
FileUtils.mkdir_p(tetris_dir)
|
98
|
+
File.write(tetris_high_score_file, high_score_file_content)
|
99
|
+
rescue => e
|
100
|
+
# Fail safely by keeping high scores in memory if unable to access disk
|
101
|
+
Glimmer::Config.logger.error {"Failed to save high scores in: #{tetris_high_score_file}\n#{e.full_message}"}
|
102
|
+
end
|
103
|
+
|
104
|
+
def load_high_scores!
|
105
|
+
if File.exist?(tetris_high_score_file)
|
106
|
+
self.high_scores = File.read(tetris_high_score_file).split("\n").map {|line| PastGame.new(*line.split("\t")) }
|
107
|
+
end
|
108
|
+
rescue => e
|
109
|
+
# Fail safely by keeping high scores in memory if unable to access disk
|
110
|
+
Glimmer::Config.logger.error {"Failed to load high scores from: #{tetris_high_score_file}\n#{e.full_message}"}
|
111
|
+
end
|
112
|
+
|
113
|
+
def tetris_dir
|
114
|
+
@tetris_dir ||= File.join(File.expand_path('~'), '.glimmer-tetris')
|
115
|
+
end
|
116
|
+
|
117
|
+
def tetris_high_score_file
|
118
|
+
File.join(tetris_dir, "high_scores.txt")
|
119
|
+
end
|
120
|
+
|
121
|
+
def down!(instant: false)
|
122
|
+
return unless game_in_progress?
|
123
|
+
current_tetromino.down!(instant: instant)
|
124
|
+
game_over! if current_tetromino.row <= 0 && current_tetromino.stopped?
|
125
|
+
end
|
126
|
+
|
127
|
+
def right!
|
128
|
+
return unless game_in_progress?
|
129
|
+
current_tetromino.right!
|
130
|
+
end
|
131
|
+
|
132
|
+
def left!
|
133
|
+
return unless game_in_progress?
|
134
|
+
current_tetromino.left!
|
135
|
+
end
|
136
|
+
|
137
|
+
def rotate!(direction)
|
138
|
+
return unless game_in_progress?
|
139
|
+
current_tetromino.rotate!(direction)
|
140
|
+
end
|
141
|
+
|
142
|
+
def current_tetromino
|
143
|
+
tetrominoes.last
|
144
|
+
end
|
145
|
+
|
146
|
+
def tetrominoes
|
147
|
+
@tetrominoes ||= reset_tetrominoes
|
148
|
+
end
|
149
|
+
|
150
|
+
# Returns blocks in the playfield
|
151
|
+
def playfield
|
152
|
+
@playfield ||= @original_playfield = @playfield_height.times.map do
|
153
|
+
@playfield_width.times.map do
|
154
|
+
Block.new
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Executes a hypothetical scenario without truly changing playfield permanently
|
160
|
+
def hypothetical(&block)
|
161
|
+
@playfield = hypothetical_playfield
|
162
|
+
block.call
|
163
|
+
@playfield = @original_playfield
|
164
|
+
end
|
165
|
+
|
166
|
+
# Returns whether currently executing a hypothetical scenario
|
167
|
+
def hypothetical?
|
168
|
+
@playfield != @original_playfield
|
169
|
+
end
|
170
|
+
|
171
|
+
def hypothetical_playfield
|
172
|
+
@playfield_height.times.map { |row|
|
173
|
+
@playfield_width.times.map { |column|
|
174
|
+
playfield[row][column].clone
|
175
|
+
}
|
176
|
+
}
|
177
|
+
end
|
178
|
+
|
179
|
+
def preview_playfield
|
180
|
+
@preview_playfield ||= PREVIEW_PLAYFIELD_HEIGHT.times.map {|row|
|
181
|
+
PREVIEW_PLAYFIELD_WIDTH.times.map {|column|
|
182
|
+
Block.new
|
183
|
+
}
|
184
|
+
}
|
185
|
+
end
|
186
|
+
|
187
|
+
def preview_next_tetromino!
|
188
|
+
self.preview_tetromino = Tetromino.new(self)
|
189
|
+
end
|
190
|
+
|
191
|
+
def calculate_score!(eliminated_lines)
|
192
|
+
new_score = SCORE_MULTIPLIER[eliminated_lines] * (level + 1)
|
193
|
+
self.score += new_score
|
194
|
+
end
|
195
|
+
|
196
|
+
def level_up!
|
197
|
+
self.level += 1 if lines >= self.level*10
|
198
|
+
end
|
199
|
+
|
200
|
+
def delay
|
201
|
+
[1.1 - (level.to_i * 0.1), 0.001].max
|
202
|
+
end
|
203
|
+
|
204
|
+
def beep
|
205
|
+
@beeper&.call if beeping
|
206
|
+
end
|
207
|
+
|
208
|
+
def instant_down_on_up=(value)
|
209
|
+
self.up_arrow_action = :instant_down if value
|
210
|
+
end
|
211
|
+
|
212
|
+
def instant_down_on_up
|
213
|
+
self.up_arrow_action == :instant_down
|
214
|
+
end
|
215
|
+
|
216
|
+
def rotate_right_on_up=(value)
|
217
|
+
self.up_arrow_action = :rotate_right if value
|
218
|
+
end
|
219
|
+
|
220
|
+
def rotate_right_on_up
|
221
|
+
self.up_arrow_action == :rotate_right
|
222
|
+
end
|
223
|
+
|
224
|
+
def rotate_left_on_up=(value)
|
225
|
+
self.up_arrow_action = :rotate_left if value
|
226
|
+
end
|
227
|
+
|
228
|
+
def rotate_left_on_up
|
229
|
+
self.up_arrow_action == :rotate_left
|
230
|
+
end
|
231
|
+
|
232
|
+
def reset_tetrominoes
|
233
|
+
@tetrominoes = []
|
234
|
+
end
|
235
|
+
|
236
|
+
def reset_playfield
|
237
|
+
playfield.each do |row|
|
238
|
+
row.each do |block|
|
239
|
+
block.clear
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
def reset_preview_playfield
|
245
|
+
preview_playfield.each do |row|
|
246
|
+
row.each do |block|
|
247
|
+
block.clear
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
def consider_adding_tetromino
|
253
|
+
if tetrominoes.empty? || current_tetromino.stopped?
|
254
|
+
preview_tetromino.launch!
|
255
|
+
preview_next_tetromino!
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
def consider_eliminating_lines
|
260
|
+
eliminated_lines = 0
|
261
|
+
playfield.each_with_index do |row, playfield_row|
|
262
|
+
if row.all? {|block| !block.clear?}
|
263
|
+
eliminated_lines += 1
|
264
|
+
shift_blocks_down_above_row(playfield_row)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
if eliminated_lines > 0
|
268
|
+
beep
|
269
|
+
self.lines += eliminated_lines
|
270
|
+
level_up!
|
271
|
+
calculate_score!(eliminated_lines)
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
def playfield_remaining_heights(tetromino = nil)
|
276
|
+
@playfield_width.times.map do |playfield_column|
|
277
|
+
bottom_most_block = tetromino.bottom_most_block_for_column(playfield_column)
|
278
|
+
(playfield.each_with_index.detect do |row, playfield_row|
|
279
|
+
!row[playfield_column].clear? &&
|
280
|
+
(
|
281
|
+
tetromino.nil? ||
|
282
|
+
bottom_most_block.nil? ||
|
283
|
+
(playfield_row > tetromino.row + bottom_most_block[:row_index])
|
284
|
+
)
|
285
|
+
end || [nil, @playfield_height])[1]
|
286
|
+
end.to_a
|
287
|
+
end
|
288
|
+
|
289
|
+
private
|
290
|
+
|
291
|
+
def shift_blocks_down_above_row(row)
|
292
|
+
row.downto(0) do |playfield_row|
|
293
|
+
playfield[playfield_row].each_with_index do |block, playfield_column|
|
294
|
+
previous_row = playfield[playfield_row - 1]
|
295
|
+
previous_block = previous_row[playfield_column]
|
296
|
+
block.color = previous_block.color unless block.color == previous_block.color
|
297
|
+
end
|
298
|
+
end
|
299
|
+
playfield[0].each(&:clear)
|
300
|
+
end
|
301
|
+
|
302
|
+
end
|
303
|
+
|
304
|
+
end
|
305
|
+
|
306
|
+
end
|