glimmer-dsl-swt 4.22.1.2 → 4.22.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -0
- data/LICENSE.txt +1 -1
- data/README.md +12 -9
- data/RUBY_VERSION +1 -1
- data/VERSION +1 -1
- data/bin/girb +1 -1
- data/bin/girb_runner.rb +1 -1
- data/bin/glimmer +1 -1
- data/bin/glimmer-setup +1 -1
- data/docs/reference/GLIMMER_COMMAND.md +2 -2
- data/docs/reference/GLIMMER_CONFIGURATION.md +1 -1
- data/docs/reference/GLIMMER_GIRB.md +1 -1
- data/docs/reference/GLIMMER_GUI_DSL_SYNTAX.md +146 -1
- data/docs/reference/GLIMMER_PACKAGING_AND_DISTRIBUTION.md +13 -32
- data/docs/reference/GLIMMER_SAMPLES.md +16 -1
- data/docs/reference/GLIMMER_STYLE_GUIDE.md +6 -0
- data/glimmer-dsl-swt.gemspec +0 -0
- data/lib/ext/glimmer/config.rb +1 -1
- data/lib/ext/glimmer.rb +4 -4
- data/lib/ext/rouge/themes/glimmer.rb +21 -0
- data/lib/glimmer/Rakefile +6 -6
- data/lib/glimmer/data_binding/list_selection_binding.rb +1 -1
- data/lib/glimmer/data_binding/observable_widget.rb +1 -1
- data/lib/glimmer/data_binding/table_items_binding.rb +1 -1
- data/lib/glimmer/data_binding/tree_items_binding.rb +1 -1
- data/lib/glimmer/data_binding/widget_binding.rb +1 -1
- data/lib/glimmer/dsl/swt/animation_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/async_exec_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/auto_exec_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/bind_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/block_property_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/c_tab_item_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/checkbox_group_selection_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/color_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/column_properties_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/combo_selection_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/cursor_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/custom_shape_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/custom_widget_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/dialog_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/display_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/dnd_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/dsl.rb +1 -1
- data/lib/glimmer/dsl/swt/exec_expression.rb +2 -3
- data/lib/glimmer/dsl/swt/expand_item_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/font_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/image_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/layout_data_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/layout_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/list_selection_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/listener_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/menu_bar_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/menu_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/message_box_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/multiply_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/observe_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/pixel_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/property_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/radio_group_selection_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/rgb_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/rgba_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/shape_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/shell_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/shine_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/swt_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/sync_call_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/sync_exec_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/tab_item_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/table_items_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/timer_exec_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/transform_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/tray_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/tray_item_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/tree_items_data_binding_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/tree_properties_expression.rb +1 -1
- data/lib/glimmer/dsl/swt/widget_expression.rb +1 -1
- data/lib/glimmer/launcher.rb +1 -1
- data/lib/glimmer/rake_task/list.rb +1 -1
- data/lib/glimmer/rake_task/package.rb +1 -1
- data/lib/glimmer/rake_task/scaffold.rb +1 -3
- data/lib/glimmer/rake_task.rb +1 -1
- data/lib/glimmer/swt/c_tab_item_proxy.rb +1 -1
- data/lib/glimmer/swt/color_proxy.rb +1 -1
- data/lib/glimmer/swt/combo_proxy.rb +1 -1
- data/lib/glimmer/swt/cursor_proxy.rb +1 -1
- data/lib/glimmer/swt/custom/animation.rb +1 -1
- data/lib/glimmer/swt/custom/checkbox_group.rb +1 -1
- data/lib/glimmer/swt/custom/code_text.rb +1 -1
- data/lib/glimmer/swt/custom/drawable.rb +1 -1
- data/lib/glimmer/swt/custom/radio_group.rb +1 -1
- data/lib/glimmer/swt/custom/shape/arc.rb +1 -1
- data/lib/glimmer/swt/custom/shape/cubic.rb +1 -1
- data/lib/glimmer/swt/custom/shape/focus.rb +1 -1
- data/lib/glimmer/swt/custom/shape/image.rb +1 -1
- data/lib/glimmer/swt/custom/shape/line.rb +1 -1
- data/lib/glimmer/swt/custom/shape/oval.rb +1 -1
- data/lib/glimmer/swt/custom/shape/path.rb +1 -1
- data/lib/glimmer/swt/custom/shape/path_segment.rb +1 -1
- data/lib/glimmer/swt/custom/shape/point.rb +1 -1
- data/lib/glimmer/swt/custom/shape/polygon.rb +1 -1
- data/lib/glimmer/swt/custom/shape/polyline.rb +1 -1
- data/lib/glimmer/swt/custom/shape/quad.rb +1 -1
- data/lib/glimmer/swt/custom/shape/rectangle.rb +1 -1
- data/lib/glimmer/swt/custom/shape/text.rb +1 -1
- data/lib/glimmer/swt/custom/shape.rb +1 -1
- data/lib/glimmer/swt/date_time_proxy.rb +1 -1
- data/lib/glimmer/swt/dialog_proxy.rb +1 -1
- data/lib/glimmer/swt/display_proxy.rb +1 -1
- data/lib/glimmer/swt/dnd_proxy.rb +1 -1
- data/lib/glimmer/swt/expand_item_proxy.rb +1 -1
- data/lib/glimmer/swt/font_proxy.rb +1 -1
- data/lib/glimmer/swt/image_proxy.rb +1 -1
- data/lib/glimmer/swt/layout_data_proxy.rb +5 -1
- data/lib/glimmer/swt/layout_proxy.rb +1 -1
- data/lib/glimmer/swt/menu_proxy.rb +1 -1
- data/lib/glimmer/swt/message_box_proxy.rb +1 -1
- data/lib/glimmer/swt/packages.rb +1 -1
- data/lib/glimmer/swt/properties.rb +1 -1
- data/lib/glimmer/swt/proxy_properties.rb +1 -1
- data/lib/glimmer/swt/sash_form_proxy.rb +1 -1
- data/lib/glimmer/swt/scrolled_composite_proxy.rb +1 -1
- data/lib/glimmer/swt/shape_listener_proxy.rb +1 -1
- data/lib/glimmer/swt/shell_proxy.rb +1 -1
- data/lib/glimmer/swt/style_constantizable.rb +1 -1
- data/lib/glimmer/swt/styled_text_proxy.rb +1 -1
- data/lib/glimmer/swt/swt_proxy.rb +1 -1
- data/lib/glimmer/swt/tab_folder_proxy.rb +1 -1
- data/lib/glimmer/swt/tab_item_proxy.rb +1 -1
- data/lib/glimmer/swt/table_column_proxy.rb +1 -1
- data/lib/glimmer/swt/table_proxy.rb +1 -1
- data/lib/glimmer/swt/tool_bar_proxy.rb +1 -1
- data/lib/glimmer/swt/transform_proxy.rb +1 -1
- data/lib/glimmer/swt/tray_item_proxy.rb +1 -1
- data/lib/glimmer/swt/tray_proxy.rb +1 -1
- data/lib/glimmer/swt/tree_proxy.rb +1 -1
- data/lib/glimmer/swt/widget_listener_proxy.rb +1 -1
- data/lib/glimmer/swt/widget_proxy.rb +1 -1
- data/lib/glimmer/ui/custom_shape.rb +1 -1
- data/lib/glimmer/ui/custom_shell.rb +1 -1
- data/lib/glimmer/ui/custom_widget.rb +1 -1
- data/lib/glimmer/ui.rb +21 -0
- data/lib/glimmer/util/proc_tracker.rb +4 -4
- data/lib/glimmer-dsl-swt.rb +1 -1
- data/samples/elaborate/battleship/model/cell.rb +1 -1
- data/samples/elaborate/battleship/model/game.rb +1 -1
- data/samples/elaborate/battleship/model/grid.rb +1 -1
- data/samples/elaborate/battleship/model/ship.rb +1 -1
- data/samples/elaborate/battleship/model/ship_collection.rb +1 -1
- data/samples/elaborate/battleship/view/action_panel.rb +1 -1
- data/samples/elaborate/battleship/view/cell.rb +1 -1
- data/samples/elaborate/battleship/view/grid.rb +1 -1
- data/samples/elaborate/battleship/view/ship.rb +1 -1
- data/samples/elaborate/battleship/view/ship_collection.rb +1 -1
- data/samples/elaborate/battleship.rb +1 -1
- data/samples/elaborate/calculator.rb +1 -1
- data/samples/elaborate/clock.rb +1 -1
- data/samples/elaborate/connect4/model/grid.rb +1 -1
- data/samples/elaborate/connect4/model/slot.rb +1 -1
- data/samples/elaborate/connect4.rb +1 -1
- data/samples/elaborate/contact_manager/contact.rb +1 -1
- data/samples/elaborate/contact_manager/contact_manager_presenter.rb +1 -1
- data/samples/elaborate/contact_manager/contact_repository.rb +1 -1
- data/samples/elaborate/contact_manager.rb +1 -1
- data/samples/elaborate/game_of_life/model/cell.rb +1 -1
- data/samples/elaborate/game_of_life/model/grid.rb +1 -1
- data/samples/elaborate/game_of_life.rb +1 -1
- data/samples/elaborate/login.rb +1 -1
- data/samples/elaborate/mandelbrot_fractal.rb +1 -1
- data/samples/elaborate/meta_sample.rb +1 -1
- data/samples/elaborate/metronome.rb +1 -1
- data/samples/elaborate/parking/model/parking_floor.rb +1 -1
- data/samples/elaborate/parking/model/parking_presenter.rb +1 -1
- data/samples/elaborate/parking/model/parking_spot.rb +1 -1
- data/samples/elaborate/parking.rb +1 -1
- data/samples/elaborate/quarto/model/game.rb +1 -1
- data/samples/elaborate/quarto/model/piece/cube.rb +1 -1
- data/samples/elaborate/quarto/model/piece/cylinder.rb +1 -1
- data/samples/elaborate/quarto/model/piece.rb +1 -1
- data/samples/elaborate/quarto/view/available_pieces_area.rb +1 -1
- data/samples/elaborate/quarto/view/board.rb +1 -1
- data/samples/elaborate/quarto/view/cell.rb +1 -1
- data/samples/elaborate/quarto/view/cube.rb +1 -1
- data/samples/elaborate/quarto/view/cylinder.rb +1 -1
- data/samples/elaborate/quarto/view/message_box_panel.rb +1 -1
- data/samples/elaborate/quarto/view/piece.rb +1 -1
- data/samples/elaborate/quarto/view/selected_piece_area.rb +1 -1
- data/samples/elaborate/quarto.rb +1 -1
- data/samples/elaborate/snake/model/apple.rb +33 -0
- data/samples/elaborate/snake/model/game.rb +68 -0
- data/samples/elaborate/snake/model/snake.rb +95 -0
- data/samples/elaborate/snake/model/vertebra.rb +22 -0
- data/samples/elaborate/snake/presenter/cell.rb +27 -0
- data/samples/elaborate/snake/presenter/grid.rb +49 -0
- data/samples/elaborate/snake.rb +103 -0
- data/samples/elaborate/stock_ticker.rb +1 -1
- data/samples/elaborate/tetris/model/block.rb +1 -1
- data/samples/elaborate/tetris/model/game.rb +1 -1
- data/samples/elaborate/tetris/model/past_game.rb +1 -1
- data/samples/elaborate/tetris/model/tetromino.rb +1 -1
- data/samples/elaborate/tetris/view/bevel.rb +1 -1
- data/samples/elaborate/tetris/view/block.rb +1 -1
- data/samples/elaborate/tetris/view/high_score_dialog.rb +1 -1
- data/samples/elaborate/tetris/view/playfield.rb +1 -1
- data/samples/elaborate/tetris/view/score_lane.rb +1 -1
- data/samples/elaborate/tetris/view/tetris_menu_bar.rb +1 -1
- data/samples/elaborate/tetris.rb +8 -8
- data/samples/elaborate/tic_tac_toe/board.rb +1 -1
- data/samples/elaborate/tic_tac_toe/cell.rb +1 -1
- data/samples/elaborate/tic_tac_toe.rb +1 -1
- data/samples/elaborate/timer.rb +1 -1
- data/samples/elaborate/user_profile.rb +1 -1
- data/samples/elaborate/weather.rb +1 -1
- data/samples/hello/hello_arrow.rb +1 -1
- data/samples/hello/hello_browser.rb +1 -1
- data/samples/hello/hello_button.rb +1 -1
- data/samples/hello/hello_c_combo.rb +1 -1
- data/samples/hello/hello_c_tab.rb +1 -1
- data/samples/hello/hello_canvas.rb +1 -1
- data/samples/hello/hello_canvas_animation.rb +1 -1
- data/samples/hello/hello_canvas_animation_multi.rb +1 -1
- data/samples/hello/hello_canvas_data_binding.rb +1 -1
- data/samples/hello/hello_canvas_path.rb +1 -1
- data/samples/hello/hello_canvas_shape_listeners.rb +1 -1
- data/samples/hello/hello_checkbox.rb +1 -1
- data/samples/hello/hello_checkbox_group.rb +1 -1
- data/samples/hello/hello_code_text.rb +1 -1
- data/samples/hello/hello_color_dialog.rb +1 -1
- data/samples/hello/hello_combo.rb +1 -1
- data/samples/hello/hello_composite.rb +1 -1
- data/samples/hello/hello_computed.rb +1 -1
- data/samples/hello/hello_cool_bar.rb +1 -1
- data/samples/hello/hello_cursor.rb +1 -1
- data/samples/hello/hello_custom_shape.rb +1 -1
- data/samples/hello/hello_custom_shell.rb +1 -1
- data/samples/hello/hello_custom_widget.rb +1 -1
- data/samples/hello/hello_date_time.rb +1 -1
- data/samples/hello/hello_dialog.rb +1 -1
- data/samples/hello/hello_directory_dialog.rb +1 -1
- data/samples/hello/hello_drag_and_drop.rb +1 -1
- data/samples/hello/hello_expand_bar.rb +1 -1
- data/samples/hello/hello_file_dialog.rb +1 -1
- data/samples/hello/hello_font_dialog.rb +1 -1
- data/samples/hello/hello_group.rb +1 -1
- data/samples/hello/hello_label.rb +1 -1
- data/samples/hello/hello_layout.rb +1 -1
- data/samples/hello/hello_link.rb +1 -1
- data/samples/hello/hello_list_multi_selection.rb +1 -1
- data/samples/hello/hello_list_single_selection.rb +1 -1
- data/samples/hello/hello_menu_bar.rb +1 -1
- data/samples/hello/hello_message_box.rb +1 -1
- data/samples/hello/hello_pop_up_context_menu.rb +1 -1
- data/samples/hello/hello_print.rb +1 -1
- data/samples/hello/hello_print_dialog.rb +1 -1
- data/samples/hello/hello_progress_bar.rb +1 -1
- data/samples/hello/hello_radio.rb +1 -1
- data/samples/hello/hello_radio_group.rb +1 -1
- data/samples/hello/hello_sash_form.rb +1 -1
- data/samples/hello/hello_scale.rb +1 -1
- data/samples/hello/hello_scrolled_composite.rb +1 -1
- data/samples/hello/hello_shape.rb +1 -1
- data/samples/hello/hello_shell.rb +1 -1
- data/samples/hello/hello_slider.rb +1 -1
- data/samples/hello/hello_spinner.rb +1 -1
- data/samples/hello/hello_styled_text.rb +1 -1
- data/samples/hello/hello_tab.rb +1 -1
- data/samples/hello/hello_table.rb +1 -1
- data/samples/hello/hello_text.rb +1 -1
- data/samples/hello/hello_toggle.rb +1 -1
- data/samples/hello/hello_tool_bar.rb +1 -1
- data/samples/hello/hello_tray_item.rb +1 -1
- data/samples/hello/hello_tree.rb +1 -1
- data/samples/hello/hello_world.rb +1 -1
- metadata +11 -18
@@ -0,0 +1,33 @@
|
|
1
|
+
class Snake
|
2
|
+
module Model
|
3
|
+
class Apple
|
4
|
+
attr_reader :game
|
5
|
+
attr_accessor :row, :column
|
6
|
+
|
7
|
+
def initialize(game)
|
8
|
+
@game = game
|
9
|
+
end
|
10
|
+
|
11
|
+
# generates a new location from scratch or via dependency injection of what cell is (for testing purposes)
|
12
|
+
def generate(initial_row: nil, initial_column: nil)
|
13
|
+
if initial_row && initial_column
|
14
|
+
self.row, self.column = initial_row, initial_column
|
15
|
+
else
|
16
|
+
self.row, self.column = @game.height.times.zip(@game.width.times).reject do |row, column|
|
17
|
+
@game.snake.vertebrae.map {|v| [v.row, v.column]}.include?([row, column])
|
18
|
+
end.sample
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def remove
|
23
|
+
self.row = nil
|
24
|
+
self.column = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
# inspect is overridden to prevent printing very long stack traces
|
28
|
+
def inspect
|
29
|
+
"#{super[0, 120]}... >"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
require_relative 'snake'
|
4
|
+
require_relative 'apple'
|
5
|
+
|
6
|
+
class Snake
|
7
|
+
module Model
|
8
|
+
class Game
|
9
|
+
WIDTH_DEFAULT = 20
|
10
|
+
HEIGHT_DEFAULT = 20
|
11
|
+
FILE_HIGH_SCORE = File.expand_path(File.join(Dir.home, '.glimmer-snake'))
|
12
|
+
|
13
|
+
attr_reader :width, :height
|
14
|
+
attr_accessor :snake, :apple, :over, :score, :high_score, :paused
|
15
|
+
alias over? over
|
16
|
+
alias paused? paused
|
17
|
+
|
18
|
+
def initialize(width = WIDTH_DEFAULT, height = HEIGHT_DEFAULT)
|
19
|
+
@width = width
|
20
|
+
@height = height
|
21
|
+
@snake = Snake.new(self)
|
22
|
+
@apple = Apple.new(self)
|
23
|
+
FileUtils.touch(FILE_HIGH_SCORE)
|
24
|
+
@high_score = File.read(FILE_HIGH_SCORE).to_i rescue 0
|
25
|
+
end
|
26
|
+
|
27
|
+
def score=(new_score)
|
28
|
+
@score = new_score
|
29
|
+
self.high_score = @score if @score > @high_score
|
30
|
+
end
|
31
|
+
|
32
|
+
def high_score=(new_high_score)
|
33
|
+
@high_score = new_high_score
|
34
|
+
File.write(FILE_HIGH_SCORE, @high_score.to_s)
|
35
|
+
rescue => e
|
36
|
+
puts e.full_message
|
37
|
+
end
|
38
|
+
|
39
|
+
def start
|
40
|
+
self.over = false
|
41
|
+
self.score = 0
|
42
|
+
self.snake.generate
|
43
|
+
self.apple.generate
|
44
|
+
end
|
45
|
+
|
46
|
+
def pause
|
47
|
+
self.paused = true
|
48
|
+
end
|
49
|
+
|
50
|
+
def resume
|
51
|
+
self.paused = false
|
52
|
+
end
|
53
|
+
|
54
|
+
def toggle_pause
|
55
|
+
unless paused?
|
56
|
+
pause
|
57
|
+
else
|
58
|
+
resume
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# inspect is overridden to prevent printing very long stack traces
|
63
|
+
def inspect
|
64
|
+
"#{super[0, 75]}... >"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require_relative 'vertebra'
|
2
|
+
|
3
|
+
class Snake
|
4
|
+
module Model
|
5
|
+
class Snake
|
6
|
+
SCORE_EAT_APPLE = 50
|
7
|
+
RIGHT_TURN_MAP = {
|
8
|
+
north: :east,
|
9
|
+
east: :south,
|
10
|
+
south: :west,
|
11
|
+
west: :north
|
12
|
+
}
|
13
|
+
LEFT_TURN_MAP = RIGHT_TURN_MAP.invert
|
14
|
+
|
15
|
+
attr_accessor :collided
|
16
|
+
alias collided? collided
|
17
|
+
|
18
|
+
attr_reader :game
|
19
|
+
# vertebrae and joins are ordered from tail to head
|
20
|
+
attr_accessor :vertebrae
|
21
|
+
|
22
|
+
def initialize(game)
|
23
|
+
@game = game
|
24
|
+
end
|
25
|
+
|
26
|
+
# generates a new snake location and orientation from scratch or via dependency injection of what head_cell and orientation are (for testing purposes)
|
27
|
+
def generate(initial_row: nil, initial_column: nil, initial_orientation: nil)
|
28
|
+
self.collided = false
|
29
|
+
initial_vertebra = Vertebra.new(snake: self, row: initial_row, column: initial_column, orientation: initial_orientation)
|
30
|
+
self.vertebrae = [initial_vertebra]
|
31
|
+
end
|
32
|
+
|
33
|
+
def length
|
34
|
+
@vertebrae.length
|
35
|
+
end
|
36
|
+
|
37
|
+
def head
|
38
|
+
@vertebrae.last
|
39
|
+
end
|
40
|
+
|
41
|
+
def tail
|
42
|
+
@vertebrae.first
|
43
|
+
end
|
44
|
+
|
45
|
+
def remove
|
46
|
+
self.vertebrae.clear
|
47
|
+
self.joins.clear
|
48
|
+
end
|
49
|
+
|
50
|
+
def move
|
51
|
+
@old_tail = tail.dup
|
52
|
+
@new_head = head.dup
|
53
|
+
case @new_head.orientation
|
54
|
+
when :east
|
55
|
+
@new_head.column = (@new_head.column + 1) % @game.width
|
56
|
+
when :west
|
57
|
+
@new_head.column = (@new_head.column - 1) % @game.width
|
58
|
+
when :south
|
59
|
+
@new_head.row = (@new_head.row + 1) % @game.height
|
60
|
+
when :north
|
61
|
+
@new_head.row = (@new_head.row - 1) % @game.height
|
62
|
+
end
|
63
|
+
if @vertebrae.map {|v| [v.row, v.column]}.include?([@new_head.row, @new_head.column])
|
64
|
+
self.collided = true
|
65
|
+
@game.over = true
|
66
|
+
else
|
67
|
+
@vertebrae.append(@new_head)
|
68
|
+
@vertebrae.delete(tail)
|
69
|
+
if head.row == @game.apple.row && head.column == @game.apple.column
|
70
|
+
grow
|
71
|
+
@game.apple.generate
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def turn_right
|
77
|
+
head.orientation = RIGHT_TURN_MAP[head.orientation]
|
78
|
+
end
|
79
|
+
|
80
|
+
def turn_left
|
81
|
+
head.orientation = LEFT_TURN_MAP[head.orientation]
|
82
|
+
end
|
83
|
+
|
84
|
+
def grow
|
85
|
+
@game.score += SCORE_EAT_APPLE
|
86
|
+
@vertebrae.prepend(@old_tail)
|
87
|
+
end
|
88
|
+
|
89
|
+
# inspect is overridden to prevent printing very long stack traces
|
90
|
+
def inspect
|
91
|
+
"#{super[0, 150]}... >"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class Snake
|
2
|
+
module Model
|
3
|
+
class Vertebra
|
4
|
+
ORIENTATIONS = %i[north east south west]
|
5
|
+
# orientation is needed for snake occuppied cells (but not apple cells)
|
6
|
+
attr_reader :snake
|
7
|
+
attr_accessor :row, :column, :orientation
|
8
|
+
|
9
|
+
def initialize(snake: , row: , column: , orientation: )
|
10
|
+
@row = row || rand(snake.game.height)
|
11
|
+
@column = column || rand(snake.game.width)
|
12
|
+
@orientation = orientation || ORIENTATIONS.sample
|
13
|
+
@snake = snake
|
14
|
+
end
|
15
|
+
|
16
|
+
# inspect is overridden to prevent printing very long stack traces
|
17
|
+
def inspect
|
18
|
+
"#{super[0, 150]}... >"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Snake
|
2
|
+
module Presenter
|
3
|
+
class Cell
|
4
|
+
COLOR_CLEAR = :white
|
5
|
+
COLOR_SNAKE = :green
|
6
|
+
COLOR_APPLE = :red
|
7
|
+
|
8
|
+
attr_reader :row, :column, :grid
|
9
|
+
attr_accessor :color
|
10
|
+
|
11
|
+
def initialize(grid: ,row: ,column: )
|
12
|
+
@row = row
|
13
|
+
@column = column
|
14
|
+
@grid = grid
|
15
|
+
end
|
16
|
+
|
17
|
+
def clear
|
18
|
+
self.color = COLOR_CLEAR unless color == COLOR_CLEAR
|
19
|
+
end
|
20
|
+
|
21
|
+
# inspect is overridden to prevent printing very long stack traces
|
22
|
+
def inspect
|
23
|
+
"#{super[0, 150]}... >"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'glimmer'
|
2
|
+
require_relative '../model/game'
|
3
|
+
require_relative 'cell'
|
4
|
+
|
5
|
+
class Snake
|
6
|
+
module Presenter
|
7
|
+
class Grid
|
8
|
+
include Glimmer
|
9
|
+
|
10
|
+
attr_reader :game, :cells
|
11
|
+
|
12
|
+
def initialize(game = Model::Game.new)
|
13
|
+
@game = game
|
14
|
+
@cells = @game.height.times.map do |row|
|
15
|
+
@game.width.times.map do |column|
|
16
|
+
Cell.new(grid: self, row: row, column: column)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
observe(@game.snake, :vertebrae) do |new_vertebrae|
|
20
|
+
occupied_snake_positions = @game.snake.vertebrae.map {|v| [v.row, v.column]}
|
21
|
+
@cells.each_with_index do |row_cells, row|
|
22
|
+
row_cells.each_with_index do |cell, column|
|
23
|
+
if [@game.apple.row, @game.apple.column] == [row, column]
|
24
|
+
cell.color = Cell::COLOR_APPLE
|
25
|
+
elsif occupied_snake_positions.include?([row, column])
|
26
|
+
cell.color = Cell::COLOR_SNAKE
|
27
|
+
else
|
28
|
+
cell.clear
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def clear
|
36
|
+
@cells.each do |row_cells|
|
37
|
+
row_cells.each do |cell|
|
38
|
+
cell.clear
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# inspect is overridden to prevent printing very long stack traces
|
44
|
+
def inspect
|
45
|
+
"#{super[0, 75]}... >"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'glimmer-dsl-swt'
|
2
|
+
|
3
|
+
require_relative 'snake/presenter/grid'
|
4
|
+
|
5
|
+
class Snake
|
6
|
+
include Glimmer::UI::CustomShell
|
7
|
+
|
8
|
+
CELL_SIZE = 15
|
9
|
+
SNAKE_MOVE_DELAY = 100 # millis
|
10
|
+
|
11
|
+
before_body do
|
12
|
+
@game = Model::Game.new
|
13
|
+
@grid = Presenter::Grid.new(@game)
|
14
|
+
@game.start
|
15
|
+
@keypress_queue = []
|
16
|
+
|
17
|
+
display {
|
18
|
+
on_swt_keydown do |key_event|
|
19
|
+
if key_event.keyCode == 32
|
20
|
+
@game.toggle_pause
|
21
|
+
else
|
22
|
+
@keypress_queue << key_event.keyCode
|
23
|
+
end
|
24
|
+
end
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
after_body do
|
29
|
+
register_observers
|
30
|
+
end
|
31
|
+
|
32
|
+
body {
|
33
|
+
shell(:no_resize) {
|
34
|
+
grid_layout(@game.width, true) {
|
35
|
+
margin_width 0
|
36
|
+
margin_height 0
|
37
|
+
horizontal_spacing 0
|
38
|
+
vertical_spacing 0
|
39
|
+
}
|
40
|
+
|
41
|
+
# data-bind window title to game score, converting it to a title string on read from the model
|
42
|
+
text <= [@game, :score, on_read: -> (score) {"Snake (Score: #{@game.score})"}]
|
43
|
+
minimum_size @game.width * CELL_SIZE, @game.height * CELL_SIZE
|
44
|
+
|
45
|
+
@game.height.times do |row|
|
46
|
+
@game.width.times do |column|
|
47
|
+
canvas {
|
48
|
+
layout_data {
|
49
|
+
width_hint CELL_SIZE
|
50
|
+
height_hint CELL_SIZE
|
51
|
+
}
|
52
|
+
|
53
|
+
rectangle(0, 0, CELL_SIZE, CELL_SIZE) {
|
54
|
+
background <= [@grid.cells[row][column], :color] # data-bind square fill to grid cell color
|
55
|
+
}
|
56
|
+
}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
def register_observers
|
63
|
+
observe(@game, :over) do |game_over|
|
64
|
+
async_exec do
|
65
|
+
if game_over
|
66
|
+
message_box {
|
67
|
+
text 'Game Over!'
|
68
|
+
message "Score: #{@game.score} | High Score: #{@game.high_score}"
|
69
|
+
}.open
|
70
|
+
@game.start
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
timer_exec(SNAKE_MOVE_DELAY, &method(:move_snake))
|
76
|
+
end
|
77
|
+
|
78
|
+
def move_snake
|
79
|
+
unless @game.paused? || @game.over?
|
80
|
+
process_queued_keypress
|
81
|
+
@game.snake.move
|
82
|
+
end
|
83
|
+
timer_exec(SNAKE_MOVE_DELAY, &method(:move_snake))
|
84
|
+
end
|
85
|
+
|
86
|
+
def process_queued_keypress
|
87
|
+
# key press queue ensures one turn per snake move to avoid a double-turn resulting in instant death (due to snake illogically going back against itself)
|
88
|
+
key = @keypress_queue.shift
|
89
|
+
if [@game.snake.head.orientation, key] == [:north, swt(:arrow_right)] ||
|
90
|
+
[@game.snake.head.orientation, key] == [:east, swt(:arrow_down)] ||
|
91
|
+
[@game.snake.head.orientation, key] == [:south, swt(:arrow_left)] ||
|
92
|
+
[@game.snake.head.orientation, key] == [:west, swt(:arrow_up)]
|
93
|
+
@game.snake.turn_right
|
94
|
+
elsif [@game.snake.head.orientation, key] == [:north, swt(:arrow_left)] ||
|
95
|
+
[@game.snake.head.orientation, key] == [:west, swt(:arrow_down)] ||
|
96
|
+
[@game.snake.head.orientation, key] == [:south, swt(:arrow_right)] ||
|
97
|
+
[@game.snake.head.orientation, key] == [:east, swt(:arrow_up)]
|
98
|
+
@game.snake.turn_left
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
Snake.launch
|
data/samples/elaborate/tetris.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c) 2007-
|
1
|
+
# Copyright (c) 2007-2022 Andy Maleh
|
2
2
|
#
|
3
3
|
# Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
# a copy of this software and associated documentation files (the
|
@@ -54,7 +54,7 @@ class Tetris
|
|
54
54
|
Display.app_name = 'Glimmer Tetris'
|
55
55
|
|
56
56
|
display {
|
57
|
-
on_swt_keydown
|
57
|
+
on_swt_keydown do |key_event|
|
58
58
|
case key_event.keyCode
|
59
59
|
when swt(:arrow_down), 's'.bytes.first
|
60
60
|
if OS.mac?
|
@@ -88,16 +88,16 @@ class Tetris
|
|
88
88
|
game.rotate!(:left)
|
89
89
|
end
|
90
90
|
end
|
91
|
-
|
91
|
+
end
|
92
92
|
|
93
93
|
# if running in app mode, set the Mac app about dialog (ignored in platforms)
|
94
|
-
on_about
|
94
|
+
on_about do
|
95
95
|
show_about_dialog
|
96
|
-
|
96
|
+
end
|
97
97
|
|
98
|
-
on_quit
|
98
|
+
on_quit do
|
99
99
|
exit(0)
|
100
|
-
|
100
|
+
end
|
101
101
|
}
|
102
102
|
end
|
103
103
|
|
@@ -185,7 +185,7 @@ class Tetris
|
|
185
185
|
def show_about_dialog
|
186
186
|
message_box {
|
187
187
|
text 'Glimmer Tetris'
|
188
|
-
message "Glimmer Tetris\n\nGlimmer DSL for SWT Sample\n\nCopyright (c) 2007-
|
188
|
+
message "Glimmer Tetris\n\nGlimmer DSL for SWT Sample\n\nCopyright (c) 2007-2022 Andy Maleh"
|
189
189
|
}.open
|
190
190
|
end
|
191
191
|
end
|
data/samples/elaborate/timer.rb
CHANGED