Sha256: ed73c55a787786b1391557e3a1253707e47f2b89dfe39207bab6274b78f4735a

Contents?: true

Size: 1.35 KB

Versions: 10

Compression:

Stored size: 1.35 KB

Contents

require 'glimmer/data_binding/observer'
require_relative '../model/game'
require_relative 'cell'

class Snake
  module Presenter
    class Grid
      attr_reader :game, :cells
      
      def initialize(game = Model::Game.new)
        @game = game
        @cells = @game.height.times.map do |row|
          @game.width.times.map do |column|
            Cell.new(grid: self, row: row, column: column)
          end
        end
        Glimmer::DataBinding::Observer.proc do |new_vertebrae|
          occupied_snake_positions = @game.snake.vertebrae.map {|v| [v.row, v.column]}
          @cells.each_with_index do |row_cells, row|
            row_cells.each_with_index do |cell, column|
              if [@game.apple.row, @game.apple.column] == [row, column]
                cell.color = Cell::COLOR_APPLE
              elsif occupied_snake_positions.include?([row, column])
                cell.color = Cell::COLOR_SNAKE
              else
                cell.clear
              end
            end
          end
        end.observe(@game.snake, :vertebrae)
      end
      
      def clear
        @cells.each do |row_cells|
          row_cells.each do |cell|
            cell.clear
          end
        end
      end
      
      # inspect is overridden to prevent printing very long stack traces
      def inspect
        "#{super[0, 75]}... >"
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
glimmer-dsl-libui-0.4.0 examples/snake/presenter/grid.rb
glimmer-dsl-libui-0.3.5 examples/snake/presenter/grid.rb
glimmer-dsl-libui-0.3.4 examples/snake/presenter/grid.rb
glimmer-dsl-libui-0.3.3 examples/snake/presenter/grid.rb
glimmer-dsl-libui-0.3.2 examples/snake/presenter/grid.rb
glimmer-dsl-libui-0.3.1 examples/snake/presenter/grid.rb
glimmer-dsl-libui-0.3.0 examples/snake/presenter/grid.rb
glimmer-dsl-libui-0.2.24 examples/snake/presenter/grid.rb
glimmer-dsl-libui-0.2.23 examples/snake/presenter/grid.rb
glimmer-dsl-libui-0.2.22 examples/snake/presenter/grid.rb